Skip to content

Commit

Permalink
refactor: 서비스 계층에서 컨트롤러 계층으로 dto를 반환하도록 변경 #8
Browse files Browse the repository at this point in the history
  • Loading branch information
shimbaa committed Sep 5, 2024
1 parent b55b408 commit 79ef298
Showing 1 changed file with 10 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package buddyguard.mybuddyguard.weight.service;

import buddyguard.mybuddyguard.weight.domain.Weight;
import buddyguard.mybuddyguard.weight.domain.mapper.WeightMapper;
import buddyguard.mybuddyguard.weight.dto.WeightCreateRequest;
import buddyguard.mybuddyguard.weight.dto.WeightResponse;
import buddyguard.mybuddyguard.weight.repository.WeightRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand All @@ -16,34 +18,30 @@ public class WeightService {
private final WeightRepository weightRepository;

public void createNewWeightRecord(WeightCreateRequest request) {
Weight weight = toEntity(request);
Weight weight = WeightMapper.toEntity(request);

Weight savedWeight = weightRepository.save(weight);

log.info("SAVED WEIGHT : {}", savedWeight);
}

public List<Weight> getAllWeightRecords(Long petId) {
public List<WeightResponse> getAllWeightRecords(Long petId) {

return weightRepository.findAllByPetId(petId);
List<Weight> weights = weightRepository.findAllByPetId(petId);

return WeightMapper.toResponseList(weights);
}

public Weight getDetailWeightRecord(Long id) {
public WeightResponse getDetailWeightRecord(Long id) {
// Optional 처리 고민
// 즉 값이 없을때 어떻게 하는지에 대한 고민
// 방법1) 예외를 던지게 한다. 방법2) 기본값을 주게 한다. ex) orElse(new Weight());
// + 예외를 던지는 걸로 가려면, 어떤 상황에 어떤 예외를 던질지 정해야 한다.
// ex) id로 값을 찾는 요청인데 없다면 ResourceNotFoundException

return weightRepository.findById(id)
Weight weight = weightRepository.findById(id)
.orElseThrow(RuntimeException::new);
}

private Weight toEntity(WeightCreateRequest request) {
return Weight.builder()
.petId(request.petId())
.recordedAt(request.recordedAt())
.weight(request.weight())
.build();
return WeightMapper.toResponse(weight);
}
}

0 comments on commit 79ef298

Please sign in to comment.