diff --git a/src/main/java/com/nadoyagsa/pillaroid/controller/MedicineController.java b/src/main/java/com/nadoyagsa/pillaroid/controller/MedicineController.java index e883135..b9a3d60 100644 --- a/src/main/java/com/nadoyagsa/pillaroid/controller/MedicineController.java +++ b/src/main/java/com/nadoyagsa/pillaroid/controller/MedicineController.java @@ -8,14 +8,15 @@ import com.nadoyagsa.pillaroid.common.exception.BadRequestException; import com.nadoyagsa.pillaroid.common.exception.InternalServerException; import com.nadoyagsa.pillaroid.common.exception.NotFoundException; -import com.nadoyagsa.pillaroid.component.MedicineExcelUtils; import com.nadoyagsa.pillaroid.dto.MedicineResponse; import com.nadoyagsa.pillaroid.dto.PrescriptionResponse; import com.nadoyagsa.pillaroid.dto.VoiceResponse; import com.nadoyagsa.pillaroid.entity.Favorites; +import com.nadoyagsa.pillaroid.entity.Notification; import com.nadoyagsa.pillaroid.jwt.AuthTokenProvider; import com.nadoyagsa.pillaroid.service.BarcodeService; import com.nadoyagsa.pillaroid.service.MedicineService; + import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -31,7 +32,6 @@ public class MedicineController { private final MedicineService medicineService; private final BarcodeService barcodeService; - private final MedicineExcelUtils medicineExcelUtils; private final AuthTokenProvider authTokenProvider; @@ -41,15 +41,7 @@ public ApiResponse getMedicineInfo(HttpServletRequest request, Optional medicineResponse = medicineService.getMedicineInfoByIdx(idx); if (medicineResponse.isPresent()) { - if (request.getHeader("authorization") != null) { // 로그인 된 사용자라면 즐겨찾기 여부를 보여줌 - Long userIdx = findUserIdxByToken(request); - Optional favorites = medicineService.findFavoritesByUserAndMedicineIdx(userIdx, idx); - - if (favorites.isPresent()) { // 즐겨찾기 설정을 했을 시 - medicineResponse.get().setFavoritesIdx(favorites.get().getFavoritesIdx()); - } - } - return ApiResponse.success(medicineResponse.get()); + return reflectFavoritesAndNotificationAboutMedicine(request, medicineResponse.get()); } else throw NotFoundException.MEDICINE_NOT_FOUND; @@ -62,14 +54,14 @@ public ApiResponse getMedicineInfo(HttpServletRequest request, Optional medicineResponse = medicineService.getMedicineInfoByStandardCode(barcode); if (medicineResponse.isPresent()) { - return reflectFavoritesAboutMedicine(request, medicineResponse.get()); + return reflectFavoritesAndNotificationAboutMedicine(request, medicineResponse.get()); } else { String serialNumber = barcodeService.crawlSerialNumber(barcode); // 바코드 번호로 품목일련번호 크롤링 medicineResponse = medicineService.getMedicineInfoBySerialNumber(Integer.parseInt(serialNumber)); if (medicineResponse.isPresent()) { - return reflectFavoritesAboutMedicine(request, medicineResponse.get()); + return reflectFavoritesAndNotificationAboutMedicine(request, medicineResponse.get()); } else { throw NotFoundException.BARCODE_NOT_FOUND; @@ -121,14 +113,19 @@ public Long findUserIdxByToken(HttpServletRequest request) { } } - private ApiResponse reflectFavoritesAboutMedicine(HttpServletRequest request, MedicineResponse medicineResponse) { + private ApiResponse reflectFavoritesAndNotificationAboutMedicine(HttpServletRequest request, MedicineResponse medicineResponse) { if (request.getHeader("authorization") != null) { // 로그인 된 사용자라면 즐겨찾기 여부를 보여줌 Long userIdx = findUserIdxByToken(request); - Optional favorites = medicineService.findFavoritesByUserAndMedicineIdx(userIdx, medicineResponse.getMedicineIdx()); + Optional favorites = medicineService.findFavoritesByUserAndMedicineIdx(userIdx, medicineResponse.getMedicineIdx()); if (favorites.isPresent()) { // 즐겨찾기 설정을 했을 시 medicineResponse.setFavoritesIdx(favorites.get().getFavoritesIdx()); } + + Optional notification = medicineService.findNotificationByUserAndMedicineIdx(userIdx, medicineResponse.getMedicineIdx()); + if (notification.isPresent()) { // 알림 설정을 했을 시 + medicineResponse.setNotificationResponse(notification.get().toNotificationResponse()); + } } return ApiResponse.success(medicineResponse); } diff --git a/src/main/java/com/nadoyagsa/pillaroid/dto/MedicineResponse.java b/src/main/java/com/nadoyagsa/pillaroid/dto/MedicineResponse.java index 0cd0964..aefc583 100644 --- a/src/main/java/com/nadoyagsa/pillaroid/dto/MedicineResponse.java +++ b/src/main/java/com/nadoyagsa/pillaroid/dto/MedicineResponse.java @@ -19,4 +19,7 @@ public class MedicineResponse { @Setter private Long favoritesIdx = null; + + @Setter + private NotificationResponse notificationResponse = null; } diff --git a/src/main/java/com/nadoyagsa/pillaroid/dto/PrescriptionResponse.java b/src/main/java/com/nadoyagsa/pillaroid/dto/PrescriptionResponse.java index 81dc4ab..1ab6e5c 100644 --- a/src/main/java/com/nadoyagsa/pillaroid/dto/PrescriptionResponse.java +++ b/src/main/java/com/nadoyagsa/pillaroid/dto/PrescriptionResponse.java @@ -16,4 +16,5 @@ public class PrescriptionResponse { private String dosage; // 용법용량 private Long favoritesIdx; + private NotificationResponse notificationResponse; } diff --git a/src/main/java/com/nadoyagsa/pillaroid/entity/Medicine.java b/src/main/java/com/nadoyagsa/pillaroid/entity/Medicine.java index ef9d313..3813659 100644 --- a/src/main/java/com/nadoyagsa/pillaroid/entity/Medicine.java +++ b/src/main/java/com/nadoyagsa/pillaroid/entity/Medicine.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import com.nadoyagsa.pillaroid.dto.MedicineResponse; +import com.nadoyagsa.pillaroid.dto.NotificationResponse; import com.nadoyagsa.pillaroid.dto.PillResponse; import com.nadoyagsa.pillaroid.dto.PrescriptionResponse; import com.nadoyagsa.pillaroid.dto.VoiceResponse; @@ -81,7 +82,7 @@ public VoiceResponse toVoiceResponse() { } @JsonIgnore - public PrescriptionResponse toPrescriptionResponse(Long favoritesIdx) { + public PrescriptionResponse toPrescriptionResponse(Long favoritesIdx, NotificationResponse notificationResponse) { return PrescriptionResponse.builder() .medicineIdx(medicineIdx) .name(name) @@ -89,6 +90,7 @@ public PrescriptionResponse toPrescriptionResponse(Long favoritesIdx) { .efficacy(efficacy) .dosage(dosage) .favoritesIdx(favoritesIdx) + .notificationResponse(notificationResponse) .build(); } } diff --git a/src/main/java/com/nadoyagsa/pillaroid/service/MedicineService.java b/src/main/java/com/nadoyagsa/pillaroid/service/MedicineService.java index a164630..97b36eb 100644 --- a/src/main/java/com/nadoyagsa/pillaroid/service/MedicineService.java +++ b/src/main/java/com/nadoyagsa/pillaroid/service/MedicineService.java @@ -10,12 +10,16 @@ import com.nadoyagsa.pillaroid.component.MedicineExcelUtils; import com.nadoyagsa.pillaroid.dto.MedicineResponse; +import com.nadoyagsa.pillaroid.dto.NotificationResponse; import com.nadoyagsa.pillaroid.dto.PrescriptionResponse; import com.nadoyagsa.pillaroid.dto.VoiceResponse; import com.nadoyagsa.pillaroid.entity.Favorites; import com.nadoyagsa.pillaroid.entity.Medicine; +import com.nadoyagsa.pillaroid.entity.Notification; import com.nadoyagsa.pillaroid.repository.FavoritesRepository; import com.nadoyagsa.pillaroid.repository.MedicineRepository; +import com.nadoyagsa.pillaroid.repository.NotificationRepository; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -24,12 +28,16 @@ public class MedicineService { private final MedicineRepository medicineRepository; private final MedicineExcelUtils medicineExcelUtils; private final FavoritesRepository favoritesRepository; + private final NotificationRepository notificationRepository; @Autowired - public MedicineService(MedicineRepository medicineRepository, MedicineExcelUtils medicineExcelUtils, FavoritesRepository favoritesRepository) { + public MedicineService(MedicineRepository medicineRepository, MedicineExcelUtils medicineExcelUtils, + FavoritesRepository favoritesRepository, + NotificationRepository notificationRepository) { this.medicineRepository = medicineRepository; this.medicineExcelUtils = medicineExcelUtils; this.favoritesRepository = favoritesRepository; + this.notificationRepository = notificationRepository; } public Optional getMedicineInfoByIdx(int idx) { @@ -75,7 +83,7 @@ public List getMedicineListByNameList(String[] nameList) { boolean isAdded = false; for (Medicine medicine : medicineList) { if (medicine.getName().strip().equals(name)) { - prescriptionList.add(medicine.toPrescriptionResponse(null)); + prescriptionList.add(medicine.toPrescriptionResponse(null, null)); isAdded = true; break; @@ -83,7 +91,7 @@ public List getMedicineListByNameList(String[] nameList) { } if (!isAdded) - prescriptionList.add(medicineList.get(0).toPrescriptionResponse(null)); + prescriptionList.add(medicineList.get(0).toPrescriptionResponse(null, null)); } return prescriptionList; } @@ -98,11 +106,13 @@ public List getMedicineListByNameList(String[] nameList, L for (Medicine medicine : medicineList) { if (medicine.getName().strip().equals(name)) { Optional favorites = findFavoritesByUserAndMedicineIdx(userIdx, medicine.getMedicineIdx()); + Optional notification = findNotificationByUserAndMedicineIdx(userIdx, medicine.getMedicineIdx()); + + // Optional에 값이 없으면 null로 저장 + Long favoritesIdx = favorites.map(Favorites::getFavoritesIdx).orElse(null); + NotificationResponse notificationResponse = notification.map(Notification::toNotificationResponse).orElse(null); - if (favorites.isPresent()) - prescriptionList.add(medicine.toPrescriptionResponse(favorites.get().getFavoritesIdx())); - else - prescriptionList.add(medicine.toPrescriptionResponse(null)); + prescriptionList.add(medicine.toPrescriptionResponse(favoritesIdx, notificationResponse)); isAdded = true; break; @@ -112,11 +122,13 @@ public List getMedicineListByNameList(String[] nameList, L if (!isAdded) { Medicine firstMedicine = medicineList.get(0); Optional favorites = findFavoritesByUserAndMedicineIdx(userIdx, firstMedicine.getMedicineIdx()); + Optional notification = findNotificationByUserAndMedicineIdx(userIdx, firstMedicine.getMedicineIdx()); + + // Optional에 값이 없으면 null로 저장 + Long favoritesIdx = favorites.map(Favorites::getFavoritesIdx).orElse(null); + NotificationResponse notificationResponse = notification.map(Notification::toNotificationResponse).orElse(null); - if (favorites.isPresent()) - prescriptionList.add(firstMedicine.toPrescriptionResponse(favorites.get().getFavoritesIdx())); - else - prescriptionList.add(firstMedicine.toPrescriptionResponse(null)); + prescriptionList.add(firstMedicine.toPrescriptionResponse(favoritesIdx, notificationResponse)); } } return prescriptionList; @@ -128,6 +140,11 @@ public Optional findFavoritesByUserAndMedicineIdx(Long userIdx, int m return favoritesRepository.findFavoritesByUserAndMedicine(userIdx, medicineIdx); } + // 의약품에 해당하는 사용자 알림 조회 + public Optional findNotificationByUserAndMedicineIdx(Long userIdx, int medicineIdx) { + return notificationRepository.findByUserIdxAndMedicineIdx(userIdx, medicineIdx); + } + public boolean updateMedicineInfoInExcel() { try { medicineExcelUtils.updateMedicineExcel();