Skip to content

Commit

Permalink
Refactor: drug 조회시 drugInfo 응답 (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
pingowl authored May 27, 2024
1 parent f381e7e commit dac7429
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 25 deletions.
8 changes: 4 additions & 4 deletions src/main/java/com/drugbox/controller/DrugController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,8 +29,8 @@ public class DrugController {
private final DrugApiService drugApiService;

@GetMapping("")
public ResponseEntity<List<DrugResponse>> getDrugs(@RequestParam(value="drugboxId") Long drugboxId) {
List<DrugResponse> response = drugService.getDrugs(drugboxId);
public ResponseEntity<List<DrugDetailResponse>> getDrugs(@RequestParam(value="drugboxId") Long drugboxId) {
List<DrugDetailResponse> response = drugService.getDrugs(drugboxId);
return ResponseEntity.status(HttpStatus.OK).body(response);
}

Expand All @@ -57,7 +57,7 @@ public ResponseEntity<Void> disposeDrug(@RequestParam(value="drugboxId") Long dr
}

@GetMapping("/search-result")
public ResponseEntity<List<String>> searchDrugs(@RequestParam(value="name") String name) throws IOException, ParseException {
public ResponseEntity<List<String>> searchDrugInfoByName(@RequestParam(value="name") String name) throws IOException, ParseException {
List<String> response = drugApiService.getSearchDrugs(name);
return ResponseEntity.status(HttpStatus.OK).body(response);
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/drugbox/domain/Drug.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<DrugResponse> drugResponseList;
private List<DrugResponse> drugResponses;
private String effect;
}
8 changes: 4 additions & 4 deletions src/main/java/com/drugbox/service/DrugApiService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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 * ?")
Expand Down
47 changes: 33 additions & 14 deletions src/main/java/com/drugbox/service/DrugService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -55,9 +57,9 @@ public List<Long> 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, "약 추가 알림",
Expand All @@ -68,14 +70,30 @@ public List<Long> addDrugs(DrugSaveRequest request, Long userId) throws IOExcept
}

// 의약품 리스트 확인하기
public List<DrugResponse> getDrugs(Long drugboxId){
public List<DrugDetailResponse> getDrugs(Long drugboxId){
getDrugboxOrThrow(drugboxId);
List<Drug> drugs = drugRepository.findAllByDrugboxId(drugboxId);
return drugs.stream()
.map(drug -> getDrugOrThrow(drug))
.filter(drug -> !drug.isInDisposalList())
.map(drug-> DrugToDrugResponse(drug))
Map<String, List<Drug>> groups = getDrugsFromDrugboxGroupedByName(drugboxId);
List<DrugDetailResponse> drugDetailResponses = groups.entrySet().stream()
.map(entry -> {
String name = entry.getKey();
List<DrugResponse> 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;
}

// 의약품 사용하기
Expand Down Expand Up @@ -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> drugInfo = drugInfoRepository.findByName(name);
if(drugInfo.isEmpty()){
drugApiService.getDrugInfo(name);
return drugApiService.getDrugInfo(name);
}
return drugInfo.get();
}

private User getUserOrThrow(Long userId) {
Expand Down Expand Up @@ -242,4 +256,9 @@ public void sendDrugNearExpiredNotification(){
drugbox.getName()+"에 들어있는 "+drug.getName()+"의 유통기한이 1주일 남았습니다. 교체하세요!");
}
}

public Map<String, List<Drug>> getDrugsFromDrugboxGroupedByName(Long drugboxId) {
List<Drug> drugs = drugRepository.findAllByDrugboxId(drugboxId);
return drugs.stream().collect(Collectors.groupingBy(Drug::getName));
}
}

0 comments on commit dac7429

Please sign in to comment.