-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from Team-Going/feature/30
[feat] 여행 TODO 생성 API 구현
- Loading branch information
Showing
12 changed files
with
213 additions
and
7 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
doorip-api/src/main/java/org/doorip/todo/api/TodoApiController.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 org.doorip.todo.api; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.doorip.common.ApiResponse; | ||
import org.doorip.common.ApiResponseUtil; | ||
import org.doorip.message.SuccessMessage; | ||
import org.doorip.todo.dto.request.TodoCreateRequest; | ||
import org.doorip.todo.service.TodoService; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
||
@RequiredArgsConstructor | ||
@RequestMapping("/api/todos") | ||
@Controller | ||
public class TodoApiController { | ||
private final TodoService todoService; | ||
|
||
@PostMapping | ||
public ResponseEntity<ApiResponse<?>> createTripTodo(@RequestBody final TodoCreateRequest request) { | ||
todoService.createTripTodo(request); | ||
return ApiResponseUtil.success(SuccessMessage.CREATED); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
doorip-api/src/main/java/org/doorip/todo/dto/request/TodoCreateRequest.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,17 @@ | ||
package org.doorip.todo.dto.request; | ||
|
||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
||
public record TodoCreateRequest( | ||
Long tripId, | ||
String title, | ||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Seoul") | ||
LocalDate endDate, | ||
List<Long> allocators, | ||
String memo, | ||
boolean secret | ||
) { | ||
} |
64 changes: 64 additions & 0 deletions
64
doorip-api/src/main/java/org/doorip/todo/service/TodoService.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,64 @@ | ||
package org.doorip.todo.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.doorip.exception.EntityNotFoundException; | ||
import org.doorip.exception.InvalidValueException; | ||
import org.doorip.message.ErrorMessage; | ||
import org.doorip.todo.domain.Secret; | ||
import org.doorip.todo.domain.Todo; | ||
import org.doorip.todo.dto.request.TodoCreateRequest; | ||
import org.doorip.todo.repository.TodoRepository; | ||
import org.doorip.trip.domain.Participant; | ||
import org.doorip.trip.domain.Trip; | ||
import org.doorip.trip.repository.ParticipantRepository; | ||
import org.doorip.trip.repository.TripRepository; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
|
||
import static org.doorip.todo.domain.Allocator.createAllocator; | ||
|
||
@RequiredArgsConstructor | ||
@Transactional | ||
@Service | ||
public class TodoService { | ||
private final TripRepository tripRepository; | ||
private final ParticipantRepository participantRepository; | ||
private final TodoRepository todoRepository; | ||
|
||
public void createTripTodo(TodoCreateRequest request) { | ||
validateAllocators(request.allocators()); | ||
Trip findTrip = getTrip(request.tripId()); | ||
Todo todo = createTodo(request, findTrip); | ||
createAllocators(request.allocators(), todo); | ||
todoRepository.save(todo); | ||
} | ||
|
||
private void validateAllocators(List<Long> allocators) { | ||
if (allocators.isEmpty()) { | ||
throw new InvalidValueException(ErrorMessage.INVALID_ALLOCATOR_COUNT); | ||
} | ||
} | ||
|
||
private Trip getTrip(Long tripId) { | ||
return tripRepository.findById(tripId) | ||
.orElseThrow(() -> new EntityNotFoundException(ErrorMessage.TRIP_NOT_FOUND)); | ||
} | ||
|
||
private Todo createTodo(TodoCreateRequest request, Trip trip) { | ||
return Todo.createTodo(request.title(), request.endDate(), request.memo(), Secret.of(request.secret()), trip); | ||
} | ||
|
||
private void createAllocators(List<Long> allocators, Todo todo) { | ||
allocators.forEach(participantId -> { | ||
Participant findParticipant = getParticipant(participantId); | ||
createAllocator(todo, findParticipant); | ||
}); | ||
} | ||
|
||
private Participant getParticipant(Long participantId) { | ||
return participantRepository.findById(participantId) | ||
.orElseThrow(() -> new EntityNotFoundException(ErrorMessage.PARTICIPANT_NOT_FOUND)); | ||
} | ||
} |
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,3 @@ | ||
alter table allocator drop foreign key allocator_ibfk_1; | ||
drop index participant_id on allocator; | ||
alter table allocator add constraint allocator_ibfk_1 foreign key (participant_id) references participant (participant_id); |
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
9 changes: 8 additions & 1 deletion
9
doorip-domain/src/main/java/org/doorip/todo/domain/Secret.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,5 +1,12 @@ | ||
package org.doorip.todo.domain; | ||
|
||
public enum Secret { | ||
OUR, MY | ||
OUR, MY; | ||
|
||
public static Secret of(boolean isSecret) { | ||
if (isSecret) { | ||
return OUR; | ||
} | ||
return MY; | ||
} | ||
} |
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
7 changes: 7 additions & 0 deletions
7
doorip-domain/src/main/java/org/doorip/todo/repository/TodoRepository.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,7 @@ | ||
package org.doorip.todo.repository; | ||
|
||
import org.doorip.todo.domain.Todo; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface TodoRepository extends JpaRepository<Todo, Long> { | ||
} |
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
7 changes: 7 additions & 0 deletions
7
doorip-domain/src/main/java/org/doorip/trip/repository/ParticipantRepository.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,7 @@ | ||
package org.doorip.trip.repository; | ||
|
||
import org.doorip.trip.domain.Participant; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface ParticipantRepository extends JpaRepository<Participant, Long> { | ||
} |