Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor : 메인 페이지에서 여정에 여행 아이템 추가 기능 구현 #74

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,33 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
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 org.tenten.tentenstomp.domain.trip.dto.request.*;
import org.tenten.tentenstomp.domain.trip.dto.response.TripItemAddResponse;
import org.tenten.tentenstomp.domain.trip.service.TripService;
import org.tenten.tentenstomp.global.messaging.kafka.producer.KafkaProducer;
import org.tenten.tentenstomp.global.common.constant.ResponseConstant;
import org.tenten.tentenstomp.global.response.GlobalDataResponse;

@RestController
@RequiredArgsConstructor
@Slf4j
public class TripController {

private final TripService tripService;
private final KafkaProducer kafkaProducer;
/*
TODO : 백엔드에서 예외가 발생하면, 프론트로 예외 발생하기 전 시점 데이터를 보내줘야하는데, 이걸 어떻게 할 수 있을까
TODO : 실시간 편집인데, 노션 처럼 누가 어떤 것을 변경했는지 알려줄 필요가 있지 않을까?
*/

@PostMapping("/trips/{tripId}")
public ResponseEntity<GlobalDataResponse<TripItemAddResponse>> addTripItemFromMainPage(
@PathVariable Long tripId,
@RequestBody TripItemAddRequest tripItemAddRequest) {
return ResponseEntity.ok(GlobalDataResponse.ok(ResponseConstant.SUCCESS, tripService.addTripItemFromMainPage(tripId, tripItemAddRequest)));
}

@MessageMapping("/trips/{tripId}/connectMember")
public void connectMember(@DestinationVariable String tripId, @Payload MemberConnectMsg memberConnectMsg) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.tenten.tentenstomp.domain.trip.dto.request;

public record TripItemAddRequest(
Long tourItemId,
String visitDate
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.tenten.tentenstomp.domain.trip.dto.response;

public record TripItemAddResponse(
Long tripId,
Long tripItemId,
Long tourItemId,
String visitDate
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,17 @@ public void updateTripTransportation(String tripId, TripTransportationUpdateMsg

kafkaProducer.sendAndSaveToRedis(tripBudgetMsg, tripItemMsg, tripPathMsg);
}
@Transactional
public TripItemAddResponse addTripItemFromMainPage(Long tripId, TripItemAddRequest tripItemAddRequest) {
Trip trip = tripRepository.findTripForUpdate(tripId).orElseThrow(() -> new GlobalException("해당 아이디로 존재하는 여정이 없습니다 " + tripId, NOT_FOUND));
List<TripItem> tripItems = tripItemRepository.findTripItemByTripIdAndVisitDate(tripId, LocalDate.parse(tripItemAddRequest.visitDate()));
LocalDate visitDate = LocalDate.parse(tripItemAddRequest.visitDate());
TripItem entity = TripItemCreateRequest.toEntity(tourItemRepository.getReferenceById(tripItemAddRequest.tourItemId()), trip, (long) tripItems.size() + 1, visitDate);
tripItemRepository.save(entity);
tripItems.add(entity);

updateBudgetAndItemsAndPath(trip, tripItems, tripItemAddRequest.visitDate());

return new TripItemAddResponse(trip.getId(), entity.getId(), tripItemAddRequest.tourItemId(), tripItemAddRequest.visitDate());
}
}