Skip to content

Commit

Permalink
Merge pull request #141 from Team-Going/feature/140
Browse files Browse the repository at this point in the history
[feat] 여행 TODO 수정 API 구현
  • Loading branch information
SunwoongH authored Mar 4, 2024
2 parents 597ea79 + eb21f35 commit f8f948a
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 27 deletions.
4 changes: 2 additions & 2 deletions doorip-api/src/main/java/org/doorip/trip/api/TodoApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import org.doorip.auth.UserId;
import org.doorip.common.BaseResponse;
import org.doorip.trip.dto.request.TodoCreateRequest;
import org.doorip.trip.dto.request.TodoCreateAndUpdateRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -67,7 +67,7 @@ public interface TodoApi {
ResponseEntity<BaseResponse<?>> createTripTodo(@Parameter(hidden = true)
@UserId final Long userId,
@PathVariable final Long tripId,
@RequestBody final TodoCreateRequest request);
@RequestBody final TodoCreateAndUpdateRequest request);

@Operation(
summary = "여행 TODO 전체 조회 API",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.doorip.common.ApiResponseUtil;
import org.doorip.common.BaseResponse;
import org.doorip.message.SuccessMessage;
import org.doorip.trip.dto.request.TodoCreateRequest;
import org.doorip.trip.dto.request.TodoCreateAndUpdateRequest;
import org.doorip.trip.dto.response.TodoDetailGetResponse;
import org.doorip.trip.dto.response.TodoGetResponse;
import org.doorip.trip.service.TodoService;
Expand All @@ -25,7 +25,7 @@ public class TodoApiController implements TodoApi {
@Override
public ResponseEntity<BaseResponse<?>> createTripTodo(@UserId final Long userId,
@PathVariable final Long tripId,
@RequestBody final TodoCreateRequest request) {
@RequestBody final TodoCreateAndUpdateRequest request) {
todoService.createTripTodo(userId, tripId, request);
return ApiResponseUtil.success(SuccessMessage.CREATED);
}
Expand All @@ -49,6 +49,15 @@ public ResponseEntity<BaseResponse<?>> getTripTodo(@UserId final Long userId,
return ApiResponseUtil.success(SuccessMessage.OK, response);
}

@PatchMapping("/{tripId}/todos/{todoId}")
public ResponseEntity<BaseResponse<?>> updateTripTodo(@UserId final Long userId,
@PathVariable final Long tripId,
@PathVariable final Long todoId,
@RequestBody final TodoCreateAndUpdateRequest request) {
todoService.updateTripTodo(userId, tripId, todoId, request);
return ApiResponseUtil.success(SuccessMessage.OK);
}

@DeleteMapping("/todos/{todoId}")
@Override
public ResponseEntity<BaseResponse<?>> deleteTripTodo(@PathVariable final Long todoId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.time.LocalDate;
import java.util.List;

public record TodoCreateRequest(
public record TodoCreateAndUpdateRequest(
String title,
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy.MM.dd", timezone = "Asia/Seoul")
LocalDate endDate,
Expand Down
58 changes: 36 additions & 22 deletions doorip-api/src/main/java/org/doorip/trip/service/TodoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import org.doorip.exception.InvalidValueException;
import org.doorip.message.ErrorMessage;
import org.doorip.trip.domain.*;
import org.doorip.trip.dto.request.TodoCreateRequest;
import org.doorip.trip.dto.request.TodoCreateAndUpdateRequest;
import org.doorip.trip.dto.response.TodoAllocatorResponse;
import org.doorip.trip.dto.response.TodoDetailAllocatorResponse;
import org.doorip.trip.dto.response.TodoDetailGetResponse;
import org.doorip.trip.dto.response.TodoGetResponse;
import org.doorip.trip.repository.AllocatorRepository;
import org.doorip.trip.repository.ParticipantRepository;
import org.doorip.trip.repository.TodoRepository;
import org.doorip.trip.repository.TripRepository;
Expand All @@ -23,6 +24,7 @@
import java.util.List;

import static org.doorip.trip.domain.Allocator.createAllocator;
import static org.doorip.trip.domain.Todo.createTodo;

@RequiredArgsConstructor
@Transactional(readOnly = true)
Expand All @@ -31,13 +33,15 @@ public class TodoService {
private final TodoRepository todoRepository;
private final TripRepository tripRepository;
private final ParticipantRepository participantRepository;
private final AllocatorRepository allocatorRepository;
private final UserRepository userRepository;

@Transactional
public void createTripTodo(Long userId, Long tripId, TodoCreateRequest request) {
public void createTripTodo(Long userId, Long tripId, TodoCreateAndUpdateRequest request) {
validateAllocators(request.secret(), userId, tripId, request.allocators());
Trip findTrip = getTrip(tripId);
Todo todo = createTodo(request, findTrip);
Todo todo = createTodo(request.title(), request.endDate(), request.memo(),
Secret.of(request.secret()), findTrip);
createAllocators(request.allocators(), todo);
todoRepository.save(todo);
}
Expand All @@ -53,6 +57,16 @@ public TodoDetailGetResponse getTripTodo(Long userId, Long tripId, Long todoId)
return getTripTodoResponse(userId, findTrip, findTodo);
}

@Transactional
public void updateTripTodo(Long userId, Long tripId, Long todoId, TodoCreateAndUpdateRequest request) {
validateAllocators(request.secret(), userId, tripId, request.allocators());
Todo findTodo = getTodo(todoId);
List<Allocator> allocators = getAllocatorsFromTodo(findTodo);
allocatorRepository.deleteAll(allocators);
findTodo.updateTodo(request.title(), request.endDate(), request.memo(), Secret.of(request.secret()));
createAllocators(request.allocators(), findTodo);
}

@Transactional
public void deleteTripTodo(Long todoId) {
todoRepository.deleteById(todoId);
Expand All @@ -70,25 +84,6 @@ public void incompleteTripTodo(Long todoId) {
findTodo.incomplete();
}

private void validateAllocators(boolean isSecret, Long userId, Long tripId, List<Long> allocators) {
if (isSecret) {
validateAllocatorCount(allocators);
Long findOwnerParticipantId = getOwnerParticipantId(userId, tripId);
validateAllocatorId(findOwnerParticipantId, allocators);
}
}

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 List<Todo> getTripTodosAccordingToCategoryAndProgress(Long userId, Long tripId, String category, String progress) {
if (category.equals(Constants.OUR)) {
return getOurTodosAccordingToProgress(tripId, progress);
Expand All @@ -112,6 +107,25 @@ private TodoDetailGetResponse getTripTodoResponse(Long userId, Trip findTrip, To
return TodoDetailGetResponse.of(findTodo, allocatorResponses);
}

private void validateAllocators(boolean isSecret, Long userId, Long tripId, List<Long> allocators) {
if (isSecret) {
validateAllocatorCount(allocators);
Long findOwnerParticipantId = getOwnerParticipantId(userId, tripId);
validateAllocatorId(findOwnerParticipantId, allocators);
}
}

private List<Allocator> getAllocatorsFromTodo(Todo findTodo) {
return allocatorRepository.findByTodo(findTodo);
}

private void createAllocators(List<Long> allocators, Todo todo) {
allocators.forEach(participantId -> {
Participant findParticipant = getParticipant(participantId);
createAllocator(todo, findParticipant);
});
}

private Todo getTodo(Long todoId) {
return todoRepository.findById(todoId)
.orElseThrow(() -> new EntityNotFoundException(ErrorMessage.TODO_NOT_FOUND));
Expand Down
7 changes: 7 additions & 0 deletions doorip-domain/src/main/java/org/doorip/trip/domain/Todo.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,11 @@ public void complete() {
public void incomplete() {
this.progress = Progress.INCOMPLETE;
}

public void updateTodo(String title, LocalDate endDate, String memo, Secret secret) {
this.title = title;
this.endDate = endDate;
this.memo = memo;
this.secret = secret;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.doorip.trip.repository;

import org.doorip.trip.domain.Allocator;
import org.doorip.trip.domain.Todo;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface AllocatorRepository extends JpaRepository<Allocator, Long> {
List<Allocator> findByTodo(Todo todo);
}

0 comments on commit f8f948a

Please sign in to comment.