Skip to content

Commit

Permalink
feat: implement reply v1 handler methods
Browse files Browse the repository at this point in the history
  • Loading branch information
skgndi12 committed Mar 16, 2024
1 parent 51c40d3 commit 167015e
Showing 1 changed file with 135 additions and 1 deletion.
136 changes: 135 additions & 1 deletion api/src/controller/http/review/review.v1.controller.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@
import { Request, Response, Router } from 'express';

import { AppIdToken } from '@src/core/entities/auth.entity';
import { Review } from '@src/core/entities/review.entity';
import { Reply, Review } from '@src/core/entities/review.entity';
import { User } from '@src/core/entities/user.entity';
import { ReviewService } from '@src/core/services/review/review.service';
import {
CreateReplyDto,
CreateReviewDto,
DeleteReplyDto,
DeleteReviewDto,
GetRepliesDto,
GetReviewsDto,
UpdateReplyDto,
UpdateReviewDto
} from '@src/core/services/review/types';
import { AppErrorCode, CustomError } from '@src/error/errors';

import { methodNotAllowed } from '@controller/http/handler';
import { Middleware } from '@controller/http/middleware';
import {
CreateReplyV1Request,
CreateReviewV1Request,
ListRepliesV1QueryParameter,
ListReviewsV1QueryParameter,
ReplyV1PathParameter,
ReviewV1PathParameter,
UpdateReplyV1Request,
UpdateReviewV1Request
} from '@controller/http/review/request/review.v1.request';
import {
CreateReplyV1Response,
CreateReviewV1Response,
DeleteReplyV1Response,
DeleteReviewV1Response,
GetReviewV1Response,
ListRepliesV1Response,
ListReviewsV1Response,
ReplyV1Response,
ReviewV1FilterType,
ReviewV1Response,
UpdateReplyV1Response,
UpdateReviewV1Response
} from '@controller/http/review/response/review.v1.response';

Expand All @@ -40,6 +53,20 @@ export class ReviewV1Controller {
const router: Router = Router();
const prefix = '/v1/reviews';

router
.route(`${prefix}/:reviewId(\\d+)/replies/:replyId(\\d+)`)
.all(this.middleware.issuePassport)
.put(this.updateReply)
.delete(this.deleteReply)
.all(methodNotAllowed);

router
.route(`${prefix}/:reviewId(\\d+)/replies`)
.get(this.listReplies)
.all(this.middleware.issuePassport)
.post(this.createReply)
.all(methodNotAllowed);

router
.route(`${prefix}/:reviewId(\\d+)`)
.get(this.getReview)
Expand Down Expand Up @@ -169,6 +196,100 @@ export class ReviewV1Controller {
res.send();
};

public createReply = async (
req: Request<
ReviewV1PathParameter,
any,
any,
CreateReplyV1Request,
AppIdToken
>,
res: Response<CreateReplyV1Response>
) => {
const dto: CreateReplyDto = {
requesterIdToken: res.locals.passport,
reviewId: parseInt(req.params.reviewId),
content: req.body.content
};
const { user, reply } = await this.service.createReply(dto);

res.send({ reply: this.buildReplyResponse(user, reply) });
};

public listReplies = async (
req: Request<
ReviewV1PathParameter,
any,
any,
ListRepliesV1QueryParameter,
any
>,
res: Response<ListRepliesV1Response>
) => {
const dto: GetRepliesDto = {
reviewId: parseInt(req.params.reviewId),
direction: req.query.direction,
pageOffset: req.query.pageOffset,
pageSize: req.query.pageSize
};
const { users, replies, pagination } = await this.service.getReplies(dto);

const userMapById = users.reduce(
(acc, user) => acc.set(user.id, user),
new Map<string, User>()
);
const repliesResponse = replies.map((reply) => {
const userReplying = userMapById.get(reply.userId);

if (userReplying === undefined) {
throw new CustomError({
code: AppErrorCode.INTERNAL_ERROR,
message: 'replying user not found',
context: { reply }
});
}

return this.buildReplyResponse(userReplying, reply);
});

res.send({ replies: repliesResponse, pagination });
};

public updateReply = async (
req: Request<
ReplyV1PathParameter,
any,
UpdateReplyV1Request,
any,
AppIdToken
>,
res: Response<UpdateReplyV1Response>
) => {
const dto: UpdateReplyDto = {
requesterIdToken: res.locals.passport,
reviewId: parseInt(req.params.reviewId),
replyId: parseInt(req.params.replyId),
content: req.body.content
};
const { user, reply } = await this.service.updateReply(dto);

res.send({ reply: this.buildReplyResponse(user, reply) });
};

public deleteReply = async (
req: Request<ReplyV1PathParameter, any, any, any, AppIdToken>,
res: Response<DeleteReplyV1Response>
) => {
const dto: DeleteReplyDto = {
requesterIdToken: res.locals.passport,
reviewId: parseInt(req.params.reviewId),
replyId: parseInt(req.params.replyId)
};
await this.service.deleteReply(dto);

res.send();
};

private buildReviewResponse = (
user: User,
review: Review
Expand All @@ -186,4 +307,17 @@ export class ReviewV1Controller {
updatedAt: review.updatedAt
};
};

private buildReplyResponse = (user: User, reply: Reply): ReplyV1Response => {
return {
id: reply.id,
reviewId: reply.reviewId,
userId: user.id,
nickname: user.nickname,
tag: user.tag,
content: reply.content,
createdAt: reply.createdAt,
updatedAt: reply.updatedAt
};
};
}

0 comments on commit 167015e

Please sign in to comment.