Skip to content

Commit

Permalink
Merge pull request #58 from pyro-yolog/feat/#57-check-diary-owner
Browse files Browse the repository at this point in the history
Feat/#57 일기 및 일기장 작성자인지 검증하는 코드 구현
  • Loading branch information
yeonjy authored Jul 16, 2024
2 parents 9f80457 + eb1beb3 commit f995112
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import com.pyro.yolog.domain.member.entity.Member;
import com.pyro.yolog.domain.member.entity.SocialType;
import com.pyro.yolog.domain.member.exception.MemberNotFoundException;
import com.pyro.yolog.domain.member.repository.MemberRepository;
import com.pyro.yolog.global.jwt.service.JwtService;
import com.pyro.yolog.global.query.QueryService;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -37,7 +37,7 @@ public Long getLoginUserId() {
public Member getLoginUser() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
return memberRepository.findByEmail(userDetails.getUsername()).orElseThrow(EntityNotFoundException::new);
return memberRepository.findByEmail(userDetails.getUsername()).orElseThrow(MemberNotFoundException::new);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.pyro.yolog.domain.diary.exception;

import com.pyro.yolog.global.error.ErrorCode;
import com.pyro.yolog.global.error.exception.BusinessException;

public class DiaryNotFoundException extends BusinessException {
public DiaryNotFoundException() {
super(ErrorCode.DIARY_NOT_FOUND_ERROR);
}
}
45 changes: 33 additions & 12 deletions src/main/java/com/pyro/yolog/domain/diary/service/DiaryService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pyro.yolog.domain.diary.service;

import com.pyro.yolog.domain.auth.service.AuthService;
import com.pyro.yolog.domain.diary.dto.request.DiaryContentRequest;
import com.pyro.yolog.domain.diary.dto.request.DiaryDateRequest;
import com.pyro.yolog.domain.diary.dto.request.MoodRequest;
Expand All @@ -8,13 +9,13 @@
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;
import com.pyro.yolog.domain.diary.entity.Mood;
import com.pyro.yolog.domain.diary.entity.Weather;
import com.pyro.yolog.domain.diary.exception.DiaryNotFoundException;
import com.pyro.yolog.domain.diary.mapper.DiaryMapper;
import com.pyro.yolog.domain.diary.repository.DiaryRepository;
import com.pyro.yolog.domain.member.exception.OwnerNotEqualException;
import com.pyro.yolog.domain.trip.entity.Trip;
import com.pyro.yolog.domain.trip.exception.TripNotFoundException;
import com.pyro.yolog.domain.trip.repository.TripRepository;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -29,60 +30,80 @@
@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) {
return new DetailDiaryResponse((diaryRepository.findById(id)
.orElseThrow(EntityNotFoundException::new)));
Diary diary = diaryRepository.findById(id).orElseThrow(DiaryNotFoundException::new);
checkDiaryOwner(diary);
return new DetailDiaryResponse(diary);
}


public List<PreviewDiaryResponse> getDiaries(Long tripId, String dayName) {
checkTripOwner(tripRepository.findById(tripId)
.orElseThrow(TripNotFoundException::new));
List<Diary> diaries = diaryRepository.findAllByTripIdAndDayName(tripId, dayName);
return diaries.stream().map(PreviewDiaryResponse::new).collect(Collectors.toList());
return diaries.stream().map(PreviewDiaryResponse::new)
.collect(Collectors.toList());
}

@Transactional
public DefaultDiaryResponse createDefaultDiary(final Long tripId, final DiaryDateRequest request) {
Trip trip = tripRepository.findById(tripId).orElseThrow(EntityNotFoundException::new);
Trip trip = tripRepository.findById(tripId).orElseThrow(TripNotFoundException::new);
final String dayName = DAY + (ChronoUnit.DAYS.between(trip.getStartDate(), request.getDate()) + 1);
return diaryMapper.toDefaultFormatResponse(
diaryRepository.save(diaryMapper.toEntity(trip, dayName, request.getDate())));
}

@Transactional
public void updateDiaryTitleAndContent(Long id, DiaryContentRequest request) {
final Diary diary = diaryRepository.findById(id).orElseThrow(EntityNotFoundException::new);
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));
diaryRepository.deleteById(id);
}

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

@Transactional
public void updateMood(Long id, MoodRequest request) {
diaryRepository.findById(id).orElseThrow(EntityNotFoundException::new)
.updateMood(request.getMood());
Diary diary = diaryRepository.findById(id).orElseThrow(DiaryNotFoundException::new);
checkTripOwner(diary.getTrip());
diary.updateMood(request.getMood());
}


@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())) {
diaryRepository.deleteById(diary.getId());
}
});
}

private void checkDiaryOwner(Diary diary) {
checkTripOwner(diary.getTrip());
}

private void checkTripOwner(Trip trip) {
if (!trip.getMember().equals(authService.getLoginUser())) {
throw new OwnerNotEqualException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.pyro.yolog.domain.member.exception;

import com.pyro.yolog.global.error.ErrorCode;
import com.pyro.yolog.global.error.exception.BusinessException;

public class MemberNotFoundException extends BusinessException {
public MemberNotFoundException() {
super(ErrorCode.MEMBER_NOT_FOUND_ERROR);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.pyro.yolog.domain.member.exception;

import com.pyro.yolog.global.error.ErrorCode;
import com.pyro.yolog.global.error.exception.BusinessException;

public class OwnerNotEqualException extends BusinessException {
public OwnerNotEqualException() {
super(ErrorCode.OWNER_NOT_EQUAL_ERROR);
}
}
19 changes: 15 additions & 4 deletions src/main/java/com/pyro/yolog/domain/trip/service/TripService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.pyro.yolog.domain.diary.service.DiaryService;
import com.pyro.yolog.domain.member.entity.Member;
import com.pyro.yolog.domain.auth.service.AuthService;
import com.pyro.yolog.domain.member.exception.OwnerNotEqualException;
import com.pyro.yolog.domain.trip.dto.TripRequest;
import com.pyro.yolog.domain.trip.dto.TripResponse;
import com.pyro.yolog.domain.trip.entity.Trip;
Expand Down Expand Up @@ -33,19 +34,17 @@ public void saveTrip(final TripRequest request) {
@Transactional
public void updateTrip(final Long id, final TripRequest request) {
final Trip trip = tripRepository.findById(id).orElseThrow(TripNotFoundException::new);
checkTripOwner(trip);
trip.update(request);
diaryService.deleteOutOfDuration(trip);
}

@Transactional
public void deleteTrip(Long id) {
checkTripOwner(getTrip(id));
tripRepository.deleteById(id);
}

public Trip getTrip(final Long id) {
return tripRepository.findById(id).orElseThrow(TripNotFoundException::new);
}

public List<TripResponse> getTrips() {
Member login = authService.getLoginUser();
return tripRepository.findAllByMember(login).stream()
Expand All @@ -55,4 +54,16 @@ public List<TripResponse> getTrips() {
public TripResponse getTripDetail(Long id) {
return new TripResponse(getTrip(id));
}

public Trip getTrip(final Long id) {
Trip trip = tripRepository.findById(id).orElseThrow(TripNotFoundException::new);
checkTripOwner(trip);
return trip;
}

private void checkTripOwner(Trip trip) {
if (!trip.getMember().equals(authService.getLoginUser())) {
throw new OwnerNotEqualException();
}
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/pyro/yolog/global/error/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public enum ErrorCode {

// MEMBER
MEMBER_NOT_FOUND_ERROR(HttpStatus.NOT_FOUND, "회원 정보를 찾지 못했습니다." ),
OWNER_NOT_EQUAL_ERROR(HttpStatus.BAD_REQUEST, "작성자만 접근 가능합니다."),

// S3
FILE_UPLOAD_FAILURE_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "S3 파일 업로드에 실패했습니다."),
Expand All @@ -26,6 +27,7 @@ public enum ErrorCode {
REQUEST_COLOR_COVER_INVALID_ERROR(HttpStatus.BAD_REQUEST, "올바른 색상 코드를 입력해야 합니다."),

//DIARY
DIARY_NOT_FOUND_ERROR(HttpStatus.NOT_FOUND, "해당 일기를 찾지 못했습니다."),
REQUEST_WEATHER_NAME_INVALID_ERROR(HttpStatus.BAD_REQUEST, "올바른 날씨를 입력해야 합니다."),
REQUEST_MOOD_NAME_INVALID_ERROR(HttpStatus.BAD_REQUEST, "올바른 기분을 입력해야 합니다."),

Expand Down

0 comments on commit f995112

Please sign in to comment.