Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…2C201 into FE-chore/#1_modifyMember
  • Loading branch information
wink4u committed Aug 17, 2023
2 parents 07317f6 + 095e55e commit bb1d71d
Show file tree
Hide file tree
Showing 41 changed files with 391 additions and 137 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.refill.doctor.repository;

import com.refill.doctor.entity.Doctor;
import com.refill.hospital.entity.Hospital;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

public interface DoctorRepository extends JpaRepository<Doctor, Long> {

List<Doctor> findAllByHospital(Hospital hospital);


}
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package com.refill.doctor.service;

import com.refill.doctor.dto.response.DoctorResponse;
import com.refill.doctor.entity.Doctor;
import com.refill.doctor.repository.DoctorRepository;
import com.refill.global.exception.ErrorCode;
import com.refill.hospital.entity.Hospital;
import com.refill.member.exception.MemberException;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@RequiredArgsConstructor
@Service
public class DoctorService {

private final DoctorRepository doctorRepository;

public void deleteById(Long doctorId) {
Expand All @@ -18,10 +24,19 @@ public void deleteById(Long doctorId) {

public Doctor findById(Long doctorId) {
return doctorRepository.findById(doctorId)
.orElseThrow(()->new MemberException(ErrorCode.USERNAME_NOT_FOUND));
.orElseThrow(
() -> new MemberException(ErrorCode.USERNAME_NOT_FOUND));
}

public Doctor save(Doctor doctor) {
return doctorRepository.save(doctor);
}

@Transactional(readOnly = true)
public List<DoctorResponse> findAllByHospital(Hospital hospital) {
return doctorRepository.findAllByHospital(hospital)
.stream()
.map(DoctorResponse::new)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
Expand Down Expand Up @@ -68,17 +69,22 @@ public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFact
objectMapper.registerModule(new JavaTimeModule());
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.activateDefaultTyping(ptv, ObjectMapper.DefaultTyping.NON_FINAL);
objectMapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
objectMapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer(objectMapper);

RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.disableCachingNullValues()
.entryTtl(Duration.ofDays(1L))
.entryTtl(Duration.ofHours(1L))
.computePrefixWith(CacheKeyPrefix.simple())
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(genericJackson2JsonRedisSerializer));

Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
redisCacheConfigurationMap.put("UserCacheStore", redisCacheConfiguration);
redisCacheConfigurationMap.put("hospitalInfo", redisCacheConfiguration);
redisCacheConfigurationMap.put("hospitalHoursInfo", redisCacheConfiguration);
redisCacheConfigurationMap.put("doctorInfo", redisCacheConfiguration);

return RedisCacheManager.RedisCacheManagerBuilder
.fromConnectionFactory(redisConnectionFactory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

import com.refill.doctor.dto.request.DoctorJoinRequest;
import com.refill.doctor.dto.request.DoctorUpdateRequest;
import com.refill.doctor.dto.response.DoctorResponse;
import com.refill.hospital.dto.request.HospitalInfoUpdateRequest;
import com.refill.hospital.dto.request.HospitalOperatingHoursRequest;
import com.refill.hospital.dto.request.HospitalLocationRequest;
import com.refill.hospital.dto.request.HospitalOperatingHoursRequest;
import com.refill.hospital.dto.response.HospitalDetailResponse;
import com.refill.hospital.dto.response.HospitalOperatingHourResponse;
import com.refill.hospital.dto.response.HospitalOperatingHoursCache;
import com.refill.hospital.dto.response.HospitalResponse;
import com.refill.hospital.dto.response.HospitalSearchByLocationResponse;
import com.refill.hospital.entity.Hospital;
import com.refill.hospital.service.HospitalOperatingHourService;
import com.refill.hospital.service.HospitalService;
import com.refill.review.dto.response.ReviewResponse;
import com.refill.review.service.ReviewService;
import com.refill.security.util.LoginInfo;
import java.util.List;
import javax.validation.Valid;
Expand Down Expand Up @@ -39,7 +44,7 @@ public class HospitalController {

private final HospitalService hospitalService;
private final HospitalOperatingHourService hospitalOperatingHourService;

private final ReviewService reviewService;
@GetMapping("/")
public ResponseEntity<List<HospitalResponse>> getAllHospitals(){
List<HospitalResponse> hospitalResponses = hospitalService.findAllHospitals();
Expand Down Expand Up @@ -74,12 +79,22 @@ public ResponseEntity<List<HospitalResponse>> searchByKeyword(
}

/* 병원 상세 조회 */
// @Cacheable
@GetMapping("/{hospitalId}")
public ResponseEntity<HospitalDetailResponse> getHospitalDetail(@PathVariable Long hospitalId)
{
log.debug("hospitalId: {}", hospitalId);
HospitalDetailResponse hospitalDetailResponse = hospitalOperatingHourService.getDetailHospitalInfo(hospitalId);
//HospitalDetailResponse hospitalDetailResponse = hospitalOperatingHourService.getDetailHospitalInfo(hospitalId);

Hospital hospital = hospitalService.findByIdUsingCache(hospitalId); // 캐시 적용
List<DoctorResponse> doctorResponseList = hospitalService.getDoctorByHospital(hospital); // 캐시 적용
List<ReviewResponse> reviewResponseList = reviewService.findAllByHospital(hospital); // 캐시 미적용

HospitalResponse hospitalResponse = new HospitalResponse(hospital, reviewService.generateAverageScore(hospital));

HospitalOperatingHoursCache operatingHoursCache = hospitalOperatingHourService.getOperatingHoursUsingCache(hospitalId); // 캐시 적용

HospitalDetailResponse hospitalDetailResponse = new HospitalDetailResponse(hospitalResponse, doctorResponseList, reviewResponseList, operatingHoursCache);

return ResponseEntity.ok()
.body(hospitalDetailResponse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public record HospitalDetailResponse(
@NotNull HospitalResponse hospitalResponse,
@NotNull List<DoctorResponse> doctorResponses,
@NotNull List<ReviewResponse> reviewResponses,
@NotNull List<HospitalOperatingHourResponse> operatingHourResponses
HospitalOperatingHoursCache operatingHoursCache
) {

public HospitalDetailResponse(Hospital hospital) {
Expand All @@ -38,7 +38,7 @@ public HospitalDetailResponse(Hospital hospital, List<HospitalOperatingHourRespo
hospital.getReviews()
.stream()
.map(ReviewResponse::new).collect(Collectors.toList()),
hourResponseList
null
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.refill.hospital.dto.response;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.refill.hospital.entity.HospitalOperatingHour;
import java.time.DayOfWeek;
import java.time.LocalTime;
Expand All @@ -9,6 +11,15 @@ public record HospitalOperatingHourResponse(
LocalTime startTime,
LocalTime endTime
) {
@JsonCreator
public HospitalOperatingHourResponse(
@JsonProperty("dayOfWeek") DayOfWeek dayOfWeek,
@JsonProperty("startTime") LocalTime startTime,
@JsonProperty("endTime") LocalTime endTime) {
this.dayOfWeek = dayOfWeek;
this.startTime = startTime;
this.endTime = endTime;
}
public HospitalOperatingHourResponse(HospitalOperatingHour hospitalOperatingHour) {
this(
hospitalOperatingHour.getDayOfWeek(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.refill.hospital.dto.response;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import java.util.List;

@JsonTypeInfo(
use = JsonTypeInfo.Id.CLASS,
include = JsonTypeInfo.As.WRAPPER_ARRAY
)
@JsonTypeName("OperatingHours")
public record HospitalOperatingHoursCache(
List<HospitalOperatingHourResponse> operatingHourList
) {

public HospitalOperatingHoursCache(List<HospitalOperatingHourResponse> operatingHourList) {
this.operatingHourList = operatingHourList;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ public HospitalResponse(Hospital hospital){
hospital.getEmail());
}

public HospitalResponse(Hospital hospital, Double avg){
this(hospital.getId(),
hospital.getName(),
hospital.getLongitude(),
hospital.getLatitude(),
hospital.getHospitalProfileImg(),
hospital.getHospitalBannerImg(),
hospital.getAddress(),
hospital.getTel(),
avg,
hospital.getEmail());
}

private static double getScore(Hospital hospital) {
double score = hospital.getReviews()
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.refill.hospital.dto.request.HospitalOperatingHoursRequest;
import com.refill.hospital.dto.response.HospitalDetailResponse;
import com.refill.hospital.dto.response.HospitalOperatingHourResponse;
import com.refill.hospital.dto.response.HospitalOperatingHoursCache;
import com.refill.hospital.entity.Hospital;
import com.refill.hospital.entity.HospitalOperatingHour;
import com.refill.hospital.repository.HospitalOperatingHourRepository;
Expand All @@ -15,6 +16,7 @@
import java.util.Arrays;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -78,4 +80,18 @@ public void generateHours(Long id) {
.forEach(hospitalOperatingHourRepository::save);

}

@Cacheable(value = "hospitalHoursInfo", key = "#id")
@Transactional(readOnly = true)
public HospitalOperatingHoursCache getOperatingHoursUsingCache(Long id) {

Hospital hospital = hospitalService.findById(id);

List<HospitalOperatingHourResponse> operatingHourList = hospitalOperatingHourRepository.findAllByHospital(hospital)
.stream()
.map(HospitalOperatingHourResponse::new)
.toList();

return new HospitalOperatingHoursCache(operatingHourList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.refill.doctor.dto.request.DoctorJoinRequest;
import com.refill.doctor.dto.request.DoctorUpdateRequest;
import com.refill.doctor.dto.response.DoctorResponse;
import com.refill.doctor.entity.Doctor;
import com.refill.doctor.entity.EducationBackground;
import com.refill.doctor.entity.MajorArea;
Expand All @@ -28,6 +29,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
Expand Down Expand Up @@ -131,7 +133,8 @@ private HospitalSearchByLocationResponse createResponse(Hospital hospital,
public List<HospitalResponse> searchByKeyword(String hospitalName, String address) {

if (StringUtils.hasText(hospitalName) && StringUtils.hasText(address)) {
return hospitalRepository.findByNameContainingAndAddressContaining(hospitalName, address)
return hospitalRepository.findByNameContainingAndAddressContaining(hospitalName,
address)
.stream()
.map(HospitalResponse::new)
.collect(Collectors.toList());
Expand All @@ -150,6 +153,7 @@ public List<HospitalResponse> searchByKeyword(String hospitalName, String addres
}
}

//@Cacheable(value = "hospitalInfo", key = "#id")
@Transactional
public HospitalDetailResponse getHospitalDetail(Long id) {
return new HospitalDetailResponse(findById(id));
Expand Down Expand Up @@ -182,7 +186,8 @@ public void modifyHospitalInfo(Long hospitalId, String loginId,

processImage(profileImg, hospital.getHospitalProfileImg(), hospital::updateProfileAddress);
processImage(bannerImg, hospital.getHospitalBannerImg(), hospital::updateBannerAddress);
processImage(registrationImg, hospital.getRegistrationImg(), hospital::updateRegistrationImg);
processImage(registrationImg, hospital.getRegistrationImg(),
hospital::updateRegistrationImg);
}

private void processImage(MultipartFile newImage, String existingImageAddress, Consumer<String> addressUpdater) {
Expand All @@ -197,6 +202,7 @@ private void deleteExistingFile(String fileAddress) {
amazonS3Service.deleteFile(fileAddress);
}
}

private void uploadFileAndUpdateAddress(MultipartFile file, Consumer<String> addressUpdater) {
if (Objects.nonNull(file) && !file.isEmpty()) {
String address = amazonS3Service.uploadFile(file);
Expand All @@ -219,6 +225,8 @@ public void modifyHospitalDoctor(String loginId, Long hospitalId, Long doctorId,
processImage(profileImg, doctor.getProfileImg(), doctor::updateProfileAddress);
}


@CacheEvict(value = "doctorInfo", key = "#hospitalId")
@Transactional
public void registHospitalDoctor(String loginId, Long hospitalId,
DoctorJoinRequest doctorJoinRequest, MultipartFile profileImg, MultipartFile licenseImg) {
Expand All @@ -233,22 +241,58 @@ public void registHospitalDoctor(String loginId, Long hospitalId,
registMajor(doctorJoinRequest, doctor);
}

private void registEducationBackground(DoctorJoinRequest doctorJoinRequest, Doctor doctor) {
@Transactional
@CacheEvict(value = "doctorInfo", key = "#doctor.getHospital().id")
public void registEducationBackground(DoctorJoinRequest doctorJoinRequest, Doctor doctor) {
doctorJoinRequest.educationBackgrounds()
.stream()
.map(content -> new EducationBackground(doctor, content))
.forEach(educationBackgroundRepository::save);
}

private void registMajor(DoctorJoinRequest doctorJoinRequest, Doctor doctor) {
@Transactional
@CacheEvict(value = "doctorInfo", key = "#doctor.getHospital().id")
public void registMajor(DoctorJoinRequest doctorJoinRequest, Doctor doctor) {
doctorJoinRequest.majorAreas()
.stream()
.map(major -> new MajorArea(doctor, major))
.forEach(majorAreaRepository::save);
}


@Transactional(readOnly = true)
public List<HospitalResponse> findAllHospitals() {
return this.findAll().stream().map(hospital -> new HospitalResponse(hospital)).collect(Collectors.toList());
return this.findAll()
.stream()
.map(hospital -> new HospitalResponse(hospital))
.collect(Collectors.toList());
}

@Cacheable(value = "hospitalInfo", key = "#hospitalId")
@Transactional(readOnly = true)
public Hospital findByIdUsingCache(Long hospitalId) {
return findById(hospitalId);
}

@Cacheable(value = "doctorInfo", key = "#hospital.id")
public List<DoctorResponse> getDoctorByHospital(Hospital hospital) {

return doctorService.findAllByHospital(hospital);
}
}

// return hospital.getDoctors()
// .stream()
// .map(DoctorResponse::new)
// .collect(Collectors.toList());
// }

// public List<ReviewResponse> getReviewByHospital(Hospital hospital) {
//
// return reviewService.findAllByHospital(hospital);

// return hospital.getReviews()
// .stream()
// .map(ReviewResponse::new)
// .collect(Collectors.toList());
// }
//}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.refill.review.repository;

import com.refill.hospital.entity.Hospital;
import com.refill.review.entity.Review;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ReviewRepository extends JpaRepository<Review, Long> {

List<Review> findAllByHospital(Hospital hospital);
}
Loading

0 comments on commit bb1d71d

Please sign in to comment.