Skip to content

Commit

Permalink
Merge pull request #63 from shim-pyoo/feat/#59-getHospitalVisitTimeLeft
Browse files Browse the repository at this point in the history
โœจ FEAT. ๋ณ‘์› ๋ฐฉ๋ฌธ๊นŒ์ง€ ๋‚จ์€ ์‹œ๊ฐ„ ์กฐํšŒ ๊ธฐ๋Šฅ ๊ตฌํ˜„
  • Loading branch information
frombunny authored Sep 4, 2024
2 parents 06d39be + f7ff707 commit feedd3f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Repository
public interface HospitalVisitRepository extends JpaRepository<HospitalVisit, Long> {
List<HospitalVisit> findByUserId(User userId);


// ์‚ฌ์šฉ์ž ID๋กœ ๋ฏธ๋ž˜์˜ ๋ฐฉ๋ฌธ ์ผ์ •์„ ์กฐํšŒํ•˜๊ณ , ๋ฐฉ๋ฌธ ์‹œ๊ฐ„์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ๊ฐ€์žฅ ๋น ๋ฅธ ์ผ์ • ๋ฐ˜ํ™˜
Optional<HospitalVisit> findFirstByUserIdAndVisitTimeAfterOrderByVisitTimeAsc(User userId, LocalDateTime now);

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public interface HospitalService {
ResponseEntity<CustomAPIResponse<?>> setVisitHospital(HospitalVisitSetRequestDto hospitalVisitSetRequestDto);
ResponseEntity<CustomAPIResponse<?>> getAllHospitalVisit();
ResponseEntity<CustomAPIResponse<?>> getOneHospitalVisit(Long hospitalVisitId);
ResponseEntity<CustomAPIResponse<?>> getTimeLeftHospitalVisit();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import com.hsu.shimpyoo.domain.hospital.entity.HospitalVisit;
import com.hsu.shimpyoo.domain.hospital.repository.HospitalRepository;
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.hospital.web.dto.*;
import com.hsu.shimpyoo.domain.user.entity.User;
import com.hsu.shimpyoo.domain.user.repository.UserRepository;
import com.hsu.shimpyoo.global.response.CustomAPIResponse;
Expand All @@ -20,6 +17,8 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.ResponseStatusException;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -138,5 +137,46 @@ public ResponseEntity<CustomAPIResponse<?>> getOneHospitalVisit(Long hospitalVis
return ResponseEntity.status(HttpStatus.OK).body(res);
}

@Override
public ResponseEntity<CustomAPIResponse<?>> getTimeLeftHospitalVisit() {
// ์‚ฌ์šฉ์ž ์กด์žฌ ํ™•์ธ
Optional<User> isExistUser=userRepository.findByLoginId(SecurityContextHolder.getContext().getAuthentication().getName());
if(isExistUser.isEmpty()){
throw new ResponseStatusException(HttpStatus.NOT_FOUND,"์กด์žฌํ•˜์ง€ ์•Š๋Š” ์‚ฌ์šฉ์ž์ž…๋‹ˆ๋‹ค.");
}

LocalDateTime now = LocalDateTime.now(); // ํ˜„์žฌ ์‹œ๊ฐ„

// ํ˜„์žฌ๋ณด๋‹ค ๋ฏธ๋ž˜์ธ ์ผ์ • ์ค‘์—์„œ ๊ฐ€์žฅ ๋น ๋ฅธ ๋ฐฉ๋ฌธ ์ผ์ •์„ ์ฐพ์Œ
Optional<HospitalVisit> firstHospitalVisit =
hospitalVisitRepository.findFirstByUserIdAndVisitTimeAfterOrderByVisitTimeAsc(isExistUser.get(), now);

// ๋ฐฉ๋ฌธ ์ผ์ •์ด ์—†๋‹ค๋ฉด, ๊ทธ์— ๋งž๋Š” ์‘๋‹ต์„ ๋ฐ˜ํ™˜
if(firstHospitalVisit.isEmpty()){
CustomAPIResponse<Object> res=CustomAPIResponse.createSuccess(200, null, "์„ค์ •ํ•œ ๋ณ‘์› ๋ฐฉ๋ฌธ ์ผ์ •์ด ์—†์Šต๋‹ˆ๋‹ค.");
return ResponseEntity.status(HttpStatus.OK).body(res);
}

// ๊ฐ€์žฅ ๋น ๋ฅธ ๋ฐฉ๋ฌธ ์‹œ๊ฐ„
LocalDateTime firstVisitTime=firstHospitalVisit.get().getVisitTime();

int leftDay = (int) ChronoUnit.DAYS.between(now, firstVisitTime);
int leftHour = (int) (ChronoUnit.HOURS.between(now, firstVisitTime) % 24);
int leftMinute = (int) (ChronoUnit.MINUTES.between(now, firstVisitTime) % 60);


HospitalVisitTimeLeftDto hospitalVisitTimeLeftDto=HospitalVisitTimeLeftDto.builder()
.day(leftDay)
.hour(leftHour)
.minute(leftMinute)
.build();


CustomAPIResponse<Object> res=CustomAPIResponse.createSuccess(200, hospitalVisitTimeLeftDto,
"๋ณ‘์› ๋ฐฉ๋ฌธ๊นŒ์ง€ ๋‚จ์€ ์‹œ๊ฐ„์ด ์กฐํšŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
return ResponseEntity.status(HttpStatus.OK).body(res);

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,10 @@ public ResponseEntity<CustomAPIResponse<?>> getOneHospitalVisit(@RequestParam Lo
ResponseEntity<CustomAPIResponse<?>> result=hospitalService.getOneHospitalVisit(hospitalVisitId);
return result;
}

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

import lombok.*;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class HospitalVisitTimeLeftDto {
// ๋ฉฐ์น  ๋‚จ์•˜๋Š”์ง€
private int day;

// ๋ช‡์‹œ๊ฐ„ ๋‚จ์•˜๋Š”์ง€
private int hour;

// ๋ช‡๋ถ„ ๋‚จ์•˜๋Š”์ง€
private int minute;

}

0 comments on commit feedd3f

Please sign in to comment.