Skip to content

Commit

Permalink
Refactor: 가이드라인 관련 로직 수정 {프론트 요청) (#103)
Browse files Browse the repository at this point in the history
* deploy: 가이드라인 더미데이터 추가

* feat: 콜백과 가이드라인의 시니어가 같지 않을 때 발생하는 예외 구현

* refactor: 타입별 전체 가이드라인 및 특정 가이드라인 조회 시 검증 로직 추가

* refactor: 가이드라인 조회 api 매개변수 수정

* refactor: Reformat code

* refactor: 가이드라인 response dto에 id 추가 및 서비스단 수정

* refactor: 중복 주석 삭제

* refactor: 서비스단에서 사용하지 않는 레퍼지토리 선언 삭제
  • Loading branch information
eunsoni authored Oct 23, 2024
1 parent 70f2f3c commit ca76a5c
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 20 deletions.
125 changes: 125 additions & 0 deletions src/main/java/com/example/sinitto/common/dummy/InitialData.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public class GuardGuidelineController {

private final GuardGuidelineService guardGuidelineService;

public GuardGuidelineController(GuardGuidelineService guardGuidelineService) {this.guardGuidelineService = guardGuidelineService;}
public GuardGuidelineController(GuardGuidelineService guardGuidelineService) {
this.guardGuidelineService = guardGuidelineService;
}

@Operation(summary = "가이드라인 추가", description = "보호자가 시니어별 가이드라인을 추가합니다.")
@PostMapping
Expand All @@ -43,15 +45,15 @@ public ResponseEntity<String> updateGuardGuideline(@MemberId Long memberId, @Pat

@Operation(summary = "모든 가이드라인 조회(시니어별로)", description = "보호자가 가이드라인 수정을 위해 시니어별로 모든 가이드라인을 요청할 때 필요합니다.")
@GetMapping("/{seniorId}")
public ResponseEntity<List<GuardGuidelineResponse>> getAllGuardGuidelinesBySenior(@PathVariable Long seniorId) {
public ResponseEntity<List<GuardGuidelineResponse>> getAllGuardGuidelinesBySenior(@MemberId Long memberId, @PathVariable Long seniorId) {

return ResponseEntity.ok(guardGuidelineService.readAllGuardGuidelinesBySenior(seniorId));
return ResponseEntity.ok(guardGuidelineService.readAllGuardGuidelinesBySenior(memberId, seniorId));
}

@Operation(summary = "특정 가이드라인 조회", description = "보호자용 API입니다.")
@GetMapping("/{guidelineId}")
public ResponseEntity<GuardGuidelineResponse> getGuardGuideline(@PathVariable Long guidelineId) {
return ResponseEntity.ok(guardGuidelineService.readGuardGuideline(guidelineId));
@GetMapping("/{callbackId}/{guidelineId}")
public ResponseEntity<GuardGuidelineResponse> getGuardGuideline(@PathVariable Long callbackId, @PathVariable Long guidelineId) {
return ResponseEntity.ok(guardGuidelineService.readGuardGuideline(callbackId, guidelineId));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public record GuardGuidelineRequest(
GuardGuideline.Type type,
String title,
String content
) {}
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.example.sinitto.guardGuideline.entity.GuardGuideline;

public record GuardGuidelineResponse(
Long Id,
GuardGuideline.Type type,
String title,
String content) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.sinitto.guardGuideline.exception;

public class GuardGuidelineNotFoundException extends RuntimeException {
public GuardGuidelineNotFoundException(String message) {super(message);}
public GuardGuidelineNotFoundException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.sinitto.guardGuideline.exception;

public class SeniorAndGuardGuidelineMismatchException extends RuntimeException {
public SeniorAndGuardGuidelineMismatchException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.example.sinitto.guardGuideline.service;


import com.example.sinitto.callback.entity.Callback;
import com.example.sinitto.callback.exception.NotExistCallbackException;
import com.example.sinitto.callback.repository.CallbackRepository;
import com.example.sinitto.guard.exception.SeniorNotFoundException;
import com.example.sinitto.guard.repository.SeniorRepository;
import com.example.sinitto.guardGuideline.dto.GuardGuidelineRequest;
import com.example.sinitto.guardGuideline.dto.GuardGuidelineResponse;
import com.example.sinitto.guardGuideline.entity.GuardGuideline;
import com.example.sinitto.guardGuideline.entity.GuardGuideline.Type;
import com.example.sinitto.guardGuideline.exception.GuardGuidelineNotFoundException;
import com.example.sinitto.guardGuideline.exception.SeniorAndGuardGuidelineMismatchException;
import com.example.sinitto.guardGuideline.exception.SeniorAndGuardMemberMismatchException;
import com.example.sinitto.guardGuideline.repository.GuardGuidelineRepository;
import com.example.sinitto.member.entity.Senior;
Expand All @@ -21,10 +25,12 @@ public class GuardGuidelineService {

private final GuardGuidelineRepository guardGuidelineRepository;
private final SeniorRepository seniorRepository;
private final CallbackRepository callbackRepository;

public GuardGuidelineService (GuardGuidelineRepository guardGuidelineRepository, SeniorRepository seniorRepository){
public GuardGuidelineService(GuardGuidelineRepository guardGuidelineRepository, SeniorRepository seniorRepository, CallbackRepository callbackRepository) {
this.guardGuidelineRepository = guardGuidelineRepository;
this.seniorRepository = seniorRepository;
this.callbackRepository = callbackRepository;
}

@Transactional
Expand All @@ -40,18 +46,18 @@ public void addGuardGuideline(Long memberId, GuardGuidelineRequest guardGuidelin
}

@Transactional(readOnly = true)
public List<GuardGuidelineResponse> readAllGuardGuidelinesByCategory(Long seniorId, Type type){
public List<GuardGuidelineResponse> readAllGuardGuidelinesByCategory(Long seniorId, Type type) {
List<GuardGuideline> guardGuidelines = guardGuidelineRepository.findBySeniorIdAndType(seniorId, type);

return guardGuidelines.stream()
.map(m -> new GuardGuidelineResponse(m.getType(), m.getTitle(), m.getContent()))
.map(m -> new GuardGuidelineResponse(m.getId(), m.getType(), m.getTitle(), m.getContent()))
.toList();
}

@Transactional
public void updateGuardGuideline(Long memberId, Long guidelineId, GuardGuidelineRequest guardGuidelineRequest) {
GuardGuideline guardGuideline = guardGuidelineRepository.findById(guidelineId).orElseThrow(
()-> new GuardGuidelineNotFoundException("해당 가이드라인이 존재하지 않습니다.")
() -> new GuardGuidelineNotFoundException("해당 가이드라인이 존재하지 않습니다.")
);

Senior senior = seniorRepository.findById(guardGuidelineRequest.seniorId()).orElseThrow(
Expand All @@ -66,21 +72,32 @@ public void updateGuardGuideline(Long memberId, Long guidelineId, GuardGuideline
}

@Transactional(readOnly = true)
public List<GuardGuidelineResponse> readAllGuardGuidelinesBySenior(Long seniorId){
public List<GuardGuidelineResponse> readAllGuardGuidelinesBySenior(Long memberId, Long seniorId) {
Senior senior = seniorRepository.findById(seniorId).orElseThrow(
() -> new SeniorNotFoundException("시니어를 찾을 수 없습니다.")
);
if (senior.isNotGuard(memberId)) {
throw new SeniorAndGuardMemberMismatchException("해당 Guard의 Senior가 아닙니다.");
}
List<GuardGuideline> guardGuidelines = guardGuidelineRepository.findBySeniorId(seniorId);

return guardGuidelines.stream()
.map(m -> new GuardGuidelineResponse(m.getType(), m.getTitle(), m.getContent()))
.map(m -> new GuardGuidelineResponse(m.getId(), m.getType(), m.getTitle(), m.getContent()))
.toList();
}

@Transactional(readOnly = true)
public GuardGuidelineResponse readGuardGuideline(Long guidelineId){
public GuardGuidelineResponse readGuardGuideline(Long callbackId, Long guidelineId) {

Callback callback = callbackRepository.findById(callbackId)
.orElseThrow(() -> new NotExistCallbackException("존재하지 않는 콜백입니다"));

GuardGuideline guardGuideline = guardGuidelineRepository.findById(guidelineId).orElseThrow(
()-> new GuardGuidelineNotFoundException("해당 가이드라인이 존재하지 않습니다.")
);
() -> new GuardGuidelineNotFoundException("해당 가이드라인이 존재하지 않습니다."));

return new GuardGuidelineResponse(guardGuideline.getType(), guardGuideline.getTitle(), guardGuideline.getContent());
}
if (!callback.getSenior().equals(guardGuideline.getSenior())) {
throw new SeniorAndGuardGuidelineMismatchException("해당 Senior의 가이드라인이 아닙니다.");
}

return new GuardGuidelineResponse(guardGuideline.getId(), guardGuideline.getType(), guardGuideline.getTitle(), guardGuideline.getContent());
}
}

0 comments on commit ca76a5c

Please sign in to comment.