From 739b1eb4c8a4001ff5fd4ea49c1554dd92b045ad Mon Sep 17 00:00:00 2001 From: Julia Butenhoff Date: Tue, 12 Jul 2022 11:36:11 -0500 Subject: [PATCH] Implement the Load Crib method - Needs to be tested - Need to see if different error codes can be returned for no crib found with name vs error loading --- .../UBFunkeysServer/Galaxy/GalaxyServer.java | 76 +++++++++++++++++-- .../UBFunkeysServer/domain/Crib.java | 2 +- .../UBFunkeysServer/domain/User.java | 2 +- .../repository/CribRepository.java | 2 +- .../UBFunkeysServer/service/CribService.java | 4 +- .../service/impl/CribServiceImpl.java | 9 ++- 6 files changed, 82 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/icedberries/UBFunkeysServer/Galaxy/GalaxyServer.java b/src/main/java/com/icedberries/UBFunkeysServer/Galaxy/GalaxyServer.java index a093eb1..8559603 100644 --- a/src/main/java/com/icedberries/UBFunkeysServer/Galaxy/GalaxyServer.java +++ b/src/main/java/com/icedberries/UBFunkeysServer/Galaxy/GalaxyServer.java @@ -37,6 +37,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.StringReader; import java.io.StringWriter; +import java.util.concurrent.ThreadLocalRandom; @RestController public class GalaxyServer { @@ -133,7 +134,7 @@ public class GalaxyServer { case "savecrib": return saveCrib(nodes.item(0)); case "loadcrib": - return loadCrib(doc); + return loadCrib((Element)nodes.item(0)); default: System.out.println("[Galaxy][POST][ERROR] Unhandled type of request for: " + command); return new ResponseEntity<>(HttpStatus.BAD_REQUEST); @@ -145,12 +146,73 @@ public class GalaxyServer { } } - //TODO: USE THIS FOR THE XML PARSING: https://www.tutorialspoint.com/java_xml/java_dom_parse_document.htm# - - private ResponseEntity loadCrib(Document request) { + private ResponseEntity loadCrib(Element element) { System.out.println("[Galaxy][POST] loadcrib request received"); - //TODO: IMPLEMENT METHOD - return null; + + /* + * 0 - Success + * 1 - Error Loading Crib / Can't find a crib with that name (need to test codes) + */ + int resultCode = 0; + String reason = ""; + + // Get the requested crib name + String cribName = element.getAttribute("name"); + + // Check if looking for a specific crib or a random one + String profileData = ""; + if (cribName.isEmpty()) { + // Random Crib Requested ("Surprise Me") + + // Get a total number of all cribs + Integer cribCount = cribService.count(); + + // Ensure there are cribs in the DB + if (cribCount <= 0) { + // No cribs in the db + resultCode = 1; + reason = "Can't find a crib at this time..."; + } else { + // At least 1 crib in the db + + // Use that total to generate a random id number between 1 and that number + Integer randomCribId = ThreadLocalRandom.current().nextInt(1, cribCount + 1); + + // Get the crib + Crib randomCrib = cribService.findById(randomCribId); + + // Make sure the crib was grabbed properly + if (randomCrib == null) { + // Unable to get the crib + resultCode = 1; + reason = "Can't find a crib at this time..."; + } else { + // Random crib grabbed successfully + profileData = randomCrib.getProfileData(); + } + } + } else { + // Specific Crib Requested + Crib requestedCrib = cribService.findByCribName(cribName); + + // Check if a crib with that name exists + if (requestedCrib == null) { + // Crib doesn't exist + resultCode = 1; + reason = "Can't find a crib by that name..."; + } else { + // Crib with that name exists + profileData = requestedCrib.getProfileData(); + } + } + + // Build a response + String stringBuilder = "" + + profileData + + ""; + + // Send the response + return new ResponseEntity<>(stringBuilder, HttpStatus.OK); } private ResponseEntity saveCrib(Node node) { @@ -210,7 +272,7 @@ public class GalaxyServer { // Check if a crib with that name already exists if (cribService.existsByCribName(cribName)) { // That crib already exists, check if the same username - Crib existingCrib = cribService.getByCribName(cribName); + Crib existingCrib = cribService.findByCribName(cribName); if (existingCrib.getUsername().equals(username)) { // Same username, can update crib existingCrib.setProfileData(profileData); diff --git a/src/main/java/com/icedberries/UBFunkeysServer/domain/Crib.java b/src/main/java/com/icedberries/UBFunkeysServer/domain/Crib.java index 43323f8..9120a84 100644 --- a/src/main/java/com/icedberries/UBFunkeysServer/domain/Crib.java +++ b/src/main/java/com/icedberries/UBFunkeysServer/domain/Crib.java @@ -22,7 +22,7 @@ import javax.persistence.Table; public class Crib { @Id - @GeneratedValue(strategy = GenerationType.AUTO) + @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String cribName; diff --git a/src/main/java/com/icedberries/UBFunkeysServer/domain/User.java b/src/main/java/com/icedberries/UBFunkeysServer/domain/User.java index 041efd3..0d7fc05 100644 --- a/src/main/java/com/icedberries/UBFunkeysServer/domain/User.java +++ b/src/main/java/com/icedberries/UBFunkeysServer/domain/User.java @@ -22,7 +22,7 @@ import javax.persistence.Table; public class User { @Id - @GeneratedValue(strategy = GenerationType.AUTO) + @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer UUID; private String username; diff --git a/src/main/java/com/icedberries/UBFunkeysServer/repository/CribRepository.java b/src/main/java/com/icedberries/UBFunkeysServer/repository/CribRepository.java index 589c7c6..03675b9 100644 --- a/src/main/java/com/icedberries/UBFunkeysServer/repository/CribRepository.java +++ b/src/main/java/com/icedberries/UBFunkeysServer/repository/CribRepository.java @@ -9,5 +9,5 @@ public interface CribRepository extends CrudRepository { Boolean existsByCribName(String cribName); - Crib getByCribName(String cribName); + Crib findByCribName(String cribName); } diff --git a/src/main/java/com/icedberries/UBFunkeysServer/service/CribService.java b/src/main/java/com/icedberries/UBFunkeysServer/service/CribService.java index 0d11870..6110ae5 100644 --- a/src/main/java/com/icedberries/UBFunkeysServer/service/CribService.java +++ b/src/main/java/com/icedberries/UBFunkeysServer/service/CribService.java @@ -10,5 +10,7 @@ public interface CribService { Boolean existsByCribName(String cribName); - Crib getByCribName(String cribName); + Crib findByCribName(String cribName); + + Crib findById(Integer id); } diff --git a/src/main/java/com/icedberries/UBFunkeysServer/service/impl/CribServiceImpl.java b/src/main/java/com/icedberries/UBFunkeysServer/service/impl/CribServiceImpl.java index bee2cf1..293a1cb 100644 --- a/src/main/java/com/icedberries/UBFunkeysServer/service/impl/CribServiceImpl.java +++ b/src/main/java/com/icedberries/UBFunkeysServer/service/impl/CribServiceImpl.java @@ -28,7 +28,12 @@ public class CribServiceImpl implements CribService { } @Override - public Crib getByCribName(String cribName) { - return cribRepository.getByCribName(cribName); + public Crib findByCribName(String cribName) { + return cribRepository.findByCribName(cribName); + } + + @Override + public Crib findById(Integer id) { + return cribRepository.findById(id).orElse(null); } }