From abe492ba616e714c3fa6d19dfd69e0197adbc6b3 Mon Sep 17 00:00:00 2001 From: OhDongI Date: Tue, 11 Feb 2025 17:22:20 +0900 Subject: [PATCH 1/3] =?UTF-8?q?FIX:=20=EC=98=88=EC=99=B8=EC=B2=98=EB=A6=AC?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/errors.js | 5 ++--- src/services/like.service.js | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/errors.js b/src/errors.js index 0161e5b..c3ea36e 100644 --- a/src/errors.js +++ b/src/errors.js @@ -184,13 +184,12 @@ export class LikeIdMissingError extends Error { } } -//entityId, entityType 또는 userId가 누락 -export class ValidationError extends Error{ +export class EntityValidationError extends Error{ errorCode = "L005"; statusCode = 400; // Bad Request constructor(data) { - const reason = 'entityId, entityType 또는 userId가 누락되었습니다.'; + const reason = 'entityId 또는 entityType가가 누락되었습니다.'; super(reason); this.reason = reason; this.data = data; diff --git a/src/services/like.service.js b/src/services/like.service.js index ac6b1e7..615c660 100644 --- a/src/services/like.service.js +++ b/src/services/like.service.js @@ -1,5 +1,5 @@ import { - ValidationError, + EntityValidationError, DuplicateLikeMomentError, momentIdNotFoundError, LikeIdNotExistError, @@ -24,8 +24,8 @@ const handleDatabaseError = (error, message) => { export const likeMoment = async (data) => { try { // 입력값 검증 - if (!data.entityId || !data.entityType || !data.userId) { - throw new ValidationError(data); + if (!data.entityId || !data.entityType ) { + throw new EntityValidationError(data); } // 게시글 존재 여부 확인 From c143a0bf774bdca0af26b3f270c1a1852b1b9d33 Mon Sep 17 00:00:00 2001 From: OhDongI Date: Thu, 13 Feb 2025 23:04:16 +0900 Subject: [PATCH 2/3] =?UTF-8?q?Refactor:=20=EC=A2=8B=EC=95=84=EC=9A=94=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20request=20body?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/like.controller.js | 2 -- src/dtos/like.dto.js | 3 +-- src/repositories/like.repository.js | 3 +-- src/services/like.service.js | 1 - 4 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/controllers/like.controller.js b/src/controllers/like.controller.js index d7fc9ac..130fb21 100644 --- a/src/controllers/like.controller.js +++ b/src/controllers/like.controller.js @@ -29,7 +29,6 @@ export const handleLikeMoment = async (req, res, next) => { type: "object", properties: { id: { type: "integer", description: "Moment ID" }, - userId: { type: "integer", description: "Moment 작성자 ID" }, entityType: { type: "string", default: "moment", description: "엔터티 유형" } }, required: ["id", "userId"] @@ -39,7 +38,6 @@ export const handleLikeMoment = async (req, res, next) => { example: { moment: { id: 1, - userId: 1, entityType: "moment" } } diff --git a/src/dtos/like.dto.js b/src/dtos/like.dto.js index d8f5329..da4fc2c 100644 --- a/src/dtos/like.dto.js +++ b/src/dtos/like.dto.js @@ -1,7 +1,6 @@ export const bodyToLike = ({ moment }, userId) => { return { - fromUserId: userId, - userId: moment.userId, + fromUserId: userId, //좋아요 누른 사람 entityId: moment.id, entityType: moment.entityType || "moment", }; diff --git a/src/repositories/like.repository.js b/src/repositories/like.repository.js index a44cbb1..b5206bc 100644 --- a/src/repositories/like.repository.js +++ b/src/repositories/like.repository.js @@ -19,8 +19,7 @@ export const addMomentLike = async (data) => { const newLike = await prisma.like.create({ data: { entityType: data.entityType, - entityId: data.entityId, - user: { connect: { id: data.userId } }, + entityId: data.entityId, fromUser: { connect: { id: data.fromUserId } }, }, }); diff --git a/src/services/like.service.js b/src/services/like.service.js index 615c660..fc22552 100644 --- a/src/services/like.service.js +++ b/src/services/like.service.js @@ -8,7 +8,6 @@ import { DatabaseError, handleServerError } from "../errors.js"; - import { addMomentLike, removeMomentLike } from "../repositories/like.repository.js"; import { prisma } from "../db.config.js"; From c7525f709b6e99e04677077ec075a1d59ea737d0 Mon Sep 17 00:00:00 2001 From: OhDongI Date: Thu, 20 Feb 2025 21:03:27 +0900 Subject: [PATCH 3/3] =?UTF-8?q?Chore:=20=EA=B8=B0=EC=A1=B4=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=EB=A1=9C=20=EB=B3=B5=EA=B5=AC,=EC=98=A4=ED=83=80=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/like.controller.js | 6 +++--- src/dtos/like.dto.js | 22 +++++++++++----------- src/errors.js | 6 +++--- src/repositories/like.repository.js | 5 +++-- src/services/like.service.js | 11 ++++++----- 5 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/controllers/like.controller.js b/src/controllers/like.controller.js index e0cf9f0..d5187e6 100644 --- a/src/controllers/like.controller.js +++ b/src/controllers/like.controller.js @@ -29,7 +29,7 @@ export const handleLikeMoment = async (req, res, next) => { type: "object", properties: { id: { type: "integer", description: "Moment ID" }, - userId: { type: "integer", description: "Moment 작성자 ID" }, + userId: { type: "integer", description: "Moment 작성자 ID" }, entityType: { type: "string", default: "moment", description: "엔터티 유형" } }, required: ["id", "userId"] @@ -67,7 +67,7 @@ export const handleLikeMoment = async (req, res, next) => { */ try{ - console.log("Like를 요청했습니다!"); +// console.log("Like를 요청했습니다!"); const like = await likeMoment(bodyToLike(req.body,req.user.id)); res.status(StatusCodes.OK).success(like); } catch (error) { @@ -122,7 +122,7 @@ export const handleDeleteLikeMoment = async (req, res, next) =>{ */ try{ - console.log("Like 삭제를 요청했습니다!"); +// console.log("Like 삭제를 요청했습니다!"); const like = await deleteMomentLike(bodyToDeleteLike(req.body), req.user.id); res.status(StatusCodes.OK).success(like); } catch (error) { diff --git a/src/dtos/like.dto.js b/src/dtos/like.dto.js index 5fcaaab..df7987d 100644 --- a/src/dtos/like.dto.js +++ b/src/dtos/like.dto.js @@ -1,12 +1,12 @@ export const bodyToLike = ({ moment }, userId) => { - return { - fromUserId: userId, - userId: moment.userId, - entityId: moment.id, - entityType: moment.entityType || "moment", - }; - }; - - export const bodyToDeleteLike = ({ like }) => { - return { likeId: like?.likeId }; - }; \ No newline at end of file + return { + fromUserId: userId, + userId: moment.userId, + entityId: moment.id, + entityType: moment.entityType || "moment", + }; + }; + + export const bodyToDeleteLike = ({ like }) => { + return { likeId: like?.likeId }; + }; \ No newline at end of file diff --git a/src/errors.js b/src/errors.js index 440e5b0..69fcf62 100644 --- a/src/errors.js +++ b/src/errors.js @@ -141,7 +141,7 @@ export class DuplicateLikeMomentError extends Error { statusCode = 409; //Conflict constructor(data) { - const reason = '이미 존재하는 좋아요입니다다.'; + const reason = '이미 존재하는 좋아요입니다.'; super(reason); this.reason = reason; this.data = data; @@ -189,7 +189,7 @@ export class EntityValidationError extends Error{ statusCode = 400; // Bad Request constructor(data) { - const reason = 'entityId 또는 entityType가가 누락되었습니다.'; + const reason = 'entityId 또는 entityType가 누락되었습니다.'; super(reason); this.reason = reason; this.data = data; @@ -206,7 +206,7 @@ export class DatabaseError extends Error{ statusCode = 500; // Internal Server Error constructor(data) { - const reason = '데이터베이스 연결에 실패했습니다'; + const reason = '데이터베이스 연결에 실패했습니다.'; super(reason); this.reason = reason; this.data = data; diff --git a/src/repositories/like.repository.js b/src/repositories/like.repository.js index 04d2ac5..a44cbb1 100644 --- a/src/repositories/like.repository.js +++ b/src/repositories/like.repository.js @@ -5,6 +5,7 @@ export const addMomentLike = async (data) => { const existingLike = await prisma.like.findFirst({ where: { fromUserId: data.fromUserId, + userId: data.userId, entityId: data.entityId, entityType: data.entityType, user: { isDeleted: false }, //탈퇴 회원 배제 @@ -18,8 +19,8 @@ export const addMomentLike = async (data) => { const newLike = await prisma.like.create({ data: { entityType: data.entityType, - entityId: data.entityId, - user: { connect: { id: data.userId } }, + entityId: data.entityId, + user: { connect: { id: data.userId } }, fromUser: { connect: { id: data.fromUserId } }, }, }); diff --git a/src/services/like.service.js b/src/services/like.service.js index e415505..ac6b1e7 100644 --- a/src/services/like.service.js +++ b/src/services/like.service.js @@ -1,5 +1,5 @@ import { - EntityValidationError, + ValidationError, DuplicateLikeMomentError, momentIdNotFoundError, LikeIdNotExistError, @@ -8,6 +8,7 @@ import { DatabaseError, handleServerError } from "../errors.js"; + import { addMomentLike, removeMomentLike } from "../repositories/like.repository.js"; import { prisma } from "../db.config.js"; @@ -19,17 +20,17 @@ const handleDatabaseError = (error, message) => { throw new DatabaseError(message, error); }; + export const likeMoment = async (data) => { try { // 입력값 검증 - if (!data.entityId || !data.entityType ) { - throw new EntityValidationError(data); + if (!data.entityId || !data.entityType || !data.userId) { + throw new ValidationError(data); } // 게시글 존재 여부 확인 const momentExists = await prisma.moment.findUnique({ where: { id: data.entityId }, - select: { userId: true } , }); if (!momentExists) { @@ -55,7 +56,7 @@ export const likeMoment = async (data) => { } catch (error) { // 사용자의 잘못된 요청 - if (error instanceof EntityValidationError || + if (error instanceof ValidationError || error instanceof momentIdNotFoundError || error instanceof DuplicateLikeMomentError) { throw error;