Skip to content

Commit

Permalink
refactor: history Mapper 삭제 및 컨벤션에 맞도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Kim0914 committed Jan 2, 2024
1 parent 2a8bd71 commit 9512fad
Show file tree
Hide file tree
Showing 21 changed files with 790 additions and 548 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public class AuthController {
@PostMapping("/login")
public ResponseEntity<Void> login(
@RequestParam(name = "code") @NotBlank String code,
HttpServletRequest request) {
HttpServletRequest request
) {
Member loginMember = authService.login(code);

HttpSession session = request.getSession();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,25 @@ public class DictionaryPlantController {

@GetMapping("/{id}")
public ResponseEntity<DictionaryPlantResponse> read(
@PathVariable @Positive(message = "사전 식물 ID는 1이상의 값이어야 합니다.") Long id) {
@PathVariable @Positive(message = "사전 식물 ID는 1이상의 값이어야 합니다.") Long id
) {
DictionaryPlantResponse dictionaryPlantResponse = dictionaryPlantService.read(id);
return ResponseEntity.ok(dictionaryPlantResponse);
}

@GetMapping
public ResponseEntity<DataResponse<List<DictionaryPlantSearchResponse>>> searchDictionaryPlants(
@RequestParam @NotBlank(message = "검색어는 비어있을 수 없습니다.") String name) {
@RequestParam @NotBlank(message = "검색어는 비어있을 수 없습니다.") String name
) {
DataResponse<List<DictionaryPlantSearchResponse>> dataResponse = dictionaryPlantService.search(name);
return ResponseEntity.ok(dataResponse);
}

@PostMapping
public ResponseEntity<Void> create(
@AdminAuth Admin admin,
@RequestBody @Valid DictionaryPlantCreateRequest request) {
@RequestBody @Valid DictionaryPlantCreateRequest request
) {
Long dictionaryPlantId = dictionaryPlantService.create(request);
return ResponseEntity.created(URI.create("/dictionary-plants/" + dictionaryPlantId)).build();
}
Expand All @@ -61,15 +64,17 @@ public ResponseEntity<Void> create(
public ResponseEntity<Void> update(
@AdminAuth Admin admin,
@PathVariable Long id,
@RequestBody @Valid DictionaryPlantUpdateRequest request) {
@RequestBody @Valid DictionaryPlantUpdateRequest request
) {
dictionaryPlantService.update(id, request);
return ResponseEntity.ok().build();
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> delete(
@AdminAuth Admin admin,
@PathVariable Long id) {
@PathVariable Long id
) {
dictionaryPlantService.delete(id);
return ResponseEntity.noContent().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@ public class GardenController {
@PostMapping
public ResponseEntity<Void> create(
@RequestBody @Valid GardenCreateRequest request,
@Auth Member member) {
@Auth Member member
) {
gardenService.create(request, member);
return ResponseEntity.status(HttpStatus.CREATED).build();
}

@GetMapping
public ResponseEntity<GardenResponse> readAll(
@PageableDefault(size = 20) Pageable pageable,
@RequestParam(value = "filter", required = false) List<Long> filters) {
@RequestParam(value = "filter", required = false) List<Long> filters
) {
GardenResponse gardenResponse = gardenService.readAll(pageable, filters);
return ResponseEntity.ok(gardenResponse);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
private static final String JOINER_DELIMITER = ", ";

@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
HttpHeaders headers, HttpStatusCode status,
WebRequest request) {
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException ex,
HttpHeaders headers,
HttpStatusCode status,
WebRequest request
) {
List<ObjectError> allErrors = ex.getBindingResult().getAllErrors();
StringJoiner stringJoiner = new StringJoiner(JOINER_DELIMITER);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public ResponseEntity<HistoryResponse> read(
@RequestParam @Positive(message = "반려 식물 ID는 1이상의 값이어야 합니다.") Long petPlantId,
@PageableDefault(size = 20, sort = "date", direction = DESC) Pageable pageable,
@RequestParam(value = "filter", required = false) List<String> filters,
@Auth Member member) {

@Auth Member member
) {
HistoryResponse historyResponse = historyService.read(petPlantId, pageable, member, filters);
return ResponseEntity.ok(historyResponse);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public class PetPlantController {
@GetMapping("/{id}")
public ResponseEntity<PetPlantResponse> read(
@PathVariable @Positive(message = "반려 식물 ID는 1이상의 값이어야 합니다.") Long id,
@Auth Member member) {
@Auth Member member
) {
PetPlantResponse petPlantResponse = petPlantService.read(id, member);
return ResponseEntity.ok(petPlantResponse);
}
Expand All @@ -46,14 +47,14 @@ public ResponseEntity<PetPlantResponse> read(
public ResponseEntity<Void> create(
@RequestPart(name = "request") @Valid PetPlantCreateRequest request,
@RequestPart(name = "image", required = false) MultipartFile multipartFile,
@Auth Member member) {
@Auth Member member
) {
PetPlantResponse petPlantResponse = petPlantService.create(request, multipartFile, member);
return ResponseEntity.created(URI.create("/pet-plants/" + petPlantResponse.getId())).build();
}

@GetMapping
public ResponseEntity<DataResponse<List<SinglePetPlantResponse>>> readAll(
@Auth Member member) {
public ResponseEntity<DataResponse<List<SinglePetPlantResponse>>> readAll(@Auth Member member) {
DataResponse<List<SinglePetPlantResponse>> response = petPlantService.readAll(member);
return ResponseEntity.ok(response);
}
Expand All @@ -63,15 +64,17 @@ public ResponseEntity<Void> update(
@PathVariable @Positive(message = "반려 식물 ID는 1이상의 값이어야 합니다.") Long id,
@RequestPart(name = "request") @Valid PetPlantUpdateRequest petPlantUpdateRequest,
@RequestPart(name = "image", required = false) MultipartFile multipartFile,
@Auth Member member) {
@Auth Member member
) {
petPlantService.update(id, petPlantUpdateRequest, multipartFile, member);
return ResponseEntity.ok().build();
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> delete(
@PathVariable @Positive(message = "반려 식물 ID는 1이상의 값이어야 합니다.") Long id,
@Auth Member member) {
@Auth Member member
) {
petPlantService.delete(id, member);
return ResponseEntity.noContent().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public class ReminderController {
public ResponseEntity<Void> water(
@PathVariable @Positive(message = "반려 식물 ID는 1이상의 값이어야 합니다.") Long petPlantId,
@RequestBody @Valid ReminderCreateRequest reminderCreateRequest,
@Auth Member member) {
@Auth Member member
) {
reminderService.water(reminderCreateRequest, petPlantId, member);
return ResponseEntity.noContent().build();
}
Expand All @@ -42,7 +43,8 @@ public ResponseEntity<Void> water(
public ResponseEntity<Void> updateNextWaterDate(
@PathVariable @Positive(message = "반려 식물 ID는 1이상의 값이어야 합니다.") Long petPlantId,
@RequestBody @Valid ReminderUpdateRequest reminderUpdateRequest,
@Auth Member member) {
@Auth Member member
) {
reminderService.updateNextWaterDate(reminderUpdateRequest, petPlantId, member);
return ResponseEntity.noContent().build();
}
Expand Down
14 changes: 11 additions & 3 deletions backend/pium/src/main/java/com/official/pium/domain/Garden.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,17 @@ public class Garden extends BaseEntity {
private String manageLevel;

@Builder
private Garden(DictionaryPlant dictionaryPlant, Member member, String nickname, String imageUrl,
GardenPlantState gardenPlantState,
Long daySince, Integer waterCycle, String content, String manageLevel) {
private Garden(
DictionaryPlant dictionaryPlant,
Member member,
String nickname,
String imageUrl,
GardenPlantState gardenPlantState,
Long daySince,
Integer waterCycle,
String content,
String manageLevel
) {
this.dictionaryPlant = dictionaryPlant;
this.member = member;
this.nickname = nickname;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ public void updatePetPlant(
validateLocalDate(birthDate);
validateLocalDate(lastWaterDate);
validateImageUrl(imageUrl);
waterDate.changeLastWaterDate(lastWaterDate);
if (!Objects.equals(waterCycle, this.waterCycle)) {
waterDate.plusNextWaterDate(waterCycle);
}
waterDate.changeLastWaterDate(lastWaterDate);
this.nickname = nickname;
this.petPlantState = petPlantState;
this.waterCycle = waterCycle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,23 @@ public class HistoryEvent {
@NotNull
private final LocalDate date;

public static HistoryEvent of(Long petPlantId, String previous, String current, HistoryType historyType,
LocalDate date) {
return new HistoryEvent(
petPlantId,
previous,
current,
historyType,
date
);
public static HistoryEvent of(
Long petPlantId,
String previous,
String current,
HistoryType historyType,
LocalDate date
) {
return new HistoryEvent(petPlantId, previous, current, historyType, date);
}

public static HistoryEvent of(Long petPlantId, LocalDate previous, LocalDate current, HistoryType historyType,
LocalDate date) {
return new HistoryEvent(
petPlantId,
previous.toString(),
current.toString(),
historyType,
date
);
public static HistoryEvent of(
Long petPlantId,
LocalDate previous,
LocalDate current,
HistoryType historyType,
LocalDate date
) {
return new HistoryEvent(petPlantId, previous.toString(), current.toString(), historyType, date);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ public void savePetPlantHistories(HistoryEvents historyEvents) {
for (HistoryEvent historyEvent : historyEvents.getHistoryEvents()) {
PetPlant petPlant = petPlantRepository.findById(historyEvent.getPetPlantId())
.orElseThrow(
() -> new NoSuchElementException("일치하는 반려 식물이 존재하지 않습니다. id: " + historyEvent.getPetPlantId()));
() -> new NoSuchElementException(
"일치하는 반려 식물이 존재하지 않습니다. id: " + historyEvent.getPetPlantId()));

HistoryCategory historyCategory = categories.stream()
.filter(it -> it.getHistoryType() == historyEvent.getHistoryType())
.findAny()
.orElseThrow(() -> new NoSuchElementException("존재하지 않는 히스토리 타입입니다. type: " + historyEvent.getHistoryType()));
.orElseThrow(() -> new NoSuchElementException(
"존재하지 않는 히스토리 타입입니다. type: " + historyEvent.getHistoryType()));

History history = History.builder()
.petPlant(petPlant)
Expand Down Expand Up @@ -85,7 +87,9 @@ public void updateLastWaterDateHistory(LastWaterDateEvent lastWaterDateEvent) {
final PageRequest TOP_ORDER_BY_DATE_DESC = PageRequest.of(0, 1, Sort.Direction.DESC, "date");

Slice<History> historyAboutLastWaterDate = historyRepository.findAllByPetPlantIdAndHistoryCategoryHistoryType(
lastWaterDateEvent.getPetPlantId(), HistoryType.LAST_WATER_DATE, TOP_ORDER_BY_DATE_DESC);
lastWaterDateEvent.getPetPlantId(),
HistoryType.LAST_WATER_DATE, TOP_ORDER_BY_DATE_DESC
);

validateContentSize(historyAboutLastWaterDate);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import lombok.Builder;
import lombok.Getter;

Expand Down Expand Up @@ -80,10 +81,13 @@ public List<HistoryEvent> generateUpdateHistoryEvents(Long petPlantId, PetPlantH
return events;
}

public LastWaterDateEvent generateUpdateLastWaterDateHistoryEvent(Long petPlantId, LocalDate otherLastWaterDate) {
public Optional<LastWaterDateEvent> generateUpdateLastWaterDateHistoryEvent(
Long petPlantId,
LocalDate otherLastWaterDate
) {
if (!lastWaterDate.equals(otherLastWaterDate.toString())) {
return new LastWaterDateEvent(petPlantId, otherLastWaterDate);
return Optional.of(new LastWaterDateEvent(petPlantId, otherLastWaterDate));
}
return null;
return Optional.empty();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ public Member login(String authorizationCode) {
Long kakaoId = kakaoMemberResponse.getId();

return memberRepository.findByKakaoId(kakaoId)
.orElseGet(() -> memberRepository.save(Member.builder()
.kakaoId(kakaoId)
.build()));
.orElseGet(() -> createMember(kakaoId));
}

private Member createMember(Long kakaoId) {
Member member = Member.builder()
.kakaoId(kakaoId)
.build();
return memberRepository.save(member);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ private Garden createGarden(PetPlant petPlant, GardenCreateRequest request) {
.member(petPlant.getMember())
.nickname(petPlant.getNickname())
.imageUrl(petPlant.getImageUrl())
.gardenPlantState(
toPlantState(petPlant)
)
.gardenPlantState(toPlantState(petPlant))
.daySince(petPlant.calculateDaySince(LocalDate.now()))
.waterCycle(petPlant.getWaterCycle())
.content(request.getContent())
Expand Down
Loading

0 comments on commit 9512fad

Please sign in to comment.