doTrade /tradings

This commit is contained in:
Georg Reisinger 2020-12-30 16:35:15 +01:00
parent 2c9242e45c
commit a5c910b508
4 changed files with 150 additions and 93 deletions

View File

@ -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
*

View File

@ -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;
}
}

View File

@ -65,6 +65,12 @@ public class Request {
headers.add(header);
}
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);
@ -74,10 +80,9 @@ public class Request {
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);
}else{
this.authString = null;
}
}
/**

View File

@ -272,6 +272,68 @@ public class Response {
}
}else if (this.url.startsWith("/tradings")) {
if(login()){
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<TradingDeal> 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 {
sendError("500");
}
} else {
sendError("500");
}
} 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");
@ -316,6 +378,7 @@ public class Response {
} else {
sendError("500");
}
}
}else{
sendError("401");
}
@ -327,6 +390,7 @@ public class Response {
* @return True wenn login daten korrekt, sonst false
*/
private boolean login(){
if(this.authString != null && !this.authString.isEmpty()){
String username = basicAuthGetUsername(this.authString);
User user = null;
DBConnection con = new DBConnection();
@ -341,6 +405,9 @@ public class Response {
}else{
return false;
}
}else{
return false;
}
}
/**