added registration function
~4h work
This commit is contained in:
parent
0f55ed919b
commit
a73ce3037f
12
.idea/dataSources.xml
generated
12
.idea/dataSources.xml
generated
@ -13,17 +13,5 @@
|
|||||||
</jdbc-additional-properties>
|
</jdbc-additional-properties>
|
||||||
<working-dir>$ProjectFileDir$</working-dir>
|
<working-dir>$ProjectFileDir$</working-dir>
|
||||||
</data-source>
|
</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>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
11
.idea/libraries/postgresql.xml
generated
Normal file
11
.idea/libraries/postgresql.xml
generated
Normal 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
48
.idea/misc.xml
generated
@ -9,6 +9,54 @@
|
|||||||
</option>
|
</option>
|
||||||
<option name="workspaceImportForciblyTurnedOn" value="true" />
|
<option name="workspaceImportForciblyTurnedOn" value="true" />
|
||||||
</component>
|
</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">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_20" default="true" project-jdk-name="20" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
|
|||||||
BIN
lib/checker-qual-3.41.0.jar
Normal file
BIN
lib/checker-qual-3.41.0.jar
Normal file
Binary file not shown.
BIN
lib/lombok-1.18.30.jar
Normal file
BIN
lib/lombok-1.18.30.jar
Normal file
Binary file not shown.
BIN
lib/postgresql-42.7.1.jar
Normal file
BIN
lib/postgresql-42.7.1.jar
Normal file
Binary file not shown.
@ -1,5 +1,20 @@
|
|||||||
package at.nanopenguin.mtcg.application;
|
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 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();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
@ -1,13 +1,15 @@
|
|||||||
package at.nanopenguin.mtcg.application.service;
|
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.HttpMethod;
|
||||||
import at.nanopenguin.mtcg.http.HttpRequest;
|
import at.nanopenguin.mtcg.http.HttpRequest;
|
||||||
import at.nanopenguin.mtcg.http.HttpStatus;
|
import at.nanopenguin.mtcg.http.HttpStatus;
|
||||||
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 com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.databind.ObjectWriter;
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
public class UserService implements Service {
|
public class UserService implements Service {
|
||||||
|
|
||||||
@ -22,7 +24,10 @@ public class UserService implements Service {
|
|||||||
if (request.getPath().split("/")[1].equals("users")) {
|
if (request.getPath().split("/")[1].equals("users")) {
|
||||||
return switch (request.getMethod()) {
|
return switch (request.getMethod()) {
|
||||||
case GET -> new Response(HttpStatus.NOT_IMPLEMENTED);
|
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);
|
case PUT -> new Response(HttpStatus.NOT_IMPLEMENTED); // new ObjectMapper().readValue(request.getBody(), UserData.class);
|
||||||
default -> new Response(HttpStatus.NOT_FOUND);
|
default -> new Response(HttpStatus.NOT_FOUND);
|
||||||
};
|
};
|
||||||
@ -32,6 +37,9 @@ public class UserService implements Service {
|
|||||||
}
|
}
|
||||||
catch (ArrayIndexOutOfBoundsException e) {
|
catch (ArrayIndexOutOfBoundsException e) {
|
||||||
return new Response(HttpStatus.BAD_REQUEST);
|
return new Response(HttpStatus.BAD_REQUEST);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
return new Response(HttpStatus.INTERNAL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -11,7 +11,7 @@ import java.util.stream.Collectors;
|
|||||||
|
|
||||||
@Builder
|
@Builder
|
||||||
public final class DbQuery {
|
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
|
@NonNull
|
||||||
private final SqlCommand command;
|
private final SqlCommand command;
|
||||||
@ -61,7 +61,8 @@ public final class DbQuery {
|
|||||||
.filter(columnName -> columnName.matches("[a-zA-Z0-9_]+"))
|
.filter(columnName -> columnName.matches("[a-zA-Z0-9_]+"))
|
||||||
.collect(Collectors.joining(", "));
|
.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)) {
|
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
|
||||||
int i = 1;
|
int i = 1;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user