-
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.
Showing
19 changed files
with
446 additions
and
18 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
42 changes: 42 additions & 0 deletions
42
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,42 @@ | ||
package server.haengdong.application; | ||
|
||
import java.util.List; | ||
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 BillActionRepository billActionRepository; | ||
private final ActionRepository actionRepository; | ||
private final EventRepository eventRepository; | ||
|
||
@Transactional | ||
public void saveAllBillAction(String eventToken, List<BillActionAppRequest> requests) { | ||
Event event = eventRepository.findByToken(eventToken) | ||
.orElseThrow(() -> new IllegalArgumentException("μ‘΄μ¬νμ§ μλ μ΄λ²€νΈ ν ν°μ λλ€.")); | ||
Action action = createStartAction(event); | ||
|
||
for (BillActionAppRequest request : requests) { | ||
BillAction billAction = request.toBillAction(action); | ||
billActionRepository.save(billAction); | ||
action = action.next(); | ||
} | ||
} | ||
|
||
private Action createStartAction(Event event) { | ||
return actionRepository.findLastByEvent(event) | ||
.map(Action::next) | ||
.orElse(Action.createFirst(event)); | ||
} | ||
} |
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); | ||
} |
15 changes: 15 additions & 0 deletions
15
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,15 @@ | ||
package server.haengdong.persistence; | ||
|
||
import java.util.List; | ||
import org.springframework.data.jpa.repository.EntityGraph; | ||
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> { | ||
|
||
@EntityGraph(attributePaths = {"action"}) | ||
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); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
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,14 @@ | ||
package server.haengdong.presentation.request; | ||
|
||
import jakarta.validation.Valid; | ||
import java.util.List; | ||
import server.haengdong.application.request.BillActionAppRequest; | ||
|
||
public record BillActionsSaveRequest(@Valid 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 |
13 changes: 0 additions & 13 deletions
13
server/src/test/java/server/haengdong/ServerApplicationTests.java
This file was deleted.
Oops, something went wrong.
65 changes: 65 additions & 0 deletions
65
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,65 @@ | ||
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.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 = new Event("κ°μ", token); | ||
Event savedEvent = eventRepository.save(event); | ||
|
||
List<BillActionAppRequest> requests = List.of( | ||
new BillActionAppRequest("λ½μ‘±", 10_000L), | ||
new BillActionAppRequest("μΈμλ§₯μ£Ό", 15_000L) | ||
); | ||
|
||
billActionService.saveAllBillAction(token, requests); | ||
|
||
List<BillAction> actions = billActionRepository.findByAction_Event(savedEvent); | ||
|
||
assertThat(actions).extracting(BillAction::getTitle, BillAction::getPrice, BillAction::getSequence) | ||
.containsExactlyInAnyOrder( | ||
tuple("λ½μ‘±", 10_000L, 1L), | ||
tuple("μΈμλ§₯μ£Ό", 15_000L, 2L) | ||
); | ||
} | ||
|
||
@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("μ‘΄μ¬νμ§ μλ μ΄λ²€νΈ ν ν°μ λλ€."); | ||
} | ||
} |
Oops, something went wrong.