|
|
|
|
import BaseApi from "~/src/api/BaseApi";
|
|
|
|
|
import _ from "lodash";
|
|
|
|
|
import { Joi } from "celebrate";
|
|
|
|
|
import db from "../../database";
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
description,
|
|
|
|
|
request,
|
|
|
|
|
test,
|
|
|
|
|
response,
|
|
|
|
|
GET,
|
|
|
|
|
POST,
|
|
|
|
|
PUT,
|
|
|
|
|
DELETE,
|
|
|
|
|
SOCKET,
|
|
|
|
|
isPublic,
|
|
|
|
|
cache,
|
|
|
|
|
} = BaseApi;
|
|
|
|
|
|
|
|
|
|
export default class drinkWaterUnit extends BaseApi {
|
|
|
|
|
description = "سرویس تولید کننده آب آشامیدنی";
|
|
|
|
|
///////////////////////////////
|
|
|
|
|
|
|
|
|
|
@GET
|
|
|
|
|
@description("دریافت لیست تولید کننده آب آشامیدنیها")
|
|
|
|
|
@request(
|
|
|
|
|
optional(_.pick(db.drinkWaterUnit.validation, db.drinkWaterUnit.getList))
|
|
|
|
|
)
|
|
|
|
|
@response()
|
|
|
|
|
list = async (data) => {
|
|
|
|
|
const where = _.pick(omitNull(data), db.drinkWaterUnit.getList);
|
|
|
|
|
|
|
|
|
|
for (const key in where) {
|
|
|
|
|
if (db.drinkWaterUnit.validation[key].type == "string")
|
|
|
|
|
where[key] = { [Op.substring]: where[key] };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = await db.drinkWaterUnit.findAll({
|
|
|
|
|
where,
|
|
|
|
|
order: [["rate", "ASC"]],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@GET
|
|
|
|
|
@description("دریافت اطلاعات تولید کننده آب آشامیدنی")
|
|
|
|
|
@request(_.pick(db.drinkWaterUnit.validation, ["id"]))
|
|
|
|
|
@response()
|
|
|
|
|
info = async (data) => {
|
|
|
|
|
const result = await db.drinkWaterUnit.findByPk(data.id, {
|
|
|
|
|
raw: true,
|
|
|
|
|
attributes: ["brand"],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
|
return {
|
|
|
|
|
statusCode: 404,
|
|
|
|
|
message: "شناسه تولید کننده آب آشامیدنی نامعتبر است.",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@POST
|
|
|
|
|
@description("افزودن تولید کننده آب آشامیدنی")
|
|
|
|
|
@request(_.pick(db.drinkWaterUnit.validation, db.drinkWaterUnit.add))
|
|
|
|
|
@response(_.pick(db.drinkWaterUnit.validation, ["id"]))
|
|
|
|
|
add = async (data) => {
|
|
|
|
|
const checkedData = _.pick(omitNull(data), db.drinkWaterUnit.add);
|
|
|
|
|
|
|
|
|
|
const city = await db.city.findByPk(checkedData.cityId);
|
|
|
|
|
if (!city)
|
|
|
|
|
return {
|
|
|
|
|
statusCode: 404,
|
|
|
|
|
message: "شناسه شهر نامعتبر است",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const university = await db.university.findByPk(checkedData.universityId);
|
|
|
|
|
if (!university)
|
|
|
|
|
return {
|
|
|
|
|
statusCode: 404,
|
|
|
|
|
message: "شناسه دانشگاه نامعتبر است",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const result = await db.drinkWaterUnit.create(checkedData);
|
|
|
|
|
return { id: result.dataValues.id };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@PUT
|
|
|
|
|
@description("ویرایش تولید کننده آب آشامیدنی")
|
|
|
|
|
@request({
|
|
|
|
|
id: Joi.number().required().description("شناسه تولید کننده آب آشامیدنی"),
|
|
|
|
|
...optional(
|
|
|
|
|
_.pick(db.drinkWaterUnit.validation, db.drinkWaterUnit.updateList)
|
|
|
|
|
),
|
|
|
|
|
})
|
|
|
|
|
@response({})
|
|
|
|
|
update = async (data) => {
|
|
|
|
|
const checkedData = _.pick(omitNull(data), db.drinkWaterUnit.updateList);
|
|
|
|
|
if (!Object.keys(checkedData).length)
|
|
|
|
|
return {
|
|
|
|
|
statusCode: 400,
|
|
|
|
|
message: "بروز خطا در اطلاعات ورودی",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const { id } = data;
|
|
|
|
|
|
|
|
|
|
const result = await db.drinkWaterUnit.update(checkedData, {
|
|
|
|
|
where: { id },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
|
return {
|
|
|
|
|
statusCode: 400,
|
|
|
|
|
message: "شناسه واحد آب آشامیدنی نامعتبر است.",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@DELETE
|
|
|
|
|
@description("حذف تولید کننده آب آشامیدنی")
|
|
|
|
|
@request(_.pick(db.drinkWaterUnit.validation, ["id"]))
|
|
|
|
|
@response({})
|
|
|
|
|
delete = async (data) => {
|
|
|
|
|
const { id } = data;
|
|
|
|
|
|
|
|
|
|
const result = await db.drinkWaterUnit.destroy({ where: { id } });
|
|
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
|
return {
|
|
|
|
|
statusCode: 404,
|
|
|
|
|
message: "شناسه تولید کننده آب آشامیدنی نامعتبر است.",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
};
|
|
|
|
|
}
|