-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat : SeniorRepository 인터페이스 추가 * feat : GuardService 클래스 추가 * feat : SeniorNotFoundException 클래스 추가 * feat : GuardControllerAdvice 클래스 추가 * feat : GuardController에 있는 api 구현 * feat : updateMember 메소드 추가 * feat : mapToResponse 메소드, updateSenior 메소드 추가 및 getter 추가 * feat : SeniorTest 클래스 추가 Senior 엔티티에 대한 테스트 코드 * feat : SeniorRepositoryTest 클래스 추가 SeniorRepository에 대한 테스트 코드 * refactor : basePackage 추가 * feat : findByIsSinitto 메소드 추가 모든 보호자 불러오는데 사용 * refactor : Transactional 어노테이션 추가 및 업데이트 시 save 메소드 제거 * refactor : responseEntity 리턴하도록 변경 * refactor : 주석 제거
- Loading branch information
Showing
10 changed files
with
343 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/com/example/sinitto/guard/controller/GuardControllerAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.example.sinitto.guard.controller; | ||
|
||
import com.example.sinitto.guard.exception.SeniorNotFoundException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ProblemDetail; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
import java.net.URI; | ||
|
||
@RestControllerAdvice(basePackages = "com.example.sinitto.guard") | ||
public class GuardControllerAdvice { | ||
@ExceptionHandler(SeniorNotFoundException.class) | ||
public ResponseEntity<ProblemDetail> handleSeniorNotFoundException(SeniorNotFoundException e){ | ||
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, e.getMessage()); | ||
problemDetail.setType(URI.create("/error/senior-not-found")); | ||
problemDetail.setTitle("Senior Not Found"); | ||
|
||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(problemDetail); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/example/sinitto/guard/exception/SeniorNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.example.sinitto.guard.exception; | ||
|
||
public class SeniorNotFoundException extends RuntimeException { | ||
public SeniorNotFoundException(String message) { | ||
super(message); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/example/sinitto/guard/repository/SeniorRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.example.sinitto.guard.repository; | ||
|
||
import com.example.sinitto.member.entity.Senior; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface SeniorRepository extends JpaRepository<Senior, Long> { | ||
List<Senior> findByMemberId(Long memberId); | ||
Optional<Senior> findByIdAndMemberId(Long Id, Long memberId); | ||
} |
108 changes: 108 additions & 0 deletions
108
src/main/java/com/example/sinitto/guard/service/GuardService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package com.example.sinitto.guard.service; | ||
|
||
import com.example.sinitto.guard.dto.GuardRequest; | ||
import com.example.sinitto.guard.dto.GuardResponse; | ||
import com.example.sinitto.guard.dto.SeniorRequest; | ||
import com.example.sinitto.guard.dto.SeniorResponse; | ||
import com.example.sinitto.guard.exception.SeniorNotFoundException; | ||
import com.example.sinitto.guard.repository.SeniorRepository; | ||
import com.example.sinitto.member.entity.Member; | ||
import com.example.sinitto.member.entity.Senior; | ||
import com.example.sinitto.member.repository.MemberRepository; | ||
import org.springframework.stereotype.Service; | ||
import com.example.sinitto.member.exception.MemberNotFoundException; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
public class GuardService { | ||
private final MemberRepository memberRepository; | ||
private final SeniorRepository seniorRepository; | ||
|
||
public GuardService(MemberRepository memberRepository, SeniorRepository seniorRepository){ | ||
this.memberRepository = memberRepository; | ||
this.seniorRepository = seniorRepository; | ||
} | ||
|
||
@Transactional | ||
public GuardResponse readGuard(Long memberId){ | ||
Member member = memberRepository.findById(memberId).orElseThrow( | ||
() -> new MemberNotFoundException("이메일에 해당하는 멤버를 찾을 수 없습니다.") | ||
); | ||
|
||
return new GuardResponse(member.getName(), member.getEmail(), member.getPhoneNumber()); | ||
} | ||
|
||
@Transactional | ||
public void updateGuard(Long memberId, GuardRequest guardRequest){ | ||
Member member = memberRepository.findById(memberId).orElseThrow( | ||
() -> new MemberNotFoundException("이메일에 해당하는 멤버를 찾을 수 없습니다.") | ||
); | ||
|
||
member.updateMember(guardRequest.name(), guardRequest.email(), guardRequest.phoneNumber()); | ||
} | ||
|
||
@Transactional | ||
public void deleteGuard(Long memberId){ | ||
Member member = memberRepository.findById(memberId).orElseThrow( | ||
() -> new MemberNotFoundException("이메일에 해당하는 멤버를 찾을 수 없습니다.") | ||
); | ||
|
||
memberRepository.delete(member); | ||
} | ||
|
||
@Transactional | ||
public void createSenior(Long memberId, SeniorRequest seniorRequest){ | ||
Member member = memberRepository.findById(memberId).orElseThrow( | ||
() -> new MemberNotFoundException("이메일에 해당하는 멤버를 찾을 수 없습니다.") | ||
); | ||
|
||
Senior senior = new Senior(seniorRequest.seniorName(), seniorRequest.seniorPhoneNumber(), member); | ||
|
||
seniorRepository.save(senior); | ||
} | ||
|
||
@Transactional | ||
public List<SeniorResponse> readSeniors(Long memberId){ | ||
List<Senior> senior = seniorRepository.findByMemberId(memberId); | ||
|
||
return senior.stream().map(Senior::mapToResponse).toList(); | ||
} | ||
|
||
@Transactional | ||
public SeniorResponse readOneSenior(Long memberId, Long seniorId){ | ||
Senior senior = seniorRepository.findByIdAndMemberId(seniorId, memberId).orElseThrow( | ||
() -> new SeniorNotFoundException("이메일에 해당하는 시니어를 찾을 수 없습니다.") | ||
); | ||
|
||
return new SeniorResponse(senior.getId(), senior.getName(), senior.getPhoneNumber()); | ||
} | ||
|
||
@Transactional | ||
public void updateSenior(Long memberId, Long seniorId, SeniorRequest seniorRequest){ | ||
Senior senior = seniorRepository.findByIdAndMemberId(seniorId, memberId).orElseThrow( | ||
() -> new SeniorNotFoundException("이메일에 해당하는 시니어를 찾을 수 없습니다.") | ||
); | ||
|
||
senior.updateSenior(seniorRequest.seniorName(), seniorRequest.seniorPhoneNumber()); | ||
} | ||
|
||
@Transactional | ||
public void deleteSenior(Long memberId, Long seniorId){ | ||
Senior senior = seniorRepository.findByIdAndMemberId(seniorId, memberId).orElseThrow( | ||
() -> new SeniorNotFoundException("이메일에 해당하는 시니어를 찾을 수 없습니다.") | ||
); | ||
|
||
seniorRepository.delete(senior); | ||
} | ||
|
||
@Transactional | ||
public List<GuardResponse> readAllGuards(){ | ||
List<Member> members = memberRepository.findByIsSinitto(false); | ||
|
||
return members.stream() | ||
.map(m -> new GuardResponse(m.getName(), m.getEmail(), m.getPhoneNumber())) | ||
.toList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/test/java/com/example/sinitto/guard/entity/SeniorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.example.sinitto.guard.entity; | ||
|
||
import com.example.sinitto.guard.dto.SeniorResponse; | ||
import com.example.sinitto.member.entity.Member; | ||
import com.example.sinitto.member.entity.Senior; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
public class SeniorTest { | ||
private Member member; | ||
private Senior senior; | ||
|
||
@BeforeEach | ||
void setup(){ | ||
member = new Member( | ||
"test", | ||
"01012345678", | ||
"test@test.com", | ||
true | ||
); | ||
senior = new Senior("testSenior", "01000000000", member); | ||
} | ||
|
||
@Test | ||
@DisplayName("Senior 엔티티 생성자 테스트") | ||
void counstructorTest(){ | ||
assertThat(senior.getId()).isNull(); | ||
assertThat(senior.getName()).isEqualTo("testSenior"); | ||
assertThat(senior.getPhoneNumber()).isEqualTo("01000000000"); | ||
assertThat(senior.getMember()).isEqualTo(member); | ||
} | ||
|
||
@Test | ||
@DisplayName("updateSenior 메소드 테스트") | ||
void updateSeniorTest(){ | ||
senior.updateSenior("updateSenior", "01011111111"); | ||
assertThat(senior.getName()).isEqualTo("updateSenior"); | ||
assertThat(senior.getPhoneNumber()).isEqualTo("01011111111"); | ||
} | ||
|
||
@Test | ||
@DisplayName("mapToResponse 메소드 테스트") | ||
void mapToResponseTest(){ | ||
SeniorResponse response = senior.mapToResponse(); | ||
assertThat(response.seniorId()).isNull(); | ||
assertThat(response.seniorName()).isEqualTo(senior.getName()); | ||
assertThat(response.seniorPhoneNumber()).isEqualTo(senior.getPhoneNumber()); | ||
} | ||
} |
Oops, something went wrong.