diff --git a/backend/src/layerController.ts b/backend/src/layerController.ts new file mode 100644 index 0000000..6116a2d --- /dev/null +++ b/backend/src/layerController.ts @@ -0,0 +1,132 @@ +import { FastifyRequest, FastifyReply } from 'fastify'; +import { DatabaseInterface } from './database/DatabaseInterface'; +import { getDatabase } from './database'; + +// Type definitions +interface Layer { + id: string; + project_id: string; + name: string; + visible: number; + locked: number; + color?: string; + line_type?: string; + transparency?: number; + sort_order: number; + created_at?: string; +} + +// Get all layers for a project +export async function getLayers(request: FastifyRequest, reply: FastifyReply) { + const db: DatabaseInterface = await getDatabase(); + const projectId = (request.params as any).projectId; + + try { + const layers = await db.getLayers(projectId); + return layers; + } catch (error) { + request.log.error(error); + return reply.status(500).send({ error: 'Failed to fetch layers' }); + } +} + +// Create a new layer +export async function createLayer(request: FastifyRequest, reply: FastifyReply) { + const db: DatabaseInterface = await getDatabase(); + const projectId = (request.params as any).projectId; + const userId = (request.user as any).id; + const { name, visible, locked, color, line_type, transparency, sort_order } = request.body as Layer; + + // Generate a new ID for the layer + const layerId = Date.now().toString(); + + try { + const newLayer = { + id: layerId, + project_id: projectId, + name: name || 'New Layer', + visible: visible !== undefined ? visible : 1, + locked: locked !== undefined ? locked : 0, + color: color || null, + line_type: line_type || null, + transparency: transparency !== undefined ? transparency : 0.0, + sort_order: sort_order !== undefined ? sort_order : 0, + created_at: new Date().toISOString() + }; + + await db.createLayer(newLayer); + return reply.status(201).send(newLayer); + } catch (error) { + request.log.error(error); + return reply.status(500).send({ error: 'Failed to create layer' }); + } +} + +// Update a layer +export async function updateLayer(request: FastifyRequest, reply: FastifyReply) { + const db: DatabaseInterface = await getDatabase(); + const projectId = (request.params as any).projectId; + const layerId = (request.params as any).layerId; + const updateData = request.body as Partial; + + try { + // Verify the layer belongs to the project + const existingLayer = await db.getLayerById(layerId); + if (!existingLayer || existingLayer.project_id !== projectId) { + return reply.status(404).send({ error: 'Layer not found' }); + } + + // Update the layer + const updatedLayer = await db.updateLayer(layerId, updateData); + return updatedLayer; + } catch (error) { + request.log.error(error); + return reply.status(500).send({ error: 'Failed to update layer' }); + } +} + +// Delete a layer +export async function deleteLayer(request: FastifyRequest, reply: FastifyReply) { + const db: DatabaseInterface = await getDatabase(); + const projectId = (request.params as any).projectId; + const layerId = (request.params as any).layerId; + + try { + // Verify the layer belongs to the project + const existingLayer = await db.getLayerById(layerId); + if (!existingLayer || existingLayer.project_id !== projectId) { + return reply.status(404).send({ error: 'Layer not found' }); + } + + // Delete the layer + await db.deleteLayer(layerId); + return reply.status(204).send(); + } catch (error) { + request.log.error(error); + return reply.status(500).send({ error: 'Failed to delete layer' }); + } +} + +// Reorder layers +export async function reorderLayers(request: FastifyRequest, reply: FastifyReply) { + const db: DatabaseInterface = await getDatabase(); + const projectId = (request.params as any).projectId; + const { layerIds } = request.body as { layerIds: string[] }; + + try { + // Update sort_order for each layer + for (let i = 0; i < layerIds.length; i++) { + const layerId = layerIds[i]; + // Verify the layer belongs to the project + const existingLayer = await db.getLayerById(layerId); + if (existingLayer && existingLayer.project_id === projectId) { + await db.updateLayer(layerId, { sort_order: i }); + } + } + + return { message: 'Layers reordered successfully' }; + } catch (error) { + request.log.error(error); + return reply.status(500).send({ error: 'Failed to reorder layers' }); + } +} \ No newline at end of file