-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from ueberfuhr-trainings/feature/boundary
Implement API
- Loading branch information
Showing
8 changed files
with
437 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
customer-api-provider/src/main/java/de/samples/schulung/quarkus/CustomerDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
83 changes: 83 additions & 0 deletions
83
customer-api-provider/src/main/java/de/samples/schulung/quarkus/CustomersResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
16 changes: 0 additions & 16 deletions
16
customer-api-provider/src/main/java/de/samples/schulung/quarkus/GreetingResource.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.