/**
 * 立即购买controller
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2016/9/23
 */
'use strict';

const address = require('../models/address');

const getArea = (req, res, next) => {
    let id = req.query.id || 0;

    if (!req.user.uid) {
        return res.send({
            code: 500,
            message: '请登录后操作'
        });
    }

    req.ctx(address).getAreaListData(id).then(result => {
        res.send(result);
    }).catch(next);
};

const getList = (req, res, next) => {
    let uid = req.user.uid;

    if (!uid) {
        return res.send({
            code: 500,
            message: '请登录后操作'
        });
    }

    req.ctx(address).getAddressListData(uid).then(result => {
        res.send(result);
    }).catch(next);
};

const delAddress = (req, res, next) => {
    let uid = req.user.uid;
    let id = req.body.id;

    if (!uid) {
        return next();
    }

    req.ctx(address).delAddressById(uid, id).then(result => {
        res.send(result);
    }).catch(next);
};

const saveAddress = (req, res, next) => {
    let uid = req.user.uid;
    let params = req.body;

    if (!uid) {
        return next();
    }

    req.ctx(address).saveAddressData(uid, params).then(result => {
        res.send(result);
    }).catch(next);
};

const setDefault = (req, res, next) => {
    let uid = req.user.uid;
    let id = req.body.id;

    if (!uid) {
        return next();
    }

    req.ctx(address).setDefaultAddress(uid, id).then(result => {
        res.send(result);
    }).catch(next);
};

module.exports = {
    getArea,
    getList,
    delAddress,
    saveAddress,
    setDefault
};