add TradingDeal /tradings

This commit is contained in:
Georg Reisinger 2020-12-30 15:03:30 +01:00
parent 0df6ff129e
commit 7b2ecd3bf0
3 changed files with 57 additions and 16 deletions

View File

@ -39,6 +39,16 @@ public class DBConnection {
PostgresHelper.executeUpdateMessage("create table IF NOT EXISTS trading(username text not null constraint trading_users_username_fk references users,id text not null constraint trading_pk primary key, cardtotrade text not null constraint trading_card_name_fk references card, mindamage float not null,reqcardtype text not null,reqelement text not null);", "Trading Table created"); PostgresHelper.executeUpdateMessage("create table IF NOT EXISTS trading(username text not null constraint trading_users_username_fk references users,id text not null constraint trading_pk primary key, cardtotrade text not null constraint trading_card_name_fk references card, mindamage float not null,reqcardtype text not null,reqelement text not null);", "Trading Table created");
} }
/**
* Updated den Lock einer user card
* @param name Name der Card
* @param lock Zustand des Locks
* @return true bei erfolg, sonst false
*/
public boolean updateCardLock(String name, boolean lock){
return PostgresHelper.executeUpdate("Update user_cards set gesperrt = " + lock + " where name = \'" + name + "\';");
}
/** /**
* Fügt der DB einen Trading deal hinzu * Fügt der DB einen Trading deal hinzu
* @param username User der den Trade hinzufügt * @param username User der den Trade hinzufügt
@ -457,6 +467,7 @@ public class DBConnection {
* @return Null bei error * @return Null bei error
*/ */
public Card getCardFromID(String id){ public Card getCardFromID(String id){
System.out.println(id);
Connection b = PostgresHelper.con(); Connection b = PostgresHelper.con();
Card toreturn = null; Card toreturn = null;
try { try {

View File

@ -54,6 +54,22 @@ public class TradingDeal {
this(id, cardToTrade, EnumHelper.stringToElementType(type), requiredMinDamage, EnumHelper.stringToCardType(type), username); this(id, cardToTrade, EnumHelper.stringToElementType(type), requiredMinDamage, EnumHelper.stringToCardType(type), username);
} }
/**
* Holt die ID des Trading deals
* @return Id des Trading Deals
*/
public String getId() {
return id;
}
/**
* Setzt die id des TradingDeals
* @param id Id des TradingDeals
*/
public void setId(String id) {
this.id = id;
}
/** /**
* Holt den usernamen * Holt den usernamen
* @return Der username als String * @return Der username als String

View File

@ -262,36 +262,50 @@ public class Response {
String id = (String)map.get("Id"); String id = (String)map.get("Id");
String cardtotrade = (String)map.get("CardToTrade"); String cardtotrade = (String)map.get("CardToTrade");
String type = (String)map.get("Type"); String type = (String)map.get("Type");
double mindamage = (double)map.get("MinimumDamage"); double mindamage = Double.parseDouble(map.get("MinimumDamage") + "");
ArrayList<String> cardname = new ArrayList<String>(); String username = basicAuthGetUsername(this.authString);
cardname.add(cardtotrade); if (username != null) {
Cards cards = new DBConnection().getCardsFromIDList(cardname); Card cardtoTradeC = new DBConnection().getCardFromID(cardtotrade);
Card cardtoTradeC = null; System.out.println("CARD TO TRAD: " + cardtoTradeC.getName());
if (cards != null){ if (cardtoTradeC != null) {
cardtoTradeC = cards.getCards().get(0); TradingDeal tradingDeal = new TradingDeal(id, cardtoTradeC, mindamage, type, username);
if (cardtoTradeC != null){ if (tradingDeal != null) {
TradingDeal tradingDeal = new TradingDeal(id, cardtoTradeC, mindamage, type, basicAuthGetUsername(this.authString));
if (tradingDeal != null){
String tradingJson = JsonHelper.objToJson(tradingDeal); String tradingJson = JsonHelper.objToJson(tradingDeal);
if (tradingDeal != null){ if (tradingDeal != null) {
//DB Trading deal add //DB Trading deal add
// Lock Card for deck // Lock Card for deck
//Wenn card im deck, dann del aus deck //Wenn card im deck, dann del aus deck
//new DBConnection().; //new DBConnection().;
ArrayList<String> 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.getUsername(), true)){
sendResponse(JsonHelper.objToJson(tradingDeal), "201");
}else{
sendError("500");
}
}else{
sendError("500");
}
} else {
sendError("500");
}
sendResponse(tradingJson, "201"); sendResponse(tradingJson, "201");
}else{ } else {
sendError("500"); sendError("500");
} }
}else{ } else {
sendError("500"); sendError("500");
} }
}else { } else {
sendError("500"); sendError("500");
} }
}else{ }else{
sendError("500"); sendError("500");
} }
}else{ }else{
sendError("401"); sendError("401");
} }