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

[BE] 약속 생성 결과 조회 API 구현 #52

Merged
merged 9 commits into from
Jul 24, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.woowacourse.momo.controller.MomoApiResponse;
import com.woowacourse.momo.service.meeting.MeetingService;
import com.woowacourse.momo.service.meeting.dto.MeetingResponse;
import com.woowacourse.momo.service.meeting.dto.MeetingSharingResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -19,4 +20,10 @@ public MomoApiResponse<MeetingResponse> find(@PathVariable String uuid) {
MeetingResponse meetingResponse = meetingService.findByUUID(uuid);
return new MomoApiResponse<>(meetingResponse);
}

@GetMapping("/api/v1/meeting/{uuid}/sharing")
public MomoApiResponse<MeetingSharingResponse> findMeetingSharing(@PathVariable String uuid) {
MeetingSharingResponse response = meetingService.findMeetingSharing(uuid);
return new MomoApiResponse<>(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.woowacourse.momo.exception.code;

import org.springframework.http.HttpStatus;

public enum MeetingErrorCode implements ErrorCodeType {

INVALID_UUID(HttpStatus.BAD_REQUEST, "유효하지 않은 UUID 입니다.");

private final HttpStatus httpStatus;
private final String message;

MeetingErrorCode(HttpStatus httpStatus, String message) {
this.httpStatus = httpStatus;
this.message = message;
}

@Override
public HttpStatus httpStatus() {
return httpStatus;
}

@Override
public String message() {
return message;
}

@Override
public String errorCode() {
return name();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import com.woowacourse.momo.domain.meeting.MeetingRepository;
import com.woowacourse.momo.domain.schedule.Schedule;
import com.woowacourse.momo.domain.schedule.ScheduleRepository;
import com.woowacourse.momo.exception.MomoException;
import com.woowacourse.momo.exception.code.MeetingErrorCode;
import com.woowacourse.momo.service.meeting.dto.MeetingResponse;
import com.woowacourse.momo.service.meeting.dto.MeetingSharingResponse;
import com.woowacourse.momo.service.schedule.dto.ScheduleTimeResponse;
import java.time.LocalDate;
import java.util.Comparator;
Expand Down Expand Up @@ -47,4 +50,10 @@ public MeetingResponse findByUUID(String uuid) {

return MeetingResponse.from(meeting, dates, list);
}

public MeetingSharingResponse findMeetingSharing(String uuid) {
Meeting meeting = meetingRepository.findByUuid(uuid)
.orElseThrow(() -> new MomoException(MeetingErrorCode.INVALID_UUID));
return MeetingSharingResponse.from(meeting);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.woowacourse.momo.service.meeting.dto;

import com.woowacourse.momo.domain.meeting.Meeting;

public record MeetingSharingResponse(String uuid) {

public static MeetingSharingResponse from(Meeting meeting) {
return new MeetingSharingResponse(meeting.getUuid());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.woowacourse.momo.controller.meeting;

import com.woowacourse.momo.domain.attendee.AttendeeRepository;
import com.woowacourse.momo.domain.meeting.Meeting;
import com.woowacourse.momo.domain.meeting.MeetingRepository;
import com.woowacourse.momo.fixture.AttendeeFixture;
import com.woowacourse.momo.fixture.MeetingFixture;
import com.woowacourse.momo.support.IsolateDatabase;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.http.HttpStatus;

@IsolateDatabase
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
class MeetingControllerTest {

@LocalServerPort
private int port;

@Autowired
private AttendeeRepository attendeeRepository;

@Autowired
private MeetingRepository meetingRepository;

private Meeting meeting;

@BeforeEach
void setUp() {
RestAssured.port = port;

meeting = meetingRepository.save(MeetingFixture.COFFEE.create());
attendeeRepository.save(AttendeeFixture.HOST_JAZZ.create(meeting));
}

@DisplayName("약속 공유 정보를 조회하면 200OK와 응답을 반환한다.")
@Test
void findMeetingSharing() {
RestAssured.given().log().all()
.contentType(ContentType.JSON)
.when().get("/api/v1/meeting/{uuid}/sharing", meeting.getUuid())
.then().log().all()
.statusCode(HttpStatus.OK.value());
}

@DisplayName("약속 공유 정보 조회시 UUID가 유효하지 않으면 400 Bad Request를 반환한다.")
@Test
void findMeetingSharingFailedWithInvalidUUID() {
RestAssured.given().log().all()
.contentType(ContentType.JSON)
.when().get("/api/v1/meeting/{uuid}/sharing", "1234")
.then().log().all()
.statusCode(HttpStatus.BAD_REQUEST.value());
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enum 픽스쳐 기가 막히네요👍🏻

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.woowacourse.momo.fixture;

import com.woowacourse.momo.domain.attendee.Attendee;
import com.woowacourse.momo.domain.attendee.AttendeeName;
import com.woowacourse.momo.domain.attendee.AttendeePassword;
import com.woowacourse.momo.domain.attendee.Role;
import com.woowacourse.momo.domain.meeting.Meeting;

public enum AttendeeFixture {

HOST_JAZZ("jazz", "hostPw!123", Role.HOST),
GUEST_DAON("daon", "daonPw!123", Role.GUEST),
GUEST_BAKEY("bakey", "bakeyPw!123", Role.GUEST),
GUEST_PEDRO("pedro", "pedroPw!123", Role.GUEST);

private final String name;
private final String password;
private final Role role;

AttendeeFixture(String name, String password, Role role) {
this.name = name;
this.password = password;
this.role = role;
}

public Attendee create(Meeting meeting) {
return new Attendee(meeting, new AttendeeName(name), new AttendeePassword(password), role);
}
Comment on lines +26 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

연관관계가 있는 fixture을 어떻게 만들어야 하나 궁금했는데, 이렇게 Fixture을 만들 수 있네요! 👍👍

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.woowacourse.momo.service.meeting;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.woowacourse.momo.domain.attendee.Attendee;
import com.woowacourse.momo.domain.attendee.AttendeeRepository;
import com.woowacourse.momo.domain.attendee.Role;
Expand All @@ -10,17 +13,23 @@
import com.woowacourse.momo.domain.schedule.Schedule;
import com.woowacourse.momo.domain.schedule.ScheduleRepository;
import com.woowacourse.momo.domain.timeslot.Timeslot;
import com.woowacourse.momo.exception.MomoException;
import com.woowacourse.momo.exception.code.MeetingErrorCode;
import com.woowacourse.momo.fixture.AttendeeFixture;
import com.woowacourse.momo.fixture.MeetingFixture;
import com.woowacourse.momo.service.meeting.dto.MeetingResponse;
import com.woowacourse.momo.service.meeting.dto.MeetingSharingResponse;
import com.woowacourse.momo.support.IsolateDatabase;
import java.time.LocalDate;
import java.util.UUID;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;

@IsolateDatabase
@SpringBootTest(webEnvironment = WebEnvironment.NONE)
class MeetingServiceTest {

Expand All @@ -39,14 +48,6 @@ class MeetingServiceTest {
@Autowired
private AttendeeRepository attendeeRepository;

@BeforeEach
void setup() {
scheduleRepository.deleteAllInBatch();
availableDateRepository.deleteAllInBatch();
meetingRepository.deleteAllInBatch();
attendeeRepository.deleteAllInBatch();
}

@DisplayName("UUID로 약속 정보를 조회한다.")
@Test
void findByUUID() {
Expand Down Expand Up @@ -87,4 +88,26 @@ void findByUUID() {
softAssertions.assertThat(result.availableDates().get(0)).isEqualTo(LocalDate.now().minusDays(1));
softAssertions.assertAll();
}

@DisplayName("생성 완료된 약속의 정보를 조회한다.")
@Test
void findMeetingSharing() {
Meeting meeting = meetingRepository.save(MeetingFixture.GAME.create());

MeetingSharingResponse result = meetingService.findMeetingSharing(meeting.getUuid());

assertThat(result.uuid()).isEqualTo(meeting.getUuid());
}

@DisplayName("생성 완료된 약속의 정보를 조회시 uuid가 일치하지 않으면 예외가 발생한다.")
@Test
void doesNotFindMeetingSharingMeetingIfUUIDNotExist() {
String invalidUUID = "1234";
Meeting meeting = meetingRepository.save(MeetingFixture.GAME.create());
attendeeRepository.save(AttendeeFixture.HOST_JAZZ.create(meeting));

assertThatThrownBy(() -> meetingService.findMeetingSharing(invalidUUID))
.isInstanceOf(MomoException.class)
.hasMessage(MeetingErrorCode.INVALID_UUID.message());
}
}