-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Week3 강철 멘토님 코드 리뷰 반영하기 #28
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ | |
import dbdr.domain.careworker.dto.request.CareworkerRequestDTO; | ||
import dbdr.domain.careworker.dto.response.CareworkerResponseDTO; | ||
import dbdr.domain.careworker.repository.CareworkerRepository; | ||
import dbdr.exception.IdNotFoundException; | ||
import dbdr.exception.NotUniqueException; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
@@ -20,41 +22,39 @@ public class CareworkerService { | |
|
||
@Transactional(readOnly = true) | ||
public List<CareworkerResponseDTO> getAllCareworkers() { | ||
return careworkerRepository.findAll().stream() | ||
.map(this::toResponseDTO) | ||
.collect(Collectors.toList()); | ||
return careworkerRepository.findAll().stream().map(this::toResponseDTO) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public List<CareworkerResponseDTO> getCareworkersByInstitution(Long institutionId) { | ||
return careworkerRepository.findByInstitutionId(institutionId).stream() | ||
.map(this::toResponseDTO) | ||
.collect(Collectors.toList()); | ||
.map(this::toResponseDTO).collect(Collectors.toList()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 개행을 삭제하신 이유를 알 수 있을까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아마 ctrl+l 로 정리하는 단축키 사용하면서 저렇게 된 것 같습니다..! 일부러 하지는 않았어요. |
||
} | ||
|
||
@Transactional(readOnly = true) | ||
public CareworkerResponseDTO getCareworkerById(Long id) { | ||
Careworker careworker = careworkerRepository.findById(id) | ||
.orElseThrow(() -> new IllegalArgumentException("요양보호사를 찾을 수 없습니다.")); | ||
Careworker careworker = findCareworkerById(id); | ||
return toResponseDTO(careworker); | ||
} | ||
|
||
@Transactional | ||
public CareworkerResponseDTO createCareworker(CareworkerRequestDTO careworkerRequestDTO) { | ||
Careworker careworker = new Careworker( | ||
careworkerRequestDTO.getInstitutionId(), | ||
careworkerRequestDTO.getName(), | ||
careworkerRequestDTO.getEmail(), | ||
careworkerRequestDTO.getPhone() | ||
); | ||
emailExists(careworkerRequestDTO.getEmail()); | ||
|
||
Careworker careworker = new Careworker(careworkerRequestDTO.getInstitutionId(), | ||
careworkerRequestDTO.getName(), careworkerRequestDTO.getEmail(), | ||
careworkerRequestDTO.getPhone()); | ||
careworkerRepository.save(careworker); | ||
return toResponseDTO(careworker); | ||
} | ||
|
||
@Transactional | ||
public CareworkerResponseDTO updateCareworker(Long id, CareworkerRequestDTO careworkerRequestDTO) { | ||
Careworker careworker = careworkerRepository.findById(id) | ||
.orElseThrow(() -> new IllegalArgumentException("요양보호사를 찾을 수 없습니다.")); | ||
public CareworkerResponseDTO updateCareworker(Long id, | ||
CareworkerRequestDTO careworkerRequestDTO) { | ||
emailExists(careworkerRequestDTO.getEmail()); | ||
hyyyh0x marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Careworker careworker = findCareworkerById(id); | ||
|
||
careworker.updateCareworker(careworkerRequestDTO); | ||
careworkerRepository.save(careworker); | ||
|
@@ -64,19 +64,24 @@ public CareworkerResponseDTO updateCareworker(Long id, CareworkerRequestDTO care | |
|
||
@Transactional | ||
public void deleteCareworker(Long id) { | ||
Careworker careworker = careworkerRepository.findById(id) | ||
.orElseThrow(() -> new IllegalArgumentException("요양보호사를 찾을 수 없습니다.")); | ||
Careworker careworker = findCareworkerById(id); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 중복 코드 제거 너무 좋아요! 수고하셨습니당 |
||
careworker.deactivate(); | ||
careworkerRepository.delete(careworker); | ||
} | ||
|
||
private Careworker findCareworkerById(Long id) { | ||
return careworkerRepository.findById(id) | ||
.orElseThrow(() -> new IdNotFoundException("요양보호사를 찾을 수 없습니다.")); | ||
} | ||
|
||
private void emailExists(String email) { | ||
if (careworkerRepository.existsByEmail(email)) { | ||
throw new NotUniqueException("존재하는 이메일입니다."); | ||
} | ||
} | ||
|
||
private CareworkerResponseDTO toResponseDTO(Careworker careworker) { | ||
return new CareworkerResponseDTO( | ||
careworker.getId(), | ||
careworker.getInstitutionId(), | ||
careworker.getName(), | ||
careworker.getEmail(), | ||
careworker.getPhone() | ||
); | ||
return new CareworkerResponseDTO(careworker.getId(), careworker.getInstitutionId(), | ||
careworker.getName(), careworker.getEmail(), careworker.getPhone()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
버전관리 이렇게 적용한 것 좋은것같아용 :)