Search in sources :

Example 6 with PersistableProductReview

use of com.salesmanager.shop.model.catalog.product.PersistableProductReview in project shopizer by shopizer-ecommerce.

the class ProductReviewApi method update.

@RequestMapping(value = { "/private/products/{id}/reviews/{reviewid}", "/auth/products/{id}/reviews/{reviewid}" }, method = RequestMethod.PUT)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public PersistableProductReview update(@PathVariable final Long id, @PathVariable final Long reviewId, @Valid @RequestBody PersistableProductReview review, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language, HttpServletRequest request, HttpServletResponse response) {
    try {
        ProductReview prodReview = productReviewService.getById(reviewId);
        if (prodReview == null) {
            response.sendError(404, "Product review with id " + reviewId + " does not exist");
            return null;
        }
        if (prodReview.getCustomer().getId().longValue() != review.getCustomerId().longValue()) {
            response.sendError(404, "Product review with id " + reviewId + " does not exist");
            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);
        productCommonFacade.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;
    }
}
Also used : ReadableProductReview(com.salesmanager.shop.model.catalog.product.ReadableProductReview) ProductReview(com.salesmanager.core.model.catalog.product.review.ProductReview) PersistableProductReview(com.salesmanager.shop.model.catalog.product.PersistableProductReview) 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 7 with PersistableProductReview

use of com.salesmanager.shop.model.catalog.product.PersistableProductReview in project shopizer by shopizer-ecommerce.

the class ShopProductRESTController method createProductReview.

@RequestMapping(value = "/private/{store}/product/review", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
public PersistableProductReview createProductReview(@PathVariable final String store, @Valid @RequestBody PersistableProductReview review, HttpServletRequest request, HttpServletResponse response) throws Exception {
    try {
        MerchantStore merchantStore = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
        if (merchantStore != null) {
            if (!merchantStore.getCode().equals(store)) {
                merchantStore = null;
            }
        }
        if (merchantStore == null) {
            merchantStore = merchantStoreService.getByCode(store);
        }
        if (merchantStore == null) {
            LOGGER.error("Merchant store is null for code " + store);
            response.sendError(500, "Merchant store is null for code " + store);
            return null;
        }
        // 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;
        }
        PersistableProductReviewPopulator populator = new PersistableProductReviewPopulator();
        populator.setLanguageService(languageService);
        populator.setCustomerService(customerService);
        populator.setProductService(productService);
        com.salesmanager.core.model.catalog.product.review.ProductReview rev = new com.salesmanager.core.model.catalog.product.review.ProductReview();
        populator.populate(review, rev, merchantStore, merchantStore.getDefaultLanguage());
        productReviewService.create(rev);
        review.setId(rev.getId());
        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;
    }
}
Also used : ProductReview(com.salesmanager.core.model.catalog.product.review.ProductReview) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ProductReview(com.salesmanager.core.model.catalog.product.review.ProductReview) PersistableProductReview(com.salesmanager.shop.model.catalog.product.PersistableProductReview) PersistableProductReviewPopulator(com.salesmanager.shop.populator.catalog.PersistableProductReviewPopulator) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 8 with PersistableProductReview

use of com.salesmanager.shop.model.catalog.product.PersistableProductReview in project shopizer by shopizer-ecommerce.

the class CustomerProductReviewController method submitProductReview.

@PreAuthorize("hasRole('AUTH_CUSTOMER')")
@RequestMapping(value = "/review/submit.html", method = RequestMethod.POST)
public String submitProductReview(@ModelAttribute("review") PersistableProductReview review, BindingResult bindingResult, Model model, HttpServletRequest request, HttpServletResponse response, Locale locale) throws Exception {
    MerchantStore store = getSessionAttribute(Constants.MERCHANT_STORE, request);
    Language language = getLanguage(request);
    Customer customer = customerFacade.getCustomerByUserName(request.getRemoteUser(), store);
    if (customer == null) {
        return "redirect:" + Constants.SHOP_URI;
    }
    Product product = productService.getById(review.getProductId());
    if (product == null) {
        return "redirect:" + Constants.SHOP_URI;
    }
    if (StringUtils.isBlank(review.getDescription())) {
        FieldError error = new FieldError("description", "description", messages.getMessage("NotEmpty.review.description", locale));
        bindingResult.addError(error);
    }
    if (review.getRating() == null) {
        FieldError error = new FieldError("rating", "rating", messages.getMessage("NotEmpty.review.rating", locale, "Product rating is required"));
        bindingResult.addError(error);
    }
    review.setDescription(SanitizeUtils.getSafeString(review.getDescription()));
    ReadableProduct readableProduct = new ReadableProduct();
    ReadableProductPopulator readableProductPopulator = new ReadableProductPopulator();
    readableProductPopulator.setPricingService(pricingService);
    readableProductPopulator.setimageUtils(imageUtils);
    readableProductPopulator.populate(product, readableProduct, store, language);
    model.addAttribute("product", readableProduct);
    /**
     * template *
     */
    StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.Customer.review).append(".").append(store.getStoreTemplate());
    if (bindingResult.hasErrors()) {
        return template.toString();
    }
    // check if customer has already evaluated the product
    List<ProductReview> reviews = productReviewService.getByProduct(product);
    for (ProductReview r : reviews) {
        if (r.getCustomer().getId().longValue() == customer.getId().longValue()) {
            ReadableProductReviewPopulator reviewPopulator = new ReadableProductReviewPopulator();
            ReadableProductReview rev = new ReadableProductReview();
            reviewPopulator.populate(r, rev, store, language);
            model.addAttribute("customerReview", rev);
            return template.toString();
        }
    }
    PersistableProductReviewPopulator populator = new PersistableProductReviewPopulator();
    populator.setCustomerService(customerService);
    populator.setLanguageService(languageService);
    populator.setProductService(productService);
    review.setDate(DateUtil.formatDate(new Date()));
    review.setCustomerId(customer.getId());
    ProductReview productReview = populator.populate(review, store, language);
    productReviewService.create(productReview);
    model.addAttribute("review", review);
    model.addAttribute("success", "success");
    ReadableProductReviewPopulator reviewPopulator = new ReadableProductReviewPopulator();
    ReadableProductReview rev = new ReadableProductReview();
    reviewPopulator.populate(productReview, rev, store, language);
    model.addAttribute("customerReview", rev);
    return template.toString();
}
Also used : Customer(com.salesmanager.core.model.customer.Customer) ReadableProductReview(com.salesmanager.shop.model.catalog.product.ReadableProductReview) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) Product(com.salesmanager.core.model.catalog.product.Product) FieldError(org.springframework.validation.FieldError) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) ReadableProductReview(com.salesmanager.shop.model.catalog.product.ReadableProductReview) ProductReview(com.salesmanager.core.model.catalog.product.review.ProductReview) PersistableProductReview(com.salesmanager.shop.model.catalog.product.PersistableProductReview) Date(java.util.Date) ReadableProductReviewPopulator(com.salesmanager.shop.populator.catalog.ReadableProductReviewPopulator) PersistableProductReviewPopulator(com.salesmanager.shop.populator.catalog.PersistableProductReviewPopulator) Language(com.salesmanager.core.model.reference.language.Language) ReadableProductPopulator(com.salesmanager.shop.populator.catalog.ReadableProductPopulator) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

PersistableProductReview (com.salesmanager.shop.model.catalog.product.PersistableProductReview)8 ProductReview (com.salesmanager.core.model.catalog.product.review.ProductReview)7 ReadableProductReview (com.salesmanager.shop.model.catalog.product.ReadableProductReview)5 PersistableProductReviewPopulator (com.salesmanager.shop.populator.catalog.PersistableProductReviewPopulator)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)3 Product (com.salesmanager.core.model.catalog.product.Product)2 Customer (com.salesmanager.core.model.customer.Customer)2 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)2 Language (com.salesmanager.core.model.reference.language.Language)2 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)2 Date (java.util.Date)2 ConversionException (com.salesmanager.core.business.exception.ConversionException)1 ProductReviewDescription (com.salesmanager.core.model.catalog.product.review.ProductReviewDescription)1 ReadableProduct (com.salesmanager.shop.model.catalog.product.ReadableProduct)1 ReadableProductPopulator (com.salesmanager.shop.populator.catalog.ReadableProductPopulator)1 ReadableProductReviewPopulator (com.salesmanager.shop.populator.catalog.ReadableProductReviewPopulator)1 HashSet (java.util.HashSet)1 Ignore (org.junit.Ignore)1