-
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
Showing
16 changed files
with
320 additions
and
6 deletions.
There are no files selected for viewing
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
48 changes: 48 additions & 0 deletions
48
server/src/main/java/server/haengdong/application/BillActionService.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,48 @@ | ||
package server.haengdong.application; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import server.haengdong.application.request.BillActionAppRequest; | ||
import server.haengdong.domain.Action; | ||
import server.haengdong.domain.BillAction; | ||
import server.haengdong.domain.Event; | ||
import server.haengdong.persistence.ActionRepository; | ||
import server.haengdong.persistence.BillActionRepository; | ||
import server.haengdong.persistence.EventRepository; | ||
|
||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
@Service | ||
public class BillActionService { | ||
|
||
private final EventRepository eventRepository; | ||
private final ActionRepository actionRepository; | ||
private final BillActionRepository billActionRepository; | ||
|
||
@Transactional | ||
public void saveAllBillAction(String eventToken, List<BillActionAppRequest> requests) { | ||
Event event = eventRepository.findByToken(eventToken) | ||
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 이벤트 토큰입니다.")); | ||
long lastSequence = getLastSequence(event); | ||
|
||
List<BillAction> billActions = new ArrayList<>(); | ||
for (BillActionAppRequest request : requests) { | ||
Action action = new Action(event, ++lastSequence); | ||
BillAction billAction = request.toBillAction(action); | ||
billActions.add(billAction); | ||
} | ||
billActionRepository.saveAll(billActions); | ||
} | ||
|
||
private long getLastSequence(Event event) { | ||
Optional<Action> lastAction = actionRepository.findLastByEvent(event); | ||
if (lastAction.isPresent()) { | ||
return lastAction.get().getSequence(); | ||
} | ||
return 0L; | ||
} | ||
} |
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
14 changes: 14 additions & 0 deletions
14
server/src/main/java/server/haengdong/application/request/BillActionAppRequest.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,14 @@ | ||
package server.haengdong.application.request; | ||
|
||
import server.haengdong.domain.Action; | ||
import server.haengdong.domain.BillAction; | ||
|
||
public record BillActionAppRequest( | ||
String title, | ||
Long price | ||
) { | ||
|
||
public BillAction toBillAction(Action action) { | ||
return new BillAction(action, title, price); | ||
} | ||
} |
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
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
21 changes: 21 additions & 0 deletions
21
server/src/main/java/server/haengdong/persistence/ActionRepository.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,21 @@ | ||
package server.haengdong.persistence; | ||
|
||
import java.util.Optional; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.stereotype.Repository; | ||
import server.haengdong.domain.Action; | ||
import server.haengdong.domain.Event; | ||
|
||
@Repository | ||
public interface ActionRepository extends JpaRepository<Action, Long> { | ||
|
||
@Query(""" | ||
SELECT a | ||
FROM Action a | ||
WHERE a.event = :event | ||
ORDER BY a.sequence DESC | ||
LIMIT 1 | ||
""") | ||
Optional<Action> findLastByEvent(Event event); | ||
} |
13 changes: 13 additions & 0 deletions
13
server/src/main/java/server/haengdong/persistence/BillActionRepository.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,13 @@ | ||
package server.haengdong.persistence; | ||
|
||
import java.util.List; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
import server.haengdong.domain.BillAction; | ||
import server.haengdong.domain.Event; | ||
|
||
@Repository | ||
public interface BillActionRepository extends JpaRepository<BillAction, Long> { | ||
|
||
List<BillAction> findByAction_Event(Event event); | ||
} |
3 changes: 3 additions & 0 deletions
3
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 |
---|---|---|
@@ -1,9 +1,12 @@ | ||
package server.haengdong.persistence; | ||
|
||
import java.util.Optional; | ||
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> { | ||
|
||
Optional<Event> findByToken(String token); | ||
} |
29 changes: 29 additions & 0 deletions
29
server/src/main/java/server/haengdong/presentation/BillActionController.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,29 @@ | ||
package server.haengdong.presentation; | ||
|
||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import server.haengdong.application.BillActionService; | ||
import server.haengdong.presentation.request.BillActionsSaveRequest; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
public class BillActionController { | ||
|
||
private final BillActionService billActionService; | ||
|
||
@PostMapping("/api/events/{token}/actions/bills") | ||
public ResponseEntity<Void> saveAllBillAction( | ||
@PathVariable String token, | ||
@RequestBody @Valid BillActionsSaveRequest request | ||
) { | ||
billActionService.saveAllBillAction(token, request.toAppRequests()); | ||
|
||
return ResponseEntity.ok() | ||
.build(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
server/src/main/java/server/haengdong/presentation/request/BillActionSaveRequest.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,22 @@ | ||
package server.haengdong.presentation.request; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Positive; | ||
import jakarta.validation.constraints.Size; | ||
import server.haengdong.application.request.BillActionAppRequest; | ||
|
||
public record BillActionSaveRequest( | ||
|
||
@NotNull | ||
@Size(min = 2, max = 30) | ||
String title, | ||
|
||
@NotNull | ||
@Positive | ||
Long price | ||
) { | ||
|
||
public BillActionAppRequest toAppRequest() { | ||
return new BillActionAppRequest(title, price); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
server/src/main/java/server/haengdong/presentation/request/BillActionsSaveRequest.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,13 @@ | ||
package server.haengdong.presentation.request; | ||
|
||
import java.util.List; | ||
import server.haengdong.application.request.BillActionAppRequest; | ||
|
||
public record BillActionsSaveRequest(List<BillActionSaveRequest> actions) { | ||
|
||
public List<BillActionAppRequest> toAppRequests() { | ||
return actions.stream() | ||
.map(BillActionSaveRequest::toAppRequest) | ||
.toList(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,15 @@ | ||
spring: | ||
h2: | ||
console: | ||
enabled: true | ||
path: /h2-console | ||
datasource: | ||
url: jdbc:h2:mem:database | ||
jpa: | ||
defer-datasource-initialization: true | ||
show-sql: true | ||
properties: | ||
hibernate: | ||
format_sql: true | ||
hibernate: | ||
ddl-auto: create-drop |
69 changes: 69 additions & 0 deletions
69
server/src/test/java/server/haengdong/application/BillActionServiceTest.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,69 @@ | ||
package server.haengdong.application; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.assertj.core.api.Assertions.tuple; | ||
|
||
import java.util.Comparator; | ||
import java.util.List; | ||
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 server.haengdong.application.request.BillActionAppRequest; | ||
import server.haengdong.domain.BillAction; | ||
import server.haengdong.domain.Event; | ||
import server.haengdong.persistence.BillActionRepository; | ||
import server.haengdong.persistence.EventRepository; | ||
|
||
@SpringBootTest | ||
class BillActionServiceTest { | ||
|
||
@Autowired | ||
private BillActionService billActionService; | ||
|
||
@Autowired | ||
private EventRepository eventRepository; | ||
|
||
@Autowired | ||
private BillActionRepository billActionRepository; | ||
|
||
@DisplayName("지출 내역을 생성한다.") | ||
@Test | ||
void saveAllBillAction() { | ||
String token = "TOKEN"; | ||
Event event = eventRepository.save(new Event("감자", token)); | ||
|
||
List<BillActionAppRequest> requests = List.of( | ||
new BillActionAppRequest("뽕족", 10_000L), | ||
new BillActionAppRequest("인생맥주", 15_000L) | ||
); | ||
|
||
billActionService.saveAllBillAction(token, requests); | ||
|
||
List<BillAction> actions = billActionRepository.findByAction_Event(event) | ||
.stream() | ||
.sorted(Comparator.comparing(BillAction::getSequence).reversed()) | ||
.limit(requests.size()) | ||
.toList(); | ||
|
||
assertThat(actions).extracting("title", "price") | ||
.containsExactly( | ||
tuple("인생맥주", 15_000L), | ||
tuple("뽕족", 10_000L) | ||
); | ||
} | ||
|
||
@DisplayName("이벤트가 존재하지 않으면 지출 내역을 생성할 수 없다.") | ||
@Test | ||
void saveAllBillAction1() { | ||
List<BillActionAppRequest> requests = List.of( | ||
new BillActionAppRequest("뽕족", 10_000L), | ||
new BillActionAppRequest("인생맥주", 15_000L) | ||
); | ||
|
||
assertThatThrownBy(() -> billActionService.saveAllBillAction("token", requests)) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessage("존재하지 않는 이벤트 토큰입니다."); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
server/src/test/java/server/haengdong/presentation/BillActionControllerTest.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,53 @@ | ||
package server.haengdong.presentation; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.doNothing; | ||
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 java.util.List; | ||
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 server.haengdong.application.BillActionService; | ||
import server.haengdong.presentation.request.BillActionSaveRequest; | ||
import server.haengdong.presentation.request.BillActionsSaveRequest; | ||
|
||
@WebMvcTest(BillActionController.class) | ||
class BillActionControllerTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
@MockBean | ||
private BillActionService billActionService; | ||
|
||
@DisplayName("지출 내역을 생성한다.") | ||
@Test | ||
void saveAllBillAction() throws Exception { | ||
BillActionsSaveRequest request = new BillActionsSaveRequest( | ||
List.of( | ||
new BillActionSaveRequest("뽕족", 10_000L), | ||
new BillActionSaveRequest("인생맥주", 10_000L) | ||
) | ||
); | ||
String requestBody = objectMapper.writeValueAsString(request); | ||
String token = "TOKEN"; | ||
doNothing().when(billActionService).saveAllBillAction(any(), any()); | ||
|
||
mockMvc.perform(post("/api/events/{token}/actions/bills", token) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(requestBody)) | ||
.andDo(print()) | ||
.andExpect(status().isOk()); | ||
} | ||
} |