Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package za.ac.cput.project.universalhardwarestorev2.api; import java.util.Date; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.util.UriComponentsBuilder; import za.ac.cput.project.universalhardwarestorev2.domain.Item; import za.ac.cput.project.universalhardwarestorev2.services.ItemService; /** * * @author garran */ @RestController @RequestMapping("/unistore/api/**") public class ItemApi { @Autowired private ItemService service; private Date systemDate = new Date(); //-------------------Retrieve All Item-------------------------------------------------------- @RequestMapping(value = "/items/", method = RequestMethod.GET) public ResponseEntity<List<Item>> listAllItems() { List<Item> Items = service.findAll(); if (Items.isEmpty()) { return new ResponseEntity<List<Item>>(HttpStatus.NO_CONTENT);//You many decide to return HttpStatus.NOT_FOUND } return new ResponseEntity<List<Item>>(Items, HttpStatus.OK); } //-------------------Retrieve Single Item-------------------------------------------------------- @RequestMapping(value = "/item/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<Item> getItem(@PathVariable("id") long id) { System.out.println("Fetching Item with id " + id); Item Item = service.findById(id); if (Item == null) { System.out.println("Item with id " + id + " not found"); return new ResponseEntity<Item>(HttpStatus.NOT_FOUND); } return new ResponseEntity<Item>(Item, HttpStatus.OK); } //-------------------Create a Item-------------------------------------------------------- @RequestMapping(value = "/item/create", method = RequestMethod.POST) public ResponseEntity<Void> createItem(@RequestBody Item item, UriComponentsBuilder ucBuilder) { System.out.println("Creating Item " + item.getName()); // USE THIS IF YOU WANT TO CHECK UNIQUE OBJECT // if (ItemService.isItemExist(Item)) { // System.out.println("A Item with name " + Item.getName() + " already exist"); // return new ResponseEntity<Void>(HttpStatus.CONFLICT); // } service.save(item); HttpHeaders headers = new HttpHeaders(); headers.setLocation(ucBuilder.path("/item/{id}").buildAndExpand(item.getId()).toUri()); return new ResponseEntity<Void>(headers, HttpStatus.CREATED); } //------------------- Update a Item -------------------------------------------------------- @RequestMapping(value = "/item/update/{id}", method = RequestMethod.PUT) public ResponseEntity<Item> updateItem(@PathVariable("id") long id, @RequestBody Item Item) { System.out.println("Updating Item " + id); Item currentItem = service.findById(id); if (currentItem == null) { System.out.println("Item with id " + id + " not found"); return new ResponseEntity<Item>(HttpStatus.NOT_FOUND); } Item updatedItem = new Item.Builder(currentItem.getCode()).copy(currentItem).name(Item.getName()) .description(Item.getDescription()).price(Item.getPrice()).quantity(Item.getQuantity()).build(); service.update(updatedItem); return new ResponseEntity<Item>(updatedItem, HttpStatus.OK); } //------------------- Delete a Item -------------------------------------------------------- @RequestMapping(value = "/item/delete/{id}", method = RequestMethod.GET) public ResponseEntity<Item> deleteItem(@PathVariable("id") long id) { System.out.println("Fetching & Deleting Item with id " + id); Item item = service.findById(id); if (item == null) { System.out.println("Unable to delete. Item with id " + id + " not found"); return new ResponseEntity<Item>(HttpStatus.NOT_FOUND); } service.delete(item); return new ResponseEntity<Item>(HttpStatus.NO_CONTENT); } }