Search in sources :

Example 1 with PersistableManufacturer

use of com.salesmanager.shop.model.catalog.manufacturer.PersistableManufacturer in project shopizer by shopizer-ecommerce.

the class ManufacturerFacadeImpl method saveOrUpdateManufacturer.

@Override
public void saveOrUpdateManufacturer(PersistableManufacturer manufacturer, MerchantStore store, Language language) throws Exception {
    PersistableManufacturerPopulator populator = new PersistableManufacturerPopulator();
    populator.setLanguageService(languageService);
    Manufacturer manuf = new Manufacturer();
    if (manufacturer.getId() != null && manufacturer.getId().longValue() > 0) {
        manuf = manufacturerService.getById(manufacturer.getId());
        if (manuf == null) {
            throw new ResourceNotFoundException("Manufacturer with id [" + manufacturer.getId() + "] not found");
        }
        if (manuf.getMerchantStore().getId().intValue() != store.getId().intValue()) {
            throw new ResourceNotFoundException("Manufacturer with id [" + manufacturer.getId() + "] not found for store [" + store.getId() + "]");
        }
    }
    populator.populate(manufacturer, manuf, store, language);
    manufacturerService.saveOrUpdate(manuf);
    manufacturer.setId(manuf.getId());
}
Also used : PersistableManufacturerPopulator(com.salesmanager.shop.populator.manufacturer.PersistableManufacturerPopulator) Manufacturer(com.salesmanager.core.model.catalog.product.manufacturer.Manufacturer) PersistableManufacturer(com.salesmanager.shop.model.catalog.manufacturer.PersistableManufacturer) ReadableManufacturer(com.salesmanager.shop.model.catalog.manufacturer.ReadableManufacturer) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException)

Example 2 with PersistableManufacturer

use of com.salesmanager.shop.model.catalog.manufacturer.PersistableManufacturer in project shopizer by shopizer-ecommerce.

the class CategoryManagementAPIIntegrationTest method manufacturerForItemsInCategory.

@Test
public void manufacturerForItemsInCategory() throws Exception {
    ObjectWriter writer = new ObjectMapper().writer().withDefaultPrettyPrinter();
    // create first manufacturer
    PersistableManufacturer m1 = super.manufacturer("BRAND1");
    String json = writer.writeValueAsString(m1);
    HttpEntity<String> entity = new HttpEntity<>(json, getHeader());
    @SuppressWarnings("rawtypes") ResponseEntity response = testRestTemplate.postForEntity("/api/v1/private/manufacturer", entity, PersistableManufacturer.class);
    assertThat(response.getStatusCode(), is(CREATED));
    // create second manufacturer
    PersistableManufacturer m2 = super.manufacturer("BRAND2");
    json = writer.writeValueAsString(m2);
    entity = new HttpEntity<>(json, getHeader());
    response = testRestTemplate.postForEntity("/api/v1/private/manufacturer", entity, PersistableManufacturer.class);
    assertThat(response.getStatusCode(), is(CREATED));
    // create category
    PersistableCategory category = super.category("TEST");
    // to be used in product
    Category cat = new Category();
    cat.setCode("TEST");
    json = writer.writeValueAsString(category);
    entity = new HttpEntity<>(json, getHeader());
    @SuppressWarnings("rawtypes") ResponseEntity categoryResponse = testRestTemplate.postForEntity("/api/v1/private/category", entity, PersistableCategory.class);
    assertThat(categoryResponse.getStatusCode(), is(CREATED));
    final PersistableCategory persistable = (PersistableCategory) categoryResponse.getBody();
    Long id = persistable.getId();
    // create first item
    PersistableProduct product1 = super.product("PRODUCT1");
    product1.getCategories().add(cat);
    ProductSpecification specifications = new ProductSpecification();
    specifications.setManufacturer("BRAND1");
    product1.setProductSpecifications(specifications);
    json = writer.writeValueAsString(product1);
    entity = new HttpEntity<>(json, getHeader());
    response = testRestTemplate.postForEntity("/api/v1/private/product?store=" + Constants.DEFAULT_STORE, entity, PersistableProduct.class);
    assertThat(response.getStatusCode(), is(CREATED));
    // create second item
    PersistableProduct product2 = super.product("PRODUCT2");
    product2.getCategories().add(cat);
    specifications = new ProductSpecification();
    specifications.setManufacturer("BRAND2");
    product2.setProductSpecifications(specifications);
    json = writer.writeValueAsString(product2);
    entity = new HttpEntity<>(json, getHeader());
    response = testRestTemplate.postForEntity("/api/v1/private/product?store=" + Constants.DEFAULT_STORE, entity, PersistableProduct.class);
    assertThat(response.getStatusCode(), is(CREATED));
    entity = new HttpEntity<>(getHeader());
    // get manufacturers in category
    @SuppressWarnings("rawtypes") ResponseEntity<List> manufacturers = testRestTemplate.exchange(String.format("/api/v1/category/" + id + "/manufacturers"), HttpMethod.GET, entity, List.class);
    assertThat(manufacturers.getStatusCode(), is(OK));
    @SuppressWarnings("unchecked") List<ReadableManufacturer> manufacturerList = manufacturers.getBody();
// assertFalse(manufacturerList.isEmpty());
}
Also used : PersistableCategory(com.salesmanager.shop.model.catalog.category.PersistableCategory) ReadableManufacturer(com.salesmanager.shop.model.catalog.manufacturer.ReadableManufacturer) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) PersistableCategory(com.salesmanager.shop.model.catalog.category.PersistableCategory) Category(com.salesmanager.shop.model.catalog.category.Category) HttpEntity(org.springframework.http.HttpEntity) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ProductSpecification(com.salesmanager.shop.model.catalog.product.ProductSpecification) ResponseEntity(org.springframework.http.ResponseEntity) PersistableProduct(com.salesmanager.shop.model.catalog.product.PersistableProduct) PersistableManufacturer(com.salesmanager.shop.model.catalog.manufacturer.PersistableManufacturer) ArrayList(java.util.ArrayList) ReadableCategoryList(com.salesmanager.shop.model.catalog.category.ReadableCategoryList) List(java.util.List) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with PersistableManufacturer

use of com.salesmanager.shop.model.catalog.manufacturer.PersistableManufacturer in project shopizer by shopizer-ecommerce.

the class ServicesTestSupport method manufacturer.

protected PersistableManufacturer manufacturer(String code) {
    PersistableManufacturer m = new PersistableManufacturer();
    m.setCode(code);
    m.setOrder(0);
    ManufacturerDescription desc = new ManufacturerDescription();
    desc.setLanguage("en");
    desc.setName(code);
    m.getDescriptions().add(desc);
    return m;
}
Also used : PersistableManufacturer(com.salesmanager.shop.model.catalog.manufacturer.PersistableManufacturer) ManufacturerDescription(com.salesmanager.shop.model.catalog.manufacturer.ManufacturerDescription)

Example 4 with PersistableManufacturer

use of com.salesmanager.shop.model.catalog.manufacturer.PersistableManufacturer in project shopizer by shopizer-ecommerce.

the class ShopProductRESTController method createManufacturer.

/**
 * Method for creating a manufacturer
 * @param store
 * @param manufacturer
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
@RequestMapping(value = "/private/{store}/manufacturer", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
public PersistableManufacturer createManufacturer(@PathVariable final String store, @Valid @RequestBody PersistableManufacturer manufacturer, 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(503, "Merchant store is null for code " + store);
            return null;
        }
        PersistableManufacturerPopulator populator = new PersistableManufacturerPopulator();
        populator.setLanguageService(languageService);
        com.salesmanager.core.model.catalog.product.manufacturer.Manufacturer manuf = new com.salesmanager.core.model.catalog.product.manufacturer.Manufacturer();
        populator.populate(manufacturer, manuf, merchantStore, merchantStore.getDefaultLanguage());
        manufacturerService.save(manuf);
        manufacturer.setId(manuf.getId());
        return manufacturer;
    } catch (Exception e) {
        LOGGER.error("Error while saving product", e);
        try {
            response.sendError(503, "Error while saving product " + e.getMessage());
        } catch (Exception ignore) {
        }
        return null;
    }
}
Also used : PersistableManufacturerPopulator(com.salesmanager.shop.populator.manufacturer.PersistableManufacturerPopulator) PersistableManufacturer(com.salesmanager.shop.model.catalog.manufacturer.PersistableManufacturer) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

PersistableManufacturer (com.salesmanager.shop.model.catalog.manufacturer.PersistableManufacturer)4 ReadableManufacturer (com.salesmanager.shop.model.catalog.manufacturer.ReadableManufacturer)2 PersistableManufacturerPopulator (com.salesmanager.shop.populator.manufacturer.PersistableManufacturerPopulator)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 Manufacturer (com.salesmanager.core.model.catalog.product.manufacturer.Manufacturer)1 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)1 Category (com.salesmanager.shop.model.catalog.category.Category)1 PersistableCategory (com.salesmanager.shop.model.catalog.category.PersistableCategory)1 ReadableCategory (com.salesmanager.shop.model.catalog.category.ReadableCategory)1 ReadableCategoryList (com.salesmanager.shop.model.catalog.category.ReadableCategoryList)1 ManufacturerDescription (com.salesmanager.shop.model.catalog.manufacturer.ManufacturerDescription)1 PersistableProduct (com.salesmanager.shop.model.catalog.product.PersistableProduct)1 ProductSpecification (com.salesmanager.shop.model.catalog.product.ProductSpecification)1 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 HttpEntity (org.springframework.http.HttpEntity)1