-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #118 from BinaryStudioAcademy/task/OV-105-add-avat…
…ars-endpoint OV-105: add avatars endpoint
- Loading branch information
Showing
19 changed files
with
716 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { type AvatarService } from '~/bundles/avatars/avatar.service.js'; | ||
import { | ||
type ApiHandlerOptions, | ||
type ApiHandlerResponse, | ||
BaseController, | ||
} from '~/common/controller/controller.js'; | ||
import { ApiPath } from '~/common/enums/enums.js'; | ||
import { HttpCode, HTTPMethod } from '~/common/http/http.js'; | ||
import { type Logger } from '~/common/logger/logger.js'; | ||
|
||
import { AvatarsApiPath } from './enums/enums.js'; | ||
import { type AvatarGetOneRequestDto } from './types/types.js'; | ||
/** | ||
* @swagger | ||
* components: | ||
* schemas: | ||
* Avatar: | ||
* type: object | ||
* properties: | ||
* id: | ||
* type: string | ||
* format: uuid | ||
* name: | ||
* type: string | ||
* voice: | ||
* type: string | ||
* voiceUrl: | ||
* type: string | ||
* styles: | ||
* type: array | ||
* items: | ||
* type: object | ||
* properties: | ||
* imgUrl: | ||
* type: string | ||
* style: | ||
* type: string | ||
* gestures: | ||
* type: array | ||
* items: | ||
* type: string | ||
*/ | ||
class AvatarController extends BaseController { | ||
private avatarService: AvatarService; | ||
|
||
public constructor(logger: Logger, avatarService: AvatarService) { | ||
super(logger, ApiPath.AVATARS); | ||
|
||
this.avatarService = avatarService; | ||
|
||
this.addRoute({ | ||
path: AvatarsApiPath.ROOT, | ||
method: HTTPMethod.GET, | ||
handler: () => this.findAll(), | ||
}); | ||
|
||
this.addRoute({ | ||
path: AvatarsApiPath.ID, | ||
method: HTTPMethod.GET, | ||
handler: (options) => | ||
this.find( | ||
options as ApiHandlerOptions<{ | ||
params: AvatarGetOneRequestDto; | ||
}>, | ||
), | ||
}); | ||
} | ||
|
||
/** | ||
* @swagger | ||
* /avatars: | ||
* get: | ||
* description: Get all avatars | ||
* responses: | ||
* 200: | ||
* description: Successful operation | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* properties: | ||
* items: | ||
* type: array | ||
* description: A list of avatar objects | ||
* items: | ||
* $ref: '#/components/schemas/Avatar' | ||
*/ | ||
private findAll(): ApiHandlerResponse { | ||
return { | ||
status: HttpCode.OK, | ||
payload: this.avatarService.findAll(), | ||
}; | ||
} | ||
|
||
/** | ||
* @swagger | ||
* /avatars/{id}: | ||
* get: | ||
* description: Get avatar by id | ||
* parameters: | ||
* - in: path | ||
* name: id | ||
* required: true | ||
* schema: | ||
* type: string | ||
* format: uuid | ||
* responses: | ||
* 200: | ||
* description: Successful operation | ||
* content: | ||
* application/json: | ||
* schema: | ||
* $ref: '#/components/schemas/Avatar' | ||
* 404: | ||
* description: Failed operation. The resource was not found. | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* $ref: '#/components/schemas/Error' | ||
*/ | ||
private find( | ||
options: ApiHandlerOptions<{ | ||
params: AvatarGetOneRequestDto; | ||
}>, | ||
): ApiHandlerResponse { | ||
const avatarId = options.params.id; | ||
const avatar = this.avatarService.findById({ avatarId }); | ||
|
||
return { | ||
status: HttpCode.OK, | ||
payload: avatar, | ||
}; | ||
} | ||
} | ||
|
||
export { AvatarController }; |
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,38 @@ | ||
import { HttpCode, HttpError } from '~/common/http/http.js'; | ||
|
||
import { AvatarValidationMessage } from './enums/enums.js'; | ||
import { | ||
type AvatarGetAllResponseDto, | ||
type AvatarGetResponseDto, | ||
} from './types/types.js'; | ||
|
||
class AvatarService { | ||
private avatars: AvatarGetResponseDto[]; | ||
|
||
public constructor(avatars: AvatarGetResponseDto[]) { | ||
this.avatars = avatars; | ||
} | ||
|
||
public findById({ avatarId }: { avatarId: string }): AvatarGetResponseDto { | ||
const avatar = this.avatars.find((avatar) => avatar.id === avatarId); | ||
|
||
if (!avatar) { | ||
throw new HttpError({ | ||
message: AvatarValidationMessage.AVATAR_DOESNT_EXIST, | ||
status: HttpCode.NOT_FOUND, | ||
}); | ||
} | ||
|
||
return avatar; | ||
} | ||
|
||
public findAll(): AvatarGetAllResponseDto { | ||
const avatars = this.avatars; | ||
|
||
return { | ||
items: avatars, | ||
}; | ||
} | ||
} | ||
|
||
export { AvatarService }; |
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 { logger } from '~/common/logger/logger.js'; | ||
|
||
import { AvatarController } from './avatar.controller.js'; | ||
import { AvatarService } from './avatar.service.js'; | ||
import { avatarsConfig } from './config/config.js'; | ||
|
||
const avatarService = new AvatarService(avatarsConfig); | ||
const avatarController = new AvatarController(logger, avatarService); | ||
|
||
export { avatarController }; |
Oops, something went wrong.