コード例 #1
0
  /**
   * create and add new Catalog to database
   *
   * @param productDescription new product
   * @param subCategoryName name of subcategory of product
   * @param categoryName name of category of product
   * @return message success if not return message fail
   */
  @ResponseBody
  @RequestMapping(value = "/new", method = RequestMethod.POST)
  public ResponseEntity<?> createNewCatalog(
      @RequestBody ProductDescription productDescription,
      @RequestParam(value = "subCategory", required = false) String subCategoryName,
      @RequestParam(value = "category", required = false) String categoryName,
      @RequestHeader(value = "Authorization") String token) {

    if (!authenticationService.checkPermission(
        token,
        authenticationService.STAFF,
        authenticationService.MANAGER,
        authenticationService.OWNER)) {
      return new ResponseEntity<Message>(
          new Message("This user does not allow"), HttpStatus.FORBIDDEN);
    }

    ProductDescription product = productDescriptionService.findByKey(productDescription.getId());
    SubCategory subCategory = subCategoryService.findByName(subCategoryName);
    Category category = categoryService.findByName(categoryName);

    try {
      catalogService.createCatalog(category, subCategory, product);
    } catch (Exception e) {
      return new ResponseEntity<Message>(new Message("Created fail"), HttpStatus.BAD_REQUEST);
    }

    return new ResponseEntity<Message>(new Message("Catalog has added"), HttpStatus.CREATED);
  }
コード例 #2
0
  /**
   * Edit Catalog to database
   *
   * @param Catalog new catalog
   * @return message success if not return message fail
   */
  @ResponseBody
  @RequestMapping(value = "/edit", method = RequestMethod.PUT)
  public ResponseEntity<?> editCatalog(
      @RequestBody Catalog catalog, @RequestHeader(value = "Authorization") String token) {

    if (!authenticationService.checkPermission(
        token,
        authenticationService.STAFF,
        authenticationService.MANAGER,
        authenticationService.OWNER)) {
      return new ResponseEntity<Message>(
          new Message("This user does not allow"), HttpStatus.FORBIDDEN);
    }

    Type type = typeService.findByKey(catalog.getType().getId());
    ProductDescription product =
        productDescriptionService.findByKey(catalog.getProductDescription().getId());
    catalog.setType(type);
    catalog.setProductDescription(product);

    try {
      catalogService.update(catalog);
    } catch (Exception e) {
      return new ResponseEntity<Message>(new Message("Edited fail"), HttpStatus.BAD_REQUEST);
    }

    return new ResponseEntity<Message>(new Message("Catalog has edited"), HttpStatus.CREATED);
  }