forked from mathibodeau/petstore
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minimalistic REST interface for adding items to inventory
- Loading branch information
1 parent
f8dc7df
commit 3641d4f
Showing
3 changed files
with
101 additions
and
9 deletions.
There are no files selected for viewing
39 changes: 32 additions & 7 deletions
39
petstore-app/src/main/java/com/pyxis/petstore/controller/ItemsController.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 |
---|---|---|
@@ -1,31 +1,56 @@ | ||
package com.pyxis.petstore.controller; | ||
|
||
import com.pyxis.petstore.Maybe; | ||
import com.pyxis.petstore.domain.product.Item; | ||
import com.pyxis.petstore.domain.product.ItemInventory; | ||
import com.pyxis.petstore.domain.product.Product; | ||
import com.pyxis.petstore.domain.product.ProductCatalog; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.validation.DataBinder; | ||
import org.springframework.web.bind.annotation.InitBinder; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
|
||
import javax.servlet.http.HttpServletResponse; | ||
import javax.validation.Valid; | ||
import java.beans.PropertyEditorSupport; | ||
import java.util.List; | ||
|
||
@Controller | ||
@RequestMapping(value = "/products/{productNumber}") | ||
public class ItemsController { | ||
|
||
private final ItemInventory itemInventory; | ||
private final ProductCatalog productCatalog; | ||
private final ItemInventory itemInventory; | ||
|
||
@Autowired | ||
public ItemsController(ItemInventory itemInventory) { | ||
this.itemInventory = itemInventory; | ||
} | ||
public ItemsController(ProductCatalog productCatalog, ItemInventory itemInventory) { | ||
this.productCatalog = productCatalog; | ||
this.itemInventory = itemInventory; | ||
} | ||
|
||
@InitBinder | ||
public void initBinder(DataBinder binder) { | ||
binder.registerCustomEditor(Product.class, "product", new PropertyEditorSupport() { | ||
public void setAsText(String text) throws IllegalArgumentException { | ||
Maybe<Product> product = productCatalog.findByNumber(text); | ||
if (product.exists()) setValue(product.bare()); | ||
} | ||
}); | ||
} | ||
|
||
@RequestMapping(value = "/items", method = RequestMethod.GET) | ||
public String index(@PathVariable("productNumber") String productNumber, Model model) { | ||
@RequestMapping(value = "/products/{productNumber}/items", method = RequestMethod.GET) | ||
public String index(@PathVariable("productNumber") String productNumber, Model model) { | ||
List<Item> items = itemInventory.findByProductNumber(productNumber); | ||
model.addAttribute(items); | ||
return "items"; | ||
} | ||
|
||
@RequestMapping(value = "/items", method = RequestMethod.POST) | ||
public void create(@Valid Item item, HttpServletResponse response) { | ||
itemInventory.add(item); | ||
response.setStatus(HttpServletResponse.SC_CREATED); | ||
} | ||
} |
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
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