diff --git a/src/main/java/com/codingwasabi/bigtong/main/api/APISerivce.java b/src/main/java/com/codingwasabi/bigtong/main/api/APISerivce.java index bf581b6..574fb84 100644 --- a/src/main/java/com/codingwasabi/bigtong/main/api/APISerivce.java +++ b/src/main/java/com/codingwasabi/bigtong/main/api/APISerivce.java @@ -1,6 +1,7 @@ package com.codingwasabi.bigtong.main.api; import com.codingwasabi.bigtong.main.api.subject.entity.*; +import com.codingwasabi.bigtong.main.api.subject.exception.NoDataInDBandInput; import com.codingwasabi.bigtong.main.api.subject.exception.NoPreviousDataInTable; import com.codingwasabi.bigtong.main.api.subject.repository.*; import com.codingwasabi.bigtong.main.dto.Item; @@ -11,6 +12,7 @@ import com.fasterxml.jackson.dataformat.xml.XmlMapper; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.apache.tomcat.jni.Local; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.stereotype.Service; @@ -25,6 +27,7 @@ import java.util.ArrayList; import java.util.Comparator; import java.util.List; +import java.util.Optional; @Service @Slf4j @@ -36,7 +39,8 @@ public class APISerivce { private final FishRepository fishRepository; private final VegetableRepository vegetableRepository; private final MeatRepository meatRepository; - + private List updatedList; + private int index; // 데이터 베이스로 부터 최신 5개 불러오기 public List returnTop5(String subject){ @@ -54,77 +58,114 @@ else if (subject.equals("MEAT")) return null; } + private LocalDateTime dateFormat(String bidtime){ + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yy-MM-dd HH:mm:ss"); + bidtime = "21-".concat(bidtime); + bidtime = bidtime.replace("/", "-"); + + return LocalDateTime.parse(bidtime, formatter); + + } + /** * 동일한 데이터인지 여부 확인 * 동일한 데이터 -> 업데이트 안함 : false * 동일한 데이터가 아님 -> 업데이트 진행 : true - * @param item + * @param itemList * @param subject * @return */ - private boolean checkUpdated(Item item, Subject subject){ - if(item == null) - return false; + private Optional> checkUpdated(List itemList, Subject subject){ + + if(!itemList.isEmpty()){ + + // 데이터베이스에 저장된 subject가 없다면 들어온 데이터 전부 입력 + if(subject.getBidtime() == null) + return Optional.of(itemList); + + else{ + // 05/30 HH:mm:ss -> 21-05-30 HH:mm:ss 로 포맷 + // localdatetime 으로 포매팅 + + LocalDateTime subject_bidtime = dateFormat(subject.getBidtime()); + + updatedList = new ArrayList<>(); + + for(Item item : itemList){ - else if(item.mclassname.equals(subject.getMclassname()) - && item.bidtime.equals(subject.getBidtime()) - && item.price.equals(subject.getPrice())) - return false; + LocalDateTime item_bidtime = dateFormat(item.bidtime); - return true; + // item의 날짜가 더 최신이라면 + if(item_bidtime.isAfter(subject_bidtime)) + updatedList.add(item); + } + return Optional.of(updatedList); + } + } + return Optional.empty(); } @Transactional - public Subject manageSubject(String[] subjectNum,String subject ){ + public List manageSubject(String[] subjectNum,String subject ){ String now = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); - if(subject.equals("GRAIN")){ + + // null이면 값이 없는 빈 객체 입력 -> exception 발생 안하는 이유는, 업데이트로 값이 채워지기 때문이다. Grain grain = grainRepository.findFirstByOrderByBidtimeDesc() .orElse(new Grain()); - List itemList = apiEndPoint(now,subjectNum); + // optional 로 바꾸기 + List itemList = checkUpdated(apiEndPoint(now,subjectNum),grain) + .orElseThrow(NoDataInDBandInput::new); + // 업데이트가 되었다면 + if(!itemList.isEmpty()){ - if(!itemList.isEmpty()) { + // 리턴할 리스트 + List updateSubjectList = new ArrayList<>(); + index=0; - if(checkUpdated(itemList.get(0),grain)) { - int index = 0; - for (Item item : itemList) { - if (index > 5) - break; - index++; - Grain new_grain = new Grain(item.bidtime, item.mclassname, item.price, item.unitname); - grain = new_grain; + for(Item item : itemList){ + Grain new_grain = new Grain(item.bidtime,item.mclassname,item.price,item.unitname); grainRepository.save(new_grain); - } - return grain; + updateSubjectList.add(new_grain); + index++; } + return updateSubjectList; } + } + else if (subject.equals("FISH")){ Fish fish = fishRepository.findFirstByOrderByBidtimeDesc() .orElse(new Fish()); - List itemList = apiEndPoint(now,subjectNum); + List itemList = checkUpdated(apiEndPoint(now,subjectNum),fish) + .orElseThrow(NoDataInDBandInput::new); // update 가 되었다면 if(!itemList.isEmpty()) { - if(checkUpdated(itemList.get(0),fish)) { - int index = 0; + if(!itemList.isEmpty()) { + index = 0; + List updateSubjectList = new ArrayList<>(); + for (Item item : itemList) { if (index > 5) break; + else { + Fish new_fish = new Fish(item.bidtime, item.mclassname, item.price, item.unitname); + fishRepository.save(new_fish); + updateSubjectList.add(new_fish); + } index++; - Fish new_fish = new Fish(item.bidtime, item.mclassname, item.price, item.unitname); - fishRepository.save(new_fish); } - return fish; + return updateSubjectList; } } } @@ -135,22 +176,25 @@ else if (subject.equals("FRUIT")){ Fruit fruit = fruitRepository.findFirstByOrderByBidtimeDesc() .orElse(new Fruit()); - List itemList = apiEndPoint(now,subjectNum); + List itemList = checkUpdated(apiEndPoint(now,subjectNum),fruit) + .orElseThrow(NoDataInDBandInput::new); // update 가 되었다면 if(!itemList.isEmpty()) { - - if(checkUpdated(itemList.get(0),fruit)) { - int index = 0; - for (Item item : itemList) { - if (index > 5) - break; - index++; - Fruit new_fruit = new Fruit(item.bidtime, item.mclassname, item.price, item.unitname); + index= 0; + List updateSubjectList = new ArrayList<>(); + + for(Item item : itemList){ + if(index>5) + break; + else{ + Fruit new_fruit = new Fruit(item.bidtime,item.mclassname,item.price,item.unitname); fruitRepository.save(new_fruit); + updateSubjectList.add(new_fruit); } - return fruit; + index++; } + return updateSubjectList; } } @@ -160,46 +204,53 @@ else if (subject.equals("VEGETABLE")){ Vegetable vegetable = vegetableRepository.findFirstByOrderByBidtimeDesc() .orElse(new Vegetable()); - List itemList = apiEndPoint(now,subjectNum); + List itemList = checkUpdated(apiEndPoint(now,subjectNum),vegetable) + .orElseThrow(NoDataInDBandInput::new); // update 가 되었다면 if(!itemList.isEmpty()) { - - if(checkUpdated(itemList.get(0),vegetable)) { - int index = 0; - for (Item item : itemList) { - if (index > 5) - break; - index++; - Vegetable new_vegetable = new Vegetable(item.bidtime, item.mclassname, item.price, item.unitname); + index = 0; + List updateSubjectList = new ArrayList<>(); + + for(Item item : itemList){ + if(index>5) + break; + else{ + Vegetable new_vegetable = new Vegetable(item.bidtime,item.mclassname,item.price,item.unitname); vegetableRepository.save(new_vegetable); + updateSubjectList.add(new_vegetable); } - - return vegetable; + index++; } + return updateSubjectList; } } + else if(subject.equals("MEAT")){ Meat meat = meatRepository.findFirstByOrderByBidtimeDesc() .orElse(new Meat()); - List itemList = apiEndPoint(now,subjectNum); + List itemList = checkUpdated(apiEndPoint(now,subjectNum),meat) + .orElseThrow(NoDataInDBandInput::new); // update 가 되었다면 if(!itemList.isEmpty()) { - if(checkUpdated(itemList.get(0),meat)) { - int index = 0; - for (Item item : itemList) { - if (index > 5) - break; - index++; - Meat new_meat = new Meat(item.bidtime, item.mclassname, item.price, item.unitname); - meatRepository.save(new_meat); - } - return meat; + index=0; + List updateSubjectList = new ArrayList<>(); + + for(Item item : itemList){ + if(index>5) + break; + else{ + Meat new_meat = new Meat(item.bidtime,item.mclassname,item.price,item.unitname); + meatRepository.save(new_meat); + updateSubjectList.add(new_meat); } + index++; + } + return updateSubjectList; } } @@ -225,7 +276,6 @@ public List apiEndPoint(String now, String[] objects){ if (response.body.items != null) { for (Item item : response.body.getItems()){ objectItemList.add(item); - } } diff --git a/src/main/java/com/codingwasabi/bigtong/main/api/subject/exception/NoDataInDBandInput.java b/src/main/java/com/codingwasabi/bigtong/main/api/subject/exception/NoDataInDBandInput.java new file mode 100644 index 0000000..82848f1 --- /dev/null +++ b/src/main/java/com/codingwasabi/bigtong/main/api/subject/exception/NoDataInDBandInput.java @@ -0,0 +1,7 @@ +package com.codingwasabi.bigtong.main.api.subject.exception; + +public class NoDataInDBandInput extends RuntimeException{ + public NoDataInDBandInput (){ + super("저장된 데이터가 없으며, 입력된 데이터도 없습니다."); + } +} diff --git a/src/main/java/com/codingwasabi/bigtong/websocket/handler/WebSocketChatHandler.java b/src/main/java/com/codingwasabi/bigtong/websocket/handler/WebSocketChatHandler.java index baeec7d..89e9c9b 100644 --- a/src/main/java/com/codingwasabi/bigtong/websocket/handler/WebSocketChatHandler.java +++ b/src/main/java/com/codingwasabi/bigtong/websocket/handler/WebSocketChatHandler.java @@ -22,6 +22,7 @@ import org.springframework.web.socket.handler.TextWebSocketHandler; import java.util.HashMap; +import java.util.List; import java.util.Map; @Slf4j @@ -113,10 +114,11 @@ public void renewList_grain(){ .orElseThrow(ChatRoomNotExistException::new); // 업데이트가 되었다면 ws 로 데이터 전송 - Subject grain = apiSerivce.manageSubject(GRAIN,"GRAIN"); + List grainList = apiSerivce.manageSubject(GRAIN,"GRAIN"); - if(grain != null) - chatService.sendMessageAll(makeUpdateMessage(grain, RoomType.GRAIN), chatRoom, webSocketSessionMap); + for(Subject subject : grainList){ + chatService.sendMessageAll(makeUpdateMessage(subject,RoomType.GRAIN),chatRoom,webSocketSessionMap); + } } // 60초 마다 실행 test @@ -125,11 +127,12 @@ public void renewList_fruit(){ ChatRoom chatRoom = chatRoomRepository.findChatRoomByType(RoomType.FRUIT) .orElseThrow(ChatRoomNotExistException::new); // 업데이트가 되었다면 ws 로 데이터 전송 - Subject fruit = apiSerivce.manageSubject(FRUIT,"FRUIT"); + List fruitList = apiSerivce.manageSubject(FRUIT,"FRUIT"); - if(fruit != null) - chatService.sendMessageAll(makeUpdateMessage(fruit, RoomType.FRUIT),chatRoom,webSocketSessionMap); + for(Subject subject : fruitList){ + chatService.sendMessageAll(makeUpdateMessage(subject,RoomType.FRUIT),chatRoom,webSocketSessionMap); + } } @@ -139,12 +142,10 @@ public void renewList_fish(){ ChatRoom chatRoom = chatRoomRepository.findChatRoomByType(RoomType.FISH) .orElseThrow(ChatRoomNotExistException::new); // 업데이트가 되었다면 ws 로 데이터 전송 - Subject fish = apiSerivce.manageSubject(FISH,"FISH"); - - - if(fish != null) - chatService.sendMessageAll(makeUpdateMessage(fish, RoomType.FISH),chatRoom,webSocketSessionMap); + List fishList = apiSerivce.manageSubject(FISH,"FISH"); + for(Subject subject:fishList) + chatService.sendMessageAll(makeUpdateMessage(subject,RoomType.FISH),chatRoom,webSocketSessionMap); } // 60초 마다 실행 test @@ -153,11 +154,10 @@ public void renewList_vegetable(){ ChatRoom chatRoom = chatRoomRepository.findChatRoomByType(RoomType.VEGETABLE) .orElseThrow(ChatRoomNotExistException::new); // 업데이트가 되었다면 ws 로 데이터 전송 - Subject vegetable = apiSerivce.manageSubject(VEGETABLE,"VEGETABLE"); + List vegetableList = apiSerivce.manageSubject(VEGETABLE,"VEGETABLE"); - - if(vegetable != null) - chatService.sendMessageAll(makeUpdateMessage(vegetable, RoomType.VEGETABLE),chatRoom,webSocketSessionMap); + for(Subject subject:vegetableList) + chatService.sendMessageAll(makeUpdateMessage(subject,RoomType.VEGETABLE),chatRoom,webSocketSessionMap); } // 60초 마다 실행 test @@ -166,11 +166,10 @@ public void renewList_meat(){ ChatRoom chatRoom = chatRoomRepository.findChatRoomByType(RoomType.MEAT) .orElseThrow(ChatRoomNotExistException::new); // 업데이트가 되었다면 ws 로 데이터 전송 - Subject meat = apiSerivce.manageSubject(MEAT,"MEAT"); - + List meatList = apiSerivce.manageSubject(MEAT,"MEAT"); - if(meat != null) - chatService.sendMessageAll(makeUpdateMessage(meat, RoomType.MEAT),chatRoom,webSocketSessionMap); + for(Subject subject:meatList) + chatService.sendMessageAll(makeUpdateMessage(subject,RoomType.MEAT),chatRoom,webSocketSessionMap); } }