From c88dd811e908db6f4a4d647b03c0d015884f4d14 Mon Sep 17 00:00:00 2001 From: aqrms <111513287+aqrms@users.noreply.github.com> Date: Mon, 22 Jan 2024 10:42:25 +0900 Subject: [PATCH] =?UTF-8?q?Feature:=20=EC=BB=A4=ED=94=BC=EC=B1=97=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=EC=8B=9C=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20?= =?UTF-8?q?=EC=9C=A0=EC=A0=80=EC=9D=98=20=EC=A0=95=EB=B3=B4=EB=A1=9C=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1=EC=9E=90=20=EC=84=A4=EC=A0=95=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EB=B3=80=EA=B2=BD=20(#170)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Refactor: DTO에서 toEntity 메서드를 인스턴스 메서드로 변경 * Feat: 커피챗 등록 시 로그인 유저의 정보로 작성자 맵핑 * Fix: create 메소드 변경됨에 따라 테스트 수정 --- .../controller/CoffeeChatController.java | 8 ++++++-- .../dto/request/CreateCoffeeChatRequest.java | 14 +++++++------- .../coffeechat/service/CoffeeChatService.java | 15 ++++++++++++--- .../controller/CoffeeChatControllerTest.java | 2 +- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/module-api/src/main/java/kernel/jdon/coffeechat/controller/CoffeeChatController.java b/module-api/src/main/java/kernel/jdon/coffeechat/controller/CoffeeChatController.java index d6f90b97b..701410517 100644 --- a/module-api/src/main/java/kernel/jdon/coffeechat/controller/CoffeeChatController.java +++ b/module-api/src/main/java/kernel/jdon/coffeechat/controller/CoffeeChatController.java @@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; +import kernel.jdon.auth.dto.SessionUserInfo; import kernel.jdon.coffeechat.dto.request.CreateCoffeeChatRequest; import kernel.jdon.coffeechat.dto.request.UpdateCoffeeChatRequest; import kernel.jdon.coffeechat.dto.response.ApplyCoffeeChatResponse; @@ -23,6 +24,7 @@ import kernel.jdon.coffeechat.dto.response.UpdateCoffeeChatResponse; import kernel.jdon.coffeechat.service.CoffeeChatService; import kernel.jdon.dto.response.CommonResponse; +import kernel.jdon.global.annotation.LoginUser; import lombok.RequiredArgsConstructor; @RestController @@ -38,8 +40,10 @@ public ResponseEntity get(@PathVariable(name = "id") Long coffee } @PostMapping("/api/v1/coffeechats") - public ResponseEntity save(@RequestBody CreateCoffeeChatRequest request) { - CreateCoffeeChatResponse response = coffeeChatService.create(request); + public ResponseEntity save( + @RequestBody CreateCoffeeChatRequest request, + @LoginUser SessionUserInfo sessionUser) { + CreateCoffeeChatResponse response = coffeeChatService.create(request, sessionUser.getId()); URI uri = URI.create("/v1/coffeechats/" + response.getCoffeeChatId()); return ResponseEntity.created(uri).body(CommonResponse.of(response)); diff --git a/module-api/src/main/java/kernel/jdon/coffeechat/dto/request/CreateCoffeeChatRequest.java b/module-api/src/main/java/kernel/jdon/coffeechat/dto/request/CreateCoffeeChatRequest.java index c451ce434..4196d7cb6 100644 --- a/module-api/src/main/java/kernel/jdon/coffeechat/dto/request/CreateCoffeeChatRequest.java +++ b/module-api/src/main/java/kernel/jdon/coffeechat/dto/request/CreateCoffeeChatRequest.java @@ -25,14 +25,14 @@ public class CreateCoffeeChatRequest { private LocalDateTime meetDate; private String openChatUrl; - public static CoffeeChat toEntity(CreateCoffeeChatRequest request) { + public CoffeeChat toEntity(Member member) { return CoffeeChat.builder() - .title(request.getTitle()) - .content(request.getContent()) - .totalRecruitCount(request.getTotalRecruitCount()) - .meetDate(request.getMeetDate()) - .openChatUrl(request.getOpenChatUrl()) - .member(Member.builder().id(1L).build()) + .title(title) + .content(content) + .totalRecruitCount(totalRecruitCount) + .meetDate(meetDate) + .openChatUrl(openChatUrl) + .member(member) .build(); } diff --git a/module-api/src/main/java/kernel/jdon/coffeechat/service/CoffeeChatService.java b/module-api/src/main/java/kernel/jdon/coffeechat/service/CoffeeChatService.java index e53a08e73..41569eeb7 100644 --- a/module-api/src/main/java/kernel/jdon/coffeechat/service/CoffeeChatService.java +++ b/module-api/src/main/java/kernel/jdon/coffeechat/service/CoffeeChatService.java @@ -12,15 +12,21 @@ import kernel.jdon.coffeechat.dto.response.UpdateCoffeeChatResponse; import kernel.jdon.coffeechat.error.CoffeeChatErrorCode; import kernel.jdon.coffeechat.repository.CoffeeChatRepository; +import kernel.jdon.error.code.api.MemberErrorCode; import kernel.jdon.global.exception.ApiException; +import kernel.jdon.member.domain.Member; +import kernel.jdon.member.repository.MemberRepository; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; @Service +@Slf4j @Transactional(readOnly = true) @RequiredArgsConstructor public class CoffeeChatService { private final CoffeeChatRepository coffeeChatRepository; + private final MemberRepository memberRepository; private CoffeeChat findByIdIfNotDeleted(Long coffeeChatId) { @@ -41,8 +47,11 @@ private void increaseViewCount(CoffeeChat coffeeChat) { } @Transactional - public CreateCoffeeChatResponse create(CreateCoffeeChatRequest request) { - CoffeeChat savedCoffeeChat = coffeeChatRepository.save(CreateCoffeeChatRequest.toEntity(request)); + public CreateCoffeeChatResponse create(CreateCoffeeChatRequest request, Long memberId) { + Member findMember = memberRepository.findById(memberId) + .orElseThrow(() -> new ApiException(MemberErrorCode.NOT_FOUND_MEMBER)); + + CoffeeChat savedCoffeeChat = coffeeChatRepository.save(request.toEntity(findMember)); return CreateCoffeeChatResponse.of(savedCoffeeChat.getId()); } @@ -78,7 +87,7 @@ private void checkMeetDate(CoffeeChat findCoffeeChat, CoffeeChat target) { throw new ApiException(CoffeeChatErrorCode.MEET_DATE_ISBEFORE_NOW); } } - + private void checkRecruitCount(CoffeeChat findCoffeeChat, CoffeeChat target) { if (findCoffeeChat.isValidRecruitCount(target.getTotalRecruitCount())) { throw new ApiException(CoffeeChatErrorCode.EXPIRED_COFFEECHAT); diff --git a/module-api/src/test/java/kernel/jdon/coffeechat/controller/CoffeeChatControllerTest.java b/module-api/src/test/java/kernel/jdon/coffeechat/controller/CoffeeChatControllerTest.java index fde5bae2e..3abcee6c2 100644 --- a/module-api/src/test/java/kernel/jdon/coffeechat/controller/CoffeeChatControllerTest.java +++ b/module-api/src/test/java/kernel/jdon/coffeechat/controller/CoffeeChatControllerTest.java @@ -49,7 +49,7 @@ void givenValidRequest_whenCreateCoffeeChat_thenReturnCreatedCoffeeChatId() thro CreateCoffeeChatResponse response = createCoffeeChatResponse(); doReturn(response).when(coffeeChatService) - .create(any(CreateCoffeeChatRequest.class)); + .create(any(CreateCoffeeChatRequest.class), any(Long.class)); //when ResultActions resultActions = mockMvc.perform(