exception clean-up and prepared buy package function
~2.5h work
This commit is contained in:
parent
5a0b03a75f
commit
43a6e3596f
@ -17,7 +17,7 @@ public class Package {
|
|||||||
val result = DbQuery.builder()
|
val result = DbQuery.builder()
|
||||||
.command(SqlCommand.INSERT)
|
.command(SqlCommand.INSERT)
|
||||||
.table(Table.PACKAGES)
|
.table(Table.PACKAGES)
|
||||||
.parameter("cost", 5)
|
.parameter("uuid", UUID.randomUUID())
|
||||||
.returnColumn("uuid")
|
.returnColumn("uuid")
|
||||||
.executeQuery();
|
.executeQuery();
|
||||||
if (result.isEmpty()) throw new SQLException(); // maybe change to different exception
|
if (result.isEmpty()) throw new SQLException(); // maybe change to different exception
|
||||||
@ -41,4 +41,26 @@ public class Package {
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean addToUser(UUID userUuid) throws SQLException {
|
||||||
|
return DbQuery.builder()
|
||||||
|
.customSql("""
|
||||||
|
DO $$
|
||||||
|
DECLARE user_uuid uuid;
|
||||||
|
DECLARE package_uuid uuid;
|
||||||
|
DECLARE cost int;
|
||||||
|
BEGIN
|
||||||
|
cost = ?;
|
||||||
|
user_uuid = ?::uuid;
|
||||||
|
IF (SELECT coins FROM users WHERE uuid = user_uuid) >= cost THEN
|
||||||
|
package_uuid = (SELECT uuid FROM packages ORDER BY created_at LIMIT 1);
|
||||||
|
UPDATE cards SET owner = user_uuid WHERE package = package_uuid;
|
||||||
|
UPDATE users SET coins = coins - cost WHERE uuid = user_uuid;
|
||||||
|
DELETE FROM packages WHERE uuid = package_uuid;
|
||||||
|
END IF;
|
||||||
|
END $$;""")
|
||||||
|
.value(5) // TODO: don't hardcode cost
|
||||||
|
.value(userUuid)
|
||||||
|
.executeUpdate() > 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,7 +13,7 @@ import java.util.UUID;
|
|||||||
|
|
||||||
public final class SessionHandler {
|
public final class SessionHandler {
|
||||||
private static SessionHandler INSTANCE;
|
private static SessionHandler INSTANCE;
|
||||||
private final Map<UUID, UserInfo> Sessions = new HashMap<>();
|
private final Map<UUID, UserInfo> sessions = new HashMap<>();
|
||||||
|
|
||||||
private SessionHandler() {
|
private SessionHandler() {
|
||||||
|
|
||||||
@ -27,7 +27,13 @@ public final class SessionHandler {
|
|||||||
return INSTANCE;
|
return INSTANCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
public UUID login(UserCredentials userCredentials) throws SQLException {
|
public synchronized UUID login(UserCredentials userCredentials) throws SQLException { // avoid multiple logins of same user
|
||||||
|
for (val session : this.sessions.entrySet()) {
|
||||||
|
if (userCredentials.username().equals(session.getValue().username())) {
|
||||||
|
this.sessions.remove(session.getKey());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val result = DbQuery.builder()
|
val result = DbQuery.builder()
|
||||||
.command(SqlCommand.SELECT)
|
.command(SqlCommand.SELECT)
|
||||||
.table(Table.USERS)
|
.table(Table.USERS)
|
||||||
@ -48,7 +54,7 @@ public final class SessionHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
UUID uuid = UUID.randomUUID();
|
UUID uuid = UUID.randomUUID();
|
||||||
this.Sessions.put(uuid, new UserInfo((UUID) row1.get("uuid"), userCredentials.username(), (boolean) row1.get("admin")));
|
this.sessions.put(uuid, new UserInfo((UUID) row1.get("uuid"), userCredentials.username(), (boolean) row1.get("admin")));
|
||||||
return uuid;
|
return uuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,8 +68,8 @@ public final class SessionHandler {
|
|||||||
|
|
||||||
public TokenValidity verifyUUID(UUID uuid, boolean requireAdmin) {
|
public TokenValidity verifyUUID(UUID uuid, boolean requireAdmin) {
|
||||||
if (uuid == null) return TokenValidity.MISSING;
|
if (uuid == null) return TokenValidity.MISSING;
|
||||||
if (!Sessions.containsKey(uuid)) return TokenValidity.INVALID;
|
if (!sessions.containsKey(uuid)) return TokenValidity.INVALID;
|
||||||
if (Sessions.get(uuid).admin() || !requireAdmin) return TokenValidity.VALID;
|
if (sessions.get(uuid).admin() || !requireAdmin) return TokenValidity.VALID;
|
||||||
return TokenValidity.FORBIDDEN;
|
return TokenValidity.FORBIDDEN;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,33 +79,10 @@ public final class SessionHandler {
|
|||||||
|
|
||||||
public TokenValidity verifyUUID(UUID uuid, String username, boolean allowAdmin) {
|
public TokenValidity verifyUUID(UUID uuid, String username, boolean allowAdmin) {
|
||||||
if (uuid == null) return TokenValidity.MISSING;
|
if (uuid == null) return TokenValidity.MISSING;
|
||||||
if (!Sessions.containsKey(uuid)) return TokenValidity.INVALID;
|
if (!sessions.containsKey(uuid)) return TokenValidity.INVALID;
|
||||||
if (username.equals(Sessions.get(uuid).username())) return TokenValidity.VALID;
|
if (username.equals(sessions.get(uuid).username())) return TokenValidity.VALID;
|
||||||
if (allowAdmin && Sessions.get(uuid).admin()) return TokenValidity.VALID;
|
if (allowAdmin && sessions.get(uuid).admin()) return TokenValidity.VALID;
|
||||||
return TokenValidity.FORBIDDEN;
|
return TokenValidity.FORBIDDEN;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
*
|
|
||||||
* join() {
|
|
||||||
* lock()
|
|
||||||
* checks if someone waiting
|
|
||||||
* no -> {
|
|
||||||
* var waiting = true // sets self as waiting idk
|
|
||||||
* unlock()
|
|
||||||
* wait()
|
|
||||||
* response = battle log
|
|
||||||
* // process stuff
|
|
||||||
* unlock()
|
|
||||||
* }
|
|
||||||
* yes -> {
|
|
||||||
* start battle
|
|
||||||
* var battle log = battle log // no na ned
|
|
||||||
* // process stuff
|
|
||||||
* notifyAll()
|
|
||||||
* }
|
|
||||||
* }
|
|
||||||
*
|
|
||||||
* */
|
|
||||||
@ -20,15 +20,16 @@ import java.util.UUID;
|
|||||||
public class PackagesService implements Service {
|
public class PackagesService implements Service {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Response handleRequest(HttpRequest request) throws JsonProcessingException {
|
public Response handleRequest(HttpRequest request) throws JsonProcessingException, SQLException, ArrayIndexOutOfBoundsException {
|
||||||
try {
|
|
||||||
if (request.getPath().split("/")[1].equals("packages") && request.getMethod() == HttpMethod.POST) {
|
if (request.getPath().split("/")[1].equals("packages") && request.getMethod() == HttpMethod.POST) {
|
||||||
UUID uuid;
|
UUID uuid;
|
||||||
return switch (SessionHandler.getInstance().verifyUUID(SessionHandler.uuidFromHttpHeader(request.getHttpHeader("Authorization")), true)) {
|
return switch (SessionHandler.getInstance().verifyUUID(SessionHandler.uuidFromHttpHeader(request.getHttpHeader("Authorization")), true)) {
|
||||||
case MISSING, INVALID -> new Response(HttpStatus.UNAUTHORIZED);
|
case MISSING, INVALID -> new Response(HttpStatus.UNAUTHORIZED);
|
||||||
case FORBIDDEN -> new Response(HttpStatus.FORBIDDEN);
|
case FORBIDDEN -> new Response(HttpStatus.FORBIDDEN);
|
||||||
case VALID -> new Response(
|
case VALID -> new Response(
|
||||||
Package.create(new ObjectMapper().readValue(request.getBody(), new TypeReference<List<Card>>() {})) ?
|
Package.create(new ObjectMapper().readValue(request.getBody(), new TypeReference<List<Card>>() {
|
||||||
|
})) ?
|
||||||
HttpStatus.CREATED :
|
HttpStatus.CREATED :
|
||||||
HttpStatus.CONFLICT);
|
HttpStatus.CONFLICT);
|
||||||
};
|
};
|
||||||
@ -37,17 +38,9 @@ public class PackagesService implements Service {
|
|||||||
if (String.join("/", Arrays.copyOfRange(request.getPath().split("/"), 1, 2)).equals("transactions/packages") && request.getMethod() == HttpMethod.POST) {
|
if (String.join("/", Arrays.copyOfRange(request.getPath().split("/"), 1, 2)).equals("transactions/packages") && request.getMethod() == HttpMethod.POST) {
|
||||||
return new Response(SessionHandler.getInstance().verifyUUID(SessionHandler.uuidFromHttpHeader(request.getHttpHeader("Authorization"))) != TokenValidity.VALID ?
|
return new Response(SessionHandler.getInstance().verifyUUID(SessionHandler.uuidFromHttpHeader(request.getHttpHeader("Authorization"))) != TokenValidity.VALID ?
|
||||||
HttpStatus.UNAUTHORIZED :
|
HttpStatus.UNAUTHORIZED :
|
||||||
HttpStatus.NOT_IMPLEMENTED);
|
HttpStatus.NOT_IMPLEMENTED); // Package.addToUser();
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Response(HttpStatus.NOT_FOUND);
|
return new Response(HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
catch (ArrayIndexOutOfBoundsException e) {
|
|
||||||
return new Response(HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
catch (SQLException e) {
|
|
||||||
System.out.println(e.getMessage());
|
|
||||||
return new Response(HttpStatus.INTERNAL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,7 +4,9 @@ import at.nanopenguin.mtcg.http.HttpRequest;
|
|||||||
import at.nanopenguin.mtcg.http.Response;
|
import at.nanopenguin.mtcg.http.Response;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
public interface Service {
|
public interface Service {
|
||||||
|
|
||||||
Response handleRequest(HttpRequest request) throws JsonProcessingException;
|
Response handleRequest(HttpRequest request) throws JsonProcessingException, SQLException, ArrayIndexOutOfBoundsException;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,8 +19,7 @@ import java.util.UUID;
|
|||||||
public class UserService implements Service {
|
public class UserService implements Service {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Response handleRequest(HttpRequest request) throws JsonProcessingException {
|
public Response handleRequest(HttpRequest request) throws JsonProcessingException, SQLException, ArrayIndexOutOfBoundsException {
|
||||||
try {
|
|
||||||
if (request.getPath().split("/")[1].equals("sessions") && request.getMethod() == HttpMethod.POST) {
|
if (request.getPath().split("/")[1].equals("sessions") && request.getMethod() == HttpMethod.POST) {
|
||||||
// login
|
// login
|
||||||
UUID uuid = SessionHandler.getInstance().login(new ObjectMapper().readValue(request.getBody(), UserCredentials.class));
|
UUID uuid = SessionHandler.getInstance().login(new ObjectMapper().readValue(request.getBody(), UserCredentials.class));
|
||||||
@ -57,12 +56,4 @@ public class UserService implements Service {
|
|||||||
|
|
||||||
return new Response(HttpStatus.NOT_FOUND);
|
return new Response(HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
catch (ArrayIndexOutOfBoundsException e) {
|
|
||||||
return new Response(HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
catch (SQLException e) {
|
|
||||||
System.out.println(e.getMessage());
|
|
||||||
return new Response(HttpStatus.INTERNAL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@ -4,10 +4,7 @@ import at.nanopenguin.mtcg.application.service.Service;
|
|||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.*;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@ -42,8 +39,12 @@ public class RequestHandler implements Runnable {
|
|||||||
try {
|
try {
|
||||||
response = service.handleRequest(httpRequest);
|
response = service.handleRequest(httpRequest);
|
||||||
}
|
}
|
||||||
catch (JsonProcessingException e) {
|
catch (JsonProcessingException | ArrayIndexOutOfBoundsException e) {
|
||||||
response = new Response(HttpStatus.BAD_REQUEST, "application/json", "");
|
response = new Response(HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
System.out.println(e.getMessage()); // TODO: more info
|
||||||
|
response = new Response(HttpStatus.INTERNAL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user