Skip to content

Commit

Permalink
fix: #114 category 하위 care 없을 경우 삭제 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
psychology50 committed Feb 23, 2024
1 parent 94b25e6 commit 368da80
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ public ResponseEntity<?> getCareCategoryNames(@PathVariable("pet_id") Long petId
public ResponseEntity<?> updateCare(
@PathVariable("pet_id") Long petId,
@PathVariable("care_id") Long careId,
@RequestBody @Valid CareSaveReq.UpdateRequest request,
@AuthenticationPrincipal CustomUserDetails user
@RequestBody @Valid CareSaveReq.UpdateRequest request
) {
careUseCase.updateCare(careId, request);
return ResponseEntity.ok(SuccessResponse.noContent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import com.vdurmont.emoji.EmojiLoader;
import com.vdurmont.emoji.EmojiParser;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import kr.co.fitapet.domain.domains.care.domain.Care;
import kr.co.fitapet.domain.domains.care.domain.CareCategory;
Expand Down Expand Up @@ -60,7 +58,7 @@ public record CareInfoDto(
@Schema(description = "케어 이름", example = "1")
@NotBlank
String careName,
@NotNull
@NotNull @Valid
List<CareDateDto> careDates,
@Schema(description = "제한 시간(분 단위) - 제한 시간 없는 경우 0", example = "30")
@NotNull
Expand All @@ -70,15 +68,14 @@ public Care toCare(CareCategory category) {
return Care.of(EmojiParser.parseToAliases(careName), limitTime, category);
}

public List<CareDate> toCareDateEntity() {
return careDates.stream()
.map(careDateDto -> CareDate.of(careDateDto.week(), careDateDto.time()))
.toList();
@Override
public String careName() {
return EmojiParser.parseToAliases(careName);
}
}

@Schema(description = "케어 등록 - 케어 날짜")
private record CareDateDto(
public record CareDateDto(
@Schema(description = "요일", example = "mon")
@NotNull
WeekType week,
Expand All @@ -88,6 +85,9 @@ private record CareDateDto(
@NotNull
LocalTime time
) {
public CareDate toEntity(Care care) {
return CareDate.of(week, time, care);
}
}

@Schema(description = "케어 동물 추가 - 기존 카테고리에 묶을 거면 categoryId, 새로 만들 거면 0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private void persistAboutCare(
List<Care> cares = createCares(careInfoDto, categories);
careSaveService.saveCares(cares);

List<CareDate> dates = createCareDates(careInfoDto, cares);
List<CareDate> dates = createCareDates(careInfoDto.careDates(), cares);
careSaveService.saveCareDates(dates);
}

Expand Down Expand Up @@ -85,11 +85,11 @@ private List<Care> createCares(CareSaveReq.CareInfoDto careInfoDto, List<CareCat
.toList();
}

private List<CareDate> createCareDates(CareSaveReq.CareInfoDto careInfoDto, List<Care> cares) {
private List<CareDate> createCareDates(List<CareSaveReq.CareDateDto> dates, List<Care> cares) {
return cares.stream()
.flatMap(care -> careInfoDto.toCareDateEntity().stream()
.peek(date -> date.updateCare(care)))
.toList();
.flatMap(care -> dates.stream()
.map(date -> date.toEntity(care))
).toList();
}

@Transactional(readOnly = true)
Expand All @@ -113,11 +113,8 @@ private Stream<CareInfoRes.CareDto> mapToCareDtos(Care care, LocalDateTime now)
}

@Transactional
public void updateCareCategory(Care care, CareCategory category, CareSaveReq.CategoryDto requestCategory) {
// careCategory 갱신 여부
// request.category().categoryId() == category.getId() -> 카테고리 변경 없음
// request.category().categoryId() == 0L -> 카테고리 신규 생성 후 변경
// request.category().categoryId() != category.getId() -> 다른 카테고리 이동 후, 기존 카테고리에 케어가 없을 경우 삭제
public void updateCareCategory(Care care, CareSaveReq.CategoryDto requestCategory) {
CareCategory category = care.getCareCategory();
if (requestCategory.categoryId() == 0L) {
CareCategory newCategory = CareCategory.of(requestCategory.categoryName());
newCategory.updatePet(category.getPet());
Expand All @@ -126,47 +123,41 @@ public void updateCareCategory(Care care, CareCategory category, CareSaveReq.Cat
} else if (!requestCategory.categoryId().equals(category.getId())) {
CareCategory newCategory = careSearchService.findCareCategoryById(requestCategory.categoryId());
care.updateCareCategory(newCategory);

deleteCareCategoryIfEmptyCare(category);
}
deleteCareCategoryIfEmptyCare(category);
}

@Transactional(readOnly = true)
public void updateCareDates(Long careId, CareSaveReq.CareInfoDto requestCare) {
// careDates 갱신 여부
// request.care().careDates() -> 기존 careDates와 비교하여 추가, 삭제, 수정
// week, time이 같은 careDate가 존재하면 유지
// 같은 week, time이 다르면 수정 후 오늘 자 로그 있으면 삭제
// 기존 week가 없으면 추가
public void updateCareDates(Care care, List<CareSaveReq.CareDateDto> requestCareDates) {
Map<WeekType, Map<LocalTime, CareDate>> currentCareDatesMap = new HashMap<>();
List<CareDate> currentCareDates = careSearchService.findCareDatesFromCareId(careId);
List<CareDate> currentCareDates = careSearchService.findCareDatesFromCareId(care.getId());
for (CareDate currentCareDate : currentCareDates) {
currentCareDatesMap.computeIfAbsent(currentCareDate.getWeek(), k -> new HashMap<>()).put(currentCareDate.getCareTime(), currentCareDate);
}

List<CareDate> requestCareDates = requestCare.toCareDateEntity();
List<CareDate> newCareDates = new ArrayList<>();
for (CareDate requestCareDate : requestCareDates) {
Map<LocalTime, CareDate> weekTypeMap = currentCareDatesMap.getOrDefault(requestCareDate.getWeek(), new HashMap<>());
CareDate currentCareDate = weekTypeMap.get(requestCareDate.getCareTime());
for (CareSaveReq.CareDateDto requestCareDate : requestCareDates) {
Map<LocalTime, CareDate> weekTypeMap = currentCareDatesMap.getOrDefault(requestCareDate.week(), new HashMap<>());
CareDate currentCareDate = weekTypeMap.get(requestCareDate.time());

if (currentCareDate == null) {
newCareDates.add(requestCareDate);
} else if (!currentCareDate.getCareTime().equals(requestCareDate.getCareTime())) {
currentCareDate.updateCareTime(requestCareDate.getCareTime());
newCareDates.add(requestCareDate.toEntity(care));
} else if (!currentCareDate.getCareTime().equals(requestCareDate.time())) {
currentCareDate.updateCareTime(requestCareDate.time());
if (careLogSearchService.existsByCareDateIdOnLogDate(currentCareDate.getId(), LocalDateTime.now())) {
CareLog careLog = careLogSearchService.findByCareDateIdOnLogDate(currentCareDate.getId(), LocalDateTime.now());
careLogSaveService.delete(careLog);
}
}

weekTypeMap.remove(requestCareDate.getCareTime());
weekTypeMap.remove(requestCareDate.time());
}
careSaveService.saveCareDates(newCareDates);

List<CareDate> deleteCareDates = currentCareDatesMap.values().stream()
.flatMap(map -> map.values().stream())
.toList();

careSaveService.deleteCareDates(deleteCareDates);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ public void updateCare(Long careId, CareSaveReq.UpdateRequest request) {
Care care = careSearchService.findCareById(careId);
care.updateCare(request.care().careName(), request.care().limitTime());

CareCategory category = care.getCareCategory();
careMapper.updateCareCategory(care, category, request.category());
careMapper.updateCareDates(careId, request.care());
careMapper.updateCareCategory(care, request.category());
careMapper.updateCareDates(care, request.care().careDates());
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@ public class CareDate extends DateAuditable {
private List<CareLog> careLogs = new ArrayList<>();

@Builder
private CareDate(WeekType week, LocalTime careTime) {
private CareDate(WeekType week, LocalTime careTime, Care care) {
this.week = week;
this.careTime = careTime;
updateCare(care);
}

public static CareDate of(WeekType week, LocalTime careTime) {
public static CareDate of(WeekType week, LocalTime careTime, Care care) {
return CareDate.builder()
.week(week)
.careTime(careTime)
.care(care)
.build();
}

Expand Down

0 comments on commit 368da80

Please sign in to comment.