Search in sources :

Example 1 with PersistableProductReview

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

the class ProductFacadeImpl method saveOrUpdateReview.

@Override
public void saveOrUpdateReview(PersistableProductReview review, MerchantStore store, Language language) throws Exception {
    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, store, language);
    if (review.getId() == null) {
        productReviewService.create(rev);
    } else {
        productReviewService.update(rev);
    }
    review.setId(rev.getId());
}
Also used : ProductReview(com.salesmanager.core.model.catalog.product.review.ProductReview) ProductReview(com.salesmanager.core.model.catalog.product.review.ProductReview) PersistableProductReview(com.salesmanager.shop.model.catalog.product.PersistableProductReview) ReadableProductReview(com.salesmanager.shop.model.catalog.product.ReadableProductReview) PersistableProductReviewPopulator(com.salesmanager.shop.populator.catalog.PersistableProductReviewPopulator)

Example 2 with PersistableProductReview

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

the class ProductCommonFacadeImpl method saveOrUpdateReview.

@Override
public void saveOrUpdateReview(PersistableProductReview review, MerchantStore store, Language language) throws Exception {
    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, store, language);
    if (review.getId() == null) {
        productReviewService.create(rev);
    } else {
        productReviewService.update(rev);
    }
    review.setId(rev.getId());
}
Also used : ProductReview(com.salesmanager.core.model.catalog.product.review.ProductReview) 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) PersistableProductReviewPopulator(com.salesmanager.shop.populator.catalog.PersistableProductReviewPopulator)

Example 3 with PersistableProductReview

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

the class ProductManagementAPIIntegrationTest method createProductReview.

/**
 * Creates a ProductReview requires an existing Customer and an existing
 * Product
 *
 * @throws Exception
 */
@Ignore
@Test
public void createProductReview() throws Exception {
    final PersistableProductReview review = new PersistableProductReview();
    review.setCustomerId(1L);
    review.setProductId(1L);
    review.setLanguage("en");
    // rating is on 5
    review.setRating(2D);
    review.setDescription("Not as good as expected. From what i understood that was supposed to be premium quality but unfortunately i had to return the item after one week... Verry disapointed !");
    review.setDate("2021-06-06");
    final HttpEntity<PersistableProductReview> entity = new HttpEntity<>(review, getHeader());
    final ResponseEntity<PersistableProductReview> response = testRestTemplate.postForEntity("/api/v1/private/products/1/reviews?store=" + Constants.DEFAULT_STORE, entity, PersistableProductReview.class);
    final PersistableProductReview rev = response.getBody();
    assertThat(response.getStatusCode(), is(CREATED));
    assertNotNull(rev.getId());
}
Also used : HttpEntity(org.springframework.http.HttpEntity) PersistableProductReview(com.salesmanager.shop.model.catalog.product.PersistableProductReview) Ignore(org.junit.Ignore) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with PersistableProductReview

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

the class PersistableProductReviewPopulator method populate.

@Override
public ProductReview populate(PersistableProductReview source, ProductReview target, MerchantStore store, Language language) throws ConversionException {
    Validate.notNull(customerService, "customerService cannot be null");
    Validate.notNull(productService, "productService cannot be null");
    Validate.notNull(languageService, "languageService cannot be null");
    Validate.notNull(source.getRating(), "Rating cannot bot be null");
    try {
        if (target == null) {
            target = new ProductReview();
        }
        Customer customer = customerService.getById(source.getCustomerId());
        // check if customer belongs to store
        if (customer == null || customer.getMerchantStore().getId().intValue() != store.getId().intValue()) {
            throw new ConversionException("Invalid customer id for the given store");
        }
        if (source.getDate() == null) {
            String date = DateUtil.formatDate(new Date());
            source.setDate(date);
        }
        target.setReviewDate(DateUtil.getDate(source.getDate()));
        target.setCustomer(customer);
        target.setReviewRating(source.getRating());
        Product product = productService.getById(source.getProductId());
        // check if product belongs to store
        if (product == null || product.getMerchantStore().getId().intValue() != store.getId().intValue()) {
            throw new ConversionException("Invalid product id for the given store");
        }
        target.setProduct(product);
        Language lang = languageService.getByCode(language.getCode());
        if (lang == null) {
            throw new ConversionException("Invalid language code, use iso codes (en, fr ...)");
        }
        ProductReviewDescription description = new ProductReviewDescription();
        description.setDescription(source.getDescription());
        description.setLanguage(lang);
        description.setName("-");
        description.setProductReview(target);
        Set<ProductReviewDescription> descriptions = new HashSet<ProductReviewDescription>();
        descriptions.add(description);
        target.setDescriptions(descriptions);
        return target;
    } catch (Exception e) {
        throw new ConversionException("Cannot populate ProductReview", e);
    }
}
Also used : ConversionException(com.salesmanager.core.business.exception.ConversionException) Language(com.salesmanager.core.model.reference.language.Language) Customer(com.salesmanager.core.model.customer.Customer) Product(com.salesmanager.core.model.catalog.product.Product) ProductReviewDescription(com.salesmanager.core.model.catalog.product.review.ProductReviewDescription) ProductReview(com.salesmanager.core.model.catalog.product.review.ProductReview) PersistableProductReview(com.salesmanager.shop.model.catalog.product.PersistableProductReview) Date(java.util.Date) ConversionException(com.salesmanager.core.business.exception.ConversionException) HashSet(java.util.HashSet)

Example 5 with PersistableProductReview

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

the class ProductReviewApi method create.

@RequestMapping(value = { "/private/products/{id}/reviews", "/auth/products/{id}/reviews", "/auth/products/{id}/reviews", "/auth/products/{id}/reviews" }, method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public PersistableProductReview create(@PathVariable final Long id, @Valid @RequestBody PersistableProductReview review, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language, HttpServletRequest request, HttpServletResponse response) {
    try {
        // 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);
        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)

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