|
1 | | -import Course from '#models/course' |
2 | | -import { createCourseValidator } from '#validators/course' |
3 | | -import type { HttpContext } from '@adonisjs/core/http' |
| 1 | +import assert from "node:assert"; |
| 2 | + |
| 3 | +import type { HttpContext } from "@adonisjs/core/http"; |
| 4 | + |
| 5 | +import Course from "#models/course"; |
| 6 | +import { createCourseValidator } from "#validators/course"; |
4 | 7 |
|
5 | 8 | export default class CoursesController { |
6 | 9 | /** |
7 | 10 | * Display a list of courses in matching registration |
8 | 11 | */ |
9 | 12 | async index({ params }: HttpContext) { |
10 | | - const registrationId = decodeURIComponent(params.registration_id) |
| 13 | + assert(typeof params.registration_id === "string"); |
| 14 | + |
| 15 | + const registrationId = decodeURIComponent(params.registration_id); |
11 | 16 | if (registrationId) { |
12 | | - return await Course.query().where('registrationId', registrationId).preload('groups') |
| 17 | + return await Course.query() |
| 18 | + .where("registrationId", registrationId) |
| 19 | + .preload("groups"); |
13 | 20 | } |
14 | | - return [] |
| 21 | + return []; |
15 | 22 | } |
16 | 23 |
|
17 | 24 | /** |
18 | 25 | * Handle form submission for the create action |
19 | 26 | */ |
20 | 27 | async store({ request, params }: HttpContext) { |
21 | | - const registrationId = decodeURIComponent(params.registration_id) |
22 | | - const payload = await request.validateUsing(createCourseValidator) |
23 | | - const course = await Course.create({ ...payload, registrationId }) |
24 | | - return { message: 'Course created.', course } |
| 28 | + assert(typeof params.registration_id === "string"); |
| 29 | + |
| 30 | + const registrationId = decodeURIComponent(params.registration_id); |
| 31 | + const payload = await request.validateUsing(createCourseValidator); |
| 32 | + const course = await Course.create({ ...payload, registrationId }); |
| 33 | + return { message: "Course created.", course }; |
25 | 34 | } |
26 | 35 |
|
27 | 36 | /** |
28 | 37 | * Show individual record of course in matching registration |
29 | 38 | */ |
30 | 39 | async show({ params }: HttpContext) { |
31 | | - const registrationId = decodeURIComponent(params.registration_id) |
| 40 | + assert(typeof params.registration_id === "string"); |
| 41 | + const registrationId = decodeURIComponent(params.registration_id); |
32 | 42 | if (registrationId) { |
| 43 | + assert(typeof params.id === "string"); |
| 44 | + |
33 | 45 | return await Course.query() |
34 | | - .where('registrationId', registrationId) |
35 | | - .andWhere('id', params.id) |
36 | | - .preload('groups') |
37 | | - .firstOrFail() |
| 46 | + .where("registrationId", registrationId) |
| 47 | + .andWhere("id", params.id) |
| 48 | + .preload("groups") |
| 49 | + .firstOrFail(); |
38 | 50 | } |
39 | | - return {} |
| 51 | + return {}; |
40 | 52 | } |
41 | 53 |
|
42 | 54 | /** |
43 | 55 | * Handle form submission for the edit action |
44 | 56 | */ |
45 | 57 | async update({ params, request }: HttpContext) { |
46 | | - const registrationId = decodeURIComponent(params.registration_id) |
47 | | - const payload = await request.validateUsing(createCourseValidator) |
| 58 | + assert(typeof params.registration_id === "string"); |
| 59 | + assert(typeof params.id === "string"); |
| 60 | + |
| 61 | + const registrationId = decodeURIComponent(params.registration_id); |
| 62 | + const payload = await request.validateUsing(createCourseValidator); |
48 | 63 |
|
49 | 64 | const course = await Course.query() |
50 | | - .where('registrationId', registrationId) |
51 | | - .andWhere('id', params.id) |
52 | | - .firstOrFail() |
| 65 | + .where("registrationId", registrationId) |
| 66 | + .andWhere("id", params.id) |
| 67 | + .firstOrFail(); |
53 | 68 |
|
54 | | - course.merge(payload) |
55 | | - await course.save() |
| 69 | + course.merge(payload); |
| 70 | + await course.save(); |
56 | 71 |
|
57 | | - return { message: 'Course updated successfully.', course } |
| 72 | + return { message: "Course updated successfully.", course }; |
58 | 73 | } |
59 | 74 |
|
60 | 75 | /** |
61 | 76 | * Delete record |
62 | 77 | */ |
63 | 78 | async destroy({ params }: HttpContext) { |
64 | | - const course = await Course.findOrFail(params.id) |
65 | | - await course.delete() |
66 | | - return { message: 'Course successfully deleted.' } |
| 79 | + const course = await Course.findOrFail(params.id); |
| 80 | + await course.delete(); |
| 81 | + return { message: "Course successfully deleted." }; |
67 | 82 | } |
68 | 83 | } |
0 commit comments