Skip to content

Commit

Permalink
Merge pull request #118 from BinaryStudioAcademy/task/OV-105-add-avat…
Browse files Browse the repository at this point in the history
…ars-endpoint

OV-105: add avatars endpoint
  • Loading branch information
nikita-remeslov authored Sep 5, 2024
2 parents 19deb51 + c541133 commit e90e1dd
Show file tree
Hide file tree
Showing 19 changed files with 716 additions and 0 deletions.
137 changes: 137 additions & 0 deletions backend/src/bundles/avatars/avatar.controller.ts
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 };
38 changes: 38 additions & 0 deletions backend/src/bundles/avatars/avatar.service.ts
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 };
10 changes: 10 additions & 0 deletions backend/src/bundles/avatars/avatars.ts
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 };
Loading

0 comments on commit e90e1dd

Please sign in to comment.