Skip to content

Commit

Permalink
[feat] 의약품, 처방전 결과에 알림 표시 (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
soeunkk committed Aug 29, 2022
1 parent db96f89 commit a47195d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,7 +32,6 @@
public class MedicineController {
private final MedicineService medicineService;
private final BarcodeService barcodeService;
private final MedicineExcelUtils medicineExcelUtils;

private final AuthTokenProvider authTokenProvider;

Expand All @@ -41,15 +41,7 @@ public ApiResponse<MedicineResponse> getMedicineInfo(HttpServletRequest request,
Optional<MedicineResponse> medicineResponse = medicineService.getMedicineInfoByIdx(idx);

if (medicineResponse.isPresent()) {
if (request.getHeader("authorization") != null) { // 로그인 된 사용자라면 즐겨찾기 여부를 보여줌
Long userIdx = findUserIdxByToken(request);
Optional<Favorites> 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;
Expand All @@ -62,14 +54,14 @@ public ApiResponse<MedicineResponse> getMedicineInfo(HttpServletRequest request,
Optional<MedicineResponse> 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;
Expand Down Expand Up @@ -121,14 +113,19 @@ public Long findUserIdxByToken(HttpServletRequest request) {
}
}

private ApiResponse<MedicineResponse> reflectFavoritesAboutMedicine(HttpServletRequest request, MedicineResponse medicineResponse) {
private ApiResponse<MedicineResponse> reflectFavoritesAndNotificationAboutMedicine(HttpServletRequest request, MedicineResponse medicineResponse) {
if (request.getHeader("authorization") != null) { // 로그인 된 사용자라면 즐겨찾기 여부를 보여줌
Long userIdx = findUserIdxByToken(request);
Optional<Favorites> favorites = medicineService.findFavoritesByUserAndMedicineIdx(userIdx, medicineResponse.getMedicineIdx());

Optional<Favorites> favorites = medicineService.findFavoritesByUserAndMedicineIdx(userIdx, medicineResponse.getMedicineIdx());
if (favorites.isPresent()) { // 즐겨찾기 설정을 했을 시
medicineResponse.setFavoritesIdx(favorites.get().getFavoritesIdx());
}

Optional<Notification> notification = medicineService.findNotificationByUserAndMedicineIdx(userIdx, medicineResponse.getMedicineIdx());
if (notification.isPresent()) { // 알림 설정을 했을 시
medicineResponse.setNotificationResponse(notification.get().toNotificationResponse());
}
}
return ApiResponse.success(medicineResponse);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ public class MedicineResponse {

@Setter
private Long favoritesIdx = null;

@Setter
private NotificationResponse notificationResponse = null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ public class PrescriptionResponse {
private String dosage; // 용법용량

private Long favoritesIdx;
private NotificationResponse notificationResponse;
}
4 changes: 3 additions & 1 deletion src/main/java/com/nadoyagsa/pillaroid/entity/Medicine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,14 +82,15 @@ public VoiceResponse toVoiceResponse() {
}

@JsonIgnore
public PrescriptionResponse toPrescriptionResponse(Long favoritesIdx) {
public PrescriptionResponse toPrescriptionResponse(Long favoritesIdx, NotificationResponse notificationResponse) {
return PrescriptionResponse.builder()
.medicineIdx(medicineIdx)
.name(name)
.appearance(appearance)
.efficacy(efficacy)
.dosage(dosage)
.favoritesIdx(favoritesIdx)
.notificationResponse(notificationResponse)
.build();
}
}
39 changes: 28 additions & 11 deletions src/main/java/com/nadoyagsa/pillaroid/service/MedicineService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<MedicineResponse> getMedicineInfoByIdx(int idx) {
Expand Down Expand Up @@ -75,15 +83,15 @@ public List<PrescriptionResponse> 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;
}
}

if (!isAdded)
prescriptionList.add(medicineList.get(0).toPrescriptionResponse(null));
prescriptionList.add(medicineList.get(0).toPrescriptionResponse(null, null));
}
return prescriptionList;
}
Expand All @@ -98,11 +106,13 @@ public List<PrescriptionResponse> getMedicineListByNameList(String[] nameList, L
for (Medicine medicine : medicineList) {
if (medicine.getName().strip().equals(name)) {
Optional<Favorites> favorites = findFavoritesByUserAndMedicineIdx(userIdx, medicine.getMedicineIdx());
Optional<Notification> 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;
Expand All @@ -112,11 +122,13 @@ public List<PrescriptionResponse> getMedicineListByNameList(String[] nameList, L
if (!isAdded) {
Medicine firstMedicine = medicineList.get(0);
Optional<Favorites> favorites = findFavoritesByUserAndMedicineIdx(userIdx, firstMedicine.getMedicineIdx());
Optional<Notification> 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;
Expand All @@ -128,6 +140,11 @@ public Optional<Favorites> findFavoritesByUserAndMedicineIdx(Long userIdx, int m
return favoritesRepository.findFavoritesByUserAndMedicine(userIdx, medicineIdx);
}

// 의약품에 해당하는 사용자 알림 조회
public Optional<Notification> findNotificationByUserAndMedicineIdx(Long userIdx, int medicineIdx) {
return notificationRepository.findByUserIdxAndMedicineIdx(userIdx, medicineIdx);
}

public boolean updateMedicineInfoInExcel() {
try {
medicineExcelUtils.updateMedicineExcel();
Expand Down

0 comments on commit a47195d

Please sign in to comment.