-
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(module): ✨ added muscles module
- create Muscle Endpoint - retrive all Muscles endpoint
- Loading branch information
1 parent
7d07049
commit 4d0f63e
Showing
15 changed files
with
215 additions
and
7 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
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,7 +1,8 @@ | ||
import { Document, Model } from 'mongoose' | ||
|
||
import mongoose, { Document, Model } from 'mongoose' | ||
export interface IMuscle { | ||
name: string | ||
} | ||
export interface IMuscleDoc extends IMuscle, Document {} | ||
export type IMuscleModel = Model<IMuscleDoc> | ||
export interface IMuscleModel extends Model<IMuscleDoc> { | ||
isMuscleExist(name: string, excludeMuscleId?: 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
Empty file.
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 { MuscleModel } from '../models/muscle.model' | ||
import { MuscleService } from '../services' | ||
import Muscle from '#infra/mongodb/models/muscles/muscle.schema.js' | ||
|
||
export class MuscleController implements Routes { | ||
public controller: OpenAPIHono | ||
private readonly muscleService: MuscleService | ||
constructor() { | ||
this.controller = new OpenAPIHono() | ||
this.muscleService = new MuscleService(Muscle) | ||
} | ||
|
||
public initRoutes() { | ||
this.controller.openapi( | ||
createRoute({ | ||
method: 'post', | ||
path: '/muscles', | ||
tags: ['Muscles'], | ||
summary: 'Add a new muscle to the database', | ||
description: 'This route is used to add a new muscle name to the database.', | ||
operationId: 'createMuscle', | ||
request: { | ||
body: { | ||
content: { | ||
'application/json': { | ||
schema: z.object({ | ||
name: z.string().openapi({ | ||
title: 'Muscle Name', | ||
description: 'The name of the muscle to be added', | ||
type: 'string', | ||
example: 'Biceps Brachii' | ||
}) | ||
}) | ||
} | ||
} | ||
} | ||
}, | ||
responses: { | ||
201: { | ||
description: 'Muscle 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(MuscleModel).openapi({ | ||
title: 'Added Muscle', | ||
description: 'The newly added muscle data' | ||
}) | ||
}) | ||
} | ||
} | ||
}, | ||
400: { | ||
description: 'Bad request - Invalid input data', | ||
content: { | ||
'application/json': { | ||
schema: z.object({ | ||
success: z.boolean(), | ||
error: z.string() | ||
}) | ||
} | ||
} | ||
}, | ||
409: { | ||
description: 'Conflict - Muscle name already exists' | ||
}, | ||
500: { | ||
description: 'Internal server error' | ||
} | ||
} | ||
}), | ||
async (ctx) => { | ||
const body = await ctx.req.json() | ||
const response = await this.muscleService.createMuscle(body) | ||
return ctx.json({ success: true, data: response }) | ||
} | ||
) | ||
this.controller.openapi( | ||
createRoute({ | ||
method: 'get', | ||
path: '/muscles', | ||
tags: ['Muscles'], | ||
summary: 'Retrive all muscles.', | ||
description: 'Retrive list of all the muscles.', | ||
operationId: 'getMuscles', | ||
responses: { | ||
200: { | ||
description: 'Successful response with list of all muscles.', | ||
content: { | ||
'application/json': { | ||
schema: z.object({ | ||
success: z.boolean().openapi({ | ||
description: 'Indicates whether the request was successful', | ||
type: 'boolean', | ||
example: true | ||
}), | ||
data: z.array(MuscleModel).openapi({ | ||
description: 'Array of Muslces.' | ||
}) | ||
}) | ||
} | ||
} | ||
}, | ||
500: { | ||
description: 'Internal server error' | ||
} | ||
} | ||
}), | ||
async (ctx) => { | ||
const response = await this.muscleService.getMuscles() | ||
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,5 @@ | ||
import { z } from 'zod' | ||
|
||
export const MuscleModel = 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 @@ | ||
export * from './muscle.service' |
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 { IMuscleModel } from '#infra/mongodb/models/muscles/muscle.entity.js' | ||
import { CreateMuscleArgs, CreateMuscleUseCase } from '../use-cases/create-muscle' | ||
import { GetMusclesUseCase } from '../use-cases/get-muscles' | ||
|
||
export class MuscleService { | ||
private readonly createMuscleUseCase: CreateMuscleUseCase | ||
private readonly getMuscleUseCase: GetMusclesUseCase | ||
|
||
constructor(private readonly muscleModel: IMuscleModel) { | ||
this.createMuscleUseCase = new CreateMuscleUseCase(muscleModel) | ||
this.getMuscleUseCase = new GetMusclesUseCase(muscleModel) | ||
} | ||
|
||
createMuscle = (args: CreateMuscleArgs) => { | ||
return this.createMuscleUseCase.execute(args) | ||
} | ||
|
||
getMuscles = () => { | ||
return this.getMuscleUseCase.execute() | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/modules/muscle/use-cases/create-muscle/create-muscle.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 { IMuscleDoc, IMuscleModel } from '#infra/mongodb/models/muscles/muscle.entity.js' | ||
import { HTTPException } from 'hono/http-exception' | ||
|
||
export interface CreateMuscleArgs { | ||
name: string | ||
} | ||
|
||
export class CreateMuscleUseCase implements IUseCase<CreateMuscleArgs, IMuscleDoc> { | ||
constructor(private readonly muscleModel: IMuscleModel) {} | ||
|
||
async execute({ name }: CreateMuscleArgs): Promise<IMuscleDoc> { | ||
await this.checkIfMuscleExists(name) | ||
return this.createMuscle(name) | ||
} | ||
|
||
private async checkIfMuscleExists(name: string): Promise<void> { | ||
if (await this.muscleModel.isMuscleExist(name)) { | ||
throw new HTTPException(409, { message: 'Muscle name already exists' }) | ||
} | ||
} | ||
|
||
private async createMuscle(name: string): Promise<IMuscleDoc> { | ||
return this.muscleModel.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-muscle.use-case' |
10 changes: 10 additions & 0 deletions
10
src/modules/muscle/use-cases/get-muscles/get-muscle.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 { IMuscleDoc, IMuscleModel } from '#infra/mongodb/models/muscles/muscle.entity.js' | ||
|
||
export class GetMusclesUseCase implements IUseCase<void, IMuscleDoc[]> { | ||
constructor(private readonly muscleModel: IMuscleModel) {} | ||
|
||
async execute(): Promise<IMuscleDoc[]> { | ||
return this.muscleModel.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-muscle.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import { DalService } from '#infra/mongodb/dal.service.js' | ||
import { MuscleController } from '#modules/muscle/controllers/muscle.controller.js' | ||
import { App } from './app' | ||
|
||
const dalService = new DalService() | ||
const app = new App([]).getApp() | ||
dalService.connectDB() | ||
const app = new App([new MuscleController()]).getApp() | ||
await dalService.connectDB() | ||
|
||
export default app |