added registration function

~4h work
This commit is contained in:
Benedikt Galbavy 2024-01-02 22:35:06 +01:00
parent 0f55ed919b
commit a73ce3037f
9 changed files with 91 additions and 20 deletions

12
.idea/dataSources.xml generated
View File

@ -13,17 +13,5 @@
</jdbc-additional-properties>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
<data-source source="LOCAL" name="postgres@localhost" uuid="ef2058ed-3abf-42cf-b611-7b52e2f2b2ae">
<driver-ref>postgresql</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.postgresql.Driver</jdbc-driver>
<jdbc-url>jdbc:postgresql://localhost:5432/postgres</jdbc-url>
<jdbc-additional-properties>
<property name="com.intellij.clouds.kubernetes.db.host.port" />
<property name="com.intellij.clouds.kubernetes.db.enabled" value="false" />
<property name="com.intellij.clouds.kubernetes.db.container.port" />
</jdbc-additional-properties>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
</component>
</project>

11
.idea/libraries/postgresql.xml generated Normal file
View File

@ -0,0 +1,11 @@
<component name="libraryTable">
<library name="postgresql" type="repository">
<properties maven-id="org.postgresql:postgresql:42.7.1" />
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/postgresql-42.7.1.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/checker-qual-3.41.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

48
.idea/misc.xml generated
View File

@ -9,6 +9,54 @@
</option>
<option name="workspaceImportForciblyTurnedOn" value="true" />
</component>
<component name="NullableNotNullManager">
<option name="myDefaultNullable" value="org.jetbrains.annotations.Nullable" />
<option name="myDefaultNotNull" value="lombok.NonNull" />
<option name="myNullables">
<value>
<list size="16">
<item index="0" class="java.lang.String" itemvalue="org.jspecify.annotations.Nullable" />
<item index="1" class="java.lang.String" itemvalue="com.android.annotations.Nullable" />
<item index="2" class="java.lang.String" itemvalue="io.reactivex.rxjava3.annotations.Nullable" />
<item index="3" class="java.lang.String" itemvalue="androidx.annotation.RecentlyNullable" />
<item index="4" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.compatqual.NullableDecl" />
<item index="5" class="java.lang.String" itemvalue="org.jetbrains.annotations.Nullable" />
<item index="6" class="java.lang.String" itemvalue="androidx.annotation.Nullable" />
<item index="7" class="java.lang.String" itemvalue="org.eclipse.jdt.annotation.Nullable" />
<item index="8" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.Nullable" />
<item index="9" class="java.lang.String" itemvalue="android.support.annotation.Nullable" />
<item index="10" class="java.lang.String" itemvalue="jakarta.annotation.Nullable" />
<item index="11" class="java.lang.String" itemvalue="javax.annotation.CheckForNull" />
<item index="12" class="java.lang.String" itemvalue="javax.annotation.Nullable" />
<item index="13" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.qual.Nullable" />
<item index="14" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.compatqual.NullableType" />
<item index="15" class="java.lang.String" itemvalue="io.reactivex.annotations.Nullable" />
</list>
</value>
</option>
<option name="myNotNulls">
<value>
<list size="16">
<item index="0" class="java.lang.String" itemvalue="androidx.annotation.RecentlyNonNull" />
<item index="1" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.qual.NonNull" />
<item index="2" class="java.lang.String" itemvalue="org.jspecify.annotations.NonNull" />
<item index="3" class="java.lang.String" itemvalue="jakarta.annotation.Nonnull" />
<item index="4" class="java.lang.String" itemvalue="io.reactivex.annotations.NonNull" />
<item index="5" class="java.lang.String" itemvalue="androidx.annotation.NonNull" />
<item index="6" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.compatqual.NonNullType" />
<item index="7" class="java.lang.String" itemvalue="lombok.NonNull" />
<item index="8" class="java.lang.String" itemvalue="android.support.annotation.NonNull" />
<item index="9" class="java.lang.String" itemvalue="io.reactivex.rxjava3.annotations.NonNull" />
<item index="10" class="java.lang.String" itemvalue="com.android.annotations.NonNull" />
<item index="11" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.NonNull" />
<item index="12" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.compatqual.NonNullDecl" />
<item index="13" class="java.lang.String" itemvalue="org.jetbrains.annotations.NotNull" />
<item index="14" class="java.lang.String" itemvalue="javax.annotation.Nonnull" />
<item index="15" class="java.lang.String" itemvalue="org.eclipse.jdt.annotation.NonNull" />
</list>
</value>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_20" default="true" project-jdk-name="20" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>

BIN
lib/checker-qual-3.41.0.jar Normal file

Binary file not shown.

BIN
lib/lombok-1.18.30.jar Normal file

Binary file not shown.

BIN
lib/postgresql-42.7.1.jar Normal file

Binary file not shown.

View File

@ -1,5 +1,20 @@
package at.nanopenguin.mtcg.application;
import at.nanopenguin.mtcg.application.service.schemas.UserCredentials;
import at.nanopenguin.mtcg.db.DbQuery;
import at.nanopenguin.mtcg.db.SqlCommand;
import at.nanopenguin.mtcg.db.Table;
import java.sql.SQLException;
public class User {
public static int create(UserCredentials userCredentials) throws SQLException {
return DbQuery.builder()
.command(SqlCommand.INSERT)
.table(Table.USERS)
.parameter("username", userCredentials.username())
.parameter("password", userCredentials.password())
.executeUpdate();
}
}

View File

@ -1,13 +1,15 @@
package at.nanopenguin.mtcg.application.service;
import at.nanopenguin.mtcg.application.User;
import at.nanopenguin.mtcg.application.service.schemas.UserCredentials;
import at.nanopenguin.mtcg.http.HttpMethod;
import at.nanopenguin.mtcg.http.HttpRequest;
import at.nanopenguin.mtcg.http.HttpStatus;
import at.nanopenguin.mtcg.http.Response;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import java.sql.SQLException;
public class UserService implements Service {
@ -22,7 +24,10 @@ public class UserService implements Service {
if (request.getPath().split("/")[1].equals("users")) {
return switch (request.getMethod()) {
case GET -> new Response(HttpStatus.NOT_IMPLEMENTED);
case POST -> new Response(HttpStatus.NOT_IMPLEMENTED); // new ObjectMapper().readValue(request.getBody(), UserCredentials.class);
case POST -> {
int success = User.create(new ObjectMapper().readValue(request.getBody(), UserCredentials.class));
yield new Response(success > 0 ? HttpStatus.CREATED : HttpStatus.CONFLICT);
}
case PUT -> new Response(HttpStatus.NOT_IMPLEMENTED); // new ObjectMapper().readValue(request.getBody(), UserData.class);
default -> new Response(HttpStatus.NOT_FOUND);
};
@ -32,6 +37,9 @@ public class UserService implements Service {
}
catch (ArrayIndexOutOfBoundsException e) {
return new Response(HttpStatus.BAD_REQUEST);
} catch (SQLException e) {
System.out.println(e.getMessage());
return new Response(HttpStatus.INTERNAL);
}
}
}

View File

@ -11,7 +11,7 @@ import java.util.stream.Collectors;
@Builder
public final class DbQuery {
private static final String connectionString = "jdbc:postgres://localhost:5432/mydb?user=postgres&password=postgres";
private static final String connectionString = "jdbc:postgresql://localhost:5432/mydb?user=postgres&password=postgres";
@NonNull
private final SqlCommand command;
@ -61,7 +61,8 @@ public final class DbQuery {
.filter(columnName -> columnName.matches("[a-zA-Z0-9_]+"))
.collect(Collectors.joining(", "));
String sql = String.format("INSERT INTO %s (%s) VALUES (%s);", table.table, columns, String.join(", ", Collections.nCopies(this.parameters.size(), "?")));
String sql = String.format("INSERT INTO %s (%s) VALUES (%s) ON CONFLICT DO NOTHING;", table.table, columns, String.join(", ", Collections.nCopies(this.parameters.size(), "?")));
// on conflict return int equals 0
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
int i = 1;