Skip to content

Commit

Permalink
Merge pull request #60 from shim-pyoo/feat/#52-getHospitalVisit
Browse files Browse the repository at this point in the history
✨ FEAT. 병원 방문 일정 조회
  • Loading branch information
frombunny authored Sep 4, 2024
2 parents 4721b9a + 4326605 commit 97ecc4e
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.hsu.shimpyoo.domain.hospital.repository;

import com.hsu.shimpyoo.domain.hospital.entity.HospitalVisit;
import com.hsu.shimpyoo.domain.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface HospitalVisitRepository extends JpaRepository<HospitalVisit, Long> {
List<HospitalVisit> findByUserId(User userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
public interface HospitalService {
ResponseEntity<CustomAPIResponse<?>> searchHospital(HospitalSearchRequestDto hospitalSearchRequestDto);
ResponseEntity<CustomAPIResponse<?>> setVisitHospital(HospitalVisitSetRequestDto hospitalVisitSetRequestDto);
ResponseEntity<CustomAPIResponse<?>> getAllHospitalVisit();
ResponseEntity<CustomAPIResponse<?>> getOneHospitalVisit(Long hospitalVisitId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.hsu.shimpyoo.domain.hospital.repository.HospitalVisitRepository;
import com.hsu.shimpyoo.domain.hospital.web.dto.HospitalSearchRequestDto;
import com.hsu.shimpyoo.domain.hospital.web.dto.HospitalSearchResponseDto;
import com.hsu.shimpyoo.domain.hospital.web.dto.HospitalVisitDto;
import com.hsu.shimpyoo.domain.hospital.web.dto.HospitalVisitSetRequestDto;
import com.hsu.shimpyoo.domain.user.entity.User;
import com.hsu.shimpyoo.domain.user.repository.UserRepository;
Expand All @@ -21,6 +22,7 @@

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -54,6 +56,7 @@ public ResponseEntity<CustomAPIResponse<?>> searchHospital(HospitalSearchRequest
}
}

// 병원 방문 일정 설정
@Transactional
@Override
public ResponseEntity<CustomAPIResponse<?>> setVisitHospital(HospitalVisitSetRequestDto hospitalVisitSetRequestDto) {
Expand Down Expand Up @@ -84,5 +87,56 @@ public ResponseEntity<CustomAPIResponse<?>> setVisitHospital(HospitalVisitSetReq
return ResponseEntity.status(HttpStatus.OK).body(res);
}

// 병원 방문 일정 전체 조회
@Override
public ResponseEntity<CustomAPIResponse<?>> getAllHospitalVisit() {
// 사용자 정보를 가져온다
Optional<User> isExistUser=userRepository.findByLoginId(SecurityContextHolder.getContext().getAuthentication().getName());
if(isExistUser.isEmpty()){
throw new ResponseStatusException(HttpStatus.NOT_FOUND,"존재하지 않는 사용자입니다.");
}

// 병원 방문 기록 모두 조회
List<HospitalVisitDto> hospitalVisitDtoList=hospitalVisitRepository.findByUserId(isExistUser.get())
.stream()
.map(hospitalVisit -> HospitalVisitDto.builder()
.hospitalVisitId(hospitalVisit.getHospitalVisitId())
.hospitalName(hospitalVisit.getHospitalId().getHospitalName())
.hospitalAddress(hospitalVisit.getHospitalId().getHospitalAddress())
.hospitalPhoneNumber(hospitalVisit.getHospitalId().getHospitalPhone())
.visitTime(hospitalVisit.getVisitTime())
.build())
.toList();

CustomAPIResponse<Object> res=CustomAPIResponse.createSuccess(200, hospitalVisitDtoList, "병원 방문 일정이 조회되었습니다.");

if(hospitalVisitDtoList.isEmpty()){
res=CustomAPIResponse.createSuccess(200, null, "아직 방문 일정을 설정하지 않았습니다.");
}

return ResponseEntity.status(HttpStatus.OK).body(res);
}

@Override
public ResponseEntity<CustomAPIResponse<?>> getOneHospitalVisit(Long hospitalVisitId) {
Optional<HospitalVisit> isExistHospitalVisit=hospitalVisitRepository.findById(hospitalVisitId);

// 병원 방문 일정이 존재하지 않는 경우
if(isExistHospitalVisit.isEmpty()){
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "병원 방문 일정을 찾을 수 없습니다.");
}

HospitalVisitDto response=HospitalVisitDto.builder()
.hospitalVisitId(isExistHospitalVisit.get().getHospitalVisitId())
.hospitalName(isExistHospitalVisit.get().getHospitalId().getHospitalName())
.hospitalAddress(isExistHospitalVisit.get().getHospitalId().getHospitalAddress())
.hospitalPhoneNumber(isExistHospitalVisit.get().getHospitalId().getHospitalPhone())
.visitTime(isExistHospitalVisit.get().getVisitTime())
.build();

CustomAPIResponse<Object> res=CustomAPIResponse.createSuccess(200, response, "병원 방문 일정이 조회되었습니다.");
return ResponseEntity.status(HttpStatus.OK).body(res);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,19 @@ public ResponseEntity<CustomAPIResponse<?>> setVisitHospital(@Valid @RequestBody
CustomAPIResponse<Object> res = CustomAPIResponse.createFailWithout(400, "방문 일정은 현재보다 미래여야 합니다.");
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(res);
}

ResponseEntity<CustomAPIResponse<?>> result=hospitalService.setVisitHospital(hospitalVisitSetRequestDto);
return result;
}

@GetMapping("/getAllHospitalVisit")
public ResponseEntity<CustomAPIResponse<?>> getAllHospitalVisit(){
ResponseEntity<CustomAPIResponse<?>> result=hospitalService.getAllHospitalVisit();
return result;
}

@GetMapping("/getOneHospitalVisit")
public ResponseEntity<CustomAPIResponse<?>> getOneHospitalVisit(@RequestParam Long hospitalVisitId){
ResponseEntity<CustomAPIResponse<?>> result=hospitalService.getOneHospitalVisit(hospitalVisitId);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.hsu.shimpyoo.domain.hospital.web.dto;

import com.hsu.shimpyoo.domain.hospital.entity.Hospital;
import com.hsu.shimpyoo.domain.user.entity.User;
import jakarta.persistence.*;
import lombok.*;

import java.time.LocalDateTime;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class HospitalVisitDto {
// 병원 방문 기본 키
private Long hospitalVisitId;

// 방문할 병원 이름
private String hospitalName;

// 방문할 병원 주소
private String hospitalAddress;

// 방문할 병원의 연락처
private String hospitalPhoneNumber;

// 방문할 시각
private LocalDateTime visitTime;
}

0 comments on commit 97ecc4e

Please sign in to comment.