Skip to content

Commit

Permalink
Merge pull request #41 from kookmin-sw/BE_Feature/#13-club
Browse files Browse the repository at this point in the history
#13 feat: 대표책 선정
  • Loading branch information
wjdwlghks authored Apr 26, 2024
2 parents 945521b + a497500 commit bef190e
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.project.capstone.book.controller;

import com.project.capstone.book.controller.dto.AddBookRequest;
import com.project.capstone.book.service.BookService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.project.capstone.book.controller;
package com.project.capstone.book.controller.dto;

public record AddBookRequest(
String isbn,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.project.capstone.book.controller.dto;

import com.project.capstone.book.domain.Book;

public record BookResponse(
Long id,
String isbn,
String title,
String author,
String publisher
) {
public BookResponse(Book book) {
this(book.getId(), book.getIsbn(), book.getTitle(), book.getAuthor(), book.getPublisher());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.project.capstone.book.domain;

import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.project.capstone.book.controller.AddBookRequest;
import com.project.capstone.book.controller.dto.AddBookRequest;
import com.project.capstone.club.domain.Club;
import com.project.capstone.content.domain.Content;
import com.project.capstone.mybook.domain.MyBook;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.project.capstone.book.service;

import com.project.capstone.book.controller.AddBookRequest;
import com.project.capstone.book.controller.dto.AddBookRequest;
import com.project.capstone.book.domain.Book;
import com.project.capstone.book.domain.BookRepository;
import com.project.capstone.book.exception.BookException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.project.capstone.club.controller;

import com.project.capstone.auth.domain.PrincipalDetails;
import com.project.capstone.book.controller.dto.AddBookRequest;
import com.project.capstone.club.controller.dto.ClubCreateRequest;
import com.project.capstone.club.controller.dto.ClubResponse;
import com.project.capstone.club.service.ClubService;
Expand Down Expand Up @@ -60,14 +61,22 @@ public ResponseEntity<?> out(@AuthenticationPrincipal PrincipalDetails details,
@PutMapping("/delegate")
public ResponseEntity<?> delegateManager(@AuthenticationPrincipal PrincipalDetails details,
@RequestParam UUID memberId, @RequestParam Long clubId) {
clubService.delegateManager(details, memberId, clubId);
clubService.delegateManager(details.getUserId(), memberId, clubId);
return ResponseEntity.ok().body("위임 완료");
}
// 멤버 추방하기
@GetMapping("/expel")
public ResponseEntity<?> expelMember(@AuthenticationPrincipal PrincipalDetails details,
@RequestParam UUID memberId, @RequestParam Long clubId) {
clubService.expelMember(details, memberId, clubId);
clubService.expelMember(details.getUserId(), memberId, clubId);
return ResponseEntity.ok().body("추방 완료");
}

// 대표책 선정하기
@PostMapping("/book")
public ResponseEntity<?> setBook(@AuthenticationPrincipal PrincipalDetails details,
@RequestBody AddBookRequest request, @RequestParam Long clubId) {
clubService.setBook(details.getUserId(), request, clubId);
return ResponseEntity.ok().body("선정 완료");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.project.capstone.club.controller.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.project.capstone.book.controller.dto.BookResponse;
import com.project.capstone.book.domain.Book;
import com.project.capstone.club.domain.Club;
import com.project.capstone.club.domain.PublicStatus;
import com.project.capstone.post.controller.dto.PostResponse;
Expand All @@ -12,7 +14,7 @@

public record ClubResponse (
Long id,
Long bookId,
BookResponse book,
String topic,
String name,
LocalDateTime createdAt,
Expand All @@ -22,7 +24,7 @@ public record ClubResponse (

) {
public ClubResponse(Club club) {
this(club.getId(), club.getBook() == null ? null : club.getBook().getId(), club.getTopic(), club.getName(), club.getCreatedAt(), club.getMaximum(),
this(club.getId(), club.getBook() == null ? null : createBookResponse(club.getBook()), club.getTopic(), club.getName(), club.getCreatedAt(), club.getMaximum(),
club.getPublicStatus(), createPostResponseList(club.getPosts()));
}

Expand All @@ -33,4 +35,8 @@ private static List<PostResponse> createPostResponseList(List<Post> postList) {
}
return postResponseList;
}

private static BookResponse createBookResponse(Book book) {
return new BookResponse(book);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder
@ToString
public class Club {
Expand Down Expand Up @@ -59,6 +60,7 @@ public class Club {
private List<Quiz> quizzes = new ArrayList<>();

@ManyToOne
@JoinColumn(name = "book_id")
private Book book;

public Club(ClubCreateRequest request, UUID memberId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.project.capstone.club.domain;

import com.project.capstone.book.domain.Book;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -15,4 +16,8 @@ public interface ClubRepository extends JpaRepository<Club, Long> {
@Modifying(clearAutomatically = true)
@Query("update Club c set c.managerId = :id")
void updateManager(UUID id);

@Modifying(clearAutomatically = true)
@Query("update Club c set c.book = :book where c.id = :id")
void updateBook(Book book, Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public enum ClubExceptionType implements ExceptionType {

CLUB_NOT_FOUND(NOT_FOUND, 101, "해당 모임을 찾을 수 없습니다."),
EXIT_WITHOUT_DELEGATION(BAD_REQUEST, 102, "모임장은 위임 후 모임을 나갈 수 있습니다."),
UNAUTHORIZED_ACTION(UNAUTHORIZED, 103, "모임장만 할 수 있는 기능입니다.")
UNAUTHORIZED_ACTION(UNAUTHORIZED, 103, "모임장만 할 수 있는 기능입니다."),
INVALID_TARGET(BAD_REQUEST, 104, "모임장을 추방 또는 위임할 수 없습니다.")
;

private final HttpStatus status;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.project.capstone.club.service;

import com.project.capstone.auth.domain.PrincipalDetails;
import com.project.capstone.book.controller.dto.AddBookRequest;
import com.project.capstone.book.domain.Book;
import com.project.capstone.book.domain.BookRepository;
import com.project.capstone.club.controller.dto.ClubCreateRequest;
import com.project.capstone.club.controller.dto.ClubResponse;
import com.project.capstone.club.domain.Club;
Expand Down Expand Up @@ -32,6 +34,7 @@ public class ClubService {
private final ClubRepository clubRepository;
private final MemberClubRepository memberClubRepository;
private final MemberRepository memberRepository;
private final BookRepository bookRepository;

public List<ClubResponse> searchByTopic(String topic) {
List<ClubResponse> clubResponseList = new ArrayList<>();
Expand Down Expand Up @@ -84,25 +87,39 @@ public void out(String userId, Long clubId) {
}

@Transactional
public void delegateManager(PrincipalDetails details, UUID memberId, Long clubId) {
checkIsManagerAndTargetIsClubMember(details, memberId, clubId);
public void delegateManager(String managerId, UUID memberId, Long clubId) {
if (managerId.equals(memberId.toString())) {
throw new ClubException(INVALID_TARGET);
}
checkIsManagerAndTargetIsClubMember(managerId, memberId, clubId);
clubRepository.updateManager(memberId);
}

@Transactional
public void expelMember(PrincipalDetails details, UUID memberId, Long clubId) {
checkIsManagerAndTargetIsClubMember(details, memberId, clubId);
public void expelMember(String managerId, UUID memberId, Long clubId) {
if (managerId.equals(memberId.toString())) {
throw new ClubException(INVALID_TARGET);
}
checkIsManagerAndTargetIsClubMember(managerId, memberId, clubId);
memberClubRepository.deleteMemberClubByClub_IdAndMember_Id(clubId, memberId);
}

private void checkIsManagerAndTargetIsClubMember(PrincipalDetails details, UUID memberId, Long clubId) {
private void checkIsManagerAndTargetIsClubMember(String managerId, UUID memberId, Long clubId) {
Member member = memberRepository.findMemberById(memberId).orElseThrow(
() -> new MemberException(MEMBER_NOT_FOUND)
);
Club club = checkIsManager(managerId, clubId);
if (memberClubRepository.findMemberClubByMemberAndClub(member, club).isEmpty()) {
throw new MemberClubException(MEMBERCLUB_NOT_FOUND);
}
}

private Club checkIsManager(String managerId, Long clubId) {
Club club = findClubById(clubId);
if (!club.getManagerId().toString().equals(details.getUserId())) {
if (!club.getManagerId().toString().equals(managerId)) {
throw new ClubException(UNAUTHORIZED_ACTION);
}
if (memberClubRepository.findMemberClubByMember_IdAndClub_Id(memberId, clubId).isEmpty()) {
throw new MemberClubException(MEMBERCLUB_NOT_FOUND);
}
return club;
}

private Club findClubById(Long clubId) {
Expand All @@ -115,4 +132,14 @@ public ClubResponse getClub(Long clubId) {
Club club = findClubById(clubId);
return new ClubResponse(club);
}

@Transactional
public void setBook(String managerId, AddBookRequest request, Long clubId) {
Club club = checkIsManager(managerId, clubId);
Book book = bookRepository.findBookByIsbn(request.isbn()).orElseGet(
() -> bookRepository.save(new Book(request))
);
club.setBook(book);
clubRepository.updateBook(book, clubId);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.project.capstone.member.controller;

import com.project.capstone.auth.domain.PrincipalDetails;
import com.project.capstone.book.controller.AddBookRequest;
import com.project.capstone.book.controller.dto.AddBookRequest;
import com.project.capstone.member.controller.dto.MemberResponse;
import com.project.capstone.member.controller.dto.MyBookResponse;
import com.project.capstone.member.service.MemberService;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.project.capstone.member.service;

import com.project.capstone.book.controller.AddBookRequest;
import com.project.capstone.book.controller.dto.AddBookRequest;
import com.project.capstone.book.domain.Book;
import com.project.capstone.book.domain.BookRepository;
import com.project.capstone.member.controller.dto.MemberResponse;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.project.capstone.memberclub.domain;

import com.project.capstone.club.domain.Club;
import com.project.capstone.member.domain.Member;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
Expand All @@ -11,4 +13,6 @@ public interface MemberClubRepository extends JpaRepository<MemberClub, Long> {
void deleteMemberClubByClub_IdAndMember_Id(Long clubId, UUID memberId);
Optional<MemberClub> findMemberClubByMember_IdAndClub_Id(UUID memberId, Long clubId);
List<MemberClub> findMemberClubsByMember_Id(UUID memberId);
Optional<MemberClub> findMemberClubByMemberAndClub(Member member, Club club);

}

0 comments on commit bef190e

Please sign in to comment.