@@ -5,13 +5,15 @@ import { MuscleModel } from '../models/muscle.model'
5
5
import { MuscleService } from '../services'
6
6
import Muscle from '#infra/mongodb/models/muscles/muscle.schema.js'
7
7
import { HTTPException } from 'hono/http-exception'
8
+ import Exercise from '#infra/mongodb/models/exercises/exercise.schema.js'
9
+ import { ExerciseModel } from '#modules/exercises/models/exercise.model.js'
8
10
9
11
export class MuscleController implements Routes {
10
12
public controller : OpenAPIHono
11
13
private readonly muscleService : MuscleService
12
14
constructor ( ) {
13
15
this . controller = new OpenAPIHono ( )
14
- this . muscleService = new MuscleService ( Muscle )
16
+ this . muscleService = new MuscleService ( Muscle , Exercise )
15
17
}
16
18
17
19
public initRoutes ( ) {
@@ -129,5 +131,88 @@ export class MuscleController implements Routes {
129
131
return ctx . json ( { success : true , data : response } )
130
132
}
131
133
)
134
+
135
+ this . controller . openapi (
136
+ createRoute ( {
137
+ method : 'get' ,
138
+ path : '/muscles/{muscleName}/exercises' ,
139
+ tags : [ 'Muscles' ] ,
140
+ summary : 'Retrive exercises by muscles' ,
141
+ description : 'Retrive list of exercises by targetMuscles or secondaryMuscles.' ,
142
+ operationId : 'getExercisesByEquipment' ,
143
+ request : {
144
+ params : z . object ( {
145
+ muscleName : z . string ( ) . openapi ( {
146
+ description : 'muscles name' ,
147
+ type : 'string' ,
148
+ example : 'upper back' ,
149
+ default : 'upper back'
150
+ } )
151
+ } ) ,
152
+ query : z . object ( {
153
+ offset : z . coerce . number ( ) . nonnegative ( ) . optional ( ) . openapi ( {
154
+ title : 'Offset' ,
155
+ description :
156
+ 'The number of exercises to skip from the start of the list. Useful for pagination to fetch subsequent pages of results.' ,
157
+ type : 'number' ,
158
+ example : 10 ,
159
+ default : 0
160
+ } ) ,
161
+ limit : z . coerce . number ( ) . positive ( ) . max ( 100 ) . optional ( ) . openapi ( {
162
+ title : 'Limit' ,
163
+ description :
164
+ 'The maximum number of exercises to return in the response. Limits the number of results for pagination purposes.' ,
165
+ maximum : 100 ,
166
+ minimum : 1 ,
167
+ type : 'number' ,
168
+ example : 10 ,
169
+ default : 10
170
+ } )
171
+ } )
172
+ } ,
173
+ responses : {
174
+ 200 : {
175
+ description : 'Successful response with list of all exercises.' ,
176
+ content : {
177
+ 'application/json' : {
178
+ schema : z . object ( {
179
+ success : z . boolean ( ) . openapi ( {
180
+ description : 'Indicates whether the request was successful' ,
181
+ type : 'boolean' ,
182
+ example : true
183
+ } ) ,
184
+ data : z . array ( ExerciseModel ) . openapi ( {
185
+ description : 'Array of Exercises.'
186
+ } )
187
+ } )
188
+ }
189
+ }
190
+ } ,
191
+ 500 : {
192
+ description : 'Internal server error'
193
+ }
194
+ }
195
+ } ) ,
196
+ async ( ctx ) => {
197
+ const { offset, limit = 10 } = ctx . req . valid ( 'query' )
198
+ const search = ctx . req . param ( 'muscleName' )
199
+ const { origin, pathname } = new URL ( ctx . req . url )
200
+ const response = await this . muscleService . getExercisesByMuscles ( { offset, limit, search } )
201
+ return ctx . json ( {
202
+ success : true ,
203
+ data : {
204
+ previousPage :
205
+ response . currentPage > 1
206
+ ? `${ origin } ${ pathname } ?offset=${ ( response . currentPage - 1 ) * limit } &limit=${ limit } `
207
+ : null ,
208
+ nextPage :
209
+ response . currentPage < response . totalPages
210
+ ? `${ origin } ${ pathname } ?offset=${ response . currentPage * limit } &limit=${ limit } `
211
+ : null ,
212
+ ...response
213
+ }
214
+ } )
215
+ }
216
+ )
132
217
}
133
218
}
0 commit comments