-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(modules): ✨ added bodyPart module
add create bodypart endpoint add get all bodyparts
- Loading branch information
1 parent
7c1cda1
commit 84a556f
Showing
13 changed files
with
211 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { Document, Model } from 'mongoose' | ||
import mongoose, { Document, Model } from 'mongoose' | ||
|
||
export interface IBodyPart { | ||
name: string | ||
} | ||
export interface IBodyPartDoc extends IBodyPart, Document {} | ||
export type IBodyPartModel = Model<IBodyPartDoc> | ||
export interface IBodyPartModel extends Model<IBodyPartDoc> { | ||
isBodyPartExist(name: string, excludeBodyPartId?: mongoose.ObjectId): Promise<boolean> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
src/modules/bodyparts/controllers/bodyPart.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { Routes } from '#common/types/route.type.js' | ||
import { createRoute, OpenAPIHono } from '@hono/zod-openapi' | ||
import { z } from 'zod' | ||
import { BodyPartService } from '../services' | ||
import BodyPart from '#infra/mongodb/models/bodyparts/bodypart.schema.js' | ||
import { BodyPartModel } from '../models/bodyPart.model' | ||
|
||
export class BodyPartController implements Routes { | ||
public controller: OpenAPIHono | ||
private readonly bodyPartService: BodyPartService | ||
constructor() { | ||
this.controller = new OpenAPIHono() | ||
this.bodyPartService = new BodyPartService(BodyPart) | ||
} | ||
|
||
public initRoutes() { | ||
this.controller.openapi( | ||
createRoute({ | ||
method: 'post', | ||
path: '/bodyparts', | ||
tags: ['BodyParts'], | ||
summary: 'Add a new bodyPart to the database', | ||
description: 'This route is used to add a new bodyPart name to the database.', | ||
operationId: 'createbodyPart', | ||
request: { | ||
body: { | ||
content: { | ||
'application/json': { | ||
schema: z.object({ | ||
name: z.string().openapi({ | ||
title: 'BodyPart Name', | ||
description: 'The name of the bodyPart to be added', | ||
type: 'string', | ||
example: 'waist' | ||
}) | ||
}) | ||
} | ||
} | ||
} | ||
}, | ||
responses: { | ||
201: { | ||
description: 'bodyPart successfully added to the database', | ||
content: { | ||
'application/json': { | ||
schema: z.object({ | ||
success: z.boolean().openapi({ | ||
description: 'Indicates whether the request was successful', | ||
type: 'boolean', | ||
example: true | ||
}), | ||
data: z.array(BodyPartModel).openapi({ | ||
title: 'Added bodyPart', | ||
description: 'The newly added bodyPart data' | ||
}) | ||
}) | ||
} | ||
} | ||
}, | ||
400: { | ||
description: 'Bad request - Invalid input data', | ||
content: { | ||
'application/json': { | ||
schema: z.object({ | ||
success: z.boolean(), | ||
error: z.string() | ||
}) | ||
} | ||
} | ||
}, | ||
409: { | ||
description: 'Conflict - bodyPart name already exists' | ||
}, | ||
500: { | ||
description: 'Internal server error' | ||
} | ||
} | ||
}), | ||
async (ctx) => { | ||
const body = await ctx.req.json() | ||
const response = await this.bodyPartService.createBodyPart(body) | ||
return ctx.json({ success: true, data: [response] }) | ||
} | ||
) | ||
this.controller.openapi( | ||
createRoute({ | ||
method: 'get', | ||
path: '/bodyparts', | ||
tags: ['BodyParts'], | ||
summary: 'Retrive all bodyParts.', | ||
description: 'Retrive list of all bodyparts.', | ||
operationId: 'getBodyParts', | ||
responses: { | ||
200: { | ||
description: 'Successful response with list of all bodyparts.', | ||
content: { | ||
'application/json': { | ||
schema: z.object({ | ||
success: z.boolean().openapi({ | ||
description: 'Indicates whether the request was successful', | ||
type: 'boolean', | ||
example: true | ||
}), | ||
data: z.array(BodyPartModel).openapi({ | ||
description: 'Array of bodyparts.' | ||
}) | ||
}) | ||
} | ||
} | ||
}, | ||
500: { | ||
description: 'Internal server error' | ||
} | ||
} | ||
}), | ||
async (ctx) => { | ||
const response = await this.bodyPartService.getBodyParts() | ||
return ctx.json({ success: true, data: response }) | ||
} | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './bodyPart.controller' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { z } from 'zod' | ||
export const BodyPartModel = z.object({ | ||
name: z.string() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { IBodyPartModel } from '#infra/mongodb/models/bodyparts/bodypart.entity.js' | ||
import { CreateBodyPartArgs, CreateBodyPartUseCase } from '../use-cases/create-bodypart' | ||
import { GetBodyPartsUseCase } from '../use-cases/get-bodyparts' | ||
|
||
export class BodyPartService { | ||
private readonly createBodyPartUseCase: CreateBodyPartUseCase | ||
private readonly getBodyPartsUseCase: GetBodyPartsUseCase | ||
|
||
constructor(private readonly bodyPartModel: IBodyPartModel) { | ||
this.createBodyPartUseCase = new CreateBodyPartUseCase(bodyPartModel) | ||
this.getBodyPartsUseCase = new GetBodyPartsUseCase(bodyPartModel) | ||
} | ||
|
||
createBodyPart = (args: CreateBodyPartArgs) => { | ||
return this.createBodyPartUseCase.execute(args) | ||
} | ||
|
||
getBodyParts = () => { | ||
return this.getBodyPartsUseCase.execute() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './body-part.service' |
26 changes: 26 additions & 0 deletions
26
src/modules/bodyparts/use-cases/create-bodypart/create-bodypart.use-case.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { IUseCase } from '#common/types/use-case.type.js' | ||
import { IBodyPartDoc, IBodyPartModel } from '#infra/mongodb/models/bodyparts/bodypart.entity.js' | ||
import { HTTPException } from 'hono/http-exception' | ||
|
||
export interface CreateBodyPartArgs { | ||
name: string | ||
} | ||
|
||
export class CreateBodyPartUseCase implements IUseCase<CreateBodyPartArgs, IBodyPartDoc> { | ||
constructor(private readonly bodyPartModel: IBodyPartModel) {} | ||
|
||
async execute({ name }: CreateBodyPartArgs): Promise<IBodyPartDoc> { | ||
await this.checkIfBodyPartExists(name) | ||
return this.createBodyPart(name) | ||
} | ||
|
||
private async checkIfBodyPartExists(name: string): Promise<void> { | ||
if (await this.bodyPartModel.isBodyPartExist(name)) { | ||
throw new HTTPException(409, { message: 'BodyPart name already exists' }) | ||
} | ||
} | ||
|
||
private async createBodyPart(name: string): Promise<IBodyPartDoc> { | ||
return this.bodyPartModel.create({ name }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './create-bodypart.use-case' |
10 changes: 10 additions & 0 deletions
10
src/modules/bodyparts/use-cases/get-bodyparts/get-bodypart.usecase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { IUseCase } from '#common/types/use-case.type.js' | ||
import { IBodyPartDoc, IBodyPartModel } from '#infra/mongodb/models/bodyparts/bodypart.entity.js' | ||
|
||
export class GetBodyPartsUseCase implements IUseCase<void, IBodyPartDoc[]> { | ||
constructor(private readonly bodyPartModel: IBodyPartModel) {} | ||
|
||
async execute(): Promise<IBodyPartDoc[]> { | ||
return this.bodyPartModel.find({}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './get-bodypart.usecase' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import { DalService } from '#infra/mongodb/dal.service.js' | ||
import { BodyPartController } from '#modules/bodyparts/controllers/bodyPart.controller.js' | ||
import { EquipmentController } from '#modules/equipments/controllers/equipment.controller.js' | ||
import { MuscleController } from '#modules/muscle/controllers' | ||
import { App } from './app' | ||
|
||
const dalService = new DalService() | ||
const app = new App([new MuscleController(), new EquipmentController()]).getApp() | ||
const app = new App([new MuscleController(), new EquipmentController(), new BodyPartController()]).getApp() | ||
await dalService.connectDB() | ||
|
||
export default app |