fixes to trade
~1.5h work
This commit is contained in:
parent
db2ade5881
commit
10197340c5
0
MonsterTradingCards.sh
Normal file → Executable file
0
MonsterTradingCards.sh
Normal file → Executable file
@ -1,8 +1,10 @@
|
|||||||
package at.nanopenguin.mtcg.application;
|
package at.nanopenguin.mtcg.application;
|
||||||
|
|
||||||
|
import at.nanopenguin.mtcg.Pair;
|
||||||
import at.nanopenguin.mtcg.application.service.schemas.TradingDeal;
|
import at.nanopenguin.mtcg.application.service.schemas.TradingDeal;
|
||||||
import at.nanopenguin.mtcg.db.DbQuery;
|
import at.nanopenguin.mtcg.db.DbQuery;
|
||||||
import at.nanopenguin.mtcg.db.SqlCommand;
|
import at.nanopenguin.mtcg.db.SqlCommand;
|
||||||
|
import at.nanopenguin.mtcg.db.SqlComparisonOperator;
|
||||||
import at.nanopenguin.mtcg.db.Table;
|
import at.nanopenguin.mtcg.db.Table;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import lombok.val;
|
import lombok.val;
|
||||||
@ -12,14 +14,15 @@ import java.util.ArrayList;
|
|||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public class Trade {
|
public class Trade {
|
||||||
public static TradingDeal[] get() throws SQLException {
|
public static TradingDeal[] get(UUID userUuid) throws SQLException {
|
||||||
val dbQueryBuilder = DbQuery.builder()
|
val dbQueryBuilder = DbQuery.builder()
|
||||||
.command(SqlCommand.SELECT)
|
.command(SqlCommand.SELECT)
|
||||||
.table(Table.TRADES)
|
.table(Table.TRADES)
|
||||||
.column("uuid AS id")
|
.column("uuid AS id")
|
||||||
.column("card AS cardToTrade")
|
.column("card AS cardToTrade")
|
||||||
.column("card_type AS type")
|
.column("card_type AS type")
|
||||||
.column("min_dmg AS minimumDamage");
|
.column("min_dmg AS minimumDamage")
|
||||||
|
.condition("user_uuid", new Pair<>(userUuid, SqlComparisonOperator.NOT_EQUAL));
|
||||||
|
|
||||||
ArrayList<TradingDeal> trades = new ArrayList<>();
|
ArrayList<TradingDeal> trades = new ArrayList<>();
|
||||||
for (val row : dbQueryBuilder.executeQuery()) {
|
for (val row : dbQueryBuilder.executeQuery()) {
|
||||||
@ -65,10 +68,16 @@ public class Trade {
|
|||||||
val tradeResult = DbQuery.builder()
|
val tradeResult = DbQuery.builder()
|
||||||
.command(SqlCommand.SELECT)
|
.command(SqlCommand.SELECT)
|
||||||
.table(Table.TRADES)
|
.table(Table.TRADES)
|
||||||
|
.column("uuid AS id")
|
||||||
|
.column("card AS cardToTrade")
|
||||||
|
.column("card_type AS type")
|
||||||
|
.column("min_dmg AS minimumDamage")
|
||||||
|
.column("user_uuid")
|
||||||
.condition("uuid", tradeUuid)
|
.condition("uuid", tradeUuid)
|
||||||
.executeQuery();
|
.executeQuery();
|
||||||
|
|
||||||
if (tradeResult.isEmpty()) throw new NullPointerException();
|
if (tradeResult.isEmpty()) throw new NullPointerException();
|
||||||
|
if (tradeResult.get(0).get("user_uuid") == userUuid) return false;
|
||||||
|
|
||||||
TradingDeal trade = new ObjectMapper().convertValue(tradeResult.get(0), TradingDeal.class);
|
TradingDeal trade = new ObjectMapper().convertValue(tradeResult.get(0), TradingDeal.class);
|
||||||
|
|
||||||
|
|||||||
@ -26,7 +26,7 @@ public class TradingService implements Service {
|
|||||||
if (request.getPath().split("/")[1].equals("tradings")) {
|
if (request.getPath().split("/")[1].equals("tradings")) {
|
||||||
return switch (request.getMethod()) {
|
return switch (request.getMethod()) {
|
||||||
case GET -> {
|
case GET -> {
|
||||||
val result = Trade.get();
|
val result = Trade.get(userUuid);
|
||||||
if (result.length == 0) yield new Response(HttpStatus.NO_CONTENT);
|
if (result.length == 0) yield new Response(HttpStatus.NO_CONTENT);
|
||||||
yield new Response(HttpStatus.OK, new ObjectMapper().writeValueAsString(result));
|
yield new Response(HttpStatus.OK, new ObjectMapper().writeValueAsString(result));
|
||||||
}
|
}
|
||||||
@ -44,8 +44,8 @@ public class TradingService implements Service {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
yield new Response(Trade.acceptTrade(
|
yield new Response(Trade.acceptTrade(
|
||||||
UUID.fromString(request.getPath().split("/")[3]),
|
UUID.fromString(request.getPath().split("/")[2]),
|
||||||
UUID.fromString(request.getBody()),
|
UUID.fromString(new ObjectMapper().readValue(request.getBody(), String.class)),
|
||||||
userUuid) ?
|
userUuid) ?
|
||||||
HttpStatus.OK :
|
HttpStatus.OK :
|
||||||
HttpStatus.FORBIDDEN);
|
HttpStatus.FORBIDDEN);
|
||||||
@ -57,7 +57,7 @@ public class TradingService implements Service {
|
|||||||
case DELETE -> {
|
case DELETE -> {
|
||||||
try {
|
try {
|
||||||
yield new Response(Trade.removeTrade(
|
yield new Response(Trade.removeTrade(
|
||||||
UUID.fromString(request.getPath().split("/")[3]),
|
UUID.fromString(request.getPath().split("/")[2]),
|
||||||
userUuid) ?
|
userUuid) ?
|
||||||
HttpStatus.OK :
|
HttpStatus.OK :
|
||||||
HttpStatus.FORBIDDEN);
|
HttpStatus.FORBIDDEN);
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
package at.nanopenguin.mtcg.db;
|
package at.nanopenguin.mtcg.db;
|
||||||
|
|
||||||
|
import at.nanopenguin.mtcg.Pair;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
import lombok.Singular;
|
import lombok.Singular;
|
||||||
@ -37,7 +38,8 @@ public final class DbQuery {
|
|||||||
|
|
||||||
public List<Map<String, Object>> executeQuery() throws SQLException {
|
public List<Map<String, Object>> executeQuery() throws SQLException {
|
||||||
DbQuery dbQuery = this.build();
|
DbQuery dbQuery = this.build();
|
||||||
if (dbQuery.customSql != null) return DbQuery.executeQuery(dbQuery.customSql, dbQuery.values, dbQuery.isScript);
|
if (dbQuery.customSql != null)
|
||||||
|
return DbQuery.executeQuery(dbQuery.customSql, dbQuery.values, dbQuery.isScript);
|
||||||
if (dbQuery.command != SqlCommand.SELECT && dbQuery.returnColumn == null) throw new SQLException();
|
if (dbQuery.command != SqlCommand.SELECT && dbQuery.returnColumn == null) throw new SQLException();
|
||||||
return switch (dbQuery.command) {
|
return switch (dbQuery.command) {
|
||||||
case INSERT -> dbQuery.create(true);
|
case INSERT -> dbQuery.create(true);
|
||||||
@ -49,7 +51,8 @@ public final class DbQuery {
|
|||||||
|
|
||||||
public int executeUpdate() throws SQLException {
|
public int executeUpdate() throws SQLException {
|
||||||
DbQuery dbQuery = this.build();
|
DbQuery dbQuery = this.build();
|
||||||
if (dbQuery.customSql != null) return DbQuery.executeUpdate(dbQuery.customSql, dbQuery.values, dbQuery.isScript);
|
if (dbQuery.customSql != null)
|
||||||
|
return DbQuery.executeUpdate(dbQuery.customSql, dbQuery.values, dbQuery.isScript);
|
||||||
if (this.returnColumn != null) throw new SQLException();
|
if (this.returnColumn != null) throw new SQLException();
|
||||||
return switch (dbQuery.command) {
|
return switch (dbQuery.command) {
|
||||||
case INSERT -> dbQuery.create();
|
case INSERT -> dbQuery.create();
|
||||||
@ -82,13 +85,15 @@ public final class DbQuery {
|
|||||||
return DriverManager.getConnection(connectionString);
|
return DriverManager.getConnection(connectionString);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String buildParameterizedString(@NonNull SortedMap<String, Object> parameters, String separator) {
|
private String buildParameterizedString(@NonNull SortedMap<String, Object> parameterMap, String separator) {
|
||||||
if (parameters.isEmpty()) {
|
|
||||||
|
if (parameterMap.isEmpty()) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
StringBuilder stringBuilder = new StringBuilder();
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
|
|
||||||
for (val parameter : parameters.entrySet()) {
|
for (val parameter : parameterMap.entrySet()) {
|
||||||
if (!stringBuilder.isEmpty()) stringBuilder.append(separator);
|
if (!stringBuilder.isEmpty()) stringBuilder.append(separator);
|
||||||
if (parameter.getValue() instanceof List) {
|
if (parameter.getValue() instanceof List) {
|
||||||
stringBuilder
|
stringBuilder
|
||||||
@ -99,6 +104,15 @@ public final class DbQuery {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(parameter.getValue() instanceof Pair && ((Pair<?, ?>) parameter.getValue()).right() instanceof SqlComparisonOperator sqlComparisonOperator) {
|
||||||
|
stringBuilder
|
||||||
|
.append(parameter.getKey())
|
||||||
|
.append(" ")
|
||||||
|
.append(sqlComparisonOperator.Operator)
|
||||||
|
.append(" ?");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
stringBuilder
|
stringBuilder
|
||||||
.append(parameter.getKey())
|
.append(parameter.getKey())
|
||||||
.append(" = ?");
|
.append(" = ?");
|
||||||
@ -164,6 +178,10 @@ public final class DbQuery {
|
|||||||
new PreparedStatementExecutor(connection.prepareStatement(sql));
|
new PreparedStatementExecutor(connection.prepareStatement(sql));
|
||||||
int i = 1;
|
int i = 1;
|
||||||
for (val value : parameterValues) {
|
for (val value : parameterValues) {
|
||||||
|
if (value instanceof Pair<?,?>) {
|
||||||
|
statementExecutor.setObject(i++, ((Pair<?, ?>) value).left());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (value instanceof List<?>) {
|
if (value instanceof List<?>) {
|
||||||
for (Object o : (List<?>) value)
|
for (Object o : (List<?>) value)
|
||||||
statementExecutor.setObject(i++, o);
|
statementExecutor.setObject(i++, o);
|
||||||
|
|||||||
16
src/at/nanopenguin/mtcg/db/SqlComparisonOperator.java
Normal file
16
src/at/nanopenguin/mtcg/db/SqlComparisonOperator.java
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package at.nanopenguin.mtcg.db;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public enum SqlComparisonOperator {
|
||||||
|
EQUAL("="),
|
||||||
|
GREATER_THAN(">"),
|
||||||
|
LESS_THAN("<"),
|
||||||
|
GREATER_EQUAL(">="),
|
||||||
|
LESS_EQUAL("<="),
|
||||||
|
NOT_EQUAL("!=");
|
||||||
|
|
||||||
|
public final String Operator;
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user