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 org.khmeracademy.btb.auc.pojo.controller; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.khmeracademy.btb.auc.pojo.entity.Product; import org.khmeracademy.btb.auc.pojo.filtering.AuctionFilter; import org.khmeracademy.btb.auc.pojo.service.Product_service; import org.khmeracademy.btb.auc.pojo.utilities.Pagination; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; 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.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import springfox.documentation.annotations.ApiIgnore; /** * * @author User */ @RestController @RequestMapping(value = "/api/product") public class Product_Controller { @Autowired private Product_service pro_service; @RequestMapping(value = "/get", method = RequestMethod.GET, produces = "application/json") @ResponseBody public ResponseEntity<Map<String, Object>> getUsers() { Map<String, Object> map = new HashMap<String, Object>(); try { ArrayList<Product> pro = pro_service.getProduct(); if (!pro.isEmpty()) { map.put("DATA", pro); map.put("STATUS", true); map.put("MESSAGE", "DATA FOUND!"); } else { map.put("STATUS", true); map.put("MESSAGE", "DATA NOT FOUND"); } } catch (Exception e) { map.put("STATUS", false); map.put("MESSAGE", "Error!"); e.printStackTrace(); } return new ResponseEntity<Map<String, Object>>(map, HttpStatus.OK); } @RequestMapping(value = "/delete/{id}", method = RequestMethod.PUT, produces = "application/json") public ResponseEntity<Map<String, Object>> delete(@PathVariable("id") int id) { Map<String, Object> map = new HashMap<String, Object>(); try { if (pro_service.remove(id)) { map.put("MESSAGE", "Product has been deleted"); map.put("STATUS", true); } else { map.put("MESSAGE", "Product has not been deleted"); map.put("STATUS", false); } } catch (Exception e) { map.put("MESSAGE", "Error!"); map.put("STATUS", false); e.printStackTrace(); } return new ResponseEntity<Map<String, Object>>(map, HttpStatus.OK); } @RequestMapping(value = "/add", method = RequestMethod.POST) public ResponseEntity<Map<String, Object>> add(Product pro) { Map<String, Object> map = new HashMap<String, Object>(); try { if (pro_service.save(pro, pro.getImages())) { map.put("MESSAGE", "Product has been inserted."); map.put("STATUS", true); } else { map.put("MESSAGE", "Product has not been inserted."); map.put("STATUS", false); } } catch (Exception e) { map.put("MESSAGE", "Error!"); map.put("STATUS", false); e.printStackTrace(); } return new ResponseEntity<Map<String, Object>>(map, HttpStatus.OK); } @RequestMapping(value = "/edit", method = RequestMethod.PUT, produces = "application/json") public ResponseEntity<Map<String, Object>> edit(@RequestBody Product pro) { Map<String, Object> map = new HashMap<String, Object>(); try { if (pro_service.update(pro)) { map.put("MESSAGE", "Product has been updated."); map.put("STATUS", true); } else { map.put("MESSAGE", "Product has not been updated."); map.put("STATUS", false); } } catch (Exception e) { map.put("MESSAGE", "Error!"); map.put("STATUS", false); e.printStackTrace(); } return new ResponseEntity<Map<String, Object>>(map, HttpStatus.OK); } @RequestMapping(value = "/search/{id}", method = RequestMethod.GET, produces = "application/json") @ResponseBody public ResponseEntity<Map<String, Object>> search(@PathVariable("id") int id) { Map<String, Object> map = new HashMap<String, Object>(); try { Product pro = pro_service.search(id); if (pro != null) { map.put("DATA", pro); map.put("STATUS", true); map.put("MESSAGE", "DATA FOUND!"); } else { map.put("STATUS", true); map.put("MESSAGE", "DATA NOT FOUND"); } } catch (Exception e) { map.put("STATUS", false); map.put("MESSAGE", "Error!"); e.printStackTrace(); } return new ResponseEntity<Map<String, Object>>(map, HttpStatus.OK); } @RequestMapping(value = "/getlatest", method = RequestMethod.GET, produces = "application/json") @ResponseBody public ResponseEntity<Map<String, Object>> getlastest() { Map<String, Object> map = new HashMap<String, Object>(); try { Product pro = pro_service.getLatest(); if (pro != null) { map.put("DATA", pro); map.put("STATUS", true); map.put("MESSAGE", "DATA FOUND!"); } else { map.put("STATUS", true); map.put("MESSAGE", "DATA NOT FOUND"); } } catch (Exception e) { map.put("STATUS", false); map.put("MESSAGE", "Error!"); e.printStackTrace(); } return new ResponseEntity<Map<String, Object>>(map, HttpStatus.OK); } @RequestMapping(value = "/get-number-of-product", method = RequestMethod.GET, produces = "application/json") @ResponseBody public ResponseEntity<Map<String, Object>> getNumberofProduct() { Map<String, Object> map = new HashMap<String, Object>(); try { int number = pro_service.countProduct(); if (number != 0) { map.put("DATA", number); map.put("STATUS", true); map.put("MESSAGE", "DATA FOUND!"); } else { map.put("STATUS", true); map.put("MESSAGE", "DATA NOT FOUND"); } } catch (Exception e) { map.put("STATUS", false); map.put("MESSAGE", "Error!"); e.printStackTrace(); } return new ResponseEntity<Map<String, Object>>(map, HttpStatus.OK); } @RequestMapping(value = "/get-all", method = RequestMethod.GET, produces = "application/json") @ApiImplicitParams({ @ApiImplicitParam(name = "page", paramType = "query", defaultValue = "1"), @ApiImplicitParam(name = "limit", paramType = "query", defaultValue = "10"), @ApiImplicitParam(name = "name", paramType = "query", defaultValue = ""), @ApiImplicitParam(name = "brandId", paramType = "query", defaultValue = "") }) @ResponseBody public ResponseEntity<Map<String, Object>> findAll(@ApiIgnore AuctionFilter filter, @ApiIgnore Pagination pagination) { Map<String, Object> map = new HashMap<String, Object>(); try { pagination.setTotalCount(pro_service.countProduct()); ArrayList<Product> pro = (ArrayList<Product>) pro_service.findAll(filter, pagination); if (!pro.isEmpty()) { map.put("DATA", pro); map.put("STATUS", true); map.put("MESSAGE", "DATA FOUND!"); map.put("PAGINATION", pagination); } else { map.put("STATUS", true); map.put("MESSAGE", "DATA NOT FOUND"); } } catch (Exception e) { map.put("STATUS", false); map.put("MESSAGE", "Error!"); e.printStackTrace(); } return new ResponseEntity<Map<String, Object>>(map, HttpStatus.OK); } }