Created
April 5, 2015 20:41
-
-
Save linyatis/471ed85aaeebd1874dae to your computer and use it in GitHub Desktop.
Classes Projeto VRaptor + Angular (cookbook vraptor)
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 characters
| package br.com.caelum.vraptor.controller; | |
| import java.util.List; | |
| import javax.inject.Inject; | |
| import br.com.caelum.vraptor.Consumes; | |
| import br.com.caelum.vraptor.Controller; | |
| import br.com.caelum.vraptor.Delete; | |
| import br.com.caelum.vraptor.Get; | |
| import br.com.caelum.vraptor.Path; | |
| import br.com.caelum.vraptor.Post; | |
| import br.com.caelum.vraptor.Put; | |
| import br.com.caelum.vraptor.Result; | |
| import br.com.caelum.vraptor.dao.ProductDao; | |
| import br.com.caelum.vraptor.model.Product; | |
| import br.com.caelum.vraptor.view.Results; | |
| @Controller | |
| @Path("/products") | |
| public class ProductController { | |
| private Result result; | |
| private ProductDao dao; | |
| @Inject | |
| public ProductController(Result result, ProductDao dao) { | |
| this.result = result; | |
| this.dao = dao; | |
| } | |
| /** | |
| * @deprecated CDI's eyes only | |
| */ | |
| public ProductController() { | |
| this(null, null); | |
| } | |
| @Get | |
| public void list() { | |
| List<Product> products = dao.list(); | |
| result.use(Results.json()).from(products).serialize(); | |
| } | |
| @Get("/{id}") | |
| public void getById(Long id) { | |
| Product product = dao.getById(id); | |
| result.use(Results.json()).from(product).serialize(); | |
| } | |
| @Post | |
| @Consumes("application/json") | |
| public void add(Product product) { | |
| dao.add(product); | |
| result.use(Results.json()).from(product).serialize(); | |
| } | |
| @Put("/{product.id}") | |
| @Consumes("application/json") | |
| public void update(Product product) { | |
| dao.update(product); | |
| result.use(Results.json()).from(product).serialize(); | |
| } | |
| @Delete("/{id}") | |
| public void delete(Long id) { | |
| result.use(Results.status()).ok(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment