Skip to content

Commit

Permalink
Merge pull request #245 from BinaryStudioAcademy/task/OV-233-add-endp…
Browse files Browse the repository at this point in the history
…oint-to-get-video-by-userid

OV-233: Add endpoint to get video by userId
  • Loading branch information
nikita-remeslov authored Sep 12, 2024
2 parents 4456f3e + 5c3477a commit 93d7f38
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions backend/src/bundles/videos/types/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export {
type CreateVideoRequestDto,
type UpdateVideoRequestDto,
type UserGetCurrentResponseDto,
type VideoGetAllItemResponseDto,
type VideoGetAllResponseDto,
type VideoGetOneRequestDto,
Expand Down
18 changes: 14 additions & 4 deletions backend/src/bundles/videos/video.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { VideosApiPath } from './enums/enums.js';
import {
type CreateVideoRequestDto,
type UpdateVideoRequestDto,
type UserGetCurrentResponseDto,
type VideoGetOneRequestDto,
} from './types/types.js';
import {
Expand Down Expand Up @@ -49,7 +50,12 @@ class VideoController extends BaseController {
this.addRoute({
path: VideosApiPath.ROOT,
method: HTTPMethod.GET,
handler: () => this.findAll(),
handler: (options) =>
this.findAllByUser(
options as ApiHandlerOptions<{
user: UserGetCurrentResponseDto;
}>,
),
});

this.addRoute({
Expand Down Expand Up @@ -108,7 +114,7 @@ class VideoController extends BaseController {
* @swagger
* /videos/:
* get:
* description: Get all videos
* description: Get all videos by user
* security:
* - bearerAuth: []
* responses:
Expand All @@ -126,10 +132,14 @@ class VideoController extends BaseController {
* $ref: '#/components/schemas/Video'
*/

private async findAll(): Promise<ApiHandlerResponse> {
private async findAllByUser(
options: ApiHandlerOptions<{
user: UserGetCurrentResponseDto;
}>,
): Promise<ApiHandlerResponse> {
return {
status: HttpCode.OK,
payload: await this.videoService.findAll(),
payload: await this.videoService.findByUserId(options.user.id),
};
}

Expand Down
9 changes: 9 additions & 0 deletions backend/src/bundles/videos/video.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ class VideoRepository implements Repository {
return video ? VideoEntity.initialize(video) : null;
}

public async findByUserId(userId: string): Promise<VideoEntity[]> {
const videos = await this.videoModel
.query()
.where('userId', userId)
.execute();

return videos.map((it) => VideoEntity.initialize(it));
}

public async findAll(): Promise<VideoEntity[]> {
const videos = await this.videoModel.query().execute();

Expand Down
8 changes: 8 additions & 0 deletions backend/src/bundles/videos/video.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ class VideoService implements Service {
return video.toObject();
}

public async findByUserId(userId: string): Promise<VideoGetAllResponseDto> {
const items = await this.videoRepository.findByUserId(userId);

return {
items: items.map((it) => it.toObject()),
};
}

public async findAll(): Promise<VideoGetAllResponseDto> {
const items = await this.videoRepository.findAll();

Expand Down

0 comments on commit 93d7f38

Please sign in to comment.