From 0283721e4bb6e9b582db5a6fe268aa031b2aae99 Mon Sep 17 00:00:00 2001 From: Tatsuto YAMAMOTO Date: Wed, 9 Aug 2023 22:34:37 +0900 Subject: [PATCH] =?UTF-8?q?feat(api):=20=E3=83=AA=E3=82=A2=E3=82=AF?= =?UTF-8?q?=E3=82=B7=E3=83=A7=E3=83=B3=E3=81=AE=E5=89=8A=E9=99=A4=E3=81=8C?= =?UTF-8?q?=E3=81=A7=E3=81=8D=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/server/controller/post.ts | 11 +++++++++++ src/server/handlers/post.ts | 10 ++++++++++ src/server/start.ts | 1 + src/service/post/create_reaction_service.ts | 8 ++++++++ 4 files changed, 30 insertions(+) diff --git a/src/server/controller/post.ts b/src/server/controller/post.ts index 0d7d9aa..e9ab903 100644 --- a/src/server/controller/post.ts +++ b/src/server/controller/post.ts @@ -134,6 +134,17 @@ export class PostController { return new Success(resp); } + async UndoReaction(userID: string, postID: string) { + const res = await this.createReactionService.Undo( + postID as Snowflake, + userID as Snowflake, + ); + if (res.isFailure()) { + return new Failure(res.value); + } + return new Success(res); + } + private convertToCommonResponse(arg: { post: PostData; user: UserData }) { const resp: CommonPostResponse = { id: arg.post.id, diff --git a/src/server/handlers/post.ts b/src/server/handlers/post.ts index 125aeeb..f920b5c 100644 --- a/src/server/handlers/post.ts +++ b/src/server/handlers/post.ts @@ -74,4 +74,14 @@ export class PostHandler { } r.code(200).send(re.value); }; + + public UndoReaction: FastifyHandlerMethod<{ Params: { id: string } }> = + async (q, r) => { + const re = await this.controller.UndoReaction("123", q.params.id); + if (re.isFailure()) { + const [code, message] = ErrorConverter(re.value); + return r.code(code).send(message); + } + r.code(204).send(); + }; } diff --git a/src/server/start.ts b/src/server/start.ts index e27b7dc..8890cfd 100644 --- a/src/server/start.ts +++ b/src/server/start.ts @@ -87,6 +87,7 @@ export async function StartServer(port: number) { app.get("/api/v1/posts/:id", postHandler.FindByID); app.delete("/api/v1/posts/:id", postHandler.DeletePost); app.post("/api/v1/posts/:id/reaction", postHandler.CreateReaction); + app.delete("/api/v1/posts/:id/reaction", postHandler.UndoReaction); app.post("/api/v1/posts", postHandler.CreatePost); app.get("/api/v1/users/:name", userHandler.FindByHandle); app.get("/api/v1/users/:name/posts", userHandler.FindUserPosts); diff --git a/src/service/post/create_reaction_service.ts b/src/service/post/create_reaction_service.ts index 8417a6f..8d71476 100644 --- a/src/service/post/create_reaction_service.ts +++ b/src/service/post/create_reaction_service.ts @@ -26,6 +26,14 @@ export class CreateReactionService { return new Success(data); } + async Undo(postID: Snowflake, userID: Snowflake): AsyncResult { + const res = await this.repository.Undo(postID, userID); + if (res.isFailure()) { + return new Failure(res.value); + } + return new Success(void ""); + } + private async isExists(d: PostReactionEvent): Promise { const res = await this.repository.Find(d.postID, d.userID); return res.isSuccess();