Skip to content

Commit

Permalink
Merge pull request #68 from pyro-yolog/feat/#67-diary-all-update
Browse files Browse the repository at this point in the history
Feat/#67 일기 수정 API 하나로 통합
  • Loading branch information
yeonjy authored Aug 4, 2024
2 parents 719bc86 + 07dc357 commit bde074e
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 105 deletions.
56 changes: 6 additions & 50 deletions src/main/java/com/pyro/yolog/domain/diary/api/DiaryApi.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package com.pyro.yolog.domain.diary.api;

import com.pyro.yolog.domain.diary.dto.request.PutDiaryContentRequest;
import com.pyro.yolog.domain.diary.dto.request.CreateDiaryRequest;
import com.pyro.yolog.domain.diary.dto.request.MoodRequest;
import com.pyro.yolog.domain.diary.dto.request.WeatherRequest;
import com.pyro.yolog.domain.diary.dto.response.DefaultDiaryResponse;
import com.pyro.yolog.domain.diary.dto.request.*;
import com.pyro.yolog.domain.diary.dto.response.DetailDiaryResponse;
import com.pyro.yolog.domain.diary.dto.response.PreviewDiaryResponse;
import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -81,26 +77,6 @@ void createDiary(
CreateDiaryRequest request
);

@Operation(
summary = "일기 수정",
description = "일기를 수정합니다.",
security = {@SecurityRequirement(name = "access_token")}
)
@ApiResponses(
value = {
@ApiResponse(
responseCode = "204",
description = "No Content"
)
}
)
void updateDiaryTitleAndContent(
@Parameter(in = ParameterIn.PATH, description = "일기 ID", required = true)
Long id,

@RequestBody(required = true) PutDiaryContentRequest request
);

@Operation(
summary = "일기 삭제",
description = "일기를 삭제합니다.",
Expand All @@ -119,28 +95,8 @@ void deleteDiary(@Parameter(in = ParameterIn.PATH, description = "일기 ID", re
);

@Operation(
summary = "일기 날씨 수정",
description = "일기의 날씨 정보를 수정합니다.",
security = {@SecurityRequirement(name = "access_token")}
)
@ApiResponses(
value = {
@ApiResponse(
responseCode = "204",
description = "No Content"
)
}
)
void updateWeather(
@Parameter(in = ParameterIn.PATH, description = "일기 ID", required = true)
Long id,

@RequestBody(required = true) WeatherRequest request
);

@Operation(
summary = "일기 기분 수정",
description = "일기의 기분 정보를 수정합니다.",
summary = "일기 수정",
description = "일기를 수정합니다.",
security = {@SecurityRequirement(name = "access_token")}
)
@ApiResponses(
Expand All @@ -151,9 +107,9 @@ void updateWeather(
)
}
)
void updateMood(@Parameter(in = ParameterIn.PATH, description = "일기 ID", required = true)
Long id,
void updateDiary(@Parameter(in = ParameterIn.PATH, description = "일기 ID", required = true)
Long id,

@RequestBody(required = true) MoodRequest request
@RequestBody(required = true) UpdateDiaryRequest request
);
}
27 changes: 5 additions & 22 deletions src/main/java/com/pyro/yolog/domain/diary/api/DiaryController.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.pyro.yolog.domain.diary.api;

import com.pyro.yolog.domain.diary.dto.request.PutDiaryContentRequest;
import com.pyro.yolog.domain.diary.dto.request.CreateDiaryRequest;
import com.pyro.yolog.domain.diary.dto.request.MoodRequest;
import com.pyro.yolog.domain.diary.dto.request.*;
import com.pyro.yolog.domain.diary.dto.response.DetailDiaryResponse;
import com.pyro.yolog.domain.diary.dto.response.PreviewDiaryResponse;
import com.pyro.yolog.domain.diary.dto.request.WeatherRequest;
import com.pyro.yolog.domain.diary.service.DiaryService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -37,15 +34,15 @@ public List<PreviewDiaryResponse> getDiaries(@PathVariable final Long tripId, @P
@ResponseStatus(HttpStatus.CREATED)
@PostMapping("/{tripId}")
@Override
public void createDiary(@PathVariable final Long tripId, @RequestBody CreateDiaryRequest request) {
public void createDiary(@PathVariable final Long tripId, @Valid @RequestBody CreateDiaryRequest request) {
diaryService.createDiary(tripId, request);
}

@ResponseStatus(HttpStatus.NO_CONTENT)
@PutMapping("/{id}/content")
@PutMapping("/{id}")
@Override
public void updateDiaryTitleAndContent(@PathVariable final Long id, @RequestBody final PutDiaryContentRequest request) {
diaryService.updateDiaryTitleAndContent(id, request);
public void updateDiary(@PathVariable final Long id, @Valid @RequestBody final UpdateDiaryRequest request) {
diaryService.updateDiary(id, request);
}

@ResponseStatus(HttpStatus.NO_CONTENT)
Expand All @@ -54,18 +51,4 @@ public void updateDiaryTitleAndContent(@PathVariable final Long id, @RequestBody
public void deleteDiary(@PathVariable final Long id) {
diaryService.deleteDiary(id);
}

@ResponseStatus(HttpStatus.NO_CONTENT)
@PutMapping("/{id}/weather")
@Override
public void updateWeather(@PathVariable final Long id, @Valid @RequestBody final WeatherRequest request) {
diaryService.updateWeather(id, request);
}

@ResponseStatus(HttpStatus.NO_CONTENT)
@PutMapping("/{id}/mood")
@Override
public void updateMood(@PathVariable final Long id, @Valid @RequestBody final MoodRequest request) {
diaryService.updateMood(id, request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class PutDiaryContentRequest {
public class UpdateDiaryContentRequest {
private String title;
private String content;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.pyro.yolog.domain.diary.dto.request;

import com.pyro.yolog.domain.diary.entity.Mood;
import com.pyro.yolog.domain.diary.entity.Weather;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class UpdateDiaryRequest {
private String title;
private String content;
private Mood mood;
private Weather weather;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
@Builder
public class DetailDiaryResponse {
private Long id;
private String dayName;
private String title;
private String content;
private Mood mood;
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/com/pyro/yolog/domain/diary/entity/Diary.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.pyro.yolog.domain.diary.entity;

import com.pyro.yolog.domain.diary.dto.request.PutDiaryContentRequest;
import com.pyro.yolog.domain.diary.dto.request.UpdateDiaryContentRequest;
import com.pyro.yolog.domain.trip.entity.Trip;
import jakarta.persistence.*;
import lombok.AccessLevel;
Expand All @@ -20,6 +20,7 @@ public class Diary {

private String title;

@Column(columnDefinition = "TEXT")
private String content;

@Enumerated(EnumType.STRING)
Expand All @@ -43,9 +44,12 @@ public Diary(Trip trip, String title, String content, Mood mood, Weather weather
this.travelDate = travelDate;
}

public void updateTitleAndContent(PutDiaryContentRequest request) {
this.content = request.getContent();
this.title = request.getTitle();
public void updateTitle(String title) {
this.title = title;
}

public void updateContent(String content) {
this.content = content;
}

public void updateWeather(Weather weather) {
Expand Down
56 changes: 29 additions & 27 deletions src/main/java/com/pyro/yolog/domain/diary/service/DiaryService.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.pyro.yolog.domain.diary.service;

import com.pyro.yolog.domain.auth.service.AuthService;
import com.pyro.yolog.domain.diary.dto.request.PutDiaryContentRequest;
import com.pyro.yolog.domain.diary.dto.request.CreateDiaryRequest;
import com.pyro.yolog.domain.diary.dto.request.MoodRequest;
import com.pyro.yolog.domain.diary.dto.request.WeatherRequest;
import com.pyro.yolog.domain.diary.dto.request.*;
import com.pyro.yolog.domain.diary.dto.response.DetailDiaryResponse;
import com.pyro.yolog.domain.diary.dto.response.PreviewDiaryResponse;
import com.pyro.yolog.domain.diary.entity.Diary;
Expand All @@ -22,25 +19,29 @@

import java.time.LocalDate;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class DiaryService {
private static final String DAY = "Day";

private final AuthService authService;
private final DiaryRepository diaryRepository;
private final TripRepository tripRepository;
private final DiaryMapper diaryMapper;

public DetailDiaryResponse getDiary(Long id) {
Diary diary = diaryRepository.findById(id).orElseThrow(DiaryNotFoundException::new);
checkDiaryOwner(diary);
final Diary diary = getDiaryById(id);
return new DetailDiaryResponse(diary);
}

private Diary getDiaryById(Long id) {
final Diary diary = diaryRepository.findById(id)
.orElseThrow(DiaryNotFoundException::new);
checkDiaryOwner(diary);
return diary;
}

public List<PreviewDiaryResponse> getDiaries(Long tripId, LocalDate dayName) {
checkTripOwner(tripRepository.findById(tripId)
Expand All @@ -52,43 +53,44 @@ public List<PreviewDiaryResponse> getDiaries(Long tripId, LocalDate dayName) {

@Transactional
public void createDiary(final Long tripId, final CreateDiaryRequest request) {
Trip trip = tripRepository.findById(tripId).orElseThrow(TripNotFoundException::new);
final Trip trip = tripRepository.findById(tripId)
.orElseThrow(TripNotFoundException::new);
diaryRepository.save(diaryMapper.toEntity(request, trip));
}

@Transactional
public void updateDiaryTitleAndContent(Long id, PutDiaryContentRequest request) {
final Diary diary = diaryRepository.findById(id).orElseThrow(DiaryNotFoundException::new);
checkDiaryOwner(diary);
diary.updateTitleAndContent(request);
}

@Transactional
public void deleteDiary(Long id) {
checkDiaryOwner(diaryRepository.findById(id).orElseThrow(DiaryNotFoundException::new));
checkDiaryOwner(diaryRepository.findById(id)
.orElseThrow(DiaryNotFoundException::new));
diaryRepository.deleteById(id);
}

@Transactional
public void updateWeather(Long id, WeatherRequest request) {
Diary diary = diaryRepository.findById(id).orElseThrow(DiaryNotFoundException::new);
checkDiaryOwner(diary);
diary.updateWeather(request.getWeather());
public void updateDiary(Long id, UpdateDiaryRequest request) {
Diary diary = getDiaryById(id);
updateDiaryDetails(diary, request);
}

@Transactional
public void updateMood(Long id, MoodRequest request) {
Diary diary = diaryRepository.findById(id).orElseThrow(DiaryNotFoundException::new);
checkTripOwner(diary.getTrip());
diary.updateMood(request.getMood());
private void updateDiaryDetails(Diary diary, UpdateDiaryRequest request) {
updateFieldIfPresent(request.getTitle(), diary::updateTitle);
updateFieldIfPresent(request.getContent(), diary::updateContent);
updateFieldIfPresent(request.getMood(), diary::updateMood);
updateFieldIfPresent(request.getWeather(), diary::updateWeather);
}

private <T> void updateFieldIfPresent(T value, Consumer<T> updateMethod) {
if (value != null) {
updateMethod.accept(value);
}
}


@Transactional
public void deleteOutOfDuration(Trip trip) {
checkTripOwner(trip);
diaryRepository.findById(trip.getId()).ifPresent(diary -> {
if (diary.getTravelDate().isBefore(trip.getStartDate()) || diary.getTravelDate().isAfter(trip.getFinishDate())) {
if (diary.getTravelDate().isBefore(trip.getStartDate())
|| diary.getTravelDate().isAfter(trip.getFinishDate())) {
diaryRepository.deleteById(diary.getId());
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class InquiryImage {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(columnDefinition = "TEXT")
private String imageUrl;

@ManyToOne(fetch = FetchType.LAZY)
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/pyro/yolog/domain/trip/entity/Trip.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class Trip extends BaseTimeEntity {
private String name;
private String destination;

@Column(columnDefinition = "TEXT")
private String coverImageUrl;

@Enumerated(EnumType.STRING)
Expand Down

0 comments on commit bde074e

Please sign in to comment.