public async Task <ActionResult <ProductCategoryResult> > PostProductCategory(ProductCategory category) { ProductCategoryResult result = new ProductCategoryResult(); try { var createdCategory = await _categories.CreateAsync(category); result.IsSuccessful = true; result.ResultData.Add(createdCategory); } catch (ItemAlreadyExistsException e) { _logger?.LogError(e.Message); result.IsSuccessful = false; result.ErrorMessages.Add(e.Message); return(Conflict(result)); } catch (Exception e) { _logger.LogDebug($"Unknown error", e); throw e; } return(Created("", result)); }
public async Task <ActionResult <ProductCategoryResult> > GetProductCategory(string id) { ProductCategoryResult result = new ProductCategoryResult(); try { var category = await _categories.GetAsync(id); result.IsSuccessful = true; result.ResultData = new List <ProductCategory>() { category }; } catch (ItemNotFoundException e) { result.IsSuccessful = false; result.ErrorMessages.Add(e.Message); _logger?.LogError(e.Message); return(NotFound(result)); } catch (Exception e) { _logger.LogDebug($"Unknown error", e); throw e; } return(Ok(result)); }
public async Task <ActionResult <ProductCategoryResult> > GetProductCategories() { var categories = await _categories.GetAllAsync(); var result = new ProductCategoryResult() { IsSuccessful = true, ResultData = categories }; return(Ok(result)); }
public async Task <ActionResult <ProductCategoryResult> > UpdateProductCategory(string id, [FromBody] ProductCategory category) { ProductCategoryResult result = new ProductCategoryResult(); if (id != category.Id) { result.IsSuccessful = false; result.ErrorMessages.Add("Id does not match with object id"); return(BadRequest(result)); } try { var updated = await _categories.UpdateAsync(id, category); result.IsSuccessful = true; result.ResultData.Add(updated); } catch (ItemNotFoundException e) { result.IsSuccessful = false; result.ErrorMessages.Add(e.Message); return(NotFound(result)); } catch (ItemAlreadyExistsException e) { result.IsSuccessful = false; result.ErrorMessages.Add(e.Message); return(Conflict(result)); } catch (Exception e) { _logger.LogDebug($"Unknown error", e); throw e; } return(Ok(result)); }
public async Task <ActionResult <ProductCategoryResult> > DeleteProductCategory(string id) { ProductCategoryResult result = new ProductCategoryResult(); try { await _categories.DeleteByIdAsync(id); result.IsSuccessful = true; } catch (ItemNotFoundException e) { result.IsSuccessful = false; result.ErrorMessages.Add(e.Message); return(NotFound(result)); } catch (Exception e) { _logger.LogDebug($"Unknown error", e); throw e; } return(Ok(result)); }