parent
d9bdac19a6
commit
caa6d6ea12
@ -0,0 +1,42 @@
|
|||||||
|
const { handleArrayDaoData } = require("../utils/tools");
|
||||||
|
const addressModel = require("./model/addressModel");
|
||||||
|
|
||||||
|
module.exports.getAddressDao = async function () {
|
||||||
|
return handleArrayDaoData(await addressModel.findAll());
|
||||||
|
};
|
||||||
|
|
||||||
|
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());
|
||||||
|
};
|
@ -0,0 +1,46 @@
|
|||||||
|
const sequelize = require("../dbConnect");
|
||||||
|
const { DataTypes } = require("sequelize");
|
||||||
|
|
||||||
|
module.exports = sequelize.define(
|
||||||
|
"address",
|
||||||
|
{
|
||||||
|
receiver: {
|
||||||
|
type: DataTypes.STRING,
|
||||||
|
allowNull: false,
|
||||||
|
},
|
||||||
|
contact: {
|
||||||
|
type: DataTypes.STRING,
|
||||||
|
allowNull: false,
|
||||||
|
},
|
||||||
|
fullLocation: {
|
||||||
|
type: DataTypes.STRING,
|
||||||
|
allowNull: false,
|
||||||
|
},
|
||||||
|
provinceCode: {
|
||||||
|
type: DataTypes.STRING,
|
||||||
|
allowNull: false,
|
||||||
|
},
|
||||||
|
cityCode: {
|
||||||
|
type: DataTypes.STRING,
|
||||||
|
allowNull: false,
|
||||||
|
},
|
||||||
|
countyCode: {
|
||||||
|
type: DataTypes.STRING,
|
||||||
|
allowNull: false,
|
||||||
|
},
|
||||||
|
address: {
|
||||||
|
type: DataTypes.STRING,
|
||||||
|
allowNull: false,
|
||||||
|
},
|
||||||
|
isDefault: {
|
||||||
|
type: DataTypes.INTEGER,
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
freezeTableName: true,
|
||||||
|
createdAt: false,
|
||||||
|
updatedAt: false,
|
||||||
|
}
|
||||||
|
);
|
@ -0,0 +1,32 @@
|
|||||||
|
const express = require("express");
|
||||||
|
const router = express.Router();
|
||||||
|
const {
|
||||||
|
getAddressServices,
|
||||||
|
addAddressServices,
|
||||||
|
updateAddressServices,
|
||||||
|
deleteAddressServices,
|
||||||
|
} = require("../services/addressService");
|
||||||
|
const { formatResponse } = require("../utils/tools");
|
||||||
|
|
||||||
|
router.get("/", async function (req, res, next) {
|
||||||
|
const data = await getAddressServices();
|
||||||
|
res.send(formatResponse(0, "", data));
|
||||||
|
});
|
||||||
|
|
||||||
|
router.post("/", async function (req, res, next) {
|
||||||
|
console.log(req, res);
|
||||||
|
const data = await addAddressServices(req.body);
|
||||||
|
res.send(formatResponse(0, "", data));
|
||||||
|
});
|
||||||
|
|
||||||
|
router.put("/", async function (req, res, next) {
|
||||||
|
const data = await updateAddressServices(req.body);
|
||||||
|
res.send(formatResponse(0, "", data));
|
||||||
|
});
|
||||||
|
|
||||||
|
router.delete("/", async function (req, res, next) {
|
||||||
|
const data = await deleteAddressServices(req.body);
|
||||||
|
res.send(formatResponse(0, "", data));
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = router;
|
@ -0,0 +1,29 @@
|
|||||||
|
const {
|
||||||
|
getAddressDao,
|
||||||
|
addAddressDao,
|
||||||
|
updateAddressDao,
|
||||||
|
deleteAddressDao,
|
||||||
|
} = require("../dao/addressDao");
|
||||||
|
|
||||||
|
module.exports.getAddressServices = async function () {
|
||||||
|
return await getAddressDao();
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports.addAddressServices = async function (addressData) {
|
||||||
|
console.log(addressData);
|
||||||
|
// 确保isDefault是数字类型
|
||||||
|
const data = {
|
||||||
|
...addressData,
|
||||||
|
isDefault: addressData.isDefault || 0,
|
||||||
|
};
|
||||||
|
return await addAddressDao(data);
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports.updateAddressServices = async function (addressData) {
|
||||||
|
// 这里需要提供id
|
||||||
|
return await updateAddressDao(addressData);
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports.deleteAddressServices = async function ({ id }) {
|
||||||
|
return await deleteAddressDao(id);
|
||||||
|
};
|
Loading…
Reference in new issue