1
18import { Body, Controller, Delete, Get, Inject, Param, Patch, Post, Req } from "@nestjs/common";
19import type { Request } from "express";
20import { FormsService } from "./forms.service.js";
21import { QuestionsService } from "./questions.service.js";
22import { ZodValidate } from "../../common/pipes/zod-validation.pipe.js";
23import { requireUser } from "../../common/types.js";
24import { CreateFormDto, UpdateFormDto, type CreateFormInput, type UpdateFormInput } from "./form.dto.js";
25import {
26 CreateQuestionDto,
27 ReorderQuestionsDto,
28 UpdateQuestionDto,
29 type CreateQuestionInput,
30 type ReorderQuestionsInput,
31 type UpdateQuestionInput
32} from "./question.dto.js";
33
34@Controller()
35export class FormsController {
36 constructor(
37 @Inject(FormsService) private readonly forms: FormsService,
38 @Inject(QuestionsService) private readonly questions: QuestionsService
39 ) {}
40
41
42
43 @Get("databases/:databaseId/forms")
44 list(@Param("databaseId") databaseId: string, @Req() req: Request) {
45 return this.forms.listByDatabase(databaseId, requireUser(req));
46 }
47
48 @Post("databases/:databaseId/forms")
49 create(
50 @Param("databaseId") databaseId: string,
51 @Body(ZodValidate(CreateFormDto)) body: CreateFormInput,
52 @Req() req: Request
53 ) {
54 return this.forms.create(databaseId, requireUser(req), body, req);
55 }
56
57 @Get("forms/:formId")
58 get(@Param("formId") formId: string, @Req() req: Request) {
59 return this.forms.get(formId, requireUser(req));
60 }
61
62 @Patch("forms/:formId")
63 update(
64 @Param("formId") formId: string,
65 @Body(ZodValidate(UpdateFormDto)) body: UpdateFormInput,
66 @Req() req: Request
67 ) {
68 return this.forms.update(formId, requireUser(req), body, req);
69 }
70
71 @Delete("forms/:formId")
72 remove(@Param("formId") formId: string, @Req() req: Request) {
73 return this.forms.remove(formId, requireUser(req), req);
74 }
75
76
77
78 @Get("forms/:formId/questions")
79 listQuestions(@Param("formId") formId: string, @Req() req: Request) {
80 requireUser(req);
81 return this.questions.list(formId);
82 }
83
84 @Post("forms/:formId/questions")
85 createQuestion(
86 @Param("formId") formId: string,
87 @Body(ZodValidate(CreateQuestionDto)) body: CreateQuestionInput,
88 @Req() req: Request
89 ) {
90 return this.questions.create(formId, requireUser(req), body);
91 }
92
93 @Patch("forms/:formId/questions/:qid")
94 updateQuestion(
95 @Param("formId") formId: string,
96 @Param("qid") qid: string,
97 @Body(ZodValidate(UpdateQuestionDto)) body: UpdateQuestionInput,
98 @Req() req: Request
99 ) {
100 return this.questions.update(formId, qid, requireUser(req), body);
101 }
102
103 @Delete("forms/:formId/questions/:qid")
104 deleteQuestion(@Param("formId") formId: string, @Param("qid") qid: string, @Req() req: Request) {
105 return this.questions.delete(formId, qid, requireUser(req));
106 }
107
108 @Post("forms/:formId/questions/reorder")
109 reorder(
110 @Param("formId") formId: string,
111 @Body(ZodValidate(ReorderQuestionsDto)) body: ReorderQuestionsInput,
112 @Req() req: Request
113 ) {
114 return this.questions.reorder(formId, requireUser(req), body);
115 }
116}
117