|
| 1 | +import "@typespec/http"; |
| 2 | +import "@typespec/openapi"; |
| 3 | + |
| 4 | +using Http; |
| 5 | + |
| 6 | +@tag("Responses") |
| 7 | +namespace CoreSystem.Responses { |
| 8 | + @doc("The available answer types") |
| 9 | + enum AnswerType { |
| 10 | + shortText: "short_text", |
| 11 | + longText: "long_text", |
| 12 | + singleChoice: "single_choice", |
| 13 | + multipleChoice: "multiple_choice", |
| 14 | + date: "date", |
| 15 | + } |
| 16 | + |
| 17 | + @doc("A form response submission") |
| 18 | + model Response { |
| 19 | + @doc("The response's unique identifier.") |
| 20 | + id: uuid; |
| 21 | + |
| 22 | + @doc("The form that this response belongs to.") |
| 23 | + formId: uuid; |
| 24 | + |
| 25 | + @doc("The user who submitted this response.") |
| 26 | + submittedBy: uuid; |
| 27 | + |
| 28 | + @doc("The creation timestamp of the response.") |
| 29 | + createdAt: utcDateTime; |
| 30 | + |
| 31 | + @doc("The last updated timestamp of the response.") |
| 32 | + updatedAt: utcDateTime; |
| 33 | + } |
| 34 | + |
| 35 | + @doc("An individual answer") |
| 36 | + model Answer { |
| 37 | + @doc("The answer's unique identifier.") |
| 38 | + id: uuid; |
| 39 | + |
| 40 | + @doc("The response this answer belongs to.") |
| 41 | + responseId: uuid; |
| 42 | + |
| 43 | + @doc("The question this answer is for.") |
| 44 | + questionId: uuid; |
| 45 | + |
| 46 | + @doc("The type of answer, determines how to parse value.") |
| 47 | + type: AnswerType; |
| 48 | + |
| 49 | + @doc("The answer value stored as string, parsed based on type.") |
| 50 | + value: string; |
| 51 | + |
| 52 | + @doc("The creation timestamp of the answer.") |
| 53 | + createdAt: utcDateTime; |
| 54 | + |
| 55 | + @doc("The last updated timestamp of the answer.") |
| 56 | + updatedAt: utcDateTime; |
| 57 | + } |
| 58 | + |
| 59 | + @doc("Request model for submitting an individual answer") |
| 60 | + model AnswerRequest { |
| 61 | + @doc("The question being answered.") |
| 62 | + questionId: uuid; |
| 63 | + |
| 64 | + @doc("The answer value (format depends on questionType from questionId).") |
| 65 | + value: string; |
| 66 | + } |
| 67 | + |
| 68 | + @doc("Request model for submitting a complete form response") |
| 69 | + model SubmitFormResponseRequest { |
| 70 | + @doc("All answers for this form submission.") |
| 71 | + answers: AnswerRequest[]; |
| 72 | + } |
| 73 | + |
| 74 | + @doc("Complete response with all related data") |
| 75 | + model FormResponse { |
| 76 | + @doc("The response metadata.") |
| 77 | + response: Response; |
| 78 | + |
| 79 | + @doc("All answers for this response.") |
| 80 | + answers: Answer[]; |
| 81 | + } |
| 82 | + |
| 83 | + // API Endpoints |
| 84 | + |
| 85 | + @doc("List all responses for a specific form.") |
| 86 | + @route("/forms/{formId}/responses") |
| 87 | + @get |
| 88 | + op listFormResponses(@path formId: uuid): { |
| 89 | + @statusCode statusCode: 200; |
| 90 | + @body responses: Response[]; |
| 91 | + } | { |
| 92 | + @statusCode statusCode: 404; |
| 93 | + @body error: NotFound; |
| 94 | + }; |
| 95 | + |
| 96 | + @doc("Get a specific response by ID.") |
| 97 | + @route("/forms/{formId}/responses/{responseId}") |
| 98 | + @get |
| 99 | + op getFormResponse(@path formId: uuid, @path responseId: uuid): { |
| 100 | + @statusCode statusCode: 200; |
| 101 | + @body response: FormResponse; |
| 102 | + } | { |
| 103 | + @statusCode statusCode: 404; |
| 104 | + @body error: NotFound; |
| 105 | + }; |
| 106 | + |
| 107 | + @doc("Delete a response and all its associated data.") |
| 108 | + @route("/forms/{formId}/responses/{responseId}") |
| 109 | + @delete |
| 110 | + op deleteFormResponse(@path formId: uuid, @path responseId: uuid): { |
| 111 | + @statusCode statusCode: 204; |
| 112 | + } | { |
| 113 | + @statusCode statusCode: 404; |
| 114 | + @body error: NotFound; |
| 115 | + }; |
| 116 | + |
| 117 | + @doc("Submit a new response to a form.") |
| 118 | + @route("/forms/{formId}/responses") |
| 119 | + @post |
| 120 | + op submitFormResponse( |
| 121 | + @path formId: uuid, |
| 122 | + @body request: SubmitFormResponseRequest, |
| 123 | + ): { |
| 124 | + @statusCode statusCode: 201; |
| 125 | + @body response: Response; |
| 126 | + } | { |
| 127 | + @statusCode statusCode: 404; |
| 128 | + @body error: NotFound; |
| 129 | + } | { |
| 130 | + @statusCode statusCode: 400; |
| 131 | + @body error: ProblemDetail; |
| 132 | + }; |
| 133 | + |
| 134 | + @doc("Get all answers for a specific question across all form responses.") |
| 135 | + @route("/forms/{formId}/questions/{questionId}") |
| 136 | + @get |
| 137 | + op getQuestionAnswers(@path formId: uuid, @path questionId: uuid): { |
| 138 | + @statusCode statusCode: 200; |
| 139 | + @body data: { |
| 140 | + answers: Answer[]; |
| 141 | + }; |
| 142 | + } | { |
| 143 | + @statusCode statusCode: 404; |
| 144 | + @body error: NotFound; |
| 145 | + }; |
| 146 | +} |
0 commit comments