-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Glass, Dispenser API, WebSocket
- Loading branch information
Showing
12 changed files
with
181 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 0 additions & 4 deletions
4
src/main/java/dcom/ssdbackend/api/domain/dispenser/dto/DispenserRequestDto.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 67 additions & 15 deletions
82
src/main/java/dcom/ssdbackend/api/domain/glass/service/GlassService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,100 @@ | ||
package dcom.ssdbackend.api.domain.glass.service; | ||
|
||
import dcom.ssdbackend.api.domain.dispenser.Dispenser; | ||
import dcom.ssdbackend.api.domain.dispenser.repository.DispenserRepository; | ||
import dcom.ssdbackend.api.domain.glass.Glass; | ||
import dcom.ssdbackend.api.domain.glass.dto.GlassRequestDto; | ||
import dcom.ssdbackend.api.domain.glass.dto.GlassResponseDto; | ||
import dcom.ssdbackend.api.domain.glass.repository.GlassRepository; | ||
import dcom.ssdbackend.api.global.jwt.JwtProvider; | ||
import dcom.ssdbackend.api.global.websocket.WebSocketHandler; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.socket.TextMessage; | ||
import org.springframework.web.socket.WebSocketSession; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class GlassService { | ||
private final GlassRepository glassRepository; | ||
private final DispenserRepository dispenserRepository; | ||
private final JwtProvider jwtProvider; | ||
|
||
|
||
public void addGlass(GlassRequestDto.AddGlass addGlass) throws IOException{ | ||
Glass glass = new Glass(); | ||
Dispenser dispenser = dispenserRepository.findById(addGlass.getDispenserId()).get(); | ||
|
||
glass.setId(addGlass.getGlassId()); | ||
glass.setDispenser(dispenser); | ||
glass.setCurrentDrink(0); | ||
glass.setLastDrinkTimeStamp(0L); | ||
|
||
glassRepository.save(glass); | ||
|
||
List<WebSocketSession> drinkerWebSocketSessionList = WebSocketHandler.drinkerMap.get(glass.getDispenser().getId()); | ||
|
||
if(!drinkerWebSocketSessionList.isEmpty()) { | ||
for (WebSocketSession s : drinkerWebSocketSessionList) { | ||
s.sendMessage(new TextMessage("{\"eventType\":\"change\"}")); | ||
} | ||
} | ||
} | ||
|
||
public List<GlassResponseDto.GlassInfo> getGlass(){ | ||
String drinkerToken = jwtProvider.getDrinkerTokenInHeader(); | ||
boolean bool = jwtProvider.verifyDrinkerToken(drinkerToken); | ||
List<Glass> glasses = glassRepository.findAll(); | ||
return GlassResponseDto.GlassInfo.of(glasses); | ||
} | ||
|
||
public void updateGlass(String glassId, GlassRequestDto.UpdateGlass updateGlass) { | ||
public void updateGlass(String glassId, GlassRequestDto.UpdateGlass updateGlass) throws IOException { | ||
Glass glass = glassRepository.findById(glassId).get(); | ||
|
||
glass.setDrinkerName(updateGlass.getDrinkerName()); | ||
glass.setDetail(updateGlass.getDetail()); | ||
glass.setTotalCapacity(updateGlass.getTotalCapacity()); | ||
|
||
glass.builder() | ||
.id(glass.getId()) | ||
.drinkerName(glass.getDrinkerName()) | ||
.detail(glass.getDetail()) | ||
.totalCapacity(glass.getTotalCapacity()) | ||
.currentDrink(glass.getCurrentDrink()) | ||
.lastDrinkTimeStamp(glass.getLastDrinkTimeStamp()) | ||
.dispenser(glass.getDispenser()) | ||
.build(); | ||
|
||
glassRepository.save(glass); | ||
|
||
List<WebSocketSession> drinkerWebSocketSessionList = WebSocketHandler.drinkerMap.get(glass.getDispenser().getId()); | ||
|
||
if(!drinkerWebSocketSessionList.isEmpty()) { | ||
for (WebSocketSession s : drinkerWebSocketSessionList) { | ||
s.sendMessage(new TextMessage("{\"eventType\":\"change\"}")); | ||
} | ||
} | ||
} | ||
|
||
public void deleteGlass(String glassId) { | ||
glassRepository.deleteById(glassId); | ||
public void deleteGlass(String glassId) throws IOException { | ||
Glass glass = glassRepository.findById(glassId).get(); | ||
|
||
List<WebSocketSession> drinkerWebSocketSessionList = WebSocketHandler.drinkerMap.get(glass.getDispenser().getId()); | ||
|
||
glassRepository.delete(glass); | ||
|
||
if(!drinkerWebSocketSessionList.isEmpty()) { | ||
for (WebSocketSession s : drinkerWebSocketSessionList) { | ||
s.sendMessage(new TextMessage("{\"eventType\":\"change\"}")); | ||
} | ||
} | ||
} | ||
|
||
public void drinkOneGlass(String glassId) throws IOException{ | ||
Glass glass = glassRepository.findById(glassId).get(); | ||
|
||
List<WebSocketSession> drinkerWebSocketSessionList = WebSocketHandler.drinkerMap.get(glass.getDispenser().getId()); | ||
|
||
glass.setCurrentDrink(glass.getCurrentDrink() + 1); | ||
glass.setLastDrinkTimeStamp(System.currentTimeMillis()); | ||
glassRepository.save(glass); | ||
|
||
if(!drinkerWebSocketSessionList.isEmpty()) { | ||
for (WebSocketSession s : drinkerWebSocketSessionList) { | ||
s.sendMessage(new TextMessage("{\"eventType\":\"change\"}")); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.