From 451c752170747d5e155c748c856bbbbad7f6fbbd Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Tue, 19 May 2026 11:15:42 -0700 Subject: [PATCH] add tags controller and routes --- server/src/controllers/tagController.ts | 98 +++++++++++++++++++++++++ server/src/routes/tagRoutes.ts | 28 +++++++ 2 files changed, 126 insertions(+) create mode 100644 server/src/controllers/tagController.ts create mode 100644 server/src/routes/tagRoutes.ts diff --git a/server/src/controllers/tagController.ts b/server/src/controllers/tagController.ts new file mode 100644 index 000000000..66b21dc6b --- /dev/null +++ b/server/src/controllers/tagController.ts @@ -0,0 +1,98 @@ +import { Request, Response, NextFunction } from "express"; +import { ITagsService } from "@/service/index.js"; +import { requireTeamId } from "./controllerUtils.js"; +import { + createTagBodyValidation, + editTagBodyValidation, + getTagByIdParamValidation, + editTagParamValidation, + deleteTagParamValidation, +} from "@/validation/index.js"; + +export interface ITagsController { + createTag: (req: Request, res: Response, next: NextFunction) => Promise; + getTagById: (req: Request, res: Response, next: NextFunction) => Promise; + getTagsByTeamId: (req: Request, res: Response, next: NextFunction) => Promise; + editTag: (req: Request, res: Response, next: NextFunction) => Promise; + deleteTag: (req: Request, res: Response, next: NextFunction) => Promise; +} + +class TagsController implements ITagsController { + constructor(private tagsService: ITagsService) {} + + createTag = async (req: Request, res: Response, next: NextFunction) => { + try { + const validatedBody = createTagBodyValidation.parse(req.body); + const teamId = requireTeamId(req.user?.teamId); + + const tag = await this.tagsService.createTag(validatedBody, teamId); + return res.status(200).json({ + success: true, + msg: "Tag created successfully", + data: tag, + }); + } catch (error) { + next(error); + } + }; + getTagById = async (req: Request, res: Response, next: NextFunction) => { + try { + const teamId = requireTeamId(req.user?.teamId); + const { id: tagId } = getTagByIdParamValidation.parse(req.params); + const tag = await this.tagsService.getTag(tagId, teamId); + return res.status(200).json({ + success: true, + msg: "Tag retrieved successfully", + data: tag, + }); + } catch (error) { + next(error); + } + }; + + getTagsByTeamId = async (req: Request, res: Response, next: NextFunction) => { + try { + const teamId = requireTeamId(req.user?.teamId); + const tags = await this.tagsService.getTagsByTeamId(teamId); + return res.status(200).json({ + success: true, + msg: "Tags retrieved successfully", + data: tags, + }); + } catch (error) { + next(error); + } + }; + + editTag = async (req: Request, res: Response, next: NextFunction) => { + try { + const teamId = requireTeamId(req.user?.teamId); + const { id: tagId } = editTagParamValidation.parse(req.params); + const validatedBody = editTagBodyValidation.parse(req.body); + const updatedTag = await this.tagsService.updateTag(tagId, teamId, validatedBody); + return res.status(200).json({ + success: true, + msg: "Tag updated successfully", + data: updatedTag, + }); + } catch (error) { + next(error); + } + }; + + deleteTag = async (req: Request, res: Response, next: NextFunction) => { + try { + const teamId = requireTeamId(req.user?.teamId); + const { id: tagId } = deleteTagParamValidation.parse(req.params); + await this.tagsService.deleteTag(tagId, teamId); + return res.status(200).json({ + success: true, + msg: "Tag deleted successfully", + }); + } catch (error) { + next(error); + } + }; +} + +export default TagsController; diff --git a/server/src/routes/tagRoutes.ts b/server/src/routes/tagRoutes.ts new file mode 100644 index 000000000..9b237c976 --- /dev/null +++ b/server/src/routes/tagRoutes.ts @@ -0,0 +1,28 @@ +import { ITagsController } from "@/controllers/tagController.js"; +import { Router } from "express"; +class TagRoutes { + private router: Router; + private tagController: ITagsController; + + constructor(tagController: ITagsController) { + this.router = Router(); + this.tagController = tagController; + this.initializeRoutes(); + } + + initializeRoutes() { + this.router.post("/", this.tagController.createTag); + + this.router.get("/team", this.tagController.getTagsByTeamId); + + this.router.get("/:id", this.tagController.getTagById); + this.router.delete("/:id", this.tagController.deleteTag); + this.router.patch("/:id", this.tagController.editTag); + } + + getRouter() { + return this.router; + } +} + +export default TagRoutes;