-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6fe894
commit bd1b976
Showing
10 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
server/src/main/java/server/haengdong/application/EventService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package server.haengdong.application; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import server.haengdong.application.request.EventAppRequest; | ||
import server.haengdong.application.response.EventAppResponse; | ||
import server.haengdong.domain.Event; | ||
import server.haengdong.domain.EventTokenProvider; | ||
import server.haengdong.persistence.EventRepository; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class EventService { | ||
|
||
private final EventRepository eventRepository; | ||
private final EventTokenProvider eventTokenProvider; | ||
|
||
public EventAppResponse saveEvent(EventAppRequest request) { | ||
String token = eventTokenProvider.createToken(); | ||
Event event = request.toEvent(token); | ||
eventRepository.save(event); | ||
|
||
return EventAppResponse.of(event); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
server/src/main/java/server/haengdong/application/request/EventAppRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package server.haengdong.application.request; | ||
|
||
import server.haengdong.domain.Event; | ||
|
||
public record EventAppRequest(String name) { | ||
|
||
public Event toEvent(String token) { | ||
return new Event(name, token); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
server/src/main/java/server/haengdong/application/response/EventAppResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package server.haengdong.application.response; | ||
|
||
import server.haengdong.domain.Event; | ||
|
||
public record EventAppResponse(String token) { | ||
|
||
public static EventAppResponse of(Event event) { | ||
return new EventAppResponse(event.getToken()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
server/src/main/java/server/haengdong/domain/EventTokenProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package server.haengdong.domain; | ||
|
||
import java.util.UUID; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class EventTokenProvider { | ||
|
||
public String createToken() { | ||
return UUID.randomUUID().toString(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
server/src/main/java/server/haengdong/persistence/EventRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package server.haengdong.persistence; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
import server.haengdong.domain.Event; | ||
|
||
@Repository | ||
public interface EventRepository extends JpaRepository<Event, Long> { | ||
} |
26 changes: 26 additions & 0 deletions
26
server/src/main/java/server/haengdong/presentation/EventController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package server.haengdong.presentation; | ||
|
||
import java.net.URI; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
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; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
public class EventController { | ||
|
||
private final EventService eventService; | ||
|
||
@PostMapping("/api/events") | ||
public ResponseEntity<Void> saveEvent(EventSaveRequest request) { | ||
EventAppResponse eventAppResponse = eventService.saveEvent(request.toAppRequest()); | ||
|
||
return ResponseEntity.ok() | ||
.location(URI.create("events/" + eventAppResponse.token())) | ||
.build(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
server/src/main/java/server/haengdong/presentation/request/EventSaveRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package server.haengdong.presentation.request; | ||
|
||
import server.haengdong.application.request.EventAppRequest; | ||
|
||
public record EventSaveRequest(String name) { | ||
|
||
public EventAppRequest toAppRequest() { | ||
return new EventAppRequest(name); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
server/src/test/java/server/haengdong/application/EventServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package server.haengdong.application; | ||
|
||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.BDDMockito.given; | ||
|
||
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.domain.EventTokenProvider; | ||
|
||
@SpringBootTest | ||
class EventServiceTest { | ||
|
||
@Autowired | ||
private EventService eventService; | ||
|
||
@MockBean | ||
private EventTokenProvider eventTokenProvider; | ||
|
||
@DisplayName("행사를 생성한다") | ||
@Test | ||
void saveEventTest() { | ||
EventAppRequest request = new EventAppRequest("test"); | ||
given(eventTokenProvider.createToken()).willReturn("TOKEN"); | ||
|
||
EventAppResponse response = eventService.saveEvent(request); | ||
|
||
assertThat(response.token()).isEqualTo("TOKEN"); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
server/src/test/java/server/haengdong/presentation/EventControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package server.haengdong.presentation; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.BDDMockito.given; | ||
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.status; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; | ||
import server.haengdong.application.EventService; | ||
import server.haengdong.application.request.EventAppRequest; | ||
import server.haengdong.application.response.EventAppResponse; | ||
import server.haengdong.presentation.request.EventSaveRequest; | ||
|
||
@WebMvcTest(EventController.class) | ||
class EventControllerTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
@MockBean | ||
private EventService eventService; | ||
|
||
@DisplayName("이벤트를 생성한다") | ||
@Test | ||
void saveEvent() throws Exception { | ||
EventSaveRequest eventSaveRequest = new EventSaveRequest("test"); | ||
String requestBody = objectMapper.writeValueAsString(eventSaveRequest); | ||
String token = "TOKEN"; | ||
EventAppResponse eventAppResponse = new EventAppResponse(token); | ||
given(eventService.saveEvent(any(EventAppRequest.class))).willReturn(eventAppResponse); | ||
|
||
mockMvc.perform(post("/api/events") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(requestBody)) | ||
.andDo(print()) | ||
.andExpect(status().isOk()) | ||
.andExpect(MockMvcResultMatchers.redirectedUrl("events/" + token)); | ||
} | ||
} |