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: POST 북토크 참여신청 #47

Merged
merged 7 commits into from
Jul 13, 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.RequiredArgsConstructor;
import org.sophy.sophy.common.dto.ApiResponseDto;
import org.sophy.sophy.controller.dto.BooktalkUpdateDto;
import org.sophy.sophy.controller.dto.request.BooktalkParticipationRequestDto;
import org.sophy.sophy.controller.dto.request.BooktalkRequestDto;
import org.sophy.sophy.controller.dto.request.CityRequestDto;
import org.sophy.sophy.controller.dto.response.*;
Expand Down Expand Up @@ -49,6 +50,12 @@ public ApiResponseDto<BooktalkDetailResponseDto> getBooktalkDetail(@PathVariable
public ApiResponseDto<List<BooktalkResponseDto>> getPlacesByCity(@Valid @RequestBody CityRequestDto cityRequestDto) {
return ApiResponseDto.success(SuccessStatus.GET_BOOKTALKS_BY_CITY_SUCCESS, booktalkService.getBooktalksByCity(cityRequestDto));
}
@PostMapping("/participation")
@ResponseStatus(HttpStatus.CREATED)
public ApiResponseDto postBooktalkParticipation(@Valid @RequestBody BooktalkParticipationRequestDto booktalkParticipationRequestDto) {
booktalkService.postBooktalkParticipation(booktalkParticipationRequestDto);
return ApiResponseDto.success(SuccessStatus.CREATE_BOOKTALK_PARTICIPATION_SUCCESS);
}

@GetMapping("/deadline-upcoming")
@ResponseStatus(HttpStatus.OK)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.sophy.sophy.controller.dto.request;

import lombok.Getter;
import org.sophy.sophy.domain.Booktalk;
import org.sophy.sophy.domain.Member;
import org.sophy.sophy.domain.MemberBooktalk;

import javax.validation.constraints.NotNull;

@Getter
public class BooktalkParticipationRequestDto {
@NotNull
private Long booktalkId;
@NotNull
private Long memberId;

public MemberBooktalk toMemberBooktalk(Booktalk booktalk, Member member) {
return MemberBooktalk.builder()
.booktalk(booktalk)
.member(member)
.build();
}
}
18 changes: 14 additions & 4 deletions src/main/java/org/sophy/sophy/domain/Member.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package org.sophy.sophy.domain;

import lombok.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.sophy.sophy.controller.dto.request.MemberAdditionalInfoDto;
import org.sophy.sophy.domain.dto.MyInfoDto;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Member extends AuditingTimeEntity{
public class Member extends AuditingTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "member_id")
Expand Down Expand Up @@ -94,4 +98,10 @@ public void patchMyInfo(MyInfoDto myInfoDto) {
this.myCity = myInfoDto.getCity();
this.marketingAgree = myInfoDto.getMarketingAgree();
}

public void addUserBooktalkandSortByStartDate(MemberBooktalk memberBooktalk) {
this.getUserBookTalkList().add(memberBooktalk);
// 시작날짜순으로 정렬(예정된 북토크)
this.getUserBookTalkList().sort(Comparator.comparing(o -> o.getBooktalk().getStartDate()));
}
}
7 changes: 4 additions & 3 deletions src/main/java/org/sophy/sophy/domain/MemberBooktalk.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ public class MemberBooktalk extends AuditingTimeEntity {
// 연관 관계 편의 메서드
public void setMember(Member member) {
this.member = member;
if(!member.getUserBookTalkList().contains(this)) {
member.getUserBookTalkList().add(this);
if (!member.getUserBookTalkList().contains(this)) {
// 시작날짜로 순으로 정렬해서 추가
member.addUserBooktalkandSortByStartDate(this);
}
}

Expand All @@ -41,7 +42,7 @@ public void setBooktalk(Booktalk booktalk) {

@Builder
public MemberBooktalk(Member member, Booktalk booktalk) {
setMember(member);
setBooktalk(booktalk);
setMember(member);
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/sophy/sophy/exception/SuccessStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public enum SuccessStatus {
*/
SIGNUP_SUCCESS(HttpStatus.CREATED, "회원가입이 완료되었습니다."),
CREATE_BOOKTALK_SUCCESS(HttpStatus.CREATED, "북토크를 생성했습니다."),
CREATE_BOOKTALK_PARTICIPATION_SUCCESS(HttpStatus.CREATED, "북토크 참여 신청을 성공했습니다."),

;

private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.sophy.sophy.infrastructure;

import org.sophy.sophy.domain.MemberBooktalk;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface MemberBooktalkRepository extends JpaRepository<MemberBooktalk, Long> {
}
13 changes: 13 additions & 0 deletions src/main/java/org/sophy/sophy/service/BooktalkService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import org.sophy.sophy.controller.dto.BooktalkUpdateDto;
import org.sophy.sophy.controller.dto.request.BooktalkParticipationRequestDto;
import org.sophy.sophy.controller.dto.request.BooktalkRequestDto;
import org.sophy.sophy.controller.dto.request.CityRequestDto;
import org.sophy.sophy.controller.dto.response.*;
Expand All @@ -10,6 +11,7 @@
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.springframework.stereotype.Service;
Expand All @@ -25,6 +27,7 @@ public class BooktalkService {
private final BooktalkRepository booktalkRepository;
private final PlaceRepository placeRepository;
private final MemberRepository memberRepository;
private final MemberBooktalkRepository memberBooktalkRepository;

@Transactional
public BooktalkCreateResponseDto createBooktalk(BooktalkRequestDto booktalkRequestDto) {
Expand Down Expand Up @@ -60,6 +63,15 @@ public BooktalkDetailResponseDto getBooktalkDetail(Long booktalkId) {
return BooktalkDetailResponseDto.of(booktalk);
}

@Transactional
public void postBooktalkParticipation(BooktalkParticipationRequestDto booktalkParticipationRequestDto) {
Member member = getMemberById(booktalkParticipationRequestDto.getMemberId());
Booktalk booktalk = getBooktalkById(booktalkParticipationRequestDto.getBooktalkId());
// 복합키?
MemberBooktalk memberBooktalk = booktalkParticipationRequestDto.toMemberBooktalk(booktalk, member);
memberBooktalkRepository.save(memberBooktalk);
}

public List<BooktalkDeadlineUpcomingDto> getBooktalkDeadlineUpcoming() {
List<Place> placeList = placeRepository.findAll();

Expand All @@ -78,6 +90,7 @@ public List<BooktalkDeadlineUpcomingDto> getBooktalkDeadlineUpcoming() {
booktalkList.sort(Comparator.comparing(BooktalkDeadlineUpcomingDto::getEndDate));

return booktalkList;

}

private Member getMemberById(Long memberId) {
Expand Down