Skip to content

Commit

Permalink
[FEAT] 뮤멘트 삭제하기 #28
Browse files Browse the repository at this point in the history
- Service, Controller, DTO
  • Loading branch information
kimchaeeun3447 committed Jul 17, 2022
1 parent 3979f37 commit 07b6736
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/controllers/MumentController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ const createMument = async (req: Request, res: Response) => {
try {
const data: PostBaseResponseDto | null = await MumentService.createMument(userId, musicId, mumentCreateDto);

if (!data) res.status(statusCode.NOT_FOUND).send(util.fail(statusCode.NOT_FOUND, message.NOT_FOUND_ID));

res.status(statusCode.CREATED).send(util.success(statusCode.CREATED, message.CREATE_MUMENT_SUCCESS, data));
if (!data) {
res.status(statusCode.NOT_FOUND).send(util.fail(statusCode.NOT_FOUND, message.NOT_FOUND_ID));
} else {
res.status(statusCode.CREATED).send(util.success(statusCode.CREATED, message.CREATE_MUMENT_SUCCESS, data));
}
} catch (error) {
console.log(error);

Expand Down Expand Up @@ -81,6 +83,24 @@ const getMument = async (req: Request, res: Response) => {
}
};

/**
* @ROUTE DELETE /:mumentId
* @DESC Delete Mument
*/
const deleteMument = async (req: Request, res: Response) => {
const { mumentId } = req.params;

try {
const data = await MumentService.deleteMument(mumentId);

res.status(statusCode.NO_CONTENT).send(util.success(statusCode.NO_CONTENT, message.DELETE_MUMENT_SUCCESS));
} catch (error) {
console.log(error);

res.status(statusCode.INTERNAL_SERVER_ERROR).send(util.fail(statusCode.INTERNAL_SERVER_ERROR, message.INTERNAL_SERVER_ERROR));
}
};

/**
* @ROUTE GET /mument/:userId/:musicId/is-first
* @DESC 특정 음악에 대해 뮤멘트 기록하기 전 처음/다시 태그 판단
Expand Down Expand Up @@ -195,8 +215,7 @@ const deleteLike = async (req: Request, res: Response) => {
res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, message.DELETE_LIKE_FAIL));
}

res.status(statusCode.OK).send(util.success(statusCode.OK, message.DELETE_LIKE_SUCCESS, data));

res.status(statusCode.OK).send(util.success(statusCode.OK, message.DELETE_LIKE_SUCCESS));
} catch (error) {
console.log(error);
res.status(statusCode.INTERNAL_SERVER_ERROR).send(util.fail(statusCode.INTERNAL_SERVER_ERROR, message.INTERNAL_SERVER_ERROR));
Expand All @@ -207,6 +226,7 @@ export default {
createMument,
updateMument,
getMument,
deleteMument,
getIsFirst,
getMumentHistory,
createLike,
Expand Down
1 change: 1 addition & 0 deletions src/modules/responseMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const message = {
CREATE_MUMENT_SUCCESS: '뮤멘트 기록하기 성공',
UPDATE_MUMENT_SUCCESS: '뮤멘트 수정하기 성공',
READ_MUMENT_SUCEESS: '뮤멘트 상세보기 성공',
DELETE_MUMENT_SUCCESS: '뮤멘트 삭제하기 성공',
NOT_YOUR_MUMENT: '비밀글 입니다',
READ_MUMENT_HISTORY_SUCCESS: '뮤멘트 히스토리 조회 성공',
CREATE_LIKE_SUCCESS: '좋아요 등록 성공',
Expand Down
2 changes: 2 additions & 0 deletions src/routes/MumentRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ router.put('/:mumentId', [
router.get('/:mumentId/:userId', MumentController.getMument);
router.get('/:userId/:musicId/is-first', MumentController.getIsFirst);

router.delete('/:mumentId', MumentController.deleteMument);

// 히스토리 조회
router.get('/:userId/:musicId/history', [
param('musicId').isString().isLength({ min: 24, max: 24 }),
Expand Down
26 changes: 26 additions & 0 deletions src/services/MumentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,31 @@ const getMument = async (mumentId: string, userId: string): Promise<MumentRespon
}
};

const deleteMument = async (mumentId: string): Promise<void | null> => {
try {
const mument = await Mument.findById(mumentId);
if (!mument) return null;

//Mument soft delete
await Mument.findByIdAndUpdate(mumentId, {
$set: {
isDeleted: true,
},
});

//Like에서 Mument 제거
await Like.updateMany(
{},
{
$pull: { mument: { _id: mumentId } },
},
);
} catch (error) {
console.log(error);
throw error;
}
};

const getIsFirst = async (userId: string, musicId: string): Promise<IsFirstResponseDto | null> => {
try {
const music = await Music.findById(musicId);
Expand Down Expand Up @@ -408,6 +433,7 @@ export default {
createMument,
updateMument,
getMument,
deleteMument,
getIsFirst,
getMumentHistory,
createLike,
Expand Down

0 comments on commit 07b6736

Please sign in to comment.