Search in sources :

Example 61 with Product

use of com.salesmanager.core.model.catalog.product.Product in project shopizer by shopizer-ecommerce.

the class ProductGroupApi method addProductToGroup.

@ResponseStatus(HttpStatus.CREATED)
@RequestMapping(value = "/private/products/{productId}/group/{code}", method = RequestMethod.POST)
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
@ResponseBody
public ReadableProductList addProductToGroup(@PathVariable Long productId, @PathVariable String code, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language, HttpServletResponse response) {
    Product product = null;
    try {
        // get the product
        product = productService.getById(productId);
        if (product == null) {
            response.sendError(404, "Product not fount for id " + productId);
            return null;
        }
    } catch (Exception e) {
        LOGGER.error("Error while adding product to group", e);
        try {
            response.sendError(503, "Error while adding product to group " + e.getMessage());
        } catch (Exception ignore) {
        }
        return null;
    }
    ReadableProductList list = productItemsFacade.addItemToGroup(product, code, merchantStore, language);
    return list;
}
Also used : ReadableProductList(com.salesmanager.shop.model.catalog.product.ReadableProductList) Product(com.salesmanager.core.model.catalog.product.Product) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 62 with Product

use of com.salesmanager.core.model.catalog.product.Product in project shopizer by shopizer-ecommerce.

the class ProductGroupApi method removeProductFromGroup.

@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = "/private/products/{productId}/group/{code}", method = RequestMethod.DELETE)
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
@ResponseBody
public ReadableProductList removeProductFromGroup(@PathVariable Long productId, @PathVariable String code, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language, HttpServletRequest request, HttpServletResponse response) {
    try {
        // get the product
        Product product = productService.getById(productId);
        if (product == null) {
            response.sendError(404, "Product not fount for id " + productId);
            return null;
        }
        ReadableProductList list = productItemsFacade.removeItemFromGroup(product, code, merchantStore, language);
        return list;
    } catch (Exception e) {
        LOGGER.error("Error while removing product from category", e);
        try {
            response.sendError(503, "Error while removing product from category " + e.getMessage());
        } catch (Exception ignore) {
        }
        return null;
    }
}
Also used : ReadableProductList(com.salesmanager.shop.model.catalog.product.ReadableProductList) Product(com.salesmanager.core.model.catalog.product.Product) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 63 with Product

use of com.salesmanager.core.model.catalog.product.Product in project shopizer by shopizer-ecommerce.

the class ProductImageApi method uploadImage.

/**
 * To be used with MultipartFile
 *
 * @param id
 * @param uploadfiles
 * @param request
 * @param response
 * @throws Exception
 */
@ResponseStatus(HttpStatus.CREATED)
@RequestMapping(value = { "/private/products/{id}/images", "/auth/products/{id}/images" }, consumes = { MediaType.MULTIPART_FORM_DATA_VALUE }, method = RequestMethod.POST)
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public void uploadImage(@PathVariable Long id, @RequestParam(value = "file", required = true) MultipartFile[] files, @RequestParam(value = "order", required = false, defaultValue = "0") Integer position, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language) throws IOException {
    try {
        // get the product
        Product product = productService.getById(id);
        if (product == null) {
            throw new ResourceNotFoundException("Product not found");
        }
        // product belongs to merchant store
        if (product.getMerchantStore().getId().intValue() != merchantStore.getId().intValue()) {
            throw new UnauthorizedException("Resource not authorized for this merchant");
        }
        boolean hasDefaultImage = false;
        Set<ProductImage> images = product.getImages();
        if (!CollectionUtils.isEmpty(images)) {
            for (ProductImage image : images) {
                if (image.isDefaultImage()) {
                    hasDefaultImage = true;
                    break;
                }
            }
        }
        List<ProductImage> contentImagesList = new ArrayList<ProductImage>();
        int sortOrder = position;
        for (MultipartFile multipartFile : files) {
            if (!multipartFile.isEmpty()) {
                ProductImage productImage = new ProductImage();
                productImage.setImage(multipartFile.getInputStream());
                productImage.setProductImage(multipartFile.getOriginalFilename());
                productImage.setProduct(product);
                if (!hasDefaultImage) {
                    productImage.setDefaultImage(true);
                    hasDefaultImage = true;
                }
                productImage.setSortOrder(sortOrder);
                position++;
                contentImagesList.add(productImage);
            }
        }
        if (CollectionUtils.isNotEmpty(contentImagesList)) {
            productImageService.addProductImages(product, contentImagesList);
        }
    } catch (Exception e) {
        LOGGER.error("Error while creating ProductImage", e);
        throw new ServiceRuntimeException("Error while creating image");
    }
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) ArrayList(java.util.ArrayList) Product(com.salesmanager.core.model.catalog.product.Product) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) IOException(java.io.IOException) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 64 with Product

use of com.salesmanager.core.model.catalog.product.Product in project shopizer by shopizer-ecommerce.

the class ProductImageApi method images.

/**
 * Get product images
 * @param id
 * @param imageId
 * @param merchantStore
 * @param language
 * @return
 */
@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = { "/products/{productId}/images" }, method = RequestMethod.GET)
@ApiOperation(httpMethod = "GET", value = "Get images for a given product")
@ApiResponses(value = { @ApiResponse(code = 200, message = "List of ProductImage found", response = List.class) })
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public List<ReadableImage> images(@PathVariable Long productId, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language) {
    Product p = productService.getById(productId);
    if (p == null) {
        throw new ResourceNotFoundException("Product images not found for product id [" + productId + "] and merchant [" + merchantStore.getCode() + "]");
    }
    if (p.getMerchantStore().getId() != merchantStore.getId()) {
        throw new ResourceNotFoundException("Product images not found for product id [" + productId + "] and merchant [" + merchantStore.getCode() + "]");
    }
    List<ReadableImage> target = new ArrayList<ReadableImage>();
    Set<ProductImage> images = p.getImages();
    if (images != null && images.size() > 0) {
        target = images.stream().map(i -> image(i, merchantStore, language)).sorted(Comparator.comparingInt(ReadableImage::getOrder)).collect(Collectors.toList());
    }
    return target;
}
Also used : ProductService(com.salesmanager.core.business.services.catalog.product.ProductService) PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Controller(org.springframework.stereotype.Controller) ApiResponses(io.swagger.annotations.ApiResponses) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Valid(javax.validation.Valid) Language(com.salesmanager.core.model.reference.language.Language) ApiOperation(io.swagger.annotations.ApiOperation) HttpServletRequest(javax.servlet.http.HttpServletRequest) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ReadableImage(com.salesmanager.shop.model.catalog.product.ReadableImage) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Api(io.swagger.annotations.Api) Tag(io.swagger.annotations.Tag) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) Product(com.salesmanager.core.model.catalog.product.Product) Logger(org.slf4j.Logger) MediaType(org.springframework.http.MediaType) ApiImplicitParam(io.swagger.annotations.ApiImplicitParam) HttpServletResponse(javax.servlet.http.HttpServletResponse) Set(java.util.Set) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) IOException(java.io.IOException) ReadableProductImageMapper(com.salesmanager.shop.mapper.catalog.ReadableProductImageMapper) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) Collectors(java.util.stream.Collectors) ProductImageService(com.salesmanager.core.business.services.catalog.product.image.ProductImageService) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) ApiIgnore(springfox.documentation.annotations.ApiIgnore) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) NameEntity(com.salesmanager.shop.model.entity.NameEntity) SwaggerDefinition(io.swagger.annotations.SwaggerDefinition) ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ApiResponse(io.swagger.annotations.ApiResponse) Optional(java.util.Optional) MultipartFile(org.springframework.web.multipart.MultipartFile) Comparator(java.util.Comparator) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ReadableImage(com.salesmanager.shop.model.catalog.product.ReadableImage) ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) ArrayList(java.util.ArrayList) Product(com.salesmanager.core.model.catalog.product.Product) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 65 with Product

use of com.salesmanager.core.model.catalog.product.Product in project shopizer by shopizer-ecommerce.

the class ProductRelationshipApi method getAll.

/*	@RequestMapping( value={"/private/products/{id}/related","/auth/products/{id}/related"}, method=RequestMethod.POST)
  @ResponseStatus(HttpStatus.CREATED)
  @ResponseBody
  public PersistableProductReview create(@PathVariable final Long id, @Valid @RequestBody PersistableProductReview review, HttpServletRequest request, HttpServletResponse response) throws Exception {


  	try {

  		MerchantStore merchantStore = storeFacade.getByCode(request);
  		Language language = languageUtils.getRESTLanguage(request, merchantStore);

  		//rating already exist
  		ProductReview prodReview = productReviewService.getByProductAndCustomer(review.getProductId(), review.getCustomerId());
  		if(prodReview!=null) {
  			response.sendError(500, "A review already exist for this customer and product");
  			return null;
  		}

  		//rating maximum 5
  		if(review.getRating()>Constants.MAX_REVIEW_RATING_SCORE) {
  			response.sendError(503, "Maximum rating score is " + Constants.MAX_REVIEW_RATING_SCORE);
  			return null;
  		}

  		review.setProductId(id);



  		productFacade.saveOrUpdateReview(review, merchantStore, language);

  		return review;

  	} catch (Exception e) {
  		LOGGER.error("Error while saving product review",e);
  		try {
  			response.sendError(503, "Error while saving product review" + e.getMessage());
  		} catch (Exception ignore) {
  		}

  		return null;
  	}
  }*/
@RequestMapping(value = "/products/{id}/related", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ApiOperation(httpMethod = "GET", value = "Get product related items. This is used for doing cross-sell and up-sell functionality on a product details page", notes = "", produces = "application/json", response = List.class)
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public List<ReadableProduct> getAll(@PathVariable final Long id, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language, HttpServletResponse response) throws Exception {
    try {
        // product exist
        Product product = productService.getById(id);
        if (product == null) {
            response.sendError(404, "Product id " + id + " does not exists");
            return null;
        }
        List<ReadableProduct> relatedItems = productFacade.relatedItems(merchantStore, product, language);
        return relatedItems;
    } catch (Exception e) {
        LOGGER.error("Error while getting product reviews", e);
        try {
            response.sendError(503, "Error while getting product reviews" + e.getMessage());
        } catch (Exception ignore) {
        }
        return null;
    }
}
Also used : ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) Product(com.salesmanager.core.model.catalog.product.Product) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

Product (com.salesmanager.core.model.catalog.product.Product)120 ReadableProduct (com.salesmanager.shop.model.catalog.product.ReadableProduct)53 ArrayList (java.util.ArrayList)45 Language (com.salesmanager.core.model.reference.language.Language)42 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)41 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)36 ServiceException (com.salesmanager.core.business.exception.ServiceException)35 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)35 ProductAvailability (com.salesmanager.core.model.catalog.product.availability.ProductAvailability)33 ReadableProductPopulator (com.salesmanager.shop.populator.catalog.ReadableProductPopulator)33 PersistableProduct (com.salesmanager.shop.model.catalog.product.PersistableProduct)29 ProductAttribute (com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)28 List (java.util.List)25 Date (java.util.Date)23 LightPersistableProduct (com.salesmanager.shop.model.catalog.product.LightPersistableProduct)22 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)21 Category (com.salesmanager.core.model.catalog.category.Category)20 Collectors (java.util.stream.Collectors)20 FinalPrice (com.salesmanager.core.model.catalog.product.price.FinalPrice)19 ConversionException (com.salesmanager.core.business.exception.ConversionException)17