Files
Dwinzo-Backend-V0.0/src/api-server/controller/lines/line-Services.ts

117 lines
3.7 KiB
TypeScript

import { Request, Response } from "express";
import lineModel from "../../../shared/model/lines/lines-Model.ts";
export class Lines {
static async setLines(req: Request, res: Response) {
try {
const { organization, layer, line, type } = req.body;
const newLine = await lineModel(organization).create({
layer,
line,
type,
});
res.status(201).json(newLine);
// Send response with the created document
} catch (error) {
console.error("Error creating Lines:", error);
res.status(500).json({ message: "Failed to create Lines" });
}
}
static async updateLines(req: Request, res: Response) {
try {
const { organization, uuid, position } = req.body;
// const findLine = await lineModel(organization).find({ 'line.uuid': uuid });
// Update the position of the line matching the uuid
const updateResult = await lineModel(organization).updateMany(
{ "line.uuid": uuid }, // Filter: Find the line with the given uuid
{ $set: { "line.$.position": position } } // Update the position and type
);
res.status(201).json(updateResult);
// Send response with the created document
} catch (error) {
console.error("Error creating Lines:", error);
res.status(500).json({ message: "Failed to create Lines" });
}
}
static async getLines(req: Request, res: Response) {
try {
const { organization } = req.params;
const findValue = await lineModel(organization).find();
if (!findValue) {
res.status(200).json("user not found");
} else {
res.status(201).json(findValue);
}
} catch (error) {
console.error("Error get Lines:", error);
res.status(500).json({ error: "Failed to get Lines" });
}
}
static async deleteLineItems(req: Request, res: Response) {
try {
const { organization, layer, line, type } = req.body;
const inputUuids = line.map((item: any) => item.uuid);
const findValue = await lineModel(organization).findOneAndDelete({
"line.uuid": { $all: inputUuids },
});
if (!findValue) {
res.status(200).json("data not found");
} else {
res.status(201).json(findValue);
}
} catch (error) {
console.error("Error delete Lines:", error);
res.status(500).json({ error: "Failed to delete Lines" });
}
}
static async deleteLinPoiteItems(req: Request, res: Response) {
try {
const { organization, layer, uuid, type } = req.body;
const findValue = await lineModel(organization).deleteMany({
"line.uuid": uuid,
});
if (!findValue) {
res.status(200).json("data not found");
} else {
res.status(201).json(findValue);
}
} catch (error) {
console.error("Error delete Lines:", error);
res.status(500).json({ error: "Failed to delete Lines" });
}
}
static async deleteLayer(req: Request, res: Response) {
try {
const { organization, layer } = req.body;
const findValue = await lineModel(organization).find({ layer: layer });
if (!findValue) {
res.status(200).json("data not found");
} else {
await lineModel(organization).deleteMany({ layer: layer });
const updateResult = await lineModel(organization).updateMany(
{ layer: { $gt: layer } },
{ $inc: { layer: -1 } }
);
res.status(201).json(updateResult);
}
} catch (error) {
console.error("Error delete Lines:", error);
res.status(500).json({ error: "Failed to delete Lines" });
}
}
}