Skip to content
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

Merged
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 @@ -13,15 +13,15 @@
import java.util.List;

@RestController
@RequestMapping("/v1/careworker")
@RequestMapping("/${spring.app.version}/careworker")
@RequiredArgsConstructor
public class CareworkerController {

private final CareworkerService careworkerService;

@GetMapping
public ResponseEntity<List<CareworkerResponseDTO>> getAllCareworkers(
@RequestParam(value = "institutionId", required = false) Long institutionId) {
@RequestParam(value = "institutionId", required = false) Long institutionId) {
List<CareworkerResponseDTO> careworkerList;
if (institutionId != null) {
careworkerList = careworkerService.getCareworkersByInstitution(institutionId);
Expand All @@ -42,13 +42,16 @@ public ResponseEntity<CareworkerResponseDTO> getCareworkerById(
public ResponseEntity<CareworkerResponseDTO> createCareworker(
@Valid @RequestBody CareworkerRequestDTO careworkerDTO) {
CareworkerResponseDTO newCareworker = careworkerService.createCareworker(careworkerDTO);
return ResponseEntity.created(URI.create("/v1/careworker/" + newCareworker.getId()))
.body(newCareworker);
return ResponseEntity.created(
URI.create("/${spring.app.version}/careworker/" + newCareworker.getId()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

버전관리 이렇게 적용한 것 좋은것같아용 :)

.body(newCareworker);
}

@PutMapping("/{id}")
public ResponseEntity<CareworkerResponseDTO> updateCareworker(@PathVariable Long id, @Valid @RequestBody CareworkerRequestDTO careworkerDTO) {
CareworkerResponseDTO updatedCareworker = careworkerService.updateCareworker(id, careworkerDTO);
public ResponseEntity<CareworkerResponseDTO> updateCareworker(@PathVariable Long id,
@Valid @RequestBody CareworkerRequestDTO careworkerDTO) {
CareworkerResponseDTO updatedCareworker = careworkerService.updateCareworker(id,
careworkerDTO);
return ResponseEntity.ok(updatedCareworker);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
@SQLDelete(sql = "UPDATE careworkers SET is_active = false WHERE id = ?")
@SQLRestriction("is_active = true")
public class Careworker extends BaseEntity {

@Column(unique = true)
private String loginId;

Expand All @@ -33,7 +34,6 @@ public class Careworker extends BaseEntity {
@Column(nullable = false)
private String phone;


public Careworker(Long institutionId, String name, String email, String phone) {
this.institutionId = institutionId;
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
public interface CareworkerRepository extends JpaRepository<Careworker, Long> {

List<Careworker> findByInstitutionId(Long institutionId);

boolean existsByEmail(String email);
}
55 changes: 30 additions & 25 deletions src/main/java/dbdr/domain/careworker/service/CareworkerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

개행을 삭제하신 이유를 알 수 있을까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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());

Careworker careworker = findCareworkerById(id);

careworker.updateCareworker(careworkerRequestDTO);
careworkerRepository.save(careworker);
Expand All @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/v1/admin/guardian")
@RequestMapping("/${spring.app.version}/admin/guardian")
@RequiredArgsConstructor
public class GuardianAdminController {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/v1/guardian")
@RequestMapping("/${spring.app.version}/guardian")
@RequiredArgsConstructor
public class GuardianController {

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/dbdr/domain/guardian/entity/Guardian.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dbdr.domain.guardian.entity;

import jakarta.validation.constraints.Pattern;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLRestriction;
Expand All @@ -26,6 +27,7 @@ public class Guardian extends BaseEntity {
private String loginPassword;

@Column(nullable = false, unique = true)
@Pattern(regexp = "010\\d{8}")
private String phone;

@Column(nullable = false, length = 50)
Expand All @@ -37,7 +39,6 @@ public Guardian(String phone, String name) {
this.name = name;
}

@Builder
public void updateGuardian(String phone, String name) {
this.phone = phone;
this.name = name;
Expand Down
27 changes: 19 additions & 8 deletions src/main/java/dbdr/domain/guardian/service/GuardianService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import dbdr.domain.guardian.dto.request.GuardianRequest;
import dbdr.domain.guardian.dto.response.GuardianResponse;
import dbdr.domain.guardian.repository.GuardianRepository;
import dbdr.exception.IdNotFoundException;
import dbdr.exception.NotUniqueException;
import lombok.RequiredArgsConstructor;

import java.util.List;
Expand All @@ -16,22 +18,26 @@ public class GuardianService {
private final GuardianRepository guardianRepository;

public GuardianResponse getGuardianById(Long guardianId) {
Guardian guardian = guardianRepository.findById(guardianId).orElseThrow();
Guardian guardian = findGuardianById(guardianId);
return new GuardianResponse(guardian.getPhone(), guardian.getName(), guardian.isActive());
}

public GuardianResponse updateGuardianById(Long guardianId,
GuardianRequest guardianRequest) {
Guardian guardian = guardianRepository.findById(guardianId).orElseThrow();
phoneNumberExists(guardianRequest.phone());

Guardian guardian = findGuardianById(guardianId);
guardian.updateGuardian(guardianRequest.phone(), guardianRequest.name());
guardianRepository.save(guardian);
return new GuardianResponse(guardianRequest.phone(), guardianRequest.name(), guardian.isActive());
return new GuardianResponse(guardianRequest.phone(), guardianRequest.name(),
guardian.isActive());
}

public List<GuardianResponse> getAllGuardian() {
List<Guardian> guardianList = guardianRepository.findAll();
return guardianList.stream()
.map(guardian -> new GuardianResponse(guardian.getPhone(), guardian.getName(), guardian.isActive()))
.map(guardian -> new GuardianResponse(guardian.getPhone(), guardian.getName(),
guardian.isActive()))
.toList();
}

Expand All @@ -43,14 +49,19 @@ public GuardianResponse addGuardian(GuardianRequest guardianRequest) {
}

public void deleteGuardianById(Long guardianId) {
Guardian guardian = guardianRepository.findById(guardianId).orElseThrow();
Guardian guardian = findGuardianById(guardianId);
guardian.deactivate();
guardianRepository.save(guardian);
guardianRepository.delete(guardian);
}

private Guardian findGuardianById(Long guardianId) {
return guardianRepository.findById(guardianId)
.orElseThrow(() -> new IdNotFoundException("존재하지 않는 보호자입니다."));
}

private void phoneNumberExists(String phone) {
if(guardianRepository.existsByPhone(phone)){
throw new IllegalArgumentException("존재하는 전화번호입니다.");
if (guardianRepository.existsByPhone(phone)) {
throw new NotUniqueException("존재하는 전화번호입니다.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.List;

@RestController
@RequestMapping("/v1/recipient")
@RequestMapping("/${spring.app.version}/recipient")
public class RecipientController {

private final RecipientService recipientService;
Expand All @@ -35,7 +35,7 @@ public ResponseEntity<RecipientResponseDTO> getRecipientById(@PathVariable Long
@PostMapping
public ResponseEntity<RecipientResponseDTO> createRecipient(@Valid @RequestBody RecipientRequestDTO recipientDTO) {
RecipientResponseDTO newRecipient = recipientService.createRecipient(recipientDTO);
return ResponseEntity.created(URI.create("/v1/recipients/" + newRecipient.getId()))
return ResponseEntity.created(URI.create("/${spring.app.version}/recipient/" + newRecipient.getId()))
.body(newRecipient);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
import org.springframework.data.jpa.repository.JpaRepository;

public interface RecipientRepository extends JpaRepository<Recipient, Long> {

boolean existsByCareNumber(String careNumber);
}
Loading