Skip to content

Commit

Permalink
refactor: place 및 booktalk 찾는 로직 레포지토리 내부로 옮김
Browse files Browse the repository at this point in the history
  • Loading branch information
dong2ast committed Jul 17, 2023
1 parent 7a914d2 commit 71850aa
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
12 changes: 6 additions & 6 deletions src/main/java/org/sophy/sophy/controller/MemberController.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,37 @@
public class MemberController {
private final MemberService memberService;

@GetMapping("/my-page/{memberId}")
@GetMapping("/my-page/{memberId}") //
@ResponseStatus(HttpStatus.OK)
public ApiResponseDto<MyPageDto> getMyPage(@PathVariable("memberId") Long memberId) {
return ApiResponseDto.success(SuccessStatus.GET_MYPAGE_SUCCESS, memberService.getMyPage(memberId));
}

@GetMapping("/my-info/{memberId}")
@GetMapping("/my-info/{memberId}") //내 정보 조회
@ResponseStatus(HttpStatus.OK)
public ApiResponseDto<MyInfoDto> getInfo(@PathVariable("memberId") Long memberId) {
return ApiResponseDto.success(SuccessStatus.GET_MYPAGE_SUCCESS, memberService.getMyInfo(memberId));
}

@PostMapping("/my-info/{memberId}")
@PostMapping("/my-info/{memberId}") //추가 정보 입력
@ResponseStatus(HttpStatus.OK)
public ApiResponseDto<MemberAdditionalInfoDto> postAdditionalInfo(@PathVariable("memberId") Long memberId, @RequestBody MemberAdditionalInfoDto memberAdditionalInfoDto) {
return ApiResponseDto.success(SuccessStatus.POST_ADDITIONALINFO_SUCCESS, memberService.postAdditionalInfo(memberId, memberAdditionalInfoDto));
}

@PatchMapping("/my-info/{memberId}")
@PatchMapping("/my-info/{memberId}") //내 정보 업데이트
@ResponseStatus(HttpStatus.OK)
public ApiResponseDto<MyInfoDto> patchInfo(@PathVariable("memberId") Long memberId, @RequestBody MyInfoDto myInfoDto) {
return ApiResponseDto.success(SuccessStatus.PATCH_MYINFO_SUCCESS, memberService.patchMyInfo(memberId, myInfoDto));
}

@GetMapping("/my-booktalks/{memberId}")
@GetMapping("/my-booktalks/{memberId}") //예정된 북토크 조회 (신청)
@ResponseStatus(HttpStatus.OK)
public ApiResponseDto<List<MyPageBooktalkDto>> getMyBooktalks(@PathVariable("memberId") Long memberId) {
return ApiResponseDto.success(SuccessStatus.GET_MY_BOOKTALKS_SUCCESS, memberService.getBooktalksByMemberId(memberId));
}

@GetMapping("/author-booktalks/{memberId}")
@GetMapping("/author-booktalks/{memberId}") //개설한 북토크 조회
@ResponseStatus(HttpStatus.OK)
public ApiResponseDto<List<MyPageBooktalkDto>> getAuthorBooktalks(@PathVariable("memberId") Long memberId) {
return ApiResponseDto.success(SuccessStatus.GET_AUTHOR_BOOKTALKS_SUCCESS, memberService.getAuthorBooktalksByMemberId(memberId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.sophy.sophy.domain.enumerate.City;
import org.sophy.sophy.domain.Place;
import org.sophy.sophy.exception.ErrorStatus;
import org.sophy.sophy.exception.model.NotFoundException;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -12,4 +14,9 @@ public interface PlaceRepository extends JpaRepository<Place, Long> {
List<Place> findAllByCity(City city);

List<Place> findAll();

default Place getPlaceById(Long placeId) {
return this.findById(placeId)
.orElseThrow(() -> new NotFoundException(ErrorStatus.NOT_FOUND_PLACE_EXCEPTION, ErrorStatus.NOT_FOUND_PLACE_EXCEPTION.getMessage()));
}
}
25 changes: 7 additions & 18 deletions src/main/java/org/sophy/sophy/service/BooktalkService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.sophy.sophy.domain.enumerate.City;
import org.sophy.sophy.exception.ErrorStatus;
import org.sophy.sophy.exception.model.ForbiddenException;
import org.sophy.sophy.exception.model.NotFoundException;
import org.sophy.sophy.infrastructure.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -30,7 +29,7 @@ public class BooktalkService {

@Transactional
public BooktalkCreateResponseDto createBooktalk(BooktalkRequestDto booktalkRequestDto) {
Place place = getPlaceById(booktalkRequestDto.getPlaceId());
Place place = placeRepository.getPlaceById(booktalkRequestDto.getPlaceId());
Member member = memberRepository.getMemberById(booktalkRequestDto.getMemberId());
if (!member.getIsAuthor()) {
throw new ForbiddenException(ErrorStatus.FORBIDDEN_USER_EXCEPTION, ErrorStatus.FORBIDDEN_USER_EXCEPTION.getMessage());
Expand All @@ -41,14 +40,14 @@ public BooktalkCreateResponseDto createBooktalk(BooktalkRequestDto booktalkReque

@Transactional
public BooktalkUpdateDto updateBooktalk(Long booktalkId, BooktalkUpdateDto booktalkUpdateDto) {
Booktalk booktalk = getBooktalkById(booktalkId);
booktalk.patchBooktalk(booktalkUpdateDto, getPlaceById(booktalkUpdateDto.getPlaceId()));
Booktalk booktalk = booktalkRepository.getBooktalkById(booktalkId);
booktalk.patchBooktalk(booktalkUpdateDto, placeRepository.getPlaceById(booktalkUpdateDto.getPlaceId()));
return booktalkUpdateDto;
}

@Transactional
public BooktalkDeleteResponseDto deleteBooktalk(Long booktalkId) { // 수정필요 -> 테이블 외래키 고려하여 관련된 엔티티 전부 삭제해야 함
Booktalk booktalk = getBooktalkById(booktalkId);
Booktalk booktalk = booktalkRepository.getBooktalkById(booktalkId);
//TODO soft delete?
//공간이 거절 됐거나 공간 매칭중일 때만 삭제가능
booktalk.getPlace().deleteBooktalk(booktalk);
Expand All @@ -58,14 +57,14 @@ public BooktalkDeleteResponseDto deleteBooktalk(Long booktalkId) { // 수정필
}

public BooktalkDetailResponseDto getBooktalkDetail(Long booktalkId) {
Booktalk booktalk = getBooktalkById(booktalkId);
Booktalk booktalk = booktalkRepository.getBooktalkById(booktalkId);
return BooktalkDetailResponseDto.of(booktalk);
}

@Transactional
public void postBooktalkParticipation(BooktalkParticipationRequestDto booktalkParticipationRequestDto) {
Member member = memberRepository.getMemberById(booktalkParticipationRequestDto.getMemberId());
Booktalk booktalk = getBooktalkById(booktalkParticipationRequestDto.getBooktalkId());
Booktalk booktalk = booktalkRepository.getBooktalkById(booktalkParticipationRequestDto.getBooktalkId());
// 복합키?
MemberBooktalk memberBooktalk = booktalkParticipationRequestDto.toMemberBooktalk(booktalk, member);
memberBooktalkRepository.save(memberBooktalk);
Expand All @@ -92,16 +91,6 @@ public List<BooktalkDeadlineUpcomingDto> getBooktalkDeadlineUpcoming() {

}

private Place getPlaceById(Long placeId) {
return placeRepository.findById(placeId)
.orElseThrow(() -> new NotFoundException(ErrorStatus.NOT_FOUND_PLACE_EXCEPTION, ErrorStatus.NOT_FOUND_PLACE_EXCEPTION.getMessage()));
}

private Booktalk getBooktalkById(Long booktalkId) {
return booktalkRepository.findById(booktalkId)
.orElseThrow(() -> new NotFoundException(ErrorStatus.NOT_FOUND_BOOKTALK_EXCEPTION, ErrorStatus.NOT_FOUND_BOOKTALK_EXCEPTION.getMessage()));
}

@Transactional
public List<BooktalkResponseDto> getBooktalksByCity(City city) { //지역으로 북토크 조회

Expand All @@ -128,7 +117,7 @@ public List<BooktalkResponseDto> getBooktalksByCity(City city) { //지역으로
}

@Transactional
public CompletedBooktalk completeBooktalk(Long booktalkId) {
public CompletedBooktalk completeBooktalk(Long booktalkId) { //작가가 자신의 북토크를 완료상태로 변경하는 메서드
Booktalk booktalk = booktalkRepository.getBooktalkById(booktalkId);
booktalk.setBooktalkStatus(BooktalkStatus.COMPLETED);
CompletedBooktalk completedBooktalk = CompletedBooktalk.builder()
Expand Down

0 comments on commit 71850aa

Please sign in to comment.