package week4.college7.bigshopper.shopping.webservices; import week4.college7.bigshopper.shopping.helpers.SimpleShoppingListResponse; import week4.college7.bigshopper.shopping.model.Shop; import week4.college7.bigshopper.shopping.model.ShoppingList; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @Path("list") public class ListResource { @GET @Produces(MediaType.APPLICATION_JSON) public Response getShoppingLists() { // Pak alle shopping lists List shoppingLists = Shop.getShop().getAllShoppingLists(); // Return error response met Map.of() if(shoppingLists.isEmpty()) { // https://stackoverflow.com/questions/6802483/how-to-directly-initialize-a-hashmap-in-a-literal-way Map errorObject = Map.of("error", "no lists present"); return Response.ok(errorObject).build(); } // Return de shopping lists return Response.ok(shoppingLists).build(); // ... of: // Mapping met map() naar SimpleShoppingListResponse List responseObjects = shoppingLists.stream().map( shoppingList -> SimpleShoppingListResponse.fromShoppingList(shoppingList) ).collect(Collectors.toUnmodifiableList()); return Response.ok(responseObjects).build(); } @GET @Produces(MediaType.APPLICATION_JSON) @Path("{name}") public Response getShoppingListByName(@PathParam("name") String name) { ShoppingList list = Shop.getShop().getShoppingListByName(name); if(list == null) { return Response.ok(Map.of("error", "no list by that name")).build(); } return Response.ok(list).build(); } }