Skip to content
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 @@ -5,16 +5,19 @@
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
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.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import swyp.team5.greening.common.dto.response.ApiResponseDto;
import swyp.team5.greening.common.resolver.LogIn;
import swyp.team5.greening.petPlant.dto.request.CreateDailyRecordRequestDto;
import swyp.team5.greening.petPlant.dto.request.UpdateDailyRecordRequestDto;
import swyp.team5.greening.petPlant.dto.response.CreateDailyRecordResponseDto;
import swyp.team5.greening.petPlant.dto.response.FindDailyRecordResponseDto;
import swyp.team5.greening.petPlant.service.DailyRecordCommandService;
Expand Down Expand Up @@ -58,4 +61,25 @@ public ApiResponseDto<FindDailyRecordResponseDto> getDailyRecord(
));
}

@Operation(summary = "특정 애완 식물 오늘의 기록 수정 API")
@PutMapping("/daily-record/{dailyRecordId}")
@ResponseStatus(HttpStatus.OK)
public void updateDailyRecord(
@LogIn Long userId,
@PathVariable Long dailyRecordId,
@Validated @RequestBody UpdateDailyRecordRequestDto requestDto
) {
dailyRecordCommandService.updateDailyRecord(userId, dailyRecordId, requestDto);
}

@Operation(summary = "특정 애완 식물 오늘의 기록 삭제 API")
@DeleteMapping("/daily-record/{dailyRecordId}")
@ResponseStatus(HttpStatus.OK)
public void deleteDailyRecord(
@LogIn Long userId,
@PathVariable Long dailyRecordId
) {
dailyRecordCommandService.deleteDailyRecord(userId, dailyRecordId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public DailyRecord(
this.petPlantId = petPlantId;
}

public void updateTitle(String newTitle) {
this.title = newTitle;
}

public void updateContent(List<DailyRecordContent> contents) {
dailyRecordContents.clear();

Expand All @@ -68,4 +72,8 @@ public void updateContent(List<DailyRecordContent> contents) {
content.setSequence(index);
}
}

public void delete() {
this.state = DailyRecordState.DELETED;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package swyp.team5.greening.petPlant.domain.repository;

import java.time.LocalDate;
import java.util.Optional;
import swyp.team5.greening.petPlant.domain.entity.DailyRecord;
import swyp.team5.greening.petPlant.domain.entity.DailyRecordState;

public interface DailyRecordRepository {

DailyRecord save(DailyRecord dailyRecord);

boolean existsByPetPlantIdAndWriteDateAndState(Long petPlantId, LocalDate writeDate, DailyRecordState state);

Optional<DailyRecord> findByIdAndState(Long dailyRecordId, DailyRecordState state);

void deleteAll();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package swyp.team5.greening.petPlant.dto.request;

import jakarta.validation.constraints.NotEmpty;
import java.util.List;
import swyp.team5.greening.petPlant.dto.DailyRecordContentDto;

public record UpdateDailyRecordRequestDto(

@NotEmpty
String title,

@NotEmpty
List<DailyRecordContentDto> content
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public enum PetPlantExceptionMessage implements ExceptionMessage {
BAD_REQUEST_PET_PLANT_WRITER("내가 등록한 애완 식물이 아닙니다.", "400"),

NOT_FOUND_DAILY_RECORD("해당 날짜의 기록이 존재하지 않습니다.", "404"),
ALREADY_EXISTS_DAILY_RECORD("이미 작성한 오늘의 기록이 존재합니다", "409"),

INVALID_DATE_ACCESS("해당 날짜에만 접근 가능합니다.", "400");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import swyp.team5.greening.petPlant.domain.repository.DailyRecordRepository;
import swyp.team5.greening.petPlant.domain.repository.PetPlantRepository;
import swyp.team5.greening.petPlant.dto.request.CreateDailyRecordRequestDto;
import swyp.team5.greening.petPlant.dto.request.UpdateDailyRecordRequestDto;
import swyp.team5.greening.petPlant.dto.response.CreateDailyRecordResponseDto;
import swyp.team5.greening.petPlant.exception.PetPlantExceptionMessage;

Expand All @@ -33,7 +34,8 @@ public class DailyRecordCommandService {
//1. 애완 식물 조회
//2. 사용자 유효성 검증
//3. 오늘 날짜 여부 확인
//4. 오늘의 기록 저장
//4. 이미 오늘 작성한 오늘의 기록이 존재하는지 확인
//5. 오늘의 기록 작성
@Transactional
public CreateDailyRecordResponseDto createDailyRecord(
Long userId,
Expand All @@ -57,6 +59,12 @@ public CreateDailyRecordResponseDto createDailyRecord(
}

//4
if (dailyRecordRepository.existsByPetPlantIdAndWriteDateAndState(petPlantId,
nowDate.get(), DailyRecordState.IN_PROGRESS)) {
throw new GreeningGlobalException(PetPlantExceptionMessage.ALREADY_EXISTS_DAILY_RECORD);
}

//5
DailyRecord dailyRecord = DailyRecord.builder()
.title(requestDto.title())
.writeDate(requestDto.today())
Expand All @@ -78,4 +86,70 @@ public CreateDailyRecordResponseDto createDailyRecord(
return new CreateDailyRecordResponseDto(saved.getId());
}

//오늘의 기록 수정
//1. 오늘의 기록 조회
//2. 권한 확인
//3. 제목 수정
//4. 새로운 본문 저장
@Transactional
public void updateDailyRecord(
Long userId,
Long dailyRecordId,
UpdateDailyRecordRequestDto requestDto

) {
DailyRecord dailyRecord = dailyRecordRepository.findByIdAndState(dailyRecordId,
DailyRecordState.IN_PROGRESS)
.orElseThrow(() -> new GreeningGlobalException(
PetPlantExceptionMessage.NOT_FOUND_PET_PLANT));

PetPlant petPlant = petPlantRepository.findByIdAndState(dailyRecord.getPetPlantId(),
PetPlantState.IN_PROGRESS)
.orElseThrow(() -> new GreeningGlobalException(
PetPlantExceptionMessage.NOT_FOUND_PET_PLANT));

if (!Objects.equals(petPlant.getUserId(), userId)) {
throw new GreeningGlobalException(
PetPlantExceptionMessage.BAD_REQUEST_PET_PLANT_WRITER);
}

dailyRecord.updateTitle(requestDto.title());

List<DailyRecordContent> contents = requestDto.content().stream()
.map(dto -> DailyRecordContent.builder()
.content(dto.value())
.type(DailyRecordContentType.valueOf(dto.type().toUpperCase()))
.build())
.toList();

dailyRecord.updateContent(contents);
}

//오늘의 기록 삭제
//1. 오늘의 기록 조회
//2. 권한 확인
//3. 삭제
@Transactional
public void deleteDailyRecord(
Long userId,
Long dailyRecordId
) {
DailyRecord dailyRecord = dailyRecordRepository.findByIdAndState(dailyRecordId,
DailyRecordState.IN_PROGRESS)
.orElseThrow(() -> new GreeningGlobalException(
PetPlantExceptionMessage.NOT_FOUND_PET_PLANT));

PetPlant petPlant = petPlantRepository.findByIdAndState(dailyRecord.getPetPlantId(),
PetPlantState.IN_PROGRESS)
.orElseThrow(() -> new GreeningGlobalException(
PetPlantExceptionMessage.NOT_FOUND_PET_PLANT));

if (!Objects.equals(petPlant.getUserId(), userId)) {
throw new GreeningGlobalException(
PetPlantExceptionMessage.BAD_REQUEST_PET_PLANT_WRITER);
}

dailyRecord.delete();
}

}
Loading