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;
|
||||
|
||||
import at.nanopenguin.mtcg.Pair;
|
||||
import at.nanopenguin.mtcg.application.service.schemas.TradingDeal;
|
||||
import at.nanopenguin.mtcg.db.DbQuery;
|
||||
import at.nanopenguin.mtcg.db.SqlCommand;
|
||||
import at.nanopenguin.mtcg.db.SqlComparisonOperator;
|
||||
import at.nanopenguin.mtcg.db.Table;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.val;
|
||||
@ -12,14 +14,15 @@ import java.util.ArrayList;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Trade {
|
||||
public static TradingDeal[] get() throws SQLException {
|
||||
public static TradingDeal[] get(UUID userUuid) throws SQLException {
|
||||
val dbQueryBuilder = DbQuery.builder()
|
||||
.command(SqlCommand.SELECT)
|
||||
.table(Table.TRADES)
|
||||
.column("uuid AS id")
|
||||
.column("card AS cardToTrade")
|
||||
.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<>();
|
||||
for (val row : dbQueryBuilder.executeQuery()) {
|
||||
@ -65,10 +68,16 @@ public class Trade {
|
||||
val tradeResult = DbQuery.builder()
|
||||
.command(SqlCommand.SELECT)
|
||||
.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)
|
||||
.executeQuery();
|
||||
|
||||
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);
|
||||
|
||||
|
||||
@ -26,7 +26,7 @@ public class TradingService implements Service {
|
||||
if (request.getPath().split("/")[1].equals("tradings")) {
|
||||
return switch (request.getMethod()) {
|
||||
case GET -> {
|
||||
val result = Trade.get();
|
||||
val result = Trade.get(userUuid);
|
||||
if (result.length == 0) yield new Response(HttpStatus.NO_CONTENT);
|
||||
yield new Response(HttpStatus.OK, new ObjectMapper().writeValueAsString(result));
|
||||
}
|
||||
@ -44,8 +44,8 @@ public class TradingService implements Service {
|
||||
|
||||
try {
|
||||
yield new Response(Trade.acceptTrade(
|
||||
UUID.fromString(request.getPath().split("/")[3]),
|
||||
UUID.fromString(request.getBody()),
|
||||
UUID.fromString(request.getPath().split("/")[2]),
|
||||
UUID.fromString(new ObjectMapper().readValue(request.getBody(), String.class)),
|
||||
userUuid) ?
|
||||
HttpStatus.OK :
|
||||
HttpStatus.FORBIDDEN);
|
||||
@ -57,7 +57,7 @@ public class TradingService implements Service {
|
||||
case DELETE -> {
|
||||
try {
|
||||
yield new Response(Trade.removeTrade(
|
||||
UUID.fromString(request.getPath().split("/")[3]),
|
||||
UUID.fromString(request.getPath().split("/")[2]),
|
||||
userUuid) ?
|
||||
HttpStatus.OK :
|
||||
HttpStatus.FORBIDDEN);
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package at.nanopenguin.mtcg.db;
|
||||
|
||||
import at.nanopenguin.mtcg.Pair;
|
||||
import lombok.Builder;
|
||||
import lombok.NonNull;
|
||||
import lombok.Singular;
|
||||
@ -37,7 +38,8 @@ public final class DbQuery {
|
||||
|
||||
public List<Map<String, Object>> executeQuery() throws SQLException {
|
||||
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();
|
||||
return switch (dbQuery.command) {
|
||||
case INSERT -> dbQuery.create(true);
|
||||
@ -49,7 +51,8 @@ public final class DbQuery {
|
||||
|
||||
public int executeUpdate() throws SQLException {
|
||||
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();
|
||||
return switch (dbQuery.command) {
|
||||
case INSERT -> dbQuery.create();
|
||||
@ -82,13 +85,15 @@ public final class DbQuery {
|
||||
return DriverManager.getConnection(connectionString);
|
||||
}
|
||||
|
||||
private String buildParameterizedString(@NonNull SortedMap<String, Object> parameters, String separator) {
|
||||
if (parameters.isEmpty()) {
|
||||
private String buildParameterizedString(@NonNull SortedMap<String, Object> parameterMap, String separator) {
|
||||
|
||||
if (parameterMap.isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
for (val parameter : parameters.entrySet()) {
|
||||
for (val parameter : parameterMap.entrySet()) {
|
||||
if (!stringBuilder.isEmpty()) stringBuilder.append(separator);
|
||||
if (parameter.getValue() instanceof List) {
|
||||
stringBuilder
|
||||
@ -99,6 +104,15 @@ public final class DbQuery {
|
||||
continue;
|
||||
}
|
||||
|
||||
if(parameter.getValue() instanceof Pair && ((Pair<?, ?>) parameter.getValue()).right() instanceof SqlComparisonOperator sqlComparisonOperator) {
|
||||
stringBuilder
|
||||
.append(parameter.getKey())
|
||||
.append(" ")
|
||||
.append(sqlComparisonOperator.Operator)
|
||||
.append(" ?");
|
||||
continue;
|
||||
}
|
||||
|
||||
stringBuilder
|
||||
.append(parameter.getKey())
|
||||
.append(" = ?");
|
||||
@ -164,6 +178,10 @@ public final class DbQuery {
|
||||
new PreparedStatementExecutor(connection.prepareStatement(sql));
|
||||
int i = 1;
|
||||
for (val value : parameterValues) {
|
||||
if (value instanceof Pair<?,?>) {
|
||||
statementExecutor.setObject(i++, ((Pair<?, ?>) value).left());
|
||||
continue;
|
||||
}
|
||||
if (value instanceof List<?>) {
|
||||
for (Object o : (List<?>) value)
|
||||
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