Last active
May 25, 2023 09:26
-
-
Save reinder42/38f8e6f28ce88891f102856c5fc74d16 to your computer and use it in GitHub Desktop.
Revisions
-
reinder42 revised this gist
May 25, 2023 . No changes.There are no files selected for viewing
-
reinder42 created this gist
May 25, 2023 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,63 @@ 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<ShoppingList> 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<String, String> 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<SimpleShoppingListResponse> 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(); } } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,13 @@ // ... @Path("shopper") public class PersonResource { @GET @Produces(MediaType.APPLICATION_JSON) public List<Shopper> getShoppers() { return Shop.getShop().getAllPersons(); } // ... } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ // ... @Path("product") public class ProductResource { @GET @Produces(MediaType.APPLICATION_JSON) public Response getProducts() { List<Product> products = Shop.getShop().getAllProducts(); if(products.isEmpty()) { return Response.ok(Map.of("error", "no products available")).build(); } // Transformeer lijst van objecten naar lijst van strings met map() List<String> productNames = products.stream().map(product -> product.getName()).collect(Collectors.toList()); return Response.ok(productNames).build(); } } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ package week4.college7.bigshopper.shopping.helpers; import javassist.Loader; import week4.college7.bigshopper.shopping.model.Item; import week4.college7.bigshopper.shopping.model.ShoppingList; import java.util.List; public class SimpleShoppingListResponse { public String name; public String owner; public List<Item> listItems; private SimpleShoppingListResponse(String name, String owner, List<Item> listItems) { this.name = name; this.owner = owner; this.listItems = listItems; } public static SimpleShoppingListResponse fromShoppingList(ShoppingList shoppingList) { SimpleShoppingListResponse responseObject = new SimpleShoppingListResponse( shoppingList.getName(), shoppingList.getOwner().getName(), shoppingList.getListItems() ); return responseObject; } }