Skip to content

Commit

Permalink
Merge pull request #3 from ueberfuhr-trainings/feature/boundary
Browse files Browse the repository at this point in the history
Implement API
  • Loading branch information
ueberfuhr authored Aug 27, 2024
2 parents ac48578 + b6da7d7 commit 8acf86b
Show file tree
Hide file tree
Showing 8 changed files with 437 additions and 47 deletions.
4 changes: 2 additions & 2 deletions customer-api-provider/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<properties>
<compiler-plugin.version>3.13.0</compiler-plugin.version>
<maven.compiler.release>21</maven.compiler.release>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
Expand Down Expand Up @@ -37,7 +37,7 @@
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest</artifactId>
<artifactId>quarkus-resteasy-jsonb</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.samples.schulung.quarkus;

import jakarta.json.bind.annotation.JsonbProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.LocalDate;
import java.util.UUID;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class CustomerDto {

private UUID uuid;
private String name;
@JsonbProperty("birthdate")
private LocalDate birthday;
private String state;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package de.samples.schulung.quarkus;

import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriBuilder;

import java.time.LocalDate;
import java.time.Month;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Path("/api/v1/customers")
public class CustomersResource {

// GET /customers?state=disabled -> 200 mit Liste der Kunden (JSON)

private final Map<UUID, CustomerDto> customers = new HashMap<>();

{
var customer1 = new CustomerDto(
UUID.randomUUID(),
"Tom Mayer",
LocalDate.of(2006, Month.APRIL, 10),
"active"
);
customers.put(customer1.getUuid(), customer1);
var customer2 = new CustomerDto(
UUID.randomUUID(),
"Julia Smith",
LocalDate.of(2010, Month.OCTOBER, 20),
"locked"
);
customers.put(customer2.getUuid(), customer2);
}

@GET
@Produces(MediaType.APPLICATION_JSON)
public Collection<CustomerDto> getCustomers(@QueryParam("state") String state) {
return customers
.values()
.stream()
.filter(c -> null == state || c.getState().equals(state))
.toList();
}

// GET /customers/{id} -> 200 mit Customer

@GET
@Path("/{uuid}")
@Produces(MediaType.APPLICATION_JSON)
public CustomerDto findCustomerById(@PathParam("uuid") UUID uuid) {
return customers.get(uuid);
}

// POST /customers mit Kunde ohne UUID -> 201 mit Kunde + Location Header

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createCustomer(CustomerDto customer) {
customer.setUuid(UUID.randomUUID());
customers.put(customer.getUuid(), customer);
final var location = UriBuilder
.fromResource(CustomersResource.class)
.path(CustomersResource.class, "findCustomerById")
.build(customer.getUuid());
/* Alternatively, add a parameter "@Context UriInfo info" and use it like this:
*
* info
* .getAbsolutePathBuilder()
* .path(customer.getUuid().toString())
* .build();
*/
return Response
.created(location)
.entity(customer)
.build();
}

}

This file was deleted.

Loading

0 comments on commit 8acf86b

Please sign in to comment.