四好公路
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
5.9 KiB

3 years ago
'use strict';
async function get (ctx) {
try {
const models = ctx.fs.dc.models;
3 years ago
const { type } = ctx.request.body;
3 years ago
const { name } = ctx.query;
3 years ago
3 years ago
let findOption = {
3 years ago
where: {
type
},
order: [['id', 'DESC']]
3 years ago
}
if (name) {
findOption.where.name = {
$like: `%${name}%`
}
}
const vehicleRes = await models.Statistic.findAll(findOption)
3 years ago
ctx.status = 200;
ctx.body = vehicleRes
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
}
async function edit (ctx) {
try {
const models = ctx.fs.dc.models;
3 years ago
const { id, name, count, type } = ctx.request.body;
3 years ago
3 years ago
if (!id) {
await models.Statistic.create({
name, count, type: type
3 years ago
})
} else {
3 years ago
await models.Statistic.update({
name, count, type: type
3 years ago
}, {
where: {
3 years ago
id: id
3 years ago
}
})
}
ctx.status = 204
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
}
async function del (ctx) {
try {
const models = ctx.fs.dc.models;
3 years ago
const { id } = ctx.params;
3 years ago
3 years ago
await models.Statistic.destroy({
3 years ago
where: {
3 years ago
id: id
3 years ago
}
})
3 years ago
ctx.status = 204
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
}
async function specificGet (ctx) {
try {
const models = ctx.fs.dc.models;
const { type } = ctx.query;
3 years ago
const { nameOfBusinessOwner } = ctx.query;
3 years ago
3 years ago
let findOption = {
3 years ago
where: {
type
},
order: [['id', 'DESC']]
3 years ago
}
if (nameOfBusinessOwner) {
findOption.where.nameOfBusinessOwner = {
$like: `%${nameOfBusinessOwner}%`
}
}
const vehicleRes = await models.MunicipalVehicle.findAll(findOption)
3 years ago
ctx.status = 200;
ctx.body = vehicleRes
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
}
async function specificEdit (ctx) {
try {
const models = ctx.fs.dc.models;
const data = ctx.request.body;
if (!data.vehicleId) {
3 years ago
const vehicleRes = await models.MunicipalVehicle.create(data)
3 years ago
} else {
3 years ago
const vehicleRes = await models.MunicipalVehicle.update(data, {
3 years ago
where: {
id: data.vehicleId
}
})
}
ctx.status = 204
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
}
async function specificDel (ctx) {
try {
const models = ctx.fs.dc.models;
const { vehicleId } = ctx.params;
3 years ago
const vehicleRes = await models.MunicipalVehicle.destroy({
3 years ago
where: {
id: vehicleId
}
})
ctx.status = 204
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
}
async function businessGet (ctx) {
try {
const models = ctx.fs.dc.models;
3 years ago
const { nameOfBusinessOwner } = ctx.query;
3 years ago
3 years ago
let findOption = {
where: {
},
order: [['id', 'DESC']]
3 years ago
}
if (nameOfBusinessOwner) {
findOption.where.nameOfBusinessOwner = {
$like: `%${nameOfBusinessOwner}%`
}
}
const businessRes = await models.MunicipalBusiness.findAll(findOption)
3 years ago
ctx.status = 200;
ctx.body = businessRes
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
}
async function businessEdit (ctx) {
try {
const models = ctx.fs.dc.models;
const data = ctx.request.body;
if (!data.businessId) {
await models.MunicipalBusiness.create(data)
} else {
await models.MunicipalBusiness.update(data, {
where: {
id: data.businessId
}
})
}
ctx.status = 204
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
}
async function businessDel (ctx) {
try {
const models = ctx.fs.dc.models;
const { businessId } = ctx.params;
2 years ago
await models.MunicipalBusiness.destroy({
3 years ago
where: {
id: businessId
}
})
3 years ago
ctx.status = 204
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
}
module.exports = {
get, edit, del,
3 years ago
specificGet, specificEdit, specificDel,
businessGet, businessEdit, businessDel
3 years ago
};