diff --git a/src/main/java/com/drugbox/controller/DrugController.java b/src/main/java/com/drugbox/controller/DrugController.java index e54184d..ea5f1aa 100644 --- a/src/main/java/com/drugbox/controller/DrugController.java +++ b/src/main/java/com/drugbox/controller/DrugController.java @@ -4,7 +4,7 @@ import com.drugbox.dto.request.DrugSaveRequest; import com.drugbox.dto.request.DrugUpdateRequest; import com.drugbox.dto.response.DisposalResponse; -import com.drugbox.dto.response.DrugResponse; +import com.drugbox.dto.response.DrugDetailResponse; import com.drugbox.dto.response.IdResponse; import com.drugbox.service.DrugApiService; import com.drugbox.service.DrugService; @@ -29,8 +29,8 @@ public class DrugController { private final DrugApiService drugApiService; @GetMapping("") - public ResponseEntity> getDrugs(@RequestParam(value="drugboxId") Long drugboxId) { - List response = drugService.getDrugs(drugboxId); + public ResponseEntity> getDrugs(@RequestParam(value="drugboxId") Long drugboxId) { + List response = drugService.getDrugs(drugboxId); return ResponseEntity.status(HttpStatus.OK).body(response); } @@ -57,7 +57,7 @@ public ResponseEntity disposeDrug(@RequestParam(value="drugboxId") Long dr } @GetMapping("/search-result") - public ResponseEntity> searchDrugs(@RequestParam(value="name") String name) throws IOException, ParseException { + public ResponseEntity> searchDrugInfoByName(@RequestParam(value="name") String name) throws IOException, ParseException { List response = drugApiService.getSearchDrugs(name); return ResponseEntity.status(HttpStatus.OK).body(response); } diff --git a/src/main/java/com/drugbox/domain/Drug.java b/src/main/java/com/drugbox/domain/Drug.java index 0e9b25c..107395f 100644 --- a/src/main/java/com/drugbox/domain/Drug.java +++ b/src/main/java/com/drugbox/domain/Drug.java @@ -38,6 +38,10 @@ public class Drug extends BaseEntity { @OnDelete(action = OnDeleteAction.CASCADE) // delete drugbox -> delete drugs private Drugbox drugbox; + @ManyToOne(fetch = LAZY) + @JoinColumn(name = "druginfo_id") + private DrugInfo drugInfo; + public void setCount(int count) { this.count = count; } diff --git a/src/main/java/com/drugbox/dto/response/DrugDetailResponse.java b/src/main/java/com/drugbox/dto/response/DrugDetailResponse.java index fcdc054..9cbd55d 100644 --- a/src/main/java/com/drugbox/dto/response/DrugDetailResponse.java +++ b/src/main/java/com/drugbox/dto/response/DrugDetailResponse.java @@ -1,14 +1,14 @@ package com.drugbox.dto.response; -import lombok.AllArgsConstructor; import lombok.Builder; -import lombok.NoArgsConstructor; +import lombok.Getter; import java.util.List; +@Getter @Builder public class DrugDetailResponse { private String name; - private List drugResponseList; + private List drugResponses; private String effect; } diff --git a/src/main/java/com/drugbox/service/DrugApiService.java b/src/main/java/com/drugbox/service/DrugApiService.java index ed022e0..33cfa83 100644 --- a/src/main/java/com/drugbox/service/DrugApiService.java +++ b/src/main/java/com/drugbox/service/DrugApiService.java @@ -57,9 +57,9 @@ public String getDrugApi(URL url) throws IOException{ return sb.toString(); } - public void getDrugInfo(String drugName) throws IOException, ParseException{ + public DrugInfo getDrugInfo(String drugName) throws IOException, ParseException{ String result = getDrugApi(createUrlForDrugDetail(drugName)); - addDrugInfo(parseDrugInfo(result)); + return addDrugInfo(parseDrugInfo(result)); } public URL createUrlForDrugDetail(String name) throws IOException{ @@ -78,14 +78,14 @@ public JSONArray parseDrugInfo(String json) throws ParseException { return (JSONArray) object.get("items"); } - public void addDrugInfo(JSONArray array){ + public DrugInfo addDrugInfo(JSONArray array){ JSONObject getInfo = (JSONObject) array.get(0); DrugInfo drugInfo = DrugInfo.builder() .name((String)getInfo.get("itemName")) .effect((String) getInfo.get("efcyQesitm")) .updateDate(LocalDate.parse((String)getInfo.get("updateDe"))) .build(); - drugInfoRepository.save(drugInfo); + return drugInfoRepository.save(drugInfo); } @Scheduled(cron="0 0 0 */14 * ?") diff --git a/src/main/java/com/drugbox/service/DrugService.java b/src/main/java/com/drugbox/service/DrugService.java index a81df16..4d4c49e 100644 --- a/src/main/java/com/drugbox/service/DrugService.java +++ b/src/main/java/com/drugbox/service/DrugService.java @@ -7,6 +7,7 @@ import com.drugbox.dto.request.DrugSaveRequest; import com.drugbox.dto.request.DrugUpdateRequest; import com.drugbox.dto.response.DisposalResponse; +import com.drugbox.dto.response.DrugDetailResponse; import com.drugbox.dto.response.DrugResponse; import com.drugbox.repository.*; import lombok.RequiredArgsConstructor; @@ -20,6 +21,7 @@ import java.time.LocalDate; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; @@ -55,9 +57,9 @@ public List addDrugs(DrugSaveRequest request, Long userId) throws IOExcept .location(detail.getLocation()) .expDate(detail.getExpDate()) .drugbox(drugbox) + .drugInfo(getOrSaveDrugInfo(request.getName())) .build(); drugRepository.save(drug); - saveDrugInfoIfEmpty(request.getName()); ids.add(drug.getId()); sendDrugAddedNotificationToAllOtherDrugboxMember(user, drugbox, "약 추가 알림", @@ -68,14 +70,30 @@ public List addDrugs(DrugSaveRequest request, Long userId) throws IOExcept } // 의약품 리스트 확인하기 - public List getDrugs(Long drugboxId){ + public List getDrugs(Long drugboxId){ getDrugboxOrThrow(drugboxId); - List drugs = drugRepository.findAllByDrugboxId(drugboxId); - return drugs.stream() - .map(drug -> getDrugOrThrow(drug)) - .filter(drug -> !drug.isInDisposalList()) - .map(drug-> DrugToDrugResponse(drug)) + Map> groups = getDrugsFromDrugboxGroupedByName(drugboxId); + List drugDetailResponses = groups.entrySet().stream() + .map(entry -> { + String name = entry.getKey(); + List drugResponses = entry.getValue().stream() + .map(this::getDrugOrThrow) + .filter(drug -> !drug.isInDisposalList()) + .map(this::DrugToDrugResponse) + .collect(Collectors.toList()); + String effect = entry.getValue().stream() + .findFirst() + .map(drug -> drug.getDrugInfo().getEffect()) + .orElse(""); + return DrugDetailResponse.builder() + .name(name) + .drugResponses(drugResponses) + .effect(effect) + .build(); + }) .collect(Collectors.toList()); + + return drugDetailResponses; } // 의약품 사용하기 @@ -165,16 +183,12 @@ private Drug getDrugOrThrowById(Long drugId){ .orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_DRUG)); } - private DrugInfo getDrugInfoOrThrow(String name){ - return drugInfoRepository.findByName(name) - .orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_DRUGINFO)); - } - - private void saveDrugInfoIfEmpty(String name) throws IOException, ParseException { + private DrugInfo getOrSaveDrugInfo(String name) throws IOException, ParseException { Optional drugInfo = drugInfoRepository.findByName(name); if(drugInfo.isEmpty()){ - drugApiService.getDrugInfo(name); + return drugApiService.getDrugInfo(name); } + return drugInfo.get(); } private User getUserOrThrow(Long userId) { @@ -242,4 +256,9 @@ public void sendDrugNearExpiredNotification(){ drugbox.getName()+"에 들어있는 "+drug.getName()+"의 유통기한이 1주일 남았습니다. 교체하세요!"); } } + + public Map> getDrugsFromDrugboxGroupedByName(Long drugboxId) { + List drugs = drugRepository.findAllByDrugboxId(drugboxId); + return drugs.stream().collect(Collectors.groupingBy(Drug::getName)); + } }