Skip to content

Commit

Permalink
Category by friendly url decomplexified
Browse files Browse the repository at this point in the history
  • Loading branch information
shopizer-ecommerce committed Feb 4, 2023
1 parent dd7ea9e commit a2316b7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

Expand All @@ -26,14 +29,17 @@
import com.salesmanager.core.model.catalog.product.ProductCriteria;
import com.salesmanager.core.model.merchant.MerchantStore;
import com.salesmanager.core.model.reference.language.Language;
import com.salesmanager.shop.model.catalog.category.ReadableCategory;
import com.salesmanager.shop.model.catalog.product.LightPersistableProduct;
import com.salesmanager.shop.model.catalog.product.ReadableProduct;
import com.salesmanager.shop.model.catalog.product.ReadableProductList;
import com.salesmanager.shop.model.catalog.product.product.PersistableProduct;
import com.salesmanager.shop.model.catalog.product.product.definition.PersistableProductDefinition;
import com.salesmanager.shop.model.catalog.product.product.definition.ReadableProductDefinition;
import com.salesmanager.shop.model.entity.Entity;
import com.salesmanager.shop.store.api.exception.ResourceNotFoundException;
import com.salesmanager.shop.store.api.exception.ServiceRuntimeException;
import com.salesmanager.shop.store.controller.category.facade.CategoryFacade;
import com.salesmanager.shop.store.controller.product.facade.ProductCommonFacade;
import com.salesmanager.shop.store.controller.product.facade.ProductDefinitionFacade;
import com.salesmanager.shop.store.controller.product.facade.ProductFacade;
Expand Down Expand Up @@ -70,6 +76,9 @@ public class ProductApiV2 {

@Autowired
private ProductCommonFacade productCommonFacade;

@Autowired
private CategoryFacade categoryFacade;

private static final Logger LOGGER = LoggerFactory.getLogger(ProductApiV2.class);

Expand Down Expand Up @@ -193,6 +202,57 @@ public ReadableProduct getByfriendlyUrl(
}


/**
* List products by category
* count and page are supported. Default values are set when not specified
*
* @param request
* @param response
* @return
* @throws Exception
*/
@RequestMapping(value = "/products/category/slug/{friendlyUrl}", method = RequestMethod.GET)
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"),
@ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public ReadableProductList list(
@RequestParam(value = "lang", required = false) String lang,
@PathVariable String friendlyUrl,
@RequestParam(value = "page", required = false, defaultValue = "0") Integer page, // count
@RequestParam(value = "count", required = false, defaultValue = "25") Integer count, // count
@ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language) {



try {
ReadableCategory category = categoryFacade.getCategoryByFriendlyUrl(merchantStore, friendlyUrl, language);
ProductCriteria criterias = new ProductCriteria();

List<Long> listOfIds = new ArrayList<Long>();
listOfIds.add(category.getId());


criterias.setCategoryIds(listOfIds);

criterias.setMaxCount(count);
criterias.setLanguage(language.getCode());
criterias.setStartPage(page);

return productFacadeV2.getProductListsByCriterias(merchantStore, language, criterias);


} catch (ResourceNotFoundException rnf) {
throw rnf;
} catch (Exception e) {
// TODO Auto-generated catch block
LOGGER.error("Error while getting category by friendlyUrl", e);
throw new ServiceRuntimeException(e);
}

}



/**
* List products
* Filtering product lists based on product option and option value ?category=1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,12 @@ public ReadableCategory getCategoryByFriendlyUrl(MerchantStore store, String fri
ReadableCategory readableCategory = new ReadableCategory();

Category category = categoryService.getBySeUrl(store, friendlyUrl);

if(category == null) {
throw new ResourceNotFoundException("Category with friendlyUrl [" + friendlyUrl + "] was not found");
}


categoryPopulator.populate(category, readableCategory, store, language);

return readableCategory;
Expand Down

0 comments on commit a2316b7

Please sign in to comment.