diff --git a/src/main/java/at/reisinger/DBConnection.java b/src/main/java/at/reisinger/DBConnection.java index aaac6b4..0a86e90 100644 --- a/src/main/java/at/reisinger/DBConnection.java +++ b/src/main/java/at/reisinger/DBConnection.java @@ -274,18 +274,9 @@ public class DBConnection { damage = rs.getInt("damage"); Card newCard = new Card(cardname, elementtyp + cardtype, damage); cards.addCard(newCard); - Connection b = PostgresHelper.con(); - try { - stmt = b.createStatement(); - String sql = "INSERT INTO public.user_cards (username, name, gesperrt) VALUES ( \'" + username + "\',\'" + newCard.getName() + "\', \'false\');"; - stmt.executeUpdate(sql); - stmt.close(); - b.close(); - } catch (Exception e) { - System.err.println(e.getClass().getName() + ": " + e.getMessage()); + if(!addUserCard(username, newCard.getName())){ return null; } - System.out.println("User got Card successfully"); } rs.close(); stmt.close(); @@ -304,6 +295,37 @@ public class DBConnection { } } + /** + * Löscht eine user card aus der DB + * @param username Card löschen von + * @param cardname Card die vom user zu löschen ist + * @return True bei erfolg, sonst false + */ + public boolean delUserCard(String username, String cardname){ + return PostgresHelper.executeUpdate("delete from user_cards where username = \'"+username+"\' and name = \'"+cardname+"\';"); + } + + /** + * Fügt eine Card zu den User_Cards hinzu + * @param username Username der die Card bekommt + * @param cardName Card die der User bekommt + * @return True bei erfolg, sonst false + */ + public boolean addUserCard(String username, String cardName){ + Connection b = PostgresHelper.con(); + try { + stmt = b.createStatement(); + String sql = "INSERT INTO public.user_cards (username, name, gesperrt) VALUES ( \'" + username + "\',\'" + cardName + "\', \'false\');"; + stmt.executeUpdate(sql); + stmt.close(); + b.close(); + } catch (Exception e) { + System.err.println(e.getClass().getName() + ": " + e.getMessage()); + return false; + } + System.out.println("User got Card successfully"); + return true; + } /** * Löscht ein Package anhand des Namens * diff --git a/src/main/java/at/reisinger/server/objects/Msg.java b/src/main/java/at/reisinger/server/objects/Msg.java deleted file mode 100644 index 87a2ea7..0000000 --- a/src/main/java/at/reisinger/server/objects/Msg.java +++ /dev/null @@ -1,37 +0,0 @@ -package at.reisinger.server.objects; - -/** - * Message Objekt beinmhaltet die MsgId und die Msg selbst - * @author Georg Reisinger - */ -public class Msg { - private int id; - private String msg; - - /** - * Erstellt eine Message - */ - public Msg(int id, String msg){ - this.id = id; - this.msg = msg; - } - - - /** - * get field - * - * @return id Message Id - */ - public int getId() { - return this.id; - } - - /** - * get field - * - * @return msg Message String - */ - public String getMsg() { - return this.msg; - } -} diff --git a/src/main/java/at/reisinger/server/objects/Request.java b/src/main/java/at/reisinger/server/objects/Request.java index cb4f8b8..e4b703b 100644 --- a/src/main/java/at/reisinger/server/objects/Request.java +++ b/src/main/java/at/reisinger/server/objects/Request.java @@ -65,19 +65,24 @@ public class Request { headers.add(header); } - int authIndex = headers.toString().indexOf("Authorization: Basic"); - String authString = headers.toString().substring(authIndex + 21); - System.out.println("AUTH STRING: " + authString); - int authIndexEnd = authString.indexOf(','); - System.out.println("INDEX OF: " + authIndexEnd); - authString = authString.substring(0, authIndexEnd); - System.out.println("AUTH STRING: " + authString); - this.authString = authString; String accessLog = String.format("Client %s, method %s, path %s, version %s, host %s, headers %s", socket.toString(), method, path, version, host, headers.toString()); System.out.println(accessLog); + if(headers.toString().contains("Authorization: Basic")) { + int authIndex = headers.toString().indexOf("Authorization: Basic"); + String authString = headers.toString().substring(authIndex + 21); + System.out.println("AUTH STRING: " + authString); + int authIndexEnd = authString.indexOf(','); + System.out.println("INDEX OF: " + authIndexEnd); + authString = authString.substring(0, authIndexEnd); + System.out.println("AUTH STRING: " + authString); + + this.authString = authString; + }else{ + this.authString = null; + } } /** diff --git a/src/main/java/at/reisinger/server/objects/Response.java b/src/main/java/at/reisinger/server/objects/Response.java index 783b72c..90b755e 100644 --- a/src/main/java/at/reisinger/server/objects/Response.java +++ b/src/main/java/at/reisinger/server/objects/Response.java @@ -272,38 +272,103 @@ public class Response { } }else if (this.url.startsWith("/tradings")) { if(login()){ - Map map = JsonHelper.jsonPayloadToMap(this.payload); - String id = (String)map.get("Id"); - String cardtotrade = (String)map.get("CardToTrade"); - String type = (String)map.get("Type"); - double mindamage = Double.parseDouble(map.get("MinimumDamage") + ""); - String username = basicAuthGetUsername(this.authString); - if (username != null) { - Card cardtoTradeC = new DBConnection().getCardFromID(cardtotrade); - System.out.println("CARD TO TRAD: " + cardtoTradeC.getName()); - if (cardtoTradeC != null) { - TradingDeal tradingDeal = new TradingDeal(id, cardtoTradeC, mindamage, type, username); - if (tradingDeal != null) { - String tradingJson = JsonHelper.objToJson(tradingDeal); - if (tradingDeal != null) { - ArrayList deckCards = new DBConnection().getDeck(username); - if (deckCards != null) { - if (deckCards.contains(cardtotrade)) { - new DBConnection().delDeck(username); - } - if(new DBConnection().addTradingdeal(tradingDeal.getUsername(), tradingDeal.getId(), tradingDeal.getRequiredMinDamage(), tradingDeal.getRequiredCardType().name(), tradingDeal.getRequiredElementType().name(), tradingDeal.getCardToTrade().getName())){ - if(new DBConnection().updateCardLock(tradingDeal.getCardToTrade().getName(), true)){ - sendResponse(JsonHelper.objToJson(tradingDeal), "201"); - }else{ + String dotradeid = this.url.substring(this.url.lastIndexOf('/') + 1); + System.out.println("DO TradeID in /tradings/{tradeID}: " + dotradeid); + if(!dotradeid.isEmpty() && dotradeid != null && !dotradeid.equals("tradings")){ + //DO TRADE + String username = basicAuthGetUsername(this.authString); + if(username != null && !username.isEmpty()) { + System.out.println(this.payload); + ArrayList tradingDealArrayList = new DBConnection().getAllTradingDeals(); + TradingDeal tradingDeal = null; + if (tradingDealArrayList != null && !tradingDealArrayList.isEmpty()) { + for (TradingDeal tr : tradingDealArrayList) { + if (tr.getId().equals(dotradeid)) { + tradingDeal = tr; + } + } + Card card = new DBConnection().getCardFromID(this.payload); + if (card != null) { + if (tradingDeal != null) { + if (tradingDeal.cardOk(card)) { + String json = JsonHelper.objToJson(card); + if (json != null && !json.isEmpty()) { + if (new DBConnection().addUserCard(username, tradingDeal.getCardToTrade().getName())){ + if (new DBConnection().delUserCard(tradingDeal.getUsername(), tradingDeal.getCardToTrade().getName())){ + if(new DBConnection().deleteTradingDeal(tradingDeal.getId())){ + if (new DBConnection().delUserCard(username, card.getName())){ + if(new DBConnection().addUserCard(tradingDeal.getUsername(), card.getName())){ + sendResponse(json, "200"); + }else { + sendError("500"); + } + }else { + sendError("500"); + } + }else{ + sendError("500"); + } + }else{ + sendError("500"); + } + }else{ + sendError("500"); + } + } else { sendError("500"); } - }else{ + } else { sendError("500"); } } else { sendError("500"); } - sendResponse(tradingJson, "201"); + } else { + sendError("500"); + } + } else { + sendError("500"); + } + }else{ + sendError("401"); + } + }else { + //CREATE TRADING DEAL + Map map = JsonHelper.jsonPayloadToMap(this.payload); + String id = (String) map.get("Id"); + String cardtotrade = (String) map.get("CardToTrade"); + String type = (String) map.get("Type"); + double mindamage = Double.parseDouble(map.get("MinimumDamage") + ""); + String username = basicAuthGetUsername(this.authString); + if (username != null) { + Card cardtoTradeC = new DBConnection().getCardFromID(cardtotrade); + System.out.println("CARD TO TRAD: " + cardtoTradeC.getName()); + if (cardtoTradeC != null) { + TradingDeal tradingDeal = new TradingDeal(id, cardtoTradeC, mindamage, type, username); + if (tradingDeal != null) { + String tradingJson = JsonHelper.objToJson(tradingDeal); + if (tradingDeal != null) { + ArrayList deckCards = new DBConnection().getDeck(username); + if (deckCards != null) { + if (deckCards.contains(cardtotrade)) { + new DBConnection().delDeck(username); + } + if (new DBConnection().addTradingdeal(tradingDeal.getUsername(), tradingDeal.getId(), tradingDeal.getRequiredMinDamage(), tradingDeal.getRequiredCardType().name(), tradingDeal.getRequiredElementType().name(), tradingDeal.getCardToTrade().getName())) { + if (new DBConnection().updateCardLock(tradingDeal.getCardToTrade().getName(), true)) { + sendResponse(JsonHelper.objToJson(tradingDeal), "201"); + } else { + sendError("500"); + } + } else { + sendError("500"); + } + } else { + sendError("500"); + } + sendResponse(tradingJson, "201"); + } else { + sendError("500"); + } } else { sendError("500"); } @@ -313,8 +378,6 @@ public class Response { } else { sendError("500"); } - }else{ - sendError("500"); } }else{ sendError("401"); @@ -327,17 +390,21 @@ public class Response { * @return True wenn login daten korrekt, sonst false */ private boolean login(){ - String username = basicAuthGetUsername(this.authString); - User user = null; - DBConnection con = new DBConnection(); - try { - user = con.getUser(username); - } catch (SQLException throwables) { - throwables.printStackTrace(); - return false; - } - if (basicAuth(user.getCredentials().getUsername(), user.getCredentials().getPasswort(), this.authString)){ - return true; + if(this.authString != null && !this.authString.isEmpty()){ + String username = basicAuthGetUsername(this.authString); + User user = null; + DBConnection con = new DBConnection(); + try { + user = con.getUser(username); + } catch (SQLException throwables) { + throwables.printStackTrace(); + return false; + } + if (basicAuth(user.getCredentials().getUsername(), user.getCredentials().getPasswort(), this.authString)){ + return true; + }else{ + return false; + } }else{ return false; }