Skip to content

Commit

Permalink
Refactor: 시니또 은행정보 처리 로직 변경, 안부전화 시니또 저장계층 member로 변경, 은행정보 없을 경우 예외처…
Browse files Browse the repository at this point in the history
…리 구현 (#89)

* refactor: Sinitto 엔티티 이름 변경 및 위치 변경

* refactor: 안부전화 시니또 객체 멤버 타입으로 변경 및 비즈니스 로직 래팩토링

* fix: 더미데이터 수정 및 예외처리 조건 추가

* feat: 포인트 예외 추가 및 더미데이터 수정

* test: 테스트코드 수정 및 확인

* chore: 안부전화에 시니또 할당 더미데이터 추가

* refactor: 코드 자동 리팩토링 적용

* fix: 메서드명 대소문자 오류 수정

* refactor: 시니또 계좌 정보 및 시니또 여부 검증 로직 순서 조정

* fix: 인텔리제이 자동 리팩토링으로 인해 바뀐 속성값 수동 변경
  • Loading branch information
GitJIHO authored Oct 18, 2024
1 parent e4bc19a commit 5009cc8
Show file tree
Hide file tree
Showing 24 changed files with 420 additions and 430 deletions.
248 changes: 132 additions & 116 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 @@ -132,7 +132,7 @@ public ResponseEntity<StringMessageResponse> writeHelloCallEndTimeBySinitto(@Mem
@PostMapping("/reports")
public ResponseEntity<StringMessageResponse> createHelloCallReport(@MemberId Long memberId, @RequestBody HelloCallReportRequest request) {

helloCallService.SendReportBySinitto(memberId, request);
helloCallService.sendReportBySinitto(memberId, request);

return ResponseEntity.status(HttpStatus.CREATED).body(new StringMessageResponse("소통 보고서가 작성되었습니다."));
}
Expand Down

This file was deleted.

33 changes: 14 additions & 19 deletions src/main/java/com/example/sinitto/helloCall/entity/HelloCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.example.sinitto.helloCall.exception.TimeRuleException;
import com.example.sinitto.member.entity.Member;
import com.example.sinitto.member.entity.Senior;
import com.example.sinitto.member.entity.Sinitto;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import org.hibernate.annotations.OnDelete;
Expand Down Expand Up @@ -44,8 +43,8 @@ public class HelloCall {
@OneToMany(mappedBy = "helloCall", cascade = CascadeType.REMOVE)
private List<TimeSlot> timeSlots = new ArrayList<>();
@ManyToOne
@JoinColumn(name = "sinitto_id")
private Sinitto sinitto;
@JoinColumn(name = "member_id")
private Member member;
@OneToMany(mappedBy = "helloCall", cascade = CascadeType.REMOVE)
private List<HelloCallTimeLog> helloCallTimeLogs = new ArrayList<>();

Expand Down Expand Up @@ -102,12 +101,12 @@ public int getServiceTime() {
return serviceTime;
}

public Sinitto getSinitto() {
return sinitto;
public Member getMember() {
return member;
}

public void setSinitto(Sinitto sinitto) {
this.sinitto = sinitto;
public void setMember(Member member) {
this.member = member;
}

public String getReport() {
Expand All @@ -118,12 +117,8 @@ public void setReport(String report) {
this.report = report;
}

public String getSinittoName() {
return this.sinitto.getMember().getName();
}

public List<HelloCallTimeLog> getHelloCallTimeLogs() {
return helloCallTimeLogs;
public String getMemberName() {
return this.member.getName();
}

public void checkStatusIsWaiting() {
Expand All @@ -132,12 +127,6 @@ public void checkStatusIsWaiting() {
}
}

public void checkSiniitoIsSame(Sinitto sinitto) {
if (!this.sinitto.equals(sinitto)) {
throw new UnauthorizedException("안부전화 서비스 리포트를 작성할 권한이 없습니다.");
}
}

public void checkGuardIsCorrect(Member member) {
if (!this.senior.getMember().equals(member)) {
throw new UnauthorizedException("해당 시니어의 안부전화를 신청한 보호자가 아닙니다. 권한이 없습니다.");
Expand All @@ -152,6 +141,12 @@ public boolean checkReportIsNotNull() {
return this.report != null;
}

public void checkMemberIsRightSinitto(Member member) {
if (this.member == null || !this.member.equals(member)) {
throw new UnauthorizedException("해당 안부전화를 수행하는 시니또가 아닙니다.");
}
}

public void changeStatusToInProgress() {
if (status.canNotProgressStatus(Status.IN_PROGRESS)) {
throw new InvalidStatusException("안부전화 서비스가 수행 대기중일 때만 진행중 상태로 나아갈 수 있습니다. 현재 상태 : " + this.status);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.sinitto.helloCall.entity;

import com.example.sinitto.member.entity.Sinitto;
import com.example.sinitto.member.entity.Member;
import jakarta.persistence.*;

import java.time.LocalDateTime;
Expand All @@ -18,18 +18,18 @@ public class HelloCallTimeLog {
private HelloCall helloCall;
@ManyToOne
@JoinColumn(name = "sinitto_id")
private Sinitto sinitto;
private Member member;

public HelloCallTimeLog(HelloCall helloCall, Sinitto sinitto) {
public HelloCallTimeLog(HelloCall helloCall, Member member) {
this.helloCall = helloCall;
this.sinitto = sinitto;
this.member = member;
}

public HelloCallTimeLog(HelloCall helloCall, Sinitto sinitto, LocalDateTime startDateAndTime, LocalDateTime endDateAndTime) {
public HelloCallTimeLog(HelloCall helloCall, Member member, LocalDateTime startDateAndTime, LocalDateTime endDateAndTime) {
this.helloCall = helloCall;
this.startDateAndTime = startDateAndTime;
this.endDateAndTime = endDateAndTime;
this.sinitto = sinitto;
this.member = member;
}

protected HelloCallTimeLog() {
Expand All @@ -52,14 +52,14 @@ public void setEndDateAndTime(LocalDateTime endDateAndTime) {
}

public String getSinittoName() {
return this.sinitto.getMember().getName();
return this.member.getName();
}

public HelloCall getHelloCall() {
return helloCall;
}

public Sinitto getSinitto() {
return sinitto;
public Member getSinitto() {
return member;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.example.sinitto.helloCall.repository;

import com.example.sinitto.helloCall.entity.HelloCall;
import com.example.sinitto.member.entity.Member;
import com.example.sinitto.member.entity.Senior;
import com.example.sinitto.member.entity.Sinitto;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -14,7 +14,7 @@ public interface HelloCallRepository extends JpaRepository<HelloCall, Long> {

Optional<HelloCall> findBySenior(Senior senior);

List<HelloCall> findAllBySinitto(Sinitto sinitto);
List<HelloCall> findAllByMember(Member member);

List<HelloCall> findAllBySeniorIn(List<Senior> seniors);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

import com.example.sinitto.helloCall.entity.HelloCall;
import com.example.sinitto.helloCall.entity.HelloCallTimeLog;
import com.example.sinitto.member.entity.Sinitto;
import com.example.sinitto.member.entity.Member;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface HelloCallTimeLogRepository extends JpaRepository<HelloCallTimeLog, Long> {
Optional<HelloCallTimeLog> findBySinittoAndHelloCallId(Sinitto sinitto, Long helloCallId);

List<HelloCallTimeLog> findAllByHelloCallId(Long helloCallId);

Optional<HelloCallTimeLog> findTopBySinittoAndHelloCallOrderByStartDateAndTimeDesc(Sinitto sinitto, HelloCall helloCall);
Optional<HelloCallTimeLog> findByMemberAndHelloCallId(Member member, Long helloCallId);

Optional<HelloCallTimeLog> findTopByMemberAndHelloCallOrderByStartDateAndTimeDesc(Member member, HelloCall helloCall);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.example.sinitto.helloCall.repository.TimeSlotRepository;
import com.example.sinitto.member.entity.Member;
import com.example.sinitto.member.entity.Senior;
import com.example.sinitto.member.entity.Sinitto;
import com.example.sinitto.member.exception.MemberNotFoundException;
import com.example.sinitto.member.repository.MemberRepository;
import com.example.sinitto.point.entity.Point;
Expand All @@ -25,8 +24,6 @@
import com.example.sinitto.point.exception.PointNotFoundException;
import com.example.sinitto.point.repository.PointLogRepository;
import com.example.sinitto.point.repository.PointRepository;
import com.example.sinitto.sinitto.exception.SinittoNotFoundException;
import com.example.sinitto.sinitto.repository.SinittoRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
Expand All @@ -45,20 +42,18 @@ public class HelloCallService {
private final TimeSlotRepository timeSlotRepository;
private final SeniorRepository seniorRepository;
private final MemberRepository memberRepository;
private final SinittoRepository sinittoRepository;
private final HelloCallTimeLogRepository helloCallTimeLogRepository;
private final PointRepository pointRepository;
private final PointLogRepository pointLogRepository;


public HelloCallService(HelloCallRepository helloCallRepository, TimeSlotRepository timeSlotRepository,
SeniorRepository seniorRepository, MemberRepository memberRepository, SinittoRepository sinittoRepository,
HelloCallTimeLogRepository helloCallTimeLogRepository, PointRepository pointRepository, PointLogRepository pointLogRepository) {
SeniorRepository seniorRepository, MemberRepository memberRepository, HelloCallTimeLogRepository helloCallTimeLogRepository,
PointRepository pointRepository, PointLogRepository pointLogRepository) {
this.helloCallRepository = helloCallRepository;
this.timeSlotRepository = timeSlotRepository;
this.seniorRepository = seniorRepository;
this.memberRepository = memberRepository;
this.sinittoRepository = sinittoRepository;
this.helloCallTimeLogRepository = helloCallTimeLogRepository;
this.pointRepository = pointRepository;
this.pointLogRepository = pointLogRepository;
Expand Down Expand Up @@ -243,7 +238,7 @@ public HelloCallReportResponse readHelloCallReportByGuard(Long memberId, Long he
}

return new HelloCallReportResponse(helloCall.getStartDate(),
helloCall.getEndDate(), helloCall.getSinittoName(), helloCall.getReport());
helloCall.getEndDate(), helloCall.getMemberName(), helloCall.getReport());
}

@Transactional
Expand All @@ -258,7 +253,7 @@ public void makeCompleteHelloCallByGuard(Long memberId, Long helloCallId) {

helloCall.changeStatusToComplete();

Point sinittoPoint = pointRepository.findByMember(helloCall.getSinitto().getMember())
Point sinittoPoint = pointRepository.findByMember(helloCall.getMember())
.orElseThrow(() -> new PointNotFoundException("포인트 적립 받을 시니또와 연관된 포인트가 없습니다"));

sinittoPoint.earn(helloCall.getPrice());
Expand All @@ -281,7 +276,7 @@ public List<HelloCallReportResponse> readAllHelloCallReportByAdmin() {
for (HelloCall helloCall : helloCalls) {
if (helloCall.checkReportIsNotNull()) {
HelloCallReportResponse response = new HelloCallReportResponse(helloCall.getStartDate(),
helloCall.getEndDate(), helloCall.getSinittoName(), helloCall.getReport());
helloCall.getEndDate(), helloCall.getMemberName(), helloCall.getReport());
helloCallReportResponses.add(response);
}
}
Expand All @@ -293,78 +288,85 @@ public void acceptHelloCallBySinitto(Long memberId, Long helloCallId) {
HelloCall helloCall = helloCallRepository.findById(helloCallId)
.orElseThrow(() -> new HelloCallNotFoundException("id에 해당하는 안부전화 정보를 찾을 수 없습니다."));

Sinitto sinitto = sinittoRepository.findByMemberId(memberId)
.orElseThrow(() -> new SinittoNotFoundException("id에 해당하는 시니또를 찾을 수 없습니다."));
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new MemberNotFoundException("id에 해당하는 멤버가 없습니다."));

if (!member.isSinitto()) {
throw new UnauthorizedException("시니또가 아닙니다.");
}

helloCall.changeStatusToInProgress();
helloCall.setSinitto(sinitto);
helloCall.setMember(member);
}

@Transactional
public HelloCallTimeResponse writeHelloCallStartTimeBySinitto(Long memberId, Long helloCallId) {
public void writeHelloCallStartTimeBySinitto(Long memberId, Long helloCallId) {
HelloCall helloCall = helloCallRepository.findById(helloCallId)
.orElseThrow(() -> new HelloCallNotFoundException("id에 해당하는 안부전화 정보를 찾을 수 없습니다."));

Sinitto sinitto = sinittoRepository.findByMemberId(memberId)
.orElseThrow(() -> new SinittoNotFoundException("id에 해당하는 시니또를 찾을 수 없습니다."));
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new MemberNotFoundException("id에 해당하는 멤버를 찾을 수 없습니다."));

helloCall.checkMemberIsRightSinitto(member);

Optional<HelloCallTimeLog> recentLog = helloCallTimeLogRepository
.findTopBySinittoAndHelloCallOrderByStartDateAndTimeDesc(sinitto, helloCall);
.findTopByMemberAndHelloCallOrderByStartDateAndTimeDesc(member, helloCall);

if (recentLog.isPresent() && recentLog.get().getEndDateAndTime() == null) {
throw new TimeLogSequenceException("이미 시작된 안부전화가 있습니다. 종료를 먼저 완료해주세요.");
}

HelloCallTimeLog helloCallTimeLog = new HelloCallTimeLog(helloCall, sinitto);
HelloCallTimeLog helloCallTimeLog = new HelloCallTimeLog(helloCall, member);
helloCallTimeLog.setStartDateAndTime(LocalDateTime.now());

HelloCallTimeLog savedHelloCallTimeLog = helloCallTimeLogRepository.save(helloCallTimeLog);
return new HelloCallTimeResponse(savedHelloCallTimeLog.getStartDateAndTime());
helloCallTimeLogRepository.save(helloCallTimeLog);
}

@Transactional
public HelloCallTimeResponse writeHelloCallEndTimeBySinitto(Long memberId, Long helloCallId) {
public void writeHelloCallEndTimeBySinitto(Long memberId, Long helloCallId) {
HelloCall helloCall = helloCallRepository.findById(helloCallId)
.orElseThrow(() -> new HelloCallNotFoundException("id에 해당하는 안부전화 정보를 찾을 수 없습니다."));

Sinitto sinitto = sinittoRepository.findByMemberId(memberId)
.orElseThrow(() -> new SinittoNotFoundException("id에 해당하는 시니또를 찾을 수 없습니다."));
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new MemberNotFoundException("id에 해당하는 멤버를 찾을 수 없습니다."));

helloCall.checkMemberIsRightSinitto(member);

HelloCallTimeLog helloCallTimeLog = helloCallTimeLogRepository
.findTopBySinittoAndHelloCallOrderByStartDateAndTimeDesc(sinitto, helloCall)
.findTopByMemberAndHelloCallOrderByStartDateAndTimeDesc(member, helloCall)
.orElseThrow(() -> new HelloCallNotFoundException("안부전화 로그를 찾을 수 없습니다."));

if (helloCallTimeLog.getEndDateAndTime() != null) {
throw new TimeLogSequenceException("이미 종료된 안부전화입니다.");
}

helloCallTimeLog.setEndDateAndTime(LocalDateTime.now());

return new HelloCallTimeResponse(helloCallTimeLog.getEndDateAndTime());
}

@Transactional
public void cancelHelloCallBySinitto(Long memberId, Long helloCallId) {
HelloCall helloCall = helloCallRepository.findById(helloCallId)
.orElseThrow(() -> new HelloCallNotFoundException("id에 해당하는 안부전화 정보를 찾을 수 없습니다."));

if (!sinittoRepository.existsByMemberId(memberId)) {
throw new SinittoNotFoundException("id에 해당하는 시니또를 찾을 수 없습니다.");
}
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new MemberNotFoundException("id에 해당하는 멤버를 찾을 수 없습니다."));

helloCall.checkMemberIsRightSinitto(member);

helloCall.changeStatusToWaiting();
helloCall.setSinitto(null);
helloCall.setMember(null);
}

@Transactional
public void SendReportBySinitto(Long memberId, HelloCallReportRequest helloCallReportRequest) {
public void sendReportBySinitto(Long memberId, HelloCallReportRequest helloCallReportRequest) {
HelloCall helloCall = helloCallRepository.findById(helloCallReportRequest.helloCallId())
.orElseThrow(() -> new HelloCallNotFoundException("id에 해당하는 안부전화 정보를 찾을 수 없습니다."));

Sinitto sinitto = sinittoRepository.findByMemberId(memberId)
.orElseThrow(() -> new SinittoNotFoundException("id에 해당하는 시니또를 찾을 수 없습니다."));
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new MemberNotFoundException("id에 해당하는 멤버를 찾을 수 없습니다."));

helloCall.checkMemberIsRightSinitto(member);

helloCall.checkSiniitoIsSame(sinitto);
if (helloCall.checkIsNotAfterEndDate()) {
throw new CompletionConditionNotFulfilledException("서비스 종료 날짜보다 이른 날짜에 종료할 수 없습니다.");
}
Expand All @@ -375,10 +377,10 @@ public void SendReportBySinitto(Long memberId, HelloCallReportRequest helloCallR

@Transactional(readOnly = true)
public List<HelloCallResponse> readOwnHelloCallBySinitto(Long memberId) {
Sinitto sinitto = sinittoRepository.findByMemberId(memberId)
.orElseThrow(() -> new SinittoNotFoundException("id에 해당하는 시니또를 찾을 수 없습니다."));
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new MemberNotFoundException("id에 해당하는 멤버를 찾을 수 없습니다."));

List<HelloCall> helloCalls = helloCallRepository.findAllBySinitto(sinitto);
List<HelloCall> helloCalls = helloCallRepository.findAllByMember(member);

List<HelloCallResponse> helloCallResponses = new ArrayList<>();

Expand All @@ -391,5 +393,4 @@ public List<HelloCallResponse> readOwnHelloCallBySinitto(Long memberId) {
return helloCallResponses;
}


}
Loading

0 comments on commit 5009cc8

Please sign in to comment.