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

Refactor: 가이드라인 관련 로직 수정 {프론트 요청) #103

Merged
merged 8 commits into from
Oct 23, 2024
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());
}
}