|
|
@@ -1,34 +1,139 @@
|
|
|
// plantation-tree.controller.ts
|
|
|
-import { Controller, Get, Post, Delete, Body, Param } from '@nestjs/common';
|
|
|
+import {
|
|
|
+ Controller,
|
|
|
+ Get,
|
|
|
+ Post,
|
|
|
+ Put,
|
|
|
+ Delete,
|
|
|
+ Body,
|
|
|
+ Param,
|
|
|
+ Query,
|
|
|
+ NotFoundException,
|
|
|
+} from '@nestjs/common';
|
|
|
import { PlantationTreeService } from './plantation-tree.service';
|
|
|
-import { PlantationNodeData } from './plantation-node-data.interface';
|
|
|
+import { PlantationNodeData, Worker, Task } from './plantation-node-data.interface';
|
|
|
import { TreeNode } from 'src/services/tree.service';
|
|
|
|
|
|
@Controller('plantation-tree')
|
|
|
export class PlantationTreeController {
|
|
|
- constructor(private readonly treeService: PlantationTreeService) {}
|
|
|
+ constructor(private readonly treeService: PlantationTreeService) { }
|
|
|
|
|
|
+ /** --- READ: Get entire tree --- */
|
|
|
@Get()
|
|
|
getTree() {
|
|
|
return this.treeService.getTree();
|
|
|
}
|
|
|
|
|
|
+ /** --- CREATE: add a new node under parent (single or multiple nodes) --- */
|
|
|
@Post('add/:parentId')
|
|
|
addNode(
|
|
|
@Param('parentId') parentId: string,
|
|
|
- @Body() node: TreeNode<PlantationNodeData>,
|
|
|
+ @Body() nodeOrNodes: TreeNode<PlantationNodeData> | TreeNode<PlantationNodeData>[] // single or array
|
|
|
) {
|
|
|
- return this.treeService.addNode(parentId, node);
|
|
|
+ const parentNode = this.treeService.findNode(parentId);
|
|
|
+ if (!parentNode) throw new NotFoundException(`Parent node ${parentId} not found`);
|
|
|
+
|
|
|
+ const nodesArray = Array.isArray(nodeOrNodes) ? nodeOrNodes : [nodeOrNodes];
|
|
|
+ const results: TreeNode<PlantationNodeData>[] = [];
|
|
|
+
|
|
|
+ for (const node of nodesArray) {
|
|
|
+ results.push(this.treeService.addNode(parentId, node));
|
|
|
+ }
|
|
|
+
|
|
|
+ return results;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** --- READ: find a node by ID --- */
|
|
|
+ @Get('find/:nodeId')
|
|
|
+ findNode(@Param('nodeId') nodeId: string) {
|
|
|
+ const node = this.treeService.findNode(nodeId);
|
|
|
+ if (!node) throw new NotFoundException(`Node ${nodeId} not found`);
|
|
|
+ return node;
|
|
|
}
|
|
|
|
|
|
+ /** --- READ: find a node by name --- */
|
|
|
+ @Get('search')
|
|
|
+ searchNode(@Query('name') name: string) {
|
|
|
+ const node = this.treeService.findNodeByName(name);
|
|
|
+ if (!node) throw new NotFoundException(`Node with name "${name}" not found`);
|
|
|
+ return node;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** --- UPDATE: update node data by ID --- */
|
|
|
+ @Put('update/:nodeId')
|
|
|
+ updateNode(
|
|
|
+ @Param('nodeId') nodeId: string,
|
|
|
+ @Body() newData: Partial<PlantationNodeData>,
|
|
|
+ ) {
|
|
|
+ const node = this.treeService.findNode(nodeId);
|
|
|
+ if (!node) throw new NotFoundException(`Node ${nodeId} not found`);
|
|
|
+ return this.treeService.updateNode(nodeId, newData);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** --- UPDATE: update metadata --- */
|
|
|
+ @Put('metadata/:nodeId')
|
|
|
+ updateMetadata(@Param('nodeId') nodeId: string, @Body() metadata: Record<string, any>) {
|
|
|
+ const node = this.treeService.findNode(nodeId);
|
|
|
+ if (!node) throw new NotFoundException(`Node ${nodeId} not found`);
|
|
|
+ return this.treeService.updateMetadata(nodeId, metadata);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** --- DELETE: remove a node by ID --- */
|
|
|
@Delete('remove/:nodeId')
|
|
|
removeNode(@Param('nodeId') nodeId: string) {
|
|
|
+ const node = this.treeService.findNode(nodeId);
|
|
|
+ if (!node) throw new NotFoundException(`Node ${nodeId} not found`);
|
|
|
this.treeService.removeNode(nodeId);
|
|
|
return { success: true };
|
|
|
}
|
|
|
|
|
|
- @Get('find/:nodeId')
|
|
|
- findNode(@Param('nodeId') nodeId: string) {
|
|
|
- return this.treeService.findNode(nodeId);
|
|
|
+ /** --- WORKERS: Assign worker(s) to node (delegates to service) --- */
|
|
|
+ @Post('add/:nodeId/workers') // plural
|
|
|
+ assignWorkers(
|
|
|
+ @Param('nodeId') nodeId: string,
|
|
|
+ @Body() workers: Worker | Worker[] // accept single or array
|
|
|
+ ) {
|
|
|
+ const node = this.treeService.findNode(nodeId);
|
|
|
+ if (!node) throw new NotFoundException(`Node ${nodeId} not found`);
|
|
|
+
|
|
|
+ const workerArray = Array.isArray(workers) ? workers : [workers];
|
|
|
+ const results: TreeNode<PlantationNodeData>[] = [];
|
|
|
+
|
|
|
+ for (const worker of workerArray) {
|
|
|
+ results.push(this.treeService.assignWorker(nodeId, worker));
|
|
|
+ }
|
|
|
+
|
|
|
+ return results;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** --- TASKS: Assign task(s) to a worker by workerId --- */
|
|
|
+ @Post('worker/:workerId/tasks')
|
|
|
+ assignTasksByWorker(
|
|
|
+ @Param('workerId') workerId: string,
|
|
|
+ @Body() tasks: Task | Task[]
|
|
|
+ ) {
|
|
|
+ const taskArray = Array.isArray(tasks) ? tasks : [tasks];
|
|
|
+ const results: Worker[] = [];
|
|
|
+
|
|
|
+ for (const task of taskArray) {
|
|
|
+ results.push(this.treeService.assignTaskToWorkerById(workerId, task));
|
|
|
+ }
|
|
|
+
|
|
|
+ return results;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /** --- READ: Get all workers under a node (recursive) --- */
|
|
|
+ @Get(':nodeId/workers')
|
|
|
+ getWorkersUnderNode(@Param('nodeId') nodeId: string) {
|
|
|
+ const node = this.treeService.findNode(nodeId);
|
|
|
+ if (!node) throw new NotFoundException(`Node ${nodeId} not found`);
|
|
|
+ return this.treeService.getWorkersUnderNode(nodeId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** --- READ: Get a worker by unique ID --- */
|
|
|
+ @Get('worker/:workerId')
|
|
|
+ getWorker(@Param('workerId') workerId: string) {
|
|
|
+ return this.treeService.getWorkerById(workerId);
|
|
|
}
|
|
|
}
|