doTrade /tradings
This commit is contained in:
parent
2c9242e45c
commit
a5c910b508
@ -274,18 +274,9 @@ public class DBConnection {
|
|||||||
damage = rs.getInt("damage");
|
damage = rs.getInt("damage");
|
||||||
Card newCard = new Card(cardname, elementtyp + cardtype, damage);
|
Card newCard = new Card(cardname, elementtyp + cardtype, damage);
|
||||||
cards.addCard(newCard);
|
cards.addCard(newCard);
|
||||||
Connection b = PostgresHelper.con();
|
if(!addUserCard(username, newCard.getName())){
|
||||||
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());
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
System.out.println("User got Card successfully");
|
|
||||||
}
|
}
|
||||||
rs.close();
|
rs.close();
|
||||||
stmt.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
|
* Löscht ein Package anhand des Namens
|
||||||
*
|
*
|
||||||
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -65,19 +65,24 @@ public class Request {
|
|||||||
headers.add(header);
|
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",
|
String accessLog = String.format("Client %s, method %s, path %s, version %s, host %s, headers %s",
|
||||||
socket.toString(), method, path, version, host, headers.toString());
|
socket.toString(), method, path, version, host, headers.toString());
|
||||||
System.out.println(accessLog);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -272,38 +272,103 @@ public class Response {
|
|||||||
}
|
}
|
||||||
}else if (this.url.startsWith("/tradings")) {
|
}else if (this.url.startsWith("/tradings")) {
|
||||||
if(login()){
|
if(login()){
|
||||||
Map<String, Object> map = JsonHelper.jsonPayloadToMap(this.payload);
|
String dotradeid = this.url.substring(this.url.lastIndexOf('/') + 1);
|
||||||
String id = (String)map.get("Id");
|
System.out.println("DO TradeID in /tradings/{tradeID}: " + dotradeid);
|
||||||
String cardtotrade = (String)map.get("CardToTrade");
|
if(!dotradeid.isEmpty() && dotradeid != null && !dotradeid.equals("tradings")){
|
||||||
String type = (String)map.get("Type");
|
//DO TRADE
|
||||||
double mindamage = Double.parseDouble(map.get("MinimumDamage") + "");
|
String username = basicAuthGetUsername(this.authString);
|
||||||
String username = basicAuthGetUsername(this.authString);
|
if(username != null && !username.isEmpty()) {
|
||||||
if (username != null) {
|
System.out.println(this.payload);
|
||||||
Card cardtoTradeC = new DBConnection().getCardFromID(cardtotrade);
|
ArrayList<TradingDeal> tradingDealArrayList = new DBConnection().getAllTradingDeals();
|
||||||
System.out.println("CARD TO TRAD: " + cardtoTradeC.getName());
|
TradingDeal tradingDeal = null;
|
||||||
if (cardtoTradeC != null) {
|
if (tradingDealArrayList != null && !tradingDealArrayList.isEmpty()) {
|
||||||
TradingDeal tradingDeal = new TradingDeal(id, cardtoTradeC, mindamage, type, username);
|
for (TradingDeal tr : tradingDealArrayList) {
|
||||||
if (tradingDeal != null) {
|
if (tr.getId().equals(dotradeid)) {
|
||||||
String tradingJson = JsonHelper.objToJson(tradingDeal);
|
tradingDeal = tr;
|
||||||
if (tradingDeal != null) {
|
}
|
||||||
ArrayList<String> deckCards = new DBConnection().getDeck(username);
|
}
|
||||||
if (deckCards != null) {
|
Card card = new DBConnection().getCardFromID(this.payload);
|
||||||
if (deckCards.contains(cardtotrade)) {
|
if (card != null) {
|
||||||
new DBConnection().delDeck(username);
|
if (tradingDeal != null) {
|
||||||
}
|
if (tradingDeal.cardOk(card)) {
|
||||||
if(new DBConnection().addTradingdeal(tradingDeal.getUsername(), tradingDeal.getId(), tradingDeal.getRequiredMinDamage(), tradingDeal.getRequiredCardType().name(), tradingDeal.getRequiredElementType().name(), tradingDeal.getCardToTrade().getName())){
|
String json = JsonHelper.objToJson(card);
|
||||||
if(new DBConnection().updateCardLock(tradingDeal.getCardToTrade().getName(), true)){
|
if (json != null && !json.isEmpty()) {
|
||||||
sendResponse(JsonHelper.objToJson(tradingDeal), "201");
|
if (new DBConnection().addUserCard(username, tradingDeal.getCardToTrade().getName())){
|
||||||
}else{
|
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");
|
sendError("500");
|
||||||
}
|
}
|
||||||
}else{
|
} else {
|
||||||
sendError("500");
|
sendError("500");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
sendError("500");
|
sendError("500");
|
||||||
}
|
}
|
||||||
sendResponse(tradingJson, "201");
|
} else {
|
||||||
|
sendError("500");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sendError("500");
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
sendError("401");
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
//CREATE TRADING DEAL
|
||||||
|
Map<String, Object> 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<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.getCardToTrade().getName(), true)) {
|
||||||
|
sendResponse(JsonHelper.objToJson(tradingDeal), "201");
|
||||||
|
} else {
|
||||||
|
sendError("500");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sendError("500");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sendError("500");
|
||||||
|
}
|
||||||
|
sendResponse(tradingJson, "201");
|
||||||
|
} else {
|
||||||
|
sendError("500");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
sendError("500");
|
sendError("500");
|
||||||
}
|
}
|
||||||
@ -313,8 +378,6 @@ public class Response {
|
|||||||
} else {
|
} else {
|
||||||
sendError("500");
|
sendError("500");
|
||||||
}
|
}
|
||||||
}else{
|
|
||||||
sendError("500");
|
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
sendError("401");
|
sendError("401");
|
||||||
@ -327,17 +390,21 @@ public class Response {
|
|||||||
* @return True wenn login daten korrekt, sonst false
|
* @return True wenn login daten korrekt, sonst false
|
||||||
*/
|
*/
|
||||||
private boolean login(){
|
private boolean login(){
|
||||||
String username = basicAuthGetUsername(this.authString);
|
if(this.authString != null && !this.authString.isEmpty()){
|
||||||
User user = null;
|
String username = basicAuthGetUsername(this.authString);
|
||||||
DBConnection con = new DBConnection();
|
User user = null;
|
||||||
try {
|
DBConnection con = new DBConnection();
|
||||||
user = con.getUser(username);
|
try {
|
||||||
} catch (SQLException throwables) {
|
user = con.getUser(username);
|
||||||
throwables.printStackTrace();
|
} catch (SQLException throwables) {
|
||||||
return false;
|
throwables.printStackTrace();
|
||||||
}
|
return false;
|
||||||
if (basicAuth(user.getCredentials().getUsername(), user.getCredentials().getPasswort(), this.authString)){
|
}
|
||||||
return true;
|
if (basicAuth(user.getCredentials().getUsername(), user.getCredentials().getPasswort(), this.authString)){
|
||||||
|
return true;
|
||||||
|
}else{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}else{
|
}else{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user