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: { $elemMatch: { uuid: { $in: inputUuids } } }, // }); const findValue = await lineModel(organization).findOneAndDelete({ "line.uuid": { $all: inputUuids } // Ensure all UUIDs are present in the `line` key }); 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 // Fetch the documents with the specified layer value const findValue = await lineModel(organization).find({ layer: layer }); if (!findValue) { res.status(200).json("data not found"); } else { await lineModel(organization).deleteMany({ layer: layer }); // console.log(`Documents with layer ${layer} have been deleted.`); // Update documents with layer greater than -1 const updateResult = await lineModel(organization).updateMany( { layer: { $gt:layer} }, { $inc: { layer: -1 } } // Example operation: decrementing layer by 1 ); res.status(201).json(updateResult); } } catch (error) { console.error('Error delete Lines:', error); res.status(500).json({ error: "Failed to delete Lines" }); } } }