1- import { Request , Response , NextFunction } from 'express'
1+ import { NextFunction , Request , Response } from 'express'
22
33import CategoryScoreService from './categoryScore.service'
44
55import { NotFound , Updated } from '../../utils/apiResponse.utils'
6-
76import { serialize } from './categoryScore.serializer'
7+ import { CategoryScorePatch , CategoryScorePost } from 'devu-shared-modules'
88
9- export async function get ( req : Request , res : Response , next : NextFunction ) {
9+ export async function listByCourse ( req : Request , res : Response , next : NextFunction ) {
1010 try {
11- const categoryScores = await CategoryScoreService . list ( )
11+ const courseId = parseInt ( req . params . courseId , 10 )
12+ if ( isNaN ( courseId ) ) {
13+ return res . status ( 400 ) . json ( { message : 'Invalid course ID format' } )
14+ }
15+
16+ const categoryScores = await CategoryScoreService . listByCourse ( courseId )
1217 const response = categoryScores . map ( serialize )
1318
1419 res . status ( 200 ) . json ( response )
@@ -17,24 +22,43 @@ export async function get(req: Request, res: Response, next: NextFunction) {
1722 }
1823}
1924
20- export async function getByCourse ( req : Request , res : Response , next : NextFunction ) {
25+ export async function detailById ( req : Request , res : Response , next : NextFunction ) {
2126 try {
22- const courseId = parseInt ( req . params . courseId )
23- const categoryScores = await CategoryScoreService . listByCourse ( courseId )
24- const response = categoryScores . map ( serialize )
27+ const id = parseInt ( req . params . id , 10 )
28+ if ( isNaN ( id ) ) {
29+ return res . status ( 400 ) . json ( { message : 'Invalid category score ID format' } )
30+ }
31+
32+ const categoryScore = await CategoryScoreService . retrieve ( id )
33+
34+ if ( ! categoryScore ) {
35+ // Use the imported NotFound response utility
36+ return res . status ( 404 ) . json ( NotFound )
37+ }
38+
39+ // Serialize the single entity
40+ const response = serialize ( categoryScore )
2541
2642 res . status ( 200 ) . json ( response )
2743 } catch ( err ) {
2844 next ( err )
2945 }
3046}
3147
32- export async function detail ( req : Request , res : Response , next : NextFunction ) {
48+ export async function detailByName ( req : Request , res : Response , next : NextFunction ) {
3349 try {
34- const id = parseInt ( req . params . id )
35- const categoryScore = await CategoryScoreService . retrieve ( id )
50+ // Category name is likely a string, no need to parse as int
51+ const categoryName = req . params . categoryName
52+ if ( ! categoryName ) {
53+ return res . status ( 400 ) . json ( { message : 'Category name parameter is required' } )
54+ }
55+
56+ // Use the retrieveByName method from the service
57+ const categoryScore = await CategoryScoreService . retrieveByName ( categoryName )
3658
37- if ( ! categoryScore ) return res . status ( 404 ) . json ( NotFound )
59+ if ( ! categoryScore ) {
60+ return res . status ( 404 ) . json ( NotFound )
61+ }
3862
3963 const response = serialize ( categoryScore )
4064
@@ -44,40 +68,66 @@ export async function detail(req: Request, res: Response, next: NextFunction) {
4468 }
4569}
4670
71+
4772export async function post ( req : Request , res : Response , next : NextFunction ) {
4873 try {
49- const categoryScore = await CategoryScoreService . create ( req . body )
50- const response = serialize ( categoryScore )
74+ const courseId = parseInt ( req . params . courseId , 10 )
75+ const categoryScoreDto : CategoryScorePost = req . body
76+ categoryScoreDto . courseId = courseId
5177
78+ const newCategoryScore = await CategoryScoreService . create ( categoryScoreDto )
79+
80+ const response = serialize ( newCategoryScore )
5281 res . status ( 201 ) . json ( response )
5382 } catch ( err ) {
5483 next ( err )
5584 }
5685}
5786
58- export async function put ( req : Request , res : Response , next : NextFunction ) {
87+ export async function update ( req : Request , res : Response , next : NextFunction ) {
5988 try {
60- req . body . id = parseInt ( req . params . id )
61- const results = await CategoryScoreService . update ( req . body )
89+ const id = parseInt ( req . params . id , 10 )
90+ if ( isNaN ( id ) ) {
91+ return res . status ( 400 ) . json ( { message : 'Invalid category score ID format' } )
92+ }
93+
94+ const categoryScoreDto : CategoryScorePatch = req . body
95+ const results = await CategoryScoreService . update ( id , categoryScoreDto )
6296
63- if ( ! results . affected ) return res . status ( 404 ) . json ( NotFound )
97+ if ( ! results . affected || results . affected === 0 ) {
98+ return res . status ( 404 ) . json ( NotFound )
99+ }
64100
65101 res . status ( 200 ) . json ( Updated )
66102 } catch ( err ) {
67103 next ( err )
68104 }
69105}
106+
70107export async function _delete ( req : Request , res : Response , next : NextFunction ) {
71108 try {
72- const id = parseInt ( req . params . id )
109+ const id = parseInt ( req . params . id , 10 )
110+ if ( isNaN ( id ) ) {
111+ return res . status ( 400 ) . json ( { message : 'Invalid category score ID format' } )
112+ }
113+
73114 const results = await CategoryScoreService . _delete ( id )
74115
75- if ( ! results . affected ) return res . status ( 404 ) . json ( NotFound )
116+ if ( ! results . affected || results . affected === 0 ) {
117+ return res . status ( 404 ) . json ( NotFound )
118+ }
76119
77120 res . status ( 204 ) . send ( )
78121 } catch ( err ) {
79122 next ( err )
80123 }
81124}
82125
83- export default { get, getByCourse, detail, post, put, _delete }
126+ export default {
127+ listByCourse,
128+ detailById,
129+ detailByName,
130+ post,
131+ update,
132+ _delete,
133+ }
0 commit comments