Skip to content

Commit

Permalink
feat: add constraint to update pray
Browse files Browse the repository at this point in the history
  • Loading branch information
baebae02 committed Nov 20, 2023
1 parent 40c1a44 commit 44668d5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.uspray.uspray.DTO.pray.request;

import io.swagger.v3.oas.annotations.media.Schema;
import java.time.LocalDate;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Schema(description = "기도제목 수정 DTO")
public class PrayUpdateRequestDto {

@Schema(description = "기도제목 내용", example = "@@이가 $$ 할 수 있도록")
private String content;

@Schema(description = "기도제목 마감일", example = "2025-01-01")
private LocalDate deadline;

@Schema(description = "기도제목 카테고리", example = "1")
private Long categoryId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

@Repository
public interface PrayRepository extends JpaRepository<Pray, Long>, PrayRepositoryCustom {

default Pray getPrayById(Long id) {
return findById(id).orElseThrow(
() -> new NotFoundException(ErrorStatus.PRAY_NOT_FOUND_EXCEPTION,
ErrorStatus.PRAY_NOT_FOUND_EXCEPTION.getMessage()));
}

default Pray getPrayByIdAndMemberId(Long prayId, String username) throws NotFoundException {
return findById(prayId)
.filter(pray -> pray.getMember().getUserId().equals(username))
Expand All @@ -26,8 +26,10 @@ default Pray getPrayByIdAndMemberId(Long prayId, String username) throws NotFoun
ErrorStatus.PRAY_NOT_FOUND_EXCEPTION.getMessage()
));
}

List<Pray> findAllByIdIn(List<Long> prayIds);

List<Pray> findAllByDeadlineBefore(LocalDate date);

Pray getPrayByOriginPrayId(Long prayId);
}
23 changes: 19 additions & 4 deletions src/main/java/com/uspray/uspray/service/PrayFacadeService.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.uspray.uspray.service;

import com.uspray.uspray.DTO.pray.request.PrayRequestDto;
import com.uspray.uspray.DTO.pray.request.PrayResponseDto;
import com.uspray.uspray.DTO.pray.request.PrayUpdateRequestDto;
import com.uspray.uspray.DTO.pray.response.PrayResponseDto;
import com.uspray.uspray.Enums.PrayType;
import com.uspray.uspray.domain.Category;
import com.uspray.uspray.domain.History;
import com.uspray.uspray.domain.Member;
import com.uspray.uspray.domain.Pray;
import com.uspray.uspray.exception.ErrorStatus;
import com.uspray.uspray.exception.model.CustomException;
import com.uspray.uspray.infrastructure.CategoryRepository;
import com.uspray.uspray.infrastructure.HistoryRepository;
import com.uspray.uspray.infrastructure.MemberRepository;
Expand Down Expand Up @@ -38,12 +41,24 @@ public PrayResponseDto createPray(PrayRequestDto prayRequestDto, String username
}

@Transactional
public PrayResponseDto updatePray(Long prayId, String username, PrayRequestDto prayRequestDto) {
public PrayResponseDto updatePray(Long prayId, String username,
PrayUpdateRequestDto prayUpdateRequestDto) {
Pray pray = prayRepository.getPrayByIdAndMemberId(prayId, username);
categoryRepository.getCategoryByIdAndMember(
prayRequestDto.getCategoryId(),
prayUpdateRequestDto.getCategoryId(),
pray.getMember());
pray.update(prayRequestDto);

// 그룹 기도 제목은 이 API로 수정 불가능
if (pray.getPrayType() == PrayType.GROUP) {
throw new CustomException(ErrorStatus.PRAY_UNAUTHORIZED_EXCEPTION,
ErrorStatus.PRAY_UNAUTHORIZED_EXCEPTION.getMessage());
}
// 이 기도 제목을 공유한 적 없거나, 공유 받은 사람이 없으면 전부 수정 가능
// 이 기도 제목을 공유한 적 있고, 누구라도 공유 받은 사람이 있으면 기도제목 내용 수정 불가능
Pray sharedPray = prayRepository.getPrayByOriginPrayId(prayId);
pray.update(prayUpdateRequestDto,
sharedPray != null || pray.getPrayType() == PrayType.SHARED);

return PrayResponseDto.of(pray);
}

Expand Down

0 comments on commit 44668d5

Please sign in to comment.