Skip to content

Commit

Permalink
#4 실시간 알림 기능 재수정 완료
Browse files Browse the repository at this point in the history
(배포 까지)
  • Loading branch information
daehwan2yo committed Jun 1, 2021
1 parent ec9cedf commit 74d5b6a
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 82 deletions.
176 changes: 113 additions & 63 deletions src/main/java/com/codingwasabi/bigtong/main/api/APISerivce.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -25,6 +27,7 @@
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

@Service
@Slf4j
Expand All @@ -36,7 +39,8 @@ public class APISerivce {
private final FishRepository fishRepository;
private final VegetableRepository vegetableRepository;
private final MeatRepository meatRepository;

private List<Item> updatedList;
private int index;

// 데이터 베이스로 부터 최신 5개 불러오기
public List<Subject> returnTop5(String subject){
Expand All @@ -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<List<Item>> checkUpdated(List<Item> 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<Subject> 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<Item> itemList = apiEndPoint(now,subjectNum);
// optional 로 바꾸기
List<Item> itemList = checkUpdated(apiEndPoint(now,subjectNum),grain)
.orElseThrow(NoDataInDBandInput::new);

// 업데이트가 되었다면
if(!itemList.isEmpty()){

if(!itemList.isEmpty()) {
// 리턴할 리스트
List<Subject> 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<Item> itemList = apiEndPoint(now,subjectNum);
List<Item> 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<Subject> 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;
}
}
}
Expand All @@ -135,22 +176,25 @@ else if (subject.equals("FRUIT")){
Fruit fruit = fruitRepository.findFirstByOrderByBidtimeDesc()
.orElse(new Fruit());

List<Item> itemList = apiEndPoint(now,subjectNum);
List<Item> 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<Subject> 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;
}
}

Expand All @@ -160,46 +204,53 @@ else if (subject.equals("VEGETABLE")){
Vegetable vegetable = vegetableRepository.findFirstByOrderByBidtimeDesc()
.orElse(new Vegetable());

List<Item> itemList = apiEndPoint(now,subjectNum);
List<Item> 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<Subject> 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<Item> itemList = apiEndPoint(now,subjectNum);
List<Item> 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<Subject> 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;
}
}

Expand All @@ -225,7 +276,6 @@ public List<Item> apiEndPoint(String now, String[] objects){
if (response.body.items != null) {
for (Item item : response.body.getItems()){
objectItemList.add(item);

}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.codingwasabi.bigtong.main.api.subject.exception;

public class NoDataInDBandInput extends RuntimeException{
public NoDataInDBandInput (){
super("저장된 데이터가 없으며, 입력된 데이터도 없습니다.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.web.socket.handler.TextWebSocketHandler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
Expand Down Expand Up @@ -113,10 +114,11 @@ public void renewList_grain(){
.orElseThrow(ChatRoomNotExistException::new);

// 업데이트가 되었다면 ws 로 데이터 전송
Subject grain = apiSerivce.manageSubject(GRAIN,"GRAIN");
List<Subject> 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
Expand All @@ -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<Subject> 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);
}

}

Expand All @@ -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<Subject> fishList = apiSerivce.manageSubject(FISH,"FISH");

for(Subject subject:fishList)
chatService.sendMessageAll(makeUpdateMessage(subject,RoomType.FISH),chatRoom,webSocketSessionMap);
}

// 60초 마다 실행 test
Expand All @@ -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<Subject> 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
Expand All @@ -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<Subject> 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);

}
}

0 comments on commit 74d5b6a

Please sign in to comment.