Skip to content

Commit

Permalink
Merge branch 'develop/be' into hotfix/be/#351
Browse files Browse the repository at this point in the history
  • Loading branch information
hangillee authored Aug 20, 2024
2 parents 3011bde + 370652a commit 216a756
Show file tree
Hide file tree
Showing 14 changed files with 366 additions and 78 deletions.
2 changes: 1 addition & 1 deletion backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM openjdk:17-oracle
ARG JAR_FILE=build/libs/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java", "-jar", "-Duser.timezone=Asia/Seoul", "-Dspring.profiles.active=dev", "/app.jar"]
ENTRYPOINT ["java", "-jar", "-Duser.timezone=Asia/Seoul", "/app.jar"]
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpHeaders;

@Profile("local|dev")
@Configuration
public class SwaggerConfig {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.UUID;
import kr.touroot.global.auth.dto.MemberAuth;
import kr.touroot.global.exception.dto.ExceptionResponse;
import kr.touroot.travelplan.dto.request.PlanCreateRequest;
import kr.touroot.travelplan.dto.request.PlanRequest;
import kr.touroot.travelplan.dto.response.PlanCreateResponse;
import kr.touroot.travelplan.dto.response.PlanResponse;
import kr.touroot.travelplan.service.TravelPlanService;
Expand All @@ -22,6 +22,7 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Expand Down Expand Up @@ -53,7 +54,7 @@ public class TravelPlanController {
})
@PostMapping
public ResponseEntity<PlanCreateResponse> createTravelPlan(
@Valid @RequestBody PlanCreateRequest request,
@Valid @RequestBody PlanRequest request,
MemberAuth memberAuth
) {
PlanCreateResponse data = travelPlanService.createTravelPlan(request, memberAuth);
Expand Down Expand Up @@ -87,6 +88,32 @@ public ResponseEntity<PlanResponse> readTravelPlan(
return ResponseEntity.ok(data);
}

@Operation(summary = "์—ฌํ–‰ ๊ณ„ํš ์ˆ˜์ •")
@ApiResponses(value = {
@ApiResponse(
responseCode = "204",
description = "์š”์ฒญ์ด ์ •์ƒ์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋˜์—ˆ์„ ๋•Œ"
),
@ApiResponse(
responseCode = "400",
description = "์š”์ฒญ Body์— ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ด ์ „๋‹ฌ๋˜์—ˆ์„ ๋•Œ",
content = @Content(schema = @Schema(implementation = ExceptionResponse.class))
),
@ApiResponse(
responseCode = "403",
description = "์ž‘์„ฑ์ž๊ฐ€ ์•„๋‹Œ ์‚ฌ์šฉ์ž๊ฐ€ ์š”์ฒญํ–ˆ์„ ๋•Œ",
content = @Content(schema = @Schema(implementation = ExceptionResponse.class))
)
})
@PutMapping("/{id}")
public ResponseEntity<PlanCreateResponse> updateTravelPlan(
@PathVariable Long id,
@Valid MemberAuth memberAuth,
@Valid @RequestBody PlanRequest request
) {
return ResponseEntity.ok(travelPlanService.updateTravelPlan(id, memberAuth, request));
}

@Operation(summary = "์—ฌํ–‰ ๊ณ„ํš ์‚ญ์ œ")
@ApiResponses(value = {
@ApiResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ private void validateTitleLength(String title) {
}
}

public void update(String title, LocalDate startDate) {
this.title = title;
this.startDate = startDate;
}

public boolean isStartDateBefore(LocalDate date) {
return startDate.isBefore(date);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import kr.touroot.travelplan.domain.TravelPlan;
import kr.touroot.travelplan.domain.TravelPlanDay;

public record PlanDayCreateRequest(
public record PlanDayRequest(
@Schema(description = "์—ฌํ–‰ ์žฅ์†Œ ์ •๋ณด")
@Valid
@Size(min = 1, message = "์—ฌํ–‰ ์žฅ์†Œ๋Š” ํ•œ ๊ฐœ ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.")
@NotNull(message = "์—ฌํ–‰ ์žฅ์†Œ ์ •๋ณด๋Š” ๋น„์–ด์žˆ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
List<PlanPlaceCreateRequest> places
List<PlanPlaceRequest> places
) {

public TravelPlanDay toPlanDay(int order, TravelPlan plan) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
import lombok.Builder;

@Builder
public record PlanPlaceCreateRequest(
public record PlanPlaceRequest(
@Schema(description = "์—ฌํ–‰ ์žฅ์†Œ ์ด๋ฆ„", example = "์ž ์‹คํ•œ๊ฐ•๊ณต์›")
@NotBlank(message = "์žฅ์†Œ๋ช…์€ ๋น„์–ด์žˆ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.") String placeName,
@Valid
@NotNull(message = "์œ„์น˜๋Š” ๋น„์–ด์žˆ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
PlanPositionCreateRequest position,
PlanPositionRequest position,
@Valid
@NotNull(message = "TODO ๋ฆฌ์ŠคํŠธ๋Š” ํ•„์ˆ˜ ์ž…๋‹ˆ๋‹ค.")
List<PlanPlaceTodoRequest> todos
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import kr.touroot.travelplan.domain.TravelPlaceTodo;
import kr.touroot.travelplan.domain.TravelPlanPlace;
Expand All @@ -10,10 +11,13 @@ public record PlanPlaceTodoRequest(
@Schema(description = "์—ฌํ–‰ ์žฅ์†Œ์—์„œ ์ง„ํ–‰ํ•  TODO", example = "ํ•จ๋• ํ•ด์ˆ˜์š•์žฅ ์‚ฐ์ฑ…")
@NotBlank(message = "TODO ๋‚ด์šฉ์€ ๋น„์–ด ์žˆ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค")
@Size(min = 1, max = 20, message = "TODO ๋‚ด์šฉ์€ 1์ž์—์„œ 20์ž ์‚ฌ์ด์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค")
String content
String content,
@Schema(description = "TODO์˜ ์ฒดํฌ ์—ฌ๋ถ€", example = "true")
@NotNull(message = "TODO์˜ ์ฒดํฌ ์—ฌ๋ถ€๋Š” ๋น„์–ด ์žˆ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
Boolean isChecked
) {

public TravelPlaceTodo toUncheckedPlaceTodo(TravelPlanPlace travelPlanPlace, Integer order) {
return new TravelPlaceTodo(travelPlanPlace, content, order, false);
public TravelPlaceTodo toPlaceTodo(TravelPlanPlace travelPlanPlace, Integer order) {
return new TravelPlaceTodo(travelPlanPlace, content, order, isChecked);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;

public record PlanPositionCreateRequest(
public record PlanPositionRequest(
@Schema(description = "์—ฌํ–‰ ์žฅ์†Œ ์œ„๋„", example = "37.5175896")
@NotNull(message = "์œ„๋„๋Š” ๋น„์–ด์žˆ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
String lat,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import lombok.Builder;

@Builder
public record PlanCreateRequest(
public record PlanRequest(
@Schema(description = "์—ฌํ–‰ ๊ณ„ํš ์ œ๋ชฉ", example = "์‹ ๋‚˜๋Š” ์ž ์‹ค ํ•œ๊ฐ• ์—ฌํ–‰")
@NotBlank(message = "์—ฌํ–‰ ๊ณ„ํš ์ œ๋ชฉ์€ ๋น„์–ด์žˆ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
String title,
Expand All @@ -24,7 +24,7 @@ public record PlanCreateRequest(
@Valid
@Size(min = 1, message = "์—ฌํ–‰ ๋‚ ์งœ๋Š” ํ•˜๋ฃจ ์ด์ƒ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.")
@NotNull(message = "์—ฌํ–‰ ๋‚ ์งœ ์ •๋ณด๋Š” ๋น„์–ด์žˆ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
List<PlanDayCreateRequest> days
List<PlanDayRequest> days
) {

public TravelPlan toTravelPlan(Member author, UUID shareKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
import kr.touroot.travelplan.domain.TravelPlan;
import kr.touroot.travelplan.domain.TravelPlanDay;
import kr.touroot.travelplan.domain.TravelPlanPlace;
import kr.touroot.travelplan.dto.request.PlanCreateRequest;
import kr.touroot.travelplan.dto.request.PlanDayCreateRequest;
import kr.touroot.travelplan.dto.request.PlanPlaceCreateRequest;
import kr.touroot.travelplan.dto.request.PlanDayRequest;
import kr.touroot.travelplan.dto.request.PlanPlaceRequest;
import kr.touroot.travelplan.dto.request.PlanPlaceTodoRequest;
import kr.touroot.travelplan.dto.request.PlanRequest;
import kr.touroot.travelplan.dto.response.PlanCreateResponse;
import kr.touroot.travelplan.dto.response.PlanDayResponse;
import kr.touroot.travelplan.dto.response.PlanPlaceResponse;
Expand Down Expand Up @@ -46,18 +46,18 @@ public class TravelPlanService {
private final PlaceTodoRepository placeTodoRepository;

@Transactional
public PlanCreateResponse createTravelPlan(PlanCreateRequest request, MemberAuth memberAuth) {
public PlanCreateResponse createTravelPlan(PlanRequest request, MemberAuth memberAuth) {
Member author = getMemberByMemberAuth(memberAuth);
TravelPlan travelPlan = request.toTravelPlan(author, UUID.randomUUID());
validateTravelPlan(travelPlan);
validateCreateTravelPlan(travelPlan);

TravelPlan savedTravelPlan = travelPlanRepository.save(travelPlan);
createPlanDay(request.days(), savedTravelPlan);

return new PlanCreateResponse(savedTravelPlan.getId());
}

private void validateTravelPlan(TravelPlan travelPlan) {
private void validateCreateTravelPlan(TravelPlan travelPlan) {
if (travelPlan.isStartDateBefore(LocalDate.now())) {
throw new BadRequestException("์ง€๋‚œ ๋‚ ์งœ์— ๋Œ€ํ•œ ๊ณ„ํš์€ ์ž‘์„ฑํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
}
Expand All @@ -68,17 +68,17 @@ private Member getMemberByMemberAuth(MemberAuth memberAuth) {
.orElseThrow(() -> new BadRequestException("์กด์žฌํ•˜์ง€ ์•Š๋Š” ์‚ฌ์šฉ์ž์ž…๋‹ˆ๋‹ค."));
}

private void createPlanDay(List<PlanDayCreateRequest> request, TravelPlan savedTravelPlan) {
private void createPlanDay(List<PlanDayRequest> request, TravelPlan savedTravelPlan) {
for (int order = 0; order < request.size(); order++) {
PlanDayCreateRequest dayRequest = request.get(order);
PlanDayRequest dayRequest = request.get(order);
TravelPlanDay travelPlanDay = travelPlanDayRepository.save(dayRequest.toPlanDay(order, savedTravelPlan));
createPlanPlace(dayRequest.places(), travelPlanDay);
}
}

private void createPlanPlace(List<PlanPlaceCreateRequest> request, TravelPlanDay travelPlanDay) {
private void createPlanPlace(List<PlanPlaceRequest> request, TravelPlanDay travelPlanDay) {
for (int order = 0; order < request.size(); order++) {
PlanPlaceCreateRequest planRequest = request.get(order);
PlanPlaceRequest planRequest = request.get(order);
Place place = getPlace(planRequest);
TravelPlanPlace planPlace = planRequest.toPlanPlace(order, travelPlanDay, place);
TravelPlanPlace travelPlanPlace = travelPlanPlaceRepository.save(planPlace);
Expand All @@ -89,12 +89,12 @@ private void createPlanPlace(List<PlanPlaceCreateRequest> request, TravelPlanDay
private void createPlaceTodo(List<PlanPlaceTodoRequest> request, TravelPlanPlace travelPlanPlace) {
for (int order = 0; order < request.size(); order++) {
PlanPlaceTodoRequest todoRequest = request.get(order);
TravelPlaceTodo travelPlaceTodo = todoRequest.toUncheckedPlaceTodo(travelPlanPlace, order);
TravelPlaceTodo travelPlaceTodo = todoRequest.toPlaceTodo(travelPlanPlace, order);
placeTodoRepository.save(travelPlaceTodo);
}
}

private Place getPlace(PlanPlaceCreateRequest planRequest) {
private Place getPlace(PlanPlaceRequest planRequest) {
return placeRepository.findByNameAndLatitudeAndLongitude(
planRequest.placeName(),
planRequest.position().lat(),
Expand Down Expand Up @@ -173,6 +173,35 @@ public int calculateTravelPeriod(TravelPlan travelPlan) {
.size();
}

@Transactional
public PlanCreateResponse updateTravelPlan(Long planId, MemberAuth memberAuth, PlanRequest request) {
TravelPlan travelPlan = getTravelPlanById(planId);
Member author = getMemberByMemberAuth(memberAuth);
validateUpdateByAuthor(travelPlan, author);

clearTravelPlanContents(travelPlan);
updateTravelPlanContents(request, travelPlan);
return new PlanCreateResponse(travelPlan.getId());
}

private void validateUpdateByAuthor(TravelPlan travelPlan, Member member) {
if (!travelPlan.isAuthor(member)) {
throw new ForbiddenException("์—ฌํ–‰ ๊ณ„ํš ์ˆ˜์ •์€ ์ž‘์„ฑ์ž๋งŒ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.");
}
}

private void clearTravelPlanContents(TravelPlan travelPlan) {
placeTodoRepository.deleteByTravelPlanPlaceDayPlan(travelPlan);
travelPlanPlaceRepository.deleteByDayPlan(travelPlan);
travelPlanDayRepository.deleteByPlan(travelPlan);
}

private void updateTravelPlanContents(PlanRequest request, TravelPlan travelPlan) {
travelPlan.update(request.title(), request.startDate());
travelPlanRepository.save(travelPlan);
createPlanDay(request.days(), travelPlan);
}

@Transactional
public void deleteByTravelPlanId(Long planId, MemberAuth memberAuth) {
TravelPlan travelPlan = getTravelPlanById(planId);
Expand Down
Loading

0 comments on commit 216a756

Please sign in to comment.