Skip to content

Commit

Permalink
wow what a dumb idea this turned out to be
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrikSD committed Jul 9, 2024
1 parent d70e629 commit 184b943
Show file tree
Hide file tree
Showing 20 changed files with 644 additions and 3,469 deletions.
3 changes: 3 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build --java_language_version=21
build --java_runtime_version=21
test --test_output=errors
20 changes: 19 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"Bazel dependencies"

bazel_dep(name = "rules_jvm_external", version = "6.2")
bazel_dep(name = "contrib_rules_jvm", version = "0.27.0")

maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")

Expand All @@ -10,7 +11,7 @@ GUICE_VERSION = "7.0.0"
# renovate: datasource=maven depName=org.jboss.resteasy:resteasy-bom
RESTEASY_VERSION = "6.2.9.Final"
# renovate: datasource=maven depName=org.eclipse.jetty:jetty-bom
JETTY_VERSION = "12.0.11"
JETTY_VERSION = "11.0.21"
# renovate: datasource=maven depName=org.glassfish.jersey:jersey-bom
JERSEY_VERSION = "3.1.7"
# renovate: datasource=maven depName=org.flywaydb:flyway-core
Expand All @@ -24,11 +25,28 @@ POSTGRES_VERSION = "42.7.3"
# renovate: datasource=maven depName=org.assertj:assertj-core
ASSERTJ_VERSION = "3.26.0"

JUNIT_VERSION = "5.10.3"
JUNIT_PLATFORM_VERSION = "1.10.3"
MOCKITO_VERSION = "5.12.0"
TESTCONTAINERS_VERSION = "1.19.8"

maven.install(
artifacts = [
# Testing
"org.assertj:assertj-core:" + ASSERTJ_VERSION,

"org.junit.platform:junit-platform-launcher:" + JUNIT_PLATFORM_VERSION,
"org.junit.platform:junit-platform-reporting:" + JUNIT_PLATFORM_VERSION,

"org.junit.jupiter:junit-jupiter-api:" + JUNIT_VERSION,
"org.junit.jupiter:junit-jupiter-engine:" + JUNIT_VERSION,

"org.mockito:mockito-core:" + MOCKITO_VERSION,
"org.mockito:mockito-junit-jupiter:" + MOCKITO_VERSION,

"org.testcontainers:junit-jupiter:" + TESTCONTAINERS_VERSION,
"org.testcontainers:postgresql:" + TESTCONTAINERS_VERSION,

# Dependency Injection
"com.google.inject:guice:" + GUICE_VERSION,
"com.google.inject.extensions:guice-servlet:" + GUICE_VERSION,
Expand Down
3,725 changes: 340 additions & 3,385 deletions MODULE.bazel.lock

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions projects/steel/src/main/java/uk/co/amrik/steel/API/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
import java.util.Optional;

@Path("/")
public interface Api<T> {
public interface Api<Request, Response> {

@GET
@Produces(MediaType.APPLICATION_JSON)
List<T> getAll();
List<Response> getAll();

@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
Optional<T> get(@PathParam("id") Integer id);
Optional<Response> get(@PathParam("id") Integer id);

@DELETE
@Path("/{id}")
void delete(@PathParam("id") Integer id);
int delete(@PathParam("id") Integer id);

@PUT
@Produces(MediaType.APPLICATION_JSON)
T put(T entity);
Optional<Response> put(Request entity);

}
34 changes: 27 additions & 7 deletions projects/steel/src/main/java/uk/co/amrik/steel/order/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
load("@rules_java//java:defs.bzl", "java_library")
load("@bazel_tools//tools/jdk:default_java_toolchain.bzl", "default_java_toolchain")

default_java_toolchain(
name = "my_java_toolchain",
source_version = "21",
target_version = "21",
)

package(default_visibility = ["//visibility:public"])

Expand All @@ -12,17 +19,10 @@ filegroup(
srcs = glob(["api/v1/**/*.java"]),
)

filegroup(
name = "order_model",
srcs = glob(["model/**/*.java"]),
)


java_library(
name = "order",
srcs = [
":order_root",
":order_model",
":order_api_v1"
],
deps = [
Expand All @@ -38,3 +38,23 @@ java_library(
"@maven//:org_jooq_jooq"
]
)

filegroup(
name = "order_test_srcs",
srcs = glob(["**/*.java"]),
)

java_library(
name = "order_test_lib",
srcs = [":order_test_srcs"],
deps = [
"//projects/steel/src/main/java/uk/co/amrik/steel/order",
"//projects/steel/src/main/java/uk/co/amrik/steel/persistence",
"//projects/steel/src/main/java/uk/co/amrik/steel/API",
"@maven//:org_junit_jupiter_junit_jupiter_api",
"@maven//:org_junit_jupiter_junit_jupiter_engine",
"@maven//:org_assertj_assertj_core",
"@maven//:org_jooq_jooq",
"@maven//:com_google_inject_guice"
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package uk.co.amrik.steel.order.api.v1;

public record OrderRequest(String name) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package uk.co.amrik.steel.order.api.v1;

public record OrderResponse(Integer id, String name) {

}

Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import com.google.inject.Inject;
import org.jooq.impl.DSL;
import uk.co.amrik.steel.API.Api;
import uk.co.amrik.steel.order.model.Order;
import uk.co.amrik.steel.persistence.DatabaseService;

import java.util.List;
import java.util.Optional;

public class OrderService implements Api<Order> {
public class OrderService implements Api<OrderRequest, OrderResponse> {

private final DatabaseService databaseService;

Expand All @@ -19,25 +18,34 @@ public OrderService(DatabaseService databaseService) {
}

@Override
public List<Order> getAll() {
public List<OrderResponse> getAll() {
return databaseService.getDsl().select()
.from(DSL.table(DSL.name("order")))
.fetch()
.map(record -> record.into(Order.class));
.map(record -> record.into(OrderResponse.class));
}

@Override
public Optional<Order> get(Integer id) {
return Optional.empty();
public Optional<OrderResponse> get(Integer id) {
return databaseService.getDsl().select()
.from(DSL.table(DSL.name("order")))
.where(DSL.field(DSL.name("id")).eq(id))
.fetchOptional()
.map(record -> record.into(OrderResponse.class));
}

@Override
public void delete(Integer id) {

public int delete(Integer id) {
return databaseService.getDsl().delete(DSL.table(DSL.name("order")))
.where(DSL.field(DSL.name("id")).eq(id))
.execute();
}

@Override
public Order put(Order entity) {
return null;
public Optional<OrderResponse> put(OrderRequest entity) {
return databaseService.getDsl().insertInto(DSL.table(DSL.name("order")))
.set(DSL.field(DSL.name("name"), String.class), entity.name())
.returning()
.fetchOptionalInto(OrderResponse.class);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,22 @@ public class DatabaseConfig {
public DatabaseConfig(){
Map<String, String> env = System.getenv();

this.host = env.getOrDefault("STEEL_DB_HOST", "localhost");
this.port = env.getOrDefault("STEEL_DB_PORT", "5432");
this.name = env.getOrDefault("STEEL_DB_NAME", "steel");
this.user = env.getOrDefault("STEEL_DB_USER", "postgres");
this.pass = env.getOrDefault("STEEL_DB_PASS", "postgres");
this.host = System.getProperty("STEEL_DB_HOST", env.getOrDefault("STEEL_DB_HOST", "localhost"));
this.port = System.getProperty("STEEL_DB_PORT", env.getOrDefault("STEEL_DB_PORT", "5432"));
this.name = System.getProperty("STEEL_DB_NAME", env.getOrDefault("STEEL_DB_NAME", "steel"));
this.user = System.getProperty("STEEL_DB_USER", env.getOrDefault("STEEL_DB_USER", "postgres"));
this.pass = System.getProperty("STEEL_DB_PASS", env.getOrDefault("STEEL_DB_PASS", "postgres"));

}

public DatabaseConfig(String host, String port, String name, String user, String pass){
this.host = host;
this.port = port;
this.name = name;
this.user = user;
this.pass = pass;
}

public String getHost() {
return host;
}
Expand All @@ -40,4 +48,20 @@ public String getUser() {
public String getPass() {
return pass;
}

class Builder {

private String host;
private String port;
private String name;
private String user;
private String pass;

public Builder setHost(String host){
this.host = host;
return this;
}

}

}
6 changes: 0 additions & 6 deletions projects/steel/src/main/java/uk/co/amrik/steel/user/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,10 @@ filegroup(
srcs = glob(["api/v1/**/*.java"]),
)

filegroup(
name = "user_model",
srcs = glob(["model/**/*.java"]),
)

java_library(
name = "user",
srcs = [
":user_root",
":user_model",
":user_api_v1",
],
javacopts = ["-source", "21", "-target", "21"],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package uk.co.amrik.steel.user.api.v1;

public record UserRequest(Integer id, String name) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package uk.co.amrik.steel.user.api.v1;

public record UserResponse(Integer id, String name) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import org.jooq.impl.DSL;
import uk.co.amrik.steel.API.Api;
import uk.co.amrik.steel.persistence.DatabaseService;
import uk.co.amrik.steel.user.model.User;

import java.util.List;
import java.util.Optional;

public class UserService implements Api<User> {
public class UserService implements Api<UserRequest, UserResponse> {

private final DatabaseService databaseService;

Expand All @@ -20,29 +20,29 @@ public UserService(
}

@Override
public List<User> getAll() {
public List<UserResponse> getAll() {
return databaseService.getDsl().select()
.from(DSL.table(DSL.name("user")))
.fetch()
.map(record -> record.into(User.class));
.map(record -> record.into(UserResponse.class));
}

@Override
public Optional<User> get(Integer id) {
public Optional<UserResponse> get(Integer id) {
return databaseService.getDsl().select()
.from(DSL.table(DSL.name("user")))
.where(DSL.field(DSL.name("id")).eq(id))
.fetchOptional()
.map(record -> record.into(User.class));
.map(record -> record.into(UserResponse.class));
}

@Override
public void delete(Integer id) {

public int delete(Integer id) {
return 0;
}

@Override
public User put(User entity) {
return null;
public Optional<UserResponse> put(UserRequest entity) {
return Optional.empty();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE table "order" (
id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
name VARCHAR(255) NOT NULL UNIQUE
);
)
Loading

0 comments on commit 184b943

Please sign in to comment.