Skip to content

Commit

Permalink
Feature: 커피챗 생성시 로그인 유저의 정보로 작성자 설정하도록 변경 (Kernel360#170)
Browse files Browse the repository at this point in the history
* Refactor: DTO에서 toEntity 메서드를 인스턴스 메서드로 변경

* Feat: 커피챗 등록 시 로그인 유저의 정보로 작성자 맵핑

* Fix: create 메소드 변경됨에 따라 테스트 수정
  • Loading branch information
aqrms authored Jan 22, 2024
1 parent d1c6997 commit c88dd81
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -38,8 +40,10 @@ public ResponseEntity<CommonResponse> get(@PathVariable(name = "id") Long coffee
}

@PostMapping("/api/v1/coffeechats")
public ResponseEntity<CommonResponse> save(@RequestBody CreateCoffeeChatRequest request) {
CreateCoffeeChatResponse response = coffeeChatService.create(request);
public ResponseEntity<CommonResponse> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -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());
}
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit c88dd81

Please sign in to comment.