Search in sources :

Example 46 with ResourceNotFoundException

use of com.salesmanager.shop.store.api.exception.ResourceNotFoundException in project shopizer by shopizer-ecommerce.

the class PaymentApi method configure.

@PostMapping(value = "/private/modules/payment")
public void configure(@RequestBody IntegrationModuleConfiguration configuration, @ApiIgnore MerchantStore merchantStore) {
    try {
        List<IntegrationModule> modules = paymentService.getPaymentMethods(merchantStore);
        Map<String, IntegrationModule> map = modules.stream().collect(Collectors.toMap(IntegrationModule::getCode, module -> module));
        IntegrationModule config = map.get(configuration.getCode());
        if (config == null) {
            throw new ResourceNotFoundException("Payment module [" + configuration.getCode() + "] not found");
        }
        Map<String, IntegrationConfiguration> configuredModules = paymentService.getPaymentModulesConfigured(merchantStore);
        IntegrationConfiguration integrationConfiguration = configuredModules.get(configuration.getCode());
        if (integrationConfiguration == null) {
            integrationConfiguration = new IntegrationConfiguration();
            integrationConfiguration.setModuleCode(configuration.getCode());
        }
        integrationConfiguration.setActive(configuration.isActive());
        integrationConfiguration.setDefaultSelected(configuration.isDefaultSelected());
        integrationConfiguration.setIntegrationKeys(configuration.getIntegrationKeys());
        integrationConfiguration.setIntegrationOptions(configuration.getIntegrationOptions());
        paymentService.savePaymentModuleConfiguration(integrationConfiguration, merchantStore);
    } catch (ServiceException e) {
        LOGGER.error("Error getting payment modules", e);
        throw new ServiceRuntimeException("Error saving payment module", e);
    }
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) IntegrationModuleConfiguration(com.salesmanager.shop.model.system.IntegrationModuleConfiguration) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ServiceException(com.salesmanager.core.business.exception.ServiceException) RequestBody(org.springframework.web.bind.annotation.RequestBody) Language(com.salesmanager.core.model.reference.language.Language) ApiOperation(io.swagger.annotations.ApiOperation) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Map(java.util.Map) GetMapping(org.springframework.web.bind.annotation.GetMapping) IntegrationModule(com.salesmanager.core.model.system.IntegrationModule) Api(io.swagger.annotations.Api) Tag(io.swagger.annotations.Tag) PostMapping(org.springframework.web.bind.annotation.PostMapping) Logger(org.slf4j.Logger) ApiImplicitParam(io.swagger.annotations.ApiImplicitParam) Collectors(java.util.stream.Collectors) RestController(org.springframework.web.bind.annotation.RestController) PaymentService(com.salesmanager.core.business.services.payments.PaymentService) IntegrationModuleSummaryEntity(com.salesmanager.shop.model.system.IntegrationModuleSummaryEntity) ApiIgnore(springfox.documentation.annotations.ApiIgnore) List(java.util.List) IntegrationConfiguration(com.salesmanager.core.model.system.IntegrationConfiguration) SwaggerDefinition(io.swagger.annotations.SwaggerDefinition) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ServiceException(com.salesmanager.core.business.exception.ServiceException) IntegrationConfiguration(com.salesmanager.core.model.system.IntegrationConfiguration) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) IntegrationModule(com.salesmanager.core.model.system.IntegrationModule) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 47 with ResourceNotFoundException

use of com.salesmanager.shop.store.api.exception.ResourceNotFoundException in project shopizer by shopizer-ecommerce.

the class ProductApi method changeProductOrder.

/**
 * Change product sort order
 *
 * @param id
 * @param position
 * @param merchantStore
 * @param language
 * @throws IOException
 */
@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = { "/private/product/{id}", "/auth/product/{id}" }, method = RequestMethod.PATCH)
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
@ApiOperation(httpMethod = "POST", value = "Patch product sort order", notes = "Change product sortOrder")
public void changeProductOrder(@PathVariable Long id, @RequestParam(value = "order", required = false, defaultValue = "0") Integer position, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language) throws IOException {
    try {
        Product p = productService.getById(id);
        if (p == null) {
            throw new ResourceNotFoundException("Product [" + id + "] not found for merchant [" + merchantStore.getCode() + "]");
        }
        if (p.getMerchantStore().getId() != merchantStore.getId()) {
            throw new ResourceNotFoundException("Product [" + id + "] not found for merchant [" + merchantStore.getCode() + "]");
        }
        /**
         * Change order
         */
        p.setSortOrder(position);
    } catch (Exception e) {
        LOGGER.error("Error while updating Product position", e);
        throw new ServiceRuntimeException("Product [" + id + "] cannot be edited");
    }
}
Also used : PersistableProduct(com.salesmanager.shop.model.catalog.product.PersistableProduct) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) Product(com.salesmanager.core.model.catalog.product.Product) LightPersistableProduct(com.salesmanager.shop.model.catalog.product.LightPersistableProduct) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) 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) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 48 with ResourceNotFoundException

use of com.salesmanager.shop.store.api.exception.ResourceNotFoundException in project shopizer by shopizer-ecommerce.

the class ProductApi method addProductToCategory.

@ResponseStatus(HttpStatus.CREATED)
@RequestMapping(value = { "/private/product/{productId}/category/{categoryId}", "/auth/product/{productId}/category/{categoryId}" }, method = RequestMethod.POST)
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
@ResponseBody
public ReadableProduct addProductToCategory(@PathVariable Long productId, @PathVariable Long categoryId, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language, HttpServletResponse response) throws Exception {
    try {
        // get the product
        Product product = productService.getById(productId);
        if (product == null) {
            throw new ResourceNotFoundException("Product id [" + productId + "] is not found");
        }
        if (product.getMerchantStore().getId().intValue() != merchantStore.getId().intValue()) {
            throw new UnauthorizedException("Product id [" + productId + "] does not belong to store [" + merchantStore.getCode() + "]");
        }
        Category category = categoryService.getById(categoryId);
        if (category == null) {
            throw new ResourceNotFoundException("Category id [" + categoryId + "] is not found");
        }
        if (category.getMerchantStore().getId().intValue() != merchantStore.getId().intValue()) {
            throw new UnauthorizedException("Category id [" + categoryId + "] does not belong to store [" + merchantStore.getCode() + "]");
        }
        return productCommonFacade.addProductToCategory(category, product, language);
    } catch (Exception e) {
        LOGGER.error("Error while adding product to category", e);
        try {
            response.sendError(503, "Error while adding product to category " + e.getMessage());
        } catch (Exception ignore) {
        }
        return null;
    }
}
Also used : Category(com.salesmanager.core.model.catalog.category.Category) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) PersistableProduct(com.salesmanager.shop.model.catalog.product.PersistableProduct) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) Product(com.salesmanager.core.model.catalog.product.Product) LightPersistableProduct(com.salesmanager.shop.model.catalog.product.LightPersistableProduct) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) IOException(java.io.IOException) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) 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 49 with ResourceNotFoundException

use of com.salesmanager.shop.store.api.exception.ResourceNotFoundException in project shopizer by shopizer-ecommerce.

the class ProductApi method removeProductFromCategory.

@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = { "/private/product/{productId}/category/{categoryId}", "/auth/product/{productId}/category/{categoryId}" }, method = RequestMethod.DELETE)
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
@ResponseBody
public ReadableProduct removeProductFromCategory(@PathVariable Long productId, @PathVariable Long categoryId, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language, HttpServletResponse response) {
    try {
        Product product = productService.getById(productId);
        if (product == null) {
            throw new ResourceNotFoundException("Product id [" + productId + "] is not found");
        }
        if (product.getMerchantStore().getId().intValue() != merchantStore.getId().intValue()) {
            throw new UnauthorizedException("Product id [" + productId + "] does not belong to store [" + merchantStore.getCode() + "]");
        }
        Category category = categoryService.getById(categoryId);
        if (category == null) {
            throw new ResourceNotFoundException("Category id [" + categoryId + "] is not found");
        }
        if (category.getMerchantStore().getId().intValue() != merchantStore.getId().intValue()) {
            throw new UnauthorizedException("Category id [" + categoryId + "] does not belong to store [" + merchantStore.getCode() + "]");
        }
        return productCommonFacade.removeProductFromCategory(category, product, language);
    } 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 : Category(com.salesmanager.core.model.catalog.category.Category) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) PersistableProduct(com.salesmanager.shop.model.catalog.product.PersistableProduct) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) Product(com.salesmanager.core.model.catalog.product.Product) LightPersistableProduct(com.salesmanager.shop.model.catalog.product.LightPersistableProduct) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) IOException(java.io.IOException) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) 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 50 with ResourceNotFoundException

use of com.salesmanager.shop.store.api.exception.ResourceNotFoundException in project shopizer by shopizer-ecommerce.

the class ProductImageApi method imageDetails.

/**
 * Patch image (change position)
 *
 * @param id
 * @param files
 * @param position
 * @param merchantStore
 * @param language
 * @throws IOException
 */
@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = { "/private/products/{id}/image/{imageId}", "/auth/products/{id}/image/{id}" }, method = RequestMethod.PATCH)
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public void imageDetails(@PathVariable Long id, @PathVariable Long imageId, @RequestParam(value = "order", required = false, defaultValue = "0") Integer position, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language) throws IOException {
    try {
        Product p = productService.getById(id);
        if (p == null) {
            throw new ResourceNotFoundException("Product image [" + imageId + "] not found for product id [" + id + "] and merchant [" + merchantStore.getCode() + "]");
        }
        if (p.getMerchantStore().getId() != merchantStore.getId()) {
            throw new ResourceNotFoundException("Product image [" + imageId + "] not found for product id [" + id + "] and merchant [" + merchantStore.getCode() + "]");
        }
        Optional<ProductImage> productImage = productImageService.getProductImage(imageId, id, merchantStore);
        if (productImage.isPresent()) {
            productImage.get().setSortOrder(position);
            productImageService.updateProductImage(p, productImage.get());
        } else {
            throw new ResourceNotFoundException("Product image [" + imageId + "] not found for product id [" + id + "] and merchant [" + merchantStore.getCode() + "]");
        }
    } catch (Exception e) {
        LOGGER.error("Error while deleting ProductImage", e);
        throw new ServiceRuntimeException("ProductImage [" + imageId + "] cannot be edited");
    }
}
Also used : ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) 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)

Aggregations

ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)108 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)77 ServiceException (com.salesmanager.core.business.exception.ServiceException)62 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)22 UnauthorizedException (com.salesmanager.shop.store.api.exception.UnauthorizedException)22 Product (com.salesmanager.core.model.catalog.product.Product)21 Language (com.salesmanager.core.model.reference.language.Language)19 List (java.util.List)19 Collectors (java.util.stream.Collectors)19 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)17 ArrayList (java.util.ArrayList)17 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)16 OperationNotAllowedException (com.salesmanager.shop.store.api.exception.OperationNotAllowedException)15 Autowired (org.springframework.beans.factory.annotation.Autowired)15 ConversionException (com.salesmanager.core.business.exception.ConversionException)13 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)13 Inject (javax.inject.Inject)12 Optional (java.util.Optional)11 Service (org.springframework.stereotype.Service)11 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)11