Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/북토크 삭제 모듈 추가 #56

Merged
merged 5 commits into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/main/java/org/sophy/sophy/InitDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,23 @@ public void dbInit() {
.build();
em.persist(booktalk27);

Booktalk booktalk28 = Booktalk.builder()
.place(place1)
.title("테스트용 북토크")
.booktalkImageUrl("dwqE@EWQDQFQEWQ")
.book(book5)
.member(author2)
.bookCategory(book11.getBookCategory())
.startDate(LocalDateTime.of(2023, 8, 24, 11, 0))
.endDate(LocalDateTime.of(2023, 8, 24, 12, 0))
.maximum(4)
.participationFee(0)
.preliminaryInfo(PreliminaryInfo.COMFORTABLE_COMING)
.description("항상 새로운 아이디어가 쏟아지는 시대, 아이디어를 찾는 방법도 여러 가지. 하루하루 아이디어를 위해 새로움을 찾는 마케터들은 어떤 방법을 쓸까요? 마케터 이승희의 영감을 만나보세요!")
.booktalkStatus(BooktalkStatus.RECRUITING)
.build();
em.persist(booktalk28);

MemberBooktalk memberBooktalk = MemberBooktalk.builder()
.member(citizen)
.booktalk(booktalk1)
Expand All @@ -909,8 +926,14 @@ public void dbInit() {
.booktalk(booktalk2)
.build();

MemberBooktalk memberBooktalk28 = MemberBooktalk.builder()
.member(citizen)
.booktalk(booktalk28)
.build();

em.persist(memberBooktalk);
em.persist(memberBooktalk2);
em.persist(memberBooktalk28);

CompletedBooktalk completedBooktalk = CompletedBooktalk.builder()
.title("끝난 북토크")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import org.sophy.sophy.common.dto.ApiResponseDto;
import org.sophy.sophy.domain.CompletedBooktalk;
import org.sophy.sophy.domain.dto.booktalk.BooktalkUpdateDto;
import org.sophy.sophy.domain.dto.booktalk.request.BooktalkParticipationRequestDto;
import org.sophy.sophy.domain.dto.booktalk.request.BooktalkRequestDto;
Expand Down Expand Up @@ -62,4 +63,10 @@ public ApiResponseDto postBooktalkParticipation(@Valid @RequestBody BooktalkPart
public ApiResponseDto<List<BooktalkDeadlineUpcomingDto>> getBooktalkDeadlineUpcoming() {
return ApiResponseDto.success(SuccessStatus.GET_BOOKTALKS_DEADLINE_UPCOMING_SUCCESS, booktalkService.getBooktalkDeadlineUpcoming());
}

@PostMapping("/{booktalkId}")
@ResponseStatus(HttpStatus.OK)
public ApiResponseDto<CompletedBooktalk> completeBooktalk(@PathVariable("booktalkId") Long booktalkId) {
return ApiResponseDto.success(SuccessStatus.DELETE_BOOKTALK_SUCCESS, booktalkService.completeBooktalk(booktalkId));
}
}
14 changes: 10 additions & 4 deletions src/main/java/org/sophy/sophy/domain/Booktalk.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import org.sophy.sophy.domain.dto.booktalk.BooktalkUpdateDto;
import org.sophy.sophy.domain.enumerate.BookCategory;
import org.sophy.sophy.domain.enumerate.BooktalkStatus;
Expand All @@ -17,6 +19,8 @@
@Entity
@Getter
@NoArgsConstructor
@SQLDelete(sql = "UPDATE booktalk SET deleted = true WHERE booktalk_id=?")
@Where(clause = "deleted=false")
public class Booktalk extends AuditingTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -42,6 +46,10 @@ public class Booktalk extends AuditingTimeEntity {
@JoinColumn(name = "author_property_id")
private AuthorProperty authorProperty;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "operator_property_id")
private OperatorProperty operatorProperty;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "book_id")
private Book book;
Expand Down Expand Up @@ -73,16 +81,14 @@ public class Booktalk extends AuditingTimeEntity {
@Enumerated(EnumType.STRING)
private BooktalkStatus booktalkStatus;

@OneToMany(mappedBy = "booktalk")
@OneToMany(mappedBy = "booktalk", cascade = CascadeType.ALL, orphanRemoval = true)
private List<MemberBooktalk> participantList = new ArrayList<>();

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "scheduled_booktalk_id")
private ScheduledBooktalk scheduledBooktalk;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "operator_property_id")
private OperatorProperty operatorProperty;
private Boolean deleted = Boolean.FALSE;

public void setBooktalkStatus(BooktalkStatus booktalkStatus) {
this.booktalkStatus = booktalkStatus;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/sophy/sophy/domain/CompletedBooktalk.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public class CompletedBooktalk extends AuditingTimeEntity{
private String authorName;
private LocalDateTime booktalkDate;
private String placeName;
@ManyToOne

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;

Expand Down
27 changes: 23 additions & 4 deletions src/main/java/org/sophy/sophy/service/BooktalkService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
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.BooktalkRepository;
import org.sophy.sophy.infrastructure.MemberBooktalkRepository;
import org.sophy.sophy.infrastructure.MemberRepository;
import org.sophy.sophy.infrastructure.PlaceRepository;
import org.sophy.sophy.infrastructure.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -29,6 +26,7 @@ public class BooktalkService {
private final PlaceRepository placeRepository;
private final MemberRepository memberRepository;
private final MemberBooktalkRepository memberBooktalkRepository;
private final CompletedBooktalkRepository completedBooktalkRepository;

@Transactional
public BooktalkCreateResponseDto createBooktalk(BooktalkRequestDto booktalkRequestDto) {
Expand Down Expand Up @@ -128,4 +126,25 @@ public List<BooktalkResponseDto> getBooktalksByCity(City city) { //지역으로

return booktalkList;
}

@Transactional
public CompletedBooktalk completeBooktalk(Long booktalkId) {
Booktalk booktalk = booktalkRepository.getBooktalkById(booktalkId);
booktalk.setBooktalkStatus(BooktalkStatus.COMPLETED);
CompletedBooktalk completedBooktalk = CompletedBooktalk.builder()
.title(booktalk.getTitle())
.bookName(booktalk.getBook().getTitle())
.authorName(booktalk.getMember().getName())
.booktalkDate(booktalk.getEndDate())
.placeName(booktalk.getPlace().getName())
.build();
completedBooktalkRepository.save(completedBooktalk); //완료된 북토크 엔티티를 영속화 시켜야 다음 작업들에 사용 가능
for(MemberBooktalk memberBooktalk : booktalk.getParticipantList()){
Member member = memberBooktalk.getMember();
member.getCompletedBookTalkList().add(completedBooktalk);
completedBooktalk.setMember(member);
}
booktalkRepository.delete(booktalk);
return completedBooktalk;
}
}
16 changes: 12 additions & 4 deletions src/main/java/org/sophy/sophy/service/SchedulerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.sophy.sophy.domain.*;
import org.sophy.sophy.domain.enumerate.BooktalkStatus;
import org.sophy.sophy.infrastructure.BooktalkRepository;
import org.sophy.sophy.infrastructure.CompletedBooktalkRepository;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -15,20 +16,25 @@
@RequiredArgsConstructor
public class SchedulerService {
private final BooktalkRepository booktalkRepository;
private final CompletedBooktalkRepository completedBooktalkRepository;

@Scheduled(fixedDelay = 60000)
@Transactional
public void updateBooktalkStatus() { //1분마다 북토크 상태 측정
List<Booktalk> recrutingBooktalks = booktalkRepository.findAllByBooktalkStatus(BooktalkStatus.RECRUITING);
List<Booktalk> closedBooktalks = booktalkRepository.findAllByBooktalkStatus(BooktalkStatus.RECRUITING_CLOSED);

recrutingBooktalks.forEach(SchedulerService::booktalkToCompletedBooktalk);
recrutingBooktalks.forEach(booktalk -> {
booktalkToCompletedBooktalk(booktalk);
});

closedBooktalks.forEach(SchedulerService::booktalkToCompletedBooktalk);
closedBooktalks.forEach(booktalk -> {
booktalkToCompletedBooktalk(booktalk);
});
}

private static void booktalkToCompletedBooktalk(Booktalk booktalk) { //북토크가 기간이 지났을 때 지난 북토크로 매핑하면서 객체들 상태 및 연관관계 변경
if(booktalk.getEndDate().isBefore(LocalDateTime.now())){
private void booktalkToCompletedBooktalk(Booktalk booktalk) { //북토크가 기간이 지났을 때 지난 북토크로 매핑하면서 객체들 상태 및 연관관계 변경
if(booktalk.getEndDate().isBefore(LocalDateTime.now())){ //북토크 마감일이 현재시간보다 이전이라면
booktalk.setBooktalkStatus(BooktalkStatus.COMPLETED);
CompletedBooktalk completedBooktalk = CompletedBooktalk.builder()
.title(booktalk.getTitle())
Expand All @@ -37,11 +43,13 @@ private static void booktalkToCompletedBooktalk(Booktalk booktalk) { //북토크
.booktalkDate(booktalk.getEndDate())
.placeName(booktalk.getPlace().getName())
.build();
completedBooktalkRepository.save(completedBooktalk);
for(MemberBooktalk memberBooktalk : booktalk.getParticipantList()){
Member member = memberBooktalk.getMember();
member.getCompletedBookTalkList().add(completedBooktalk);
completedBooktalk.setMember(member);
}
booktalkRepository.delete(booktalk);
}
}

Expand Down