Skip to content

Commit

Permalink
feat: 공연 후기 작성자(회원) 추가 (#184)
Browse files Browse the repository at this point in the history
* feat: #182 EventReview 작성자(회원) 추가 및 리뷰 작성, 수정, 삭제 로직에 회원 관련 로직 추가

* test: #182 EventReview 테스트 부분 MemberFactory로 Member 생성하도록 변경

---------

Co-authored-by: Kyunghun Kim <[email protected]>
  • Loading branch information
park0jae and KarmaPol authored Jan 10, 2024
1 parent 2c3baf1 commit 83a0029
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.pgms.apievent.eventreview.dto.response.EventReviewResponse;
import com.pgms.apievent.eventreview.service.EventReviewService;
import com.pgms.coredomain.response.ApiResponse;
import com.pgms.coresecurity.security.resolver.CurrentAccount;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
Expand All @@ -32,9 +33,10 @@ public class EventReviewController {

@PostMapping("/{eventId}")
public ResponseEntity<ApiResponse> createEventReview(
@CurrentAccount Long memberId,
@PathVariable Long eventId,
@Valid @RequestBody EventReviewCreateRequest request) {
EventReviewResponse response = eventReviewService.createEventReview(eventId, request);
EventReviewResponse response = eventReviewService.createEventReview(memberId, eventId, request);
URI location = ServletUriComponentsBuilder.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(response.id())
Expand All @@ -45,9 +47,10 @@ public ResponseEntity<ApiResponse> createEventReview(

@PatchMapping("/{reviewId}")
public ResponseEntity<ApiResponse> updateEventReview(
@CurrentAccount Long memberId,
@PathVariable Long reviewId,
@Valid @RequestBody EventReviewUpdateRequest request) {
EventReviewResponse response = eventReviewService.updateEventReview(reviewId, request);
EventReviewResponse response = eventReviewService.updateEventReview(memberId, reviewId, request);
return ResponseEntity.ok(ApiResponse.ok(response));
}

Expand All @@ -64,8 +67,8 @@ public ResponseEntity<ApiResponse> getEventReviewsForEventByEventId(@PathVariabl
}

@DeleteMapping("/{reviewId}")
public ResponseEntity<Void> deleteEventReviewById(@PathVariable Long reviewId) {
eventReviewService.deleteEventReviewById(reviewId);
public ResponseEntity<Void> deleteEventReviewById(@CurrentAccount Long memberId, @PathVariable Long reviewId) {
eventReviewService.deleteEventReviewById(memberId, reviewId);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.pgms.coredomain.domain.event.Event;
import com.pgms.coredomain.domain.event.EventReview;
import com.pgms.coredomain.domain.member.Member;

import jakarta.validation.constraints.PositiveOrZero;
import jakarta.validation.constraints.Size;
Expand All @@ -12,12 +13,13 @@ public record EventReviewCreateRequest(

@Size(max = 1000, message = "공연 리뷰 내용은 최대 1,000자까지 입력 가능 합니다.")
String content) {
public EventReview toEntity(Event event) {

public EventReview toEntity(Event event, Member member) {
return new EventReview(
score,
content,
event
event,
member
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import com.pgms.coredomain.domain.event.EventReview;
import com.pgms.coredomain.domain.event.repository.EventRepository;
import com.pgms.coredomain.domain.event.repository.EventReviewRepository;
import com.pgms.coredomain.domain.member.Member;
import com.pgms.coredomain.domain.member.repository.MemberRepository;

import lombok.RequiredArgsConstructor;

Expand All @@ -25,29 +27,31 @@ public class EventReviewService {

private final EventReviewRepository eventReviewRepository;
private final EventRepository eventRepository;
private final MemberRepository memberRepository;

public EventReviewResponse createEventReview(Long eventId, EventReviewCreateRequest request) {
public EventReviewResponse createEventReview(Long memberId, Long eventId, EventReviewCreateRequest request) {
Member member = getMember(memberId);
Event event = eventRepository.findById(eventId).
orElseThrow(() -> new EventException(EVENT_NOT_FOUND));
EventReview eventReview = eventReviewRepository.save(request.toEntity(event));
EventReview eventReview = eventReviewRepository.save(request.toEntity(event, member));

Double averageScore = eventReviewRepository.findAverageScoreByEvent(event.getId());
event.updateAverageScore(averageScore);

return EventReviewResponse.of(eventReview);
}

public EventReviewResponse updateEventReview(Long reviewId, EventReviewUpdateRequest request) {
EventReview eventReview = eventReviewRepository.findById(reviewId)
.orElseThrow(() -> new EventException(EVENT_REVIEW_NOT_FOUND));
public EventReviewResponse updateEventReview(Long memberId, Long reviewId, EventReviewUpdateRequest request) {
// TODO : 작성자와 현재 로그인한 사람이 일치하는지 검증 로직 필요
Member member = getMember(memberId);
EventReview eventReview = getEventReview(reviewId);
eventReview.updateEventReview(request.content());
return EventReviewResponse.of(eventReview);
}

@Transactional(readOnly = true)
public EventReviewResponse getEventReviewById(Long reviewId) {
EventReview eventReview = eventReviewRepository.findById(reviewId)
.orElseThrow(() -> new EventException(EVENT_REVIEW_NOT_FOUND));
EventReview eventReview = getEventReview(reviewId);
return EventReviewResponse.of(eventReview);
}

Expand All @@ -59,9 +63,20 @@ public List<EventReviewResponse> getEventReviewsForEventByEventId(Long eventId)
.toList();
}

public void deleteEventReviewById(Long reviewId) {
EventReview eventReview = eventReviewRepository.findById(reviewId)
.orElseThrow(() -> new EventException(EVENT_REVIEW_NOT_FOUND));
public void deleteEventReviewById(Long memberId, Long reviewId) {
// TODO : 작성자와 현재 로그인한 사람이 일치하는지 검증 로직 필요
Member member = getMember(memberId);
EventReview eventReview = getEventReview(reviewId);
eventReviewRepository.delete(eventReview);
}

private EventReview getEventReview(Long reviewId) {
return eventReviewRepository.findById(reviewId)
.orElseThrow(() -> new EventException(EVENT_REVIEW_NOT_FOUND));
}

private Member getMember(Long memberId) {
return memberRepository.findById(memberId)
.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
import com.pgms.apievent.factory.event.EventFactory;
import com.pgms.apievent.factory.eventhall.EventHallFactory;
import com.pgms.apievent.factory.eventreview.EventReviewFactory;
import com.pgms.apievent.factory.member.MemberFactory;
import com.pgms.coredomain.domain.event.Event;
import com.pgms.coredomain.domain.event.EventHall;
import com.pgms.coredomain.domain.event.EventReview;
import com.pgms.coredomain.domain.event.repository.EventHallRepository;
import com.pgms.coredomain.domain.event.repository.EventRepository;
import com.pgms.coredomain.domain.event.repository.EventReviewRepository;
import com.pgms.coredomain.domain.member.Member;
import com.pgms.coredomain.domain.member.repository.MemberRepository;

@SpringBootTest
@ContextConfiguration(classes = EventTestConfig.class)
Expand All @@ -44,6 +47,9 @@ class EventReviewServiceTest {
@Autowired
private EventHallRepository eventHallRepository;

@Autowired
private MemberRepository memberRepository;

@AfterEach
void tearDown() {
eventReviewRepository.deleteAll();
Expand All @@ -54,10 +60,12 @@ void tearDown() {
// Given
EventHall eventHall = eventHallRepository.save(EventHallFactory.createEventHall());
Event event = eventRepository.save(EventFactory.createEvent(eventHall));
Member member = memberRepository.save(MemberFactory.createMember());

EventReviewCreateRequest request = new EventReviewCreateRequest(5, "공연이 너무 재밌어요 !");

// When
EventReviewResponse response = eventReviewService.createEventReview(event.getId(), request);
EventReviewResponse response = eventReviewService.createEventReview(member.getId(), event.getId(), request);

// Then
assertThat(response.eventId()).isEqualTo(event.getId());
Expand All @@ -70,13 +78,15 @@ void tearDown() {
// Given
EventHall eventHall = eventHallRepository.save(EventHallFactory.createEventHall());
Event event = eventRepository.save(EventFactory.createEvent(eventHall));
Member member = memberRepository.save(MemberFactory.createMember());

List<EventReviewCreateRequest> requestList = IntStream.range(0, REQUEST_NUMBER)
.mapToObj(i -> new EventReviewCreateRequest(i, "리뷰 내용 " + i))
.toList();

// When
IntStream.range(0, REQUEST_NUMBER)
.forEach(i -> eventReviewService.createEventReview(event.getId(), requestList.get(i)));
.forEach(i -> eventReviewService.createEventReview(member.getId(), event.getId(), requestList.get(i)));
Event savedEvent = eventRepository.findById(event.getId()).get();

// Then
Expand All @@ -88,12 +98,15 @@ void tearDown() {
// Given
EventHall eventHall = eventHallRepository.save(EventHallFactory.createEventHall());
Event event = eventRepository.save(EventFactory.createEvent(eventHall));
EventReview eventReview = eventReviewRepository.save(EventReviewFactory.createEventReview(event));
Member member = memberRepository.save(MemberFactory.createMember());

EventReview eventReview = eventReviewRepository.save(EventReviewFactory.createEventReview(event, member));

EventReviewUpdateRequest request = new EventReviewUpdateRequest("공연 후기 수정입니다~!");

// When
EventReviewResponse response = eventReviewService.updateEventReview(eventReview.getId(), request);
EventReviewResponse response = eventReviewService.updateEventReview(member.getId(), eventReview.getId(),
request);

// Then
assertThat(response.content()).isEqualTo(request.content());
Expand All @@ -104,7 +117,9 @@ void tearDown() {
// Given
EventHall eventHall = eventHallRepository.save(EventHallFactory.createEventHall());
Event event = eventRepository.save(EventFactory.createEvent(eventHall));
EventReview eventReview = eventReviewRepository.save(EventReviewFactory.createEventReview(event));
Member member = memberRepository.save(MemberFactory.createMember());

EventReview eventReview = eventReviewRepository.save(EventReviewFactory.createEventReview(event, member));
Long eventReviewId = eventReview.getId();

// When
Expand All @@ -121,8 +136,10 @@ void tearDown() {
// Given
EventHall eventHall = eventHallRepository.save(EventHallFactory.createEventHall());
Event event = eventRepository.save(EventFactory.createEvent(eventHall));
Member member = memberRepository.save(MemberFactory.createMember());

IntStream.range(0, REQUEST_NUMBER)
.forEach(i -> eventReviewRepository.save(new EventReview(i, "리뷰 내용 " + i, event)));
.forEach(i -> eventReviewRepository.save(new EventReview(i, "리뷰 내용 " + i, event, member)));

// When
List<EventReviewResponse> responses = eventReviewService.getEventReviewsForEventByEventId(event.getId());
Expand All @@ -136,10 +153,12 @@ void tearDown() {
// Given
EventHall eventHall = eventHallRepository.save(EventHallFactory.createEventHall());
Event event = eventRepository.save(EventFactory.createEvent(eventHall));
EventReview eventReview = eventReviewRepository.save(EventReviewFactory.createEventReview(event));
Member member = memberRepository.save(MemberFactory.createMember());

EventReview eventReview = eventReviewRepository.save(EventReviewFactory.createEventReview(event, member));

// When
eventReviewService.deleteEventReviewById(eventReview.getId());
eventReviewService.deleteEventReviewById(member.getId(), eventReview.getId());

// Then
assertThat(eventReviewRepository.findAll()).hasSize(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

import com.pgms.coredomain.domain.event.Event;
import com.pgms.coredomain.domain.event.EventReview;
import com.pgms.coredomain.domain.member.Member;

public class EventReviewFactory {

public static EventReview createEventReview(Event event) {
public static EventReview createEventReview(Event event, Member member) {
return new EventReview(
4,
"공연 후기입니다.",
event
event,
member
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.pgms.apievent.factory.member;

import com.pgms.coredomain.domain.member.Member;
import com.pgms.coredomain.domain.member.enums.Gender;

public class MemberFactory {
public static Member createMember() {
return Member.builder()
.email("[email protected]")
.password("encodedPassword")
.name("tester")
.phoneNumber("01011112222")
.birthDate("19990926")
.gender(Gender.MALE)
.streetAddress("서울특별시 송파구 올림픽로 300")
.detailAddress("롯데월드타워앤드롯데월드몰")
.zipCode("05551")
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.pgms.coredomain.domain.event;

import com.pgms.coredomain.domain.common.BaseEntity;
import com.pgms.coredomain.domain.member.Member;

import jakarta.persistence.Column;
import jakarta.persistence.ConstraintMode;
Expand Down Expand Up @@ -40,10 +41,15 @@ public class EventReview extends BaseEntity {
@JoinColumn(name = "event_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private Event event;

public EventReview(Integer score, String content, Event event) {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private Member member;

public EventReview(Integer score, String content, Event event, Member member) {
this.score = score;
this.content = content;
this.event = event;
this.member = member;
}

public void updateEventReview(String content) {
Expand Down

0 comments on commit 83a0029

Please sign in to comment.