rentease-backend/controllers/regionController.js

125 lines
3.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const { Region } = require('../models');
const { Op } = require('sequelize');
// 格式化时间(考虑时区,转换为北京时间)
const formatDate = (date) => {
if (!date) return null;
// 创建一个新的Date对象加上8小时的时区偏移
const beijingDate = new Date(date.getTime() + 8 * 60 * 60 * 1000);
return beijingDate.toISOString().split('T')[0];
};
// 格式化区域数据
const formatRegionData = (region) => {
return {
...region.toJSON(),
createTime: formatDate(region.createTime),
updateTime: formatDate(region.updateTime)
};
};
// 获取所有区域
const getAllRegions = async (req, res) => {
try {
const regions = await Region.findAll({
where: { isDeleted: 0 }
});
const formattedRegions = regions.map(formatRegionData);
res.status(200).json(formattedRegions);
} catch (error) {
res.status(500).json({ error: error.message });
}
};
// 获取单个区域
const getRegionById = async (req, res) => {
try {
const { id } = req.params;
const region = await Region.findOne({
where: { id, isDeleted: 0 }
});
if (!region) {
return res.status(404).json({ error: '区域不存在' });
}
const formattedRegion = formatRegionData(region);
res.status(200).json(formattedRegion);
} catch (error) {
res.status(500).json({ error: error.message });
}
};
// 创建区域
const createRegion = async (req, res) => {
try {
const { name, description } = req.body;
const region = await Region.create({ name, description });
res.status(201).json(region);
} catch (error) {
res.status(500).json({ error: error.message });
}
};
// 更新区域
const updateRegion = async (req, res) => {
try {
const { id } = req.params;
const { name, description } = req.body;
const region = await Region.findOne({
where: { id, isDeleted: 0 }
});
if (!region) {
return res.status(404).json({ error: '区域不存在' });
}
await region.update({ name, description });
res.status(200).json(region);
} catch (error) {
res.status(500).json({ error: error.message });
}
};
// 删除区域(软删除)
const deleteRegion = async (req, res) => {
try {
const { id } = req.params;
const region = await Region.findOne({
where: { id, isDeleted: 0 }
});
if (!region) {
return res.status(404).json({ error: '区域不存在' });
}
await region.update({ isDeleted: 1 });
res.status(200).json({ message: '区域删除成功' });
} catch (error) {
res.status(500).json({ error: error.message });
}
};
// 获取所有区域(不分页)
const listRegions = async (req, res) => {
try {
const { name } = req.query;
// 构建查询条件
const where = { isDeleted: 0 };
if (name) {
where.name = {
[Op.like]: `%${name}%`
};
}
const regions = await Region.findAll({ where });
const formattedRegions = regions.map(formatRegionData);
res.status(200).json(formattedRegions);
} catch (error) {
res.status(500).json({ error: error.message });
}
};
module.exports = {
getAllRegions,
listRegions,
getRegionById,
createRegion,
updateRegion,
deleteRegion
};