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] 행사 정보 조회 기능 구현 #75

Merged
merged 1 commit into from
Jul 23, 2024
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 @@ -4,9 +4,10 @@
import org.springframework.stereotype.Service;
import server.haengdong.application.request.EventAppRequest;
import server.haengdong.application.response.EventAppResponse;
import server.haengdong.application.response.EventDetailAppResponse;
import server.haengdong.domain.event.Event;
import server.haengdong.domain.event.EventTokenProvider;
import server.haengdong.domain.event.EventRepository;
import server.haengdong.domain.event.EventTokenProvider;

@RequiredArgsConstructor
@Service
Expand All @@ -22,4 +23,10 @@ public EventAppResponse saveEvent(EventAppRequest request) {

return EventAppResponse.of(event);
}

public EventDetailAppResponse findEvent(String token) {
Event event = eventRepository.findByToken(token).orElseThrow(() -> new IllegalArgumentException(""));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Event event = eventRepository.findByToken(token).orElseThrow(() -> new IllegalArgumentException(""));
Event event = eventRepository.findByToken(token)
.orElseThrow(() -> new IllegalArgumentException(""));

c: 한 줄에 하나의 점(.)만 쓰는 것은 어떨까요?


return EventDetailAppResponse.of(event);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package server.haengdong.application.response;

import server.haengdong.domain.event.Event;

public record EventDetailAppResponse(String eventName) {

public static EventDetailAppResponse of(Event event) {
return new EventDetailAppResponse(event.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import java.net.URI;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import server.haengdong.application.EventService;
import server.haengdong.application.response.EventAppResponse;
import server.haengdong.presentation.request.EventSaveRequest;
import server.haengdong.presentation.response.EventDetailResponse;

@RequiredArgsConstructor
@RestController
Expand All @@ -23,4 +26,11 @@ public ResponseEntity<Void> saveEvent(EventSaveRequest request) {
.location(URI.create("events/" + eventAppResponse.token()))
.build();
}

@GetMapping("/api/events/{token}")
public ResponseEntity<EventDetailResponse> findEvent(@PathVariable("token") String token) {
EventDetailResponse eventDetailResponse = EventDetailResponse.of(eventService.findEvent(token));

return ResponseEntity.ok(eventDetailResponse);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package server.haengdong.presentation.response;

import server.haengdong.application.response.EventDetailAppResponse;

public record EventDetailResponse(String eventName) {

public static EventDetailResponse of(EventDetailAppResponse response) {
return new EventDetailResponse(response.eventName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;

import org.junit.jupiter.api.AfterEach;
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.mock.mockito.MockBean;
import server.haengdong.application.request.EventAppRequest;
import server.haengdong.application.response.EventAppResponse;
import server.haengdong.application.response.EventDetailAppResponse;
import server.haengdong.domain.event.Event;
import server.haengdong.domain.event.EventRepository;
import server.haengdong.domain.event.EventTokenProvider;

@SpringBootTest
Expand All @@ -22,6 +26,14 @@ class EventServiceTest {
@MockBean
private EventTokenProvider eventTokenProvider;

@Autowired
private EventRepository eventRepository;

@AfterEach
void tearDown() {
eventRepository.deleteAllInBatch();
}

@DisplayName("행사를 생성한다")
@Test
void saveEventTest() {
Expand All @@ -32,4 +44,16 @@ void saveEventTest() {

assertThat(response.token()).isEqualTo("TOKEN");
}

@DisplayName("토큰으로 행사를 조회한다.")
@Test
void findEventTest() {
String token = "TOKEN";
Event event = new Event("행동대장 회식", token);
eventRepository.save(event);

EventDetailAppResponse eventDetailAppResponse = eventService.findEvent(token);

assertThat(eventDetailAppResponse.eventName()).isEqualTo("행동대장 회식");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -18,6 +20,7 @@
import server.haengdong.application.EventService;
import server.haengdong.application.request.EventAppRequest;
import server.haengdong.application.response.EventAppResponse;
import server.haengdong.application.response.EventDetailAppResponse;
import server.haengdong.presentation.request.EventSaveRequest;

@WebMvcTest(EventController.class)
Expand Down Expand Up @@ -48,4 +51,17 @@ void saveEvent() throws Exception {
.andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.redirectedUrl("events/" + token));
}

@DisplayName("토큰으로 행사를 조회한다.")
@Test
void findEventTest() throws Exception {
String token = "TOKEN";
EventDetailAppResponse eventDetailAppResponse = new EventDetailAppResponse("행동대장 회식");
given(eventService.findEvent(token)).willReturn(eventDetailAppResponse);

mockMvc.perform(get("/api/events/" + token))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.eventName").value("행동대장 회식"));
}
}
Loading