const { handleArrayDaoData } = require("../utils/tools"); const addressModel = require("./model/addressModel"); module.exports.getAddressDao = async function (senderId) { // 如果有senderId参数,则添加查询条件 const whereCondition = senderId ? { senderId } : {}; return handleArrayDaoData( await addressModel.findAll({ where: whereCondition, }) ); }; module.exports.addAddressDao = async function (addressData) { // 如果新增的是默认地址,需要先将其他地址设为非默认 if (addressData.isDefault === 1) { await addressModel.update({ isDefault: 0 }, { where: {} }); } await addressModel.create(addressData); return handleArrayDaoData(await addressModel.findAll()); }; module.exports.updateAddressDao = async function (addressData) { // 如果更新的是默认地址,需要先将其他地址设为非默认 if (addressData.isDefault === 1) { await addressModel.update( { isDefault: 0 }, { where: { id: { [Op.ne]: addressData.id } }, } ); } await addressModel.update(addressData, { where: { id: addressData.id, }, }); return handleArrayDaoData(await addressModel.findAll()); }; module.exports.deleteAddressDao = async function (id) { await addressModel.destroy({ where: { id, }, }); return handleArrayDaoData(await addressModel.findAll()); };