public async Task <IActionResult> Put(int id, [FromBody] CategoryViewModel categoryViewModel) { try { if (ModelState.IsValid) { var category = _mapper.Map <Category>(categoryViewModel); var validationResult = await _categoryHandler.CanUpdate(id, category); if (validationResult == null) { category.Id = id; var result = await _categoryService.Update(category); if (result.ValidationResults.Count == 0) { return(Ok("Category modified successfully!")); } ModelState.AddModelErrors(result.ValidationResults); } ModelState.AddModelError(validationResult); } } catch (Exception ex) { var exceptionMessage = await Helpers.GetErrors(ex); ModelState.AddModelError(new ValidationResult(exceptionMessage)); } return(BadRequest(ModelState.GetErrors())); }
public async Task <IActionResult> Post([FromBody] CategoryViewModel categoryViewModel) { try { if (ModelState.IsValid) { var category = _mapper.Map <Category>(categoryViewModel); var validationResult = await _categoryHandler.CanAdd(category); if (validationResult == null) { await _categoryService.Create(category); return(Ok("Category created successfully")); } ModelState.AddModelError(validationResult); } } catch (Exception ex) { var exceptionMessage = await Helpers.GetErrors(ex); ModelState.AddModelError(new ValidationResult(exceptionMessage)); } return(BadRequest(ModelState.GetErrors())); }
public async Task <IActionResult> Post([FromBody] ProductViewModel productViewModel) { try { if (ModelState.IsValid) { var product = _mapper.Map <Product>(productViewModel); var validationResult = await _productHandler.CanAdd(product); if (validationResult == null) { await _productService.Create(product); return(Ok("Product created successfully")); } ModelState.AddModelError(validationResult); } } catch (Exception ex) { var exceptionMessage = await Helpers.GetErrors(ex); ModelState.AddModelError(new ValidationResult(exceptionMessage)); } return(BadRequest(ModelState.GetErrors())); }
public async Task <IActionResult> Put(int id, [FromBody] ProductViewModel productViewModel) { try { if (ModelState.IsValid) { var product = _mapper.Map <Product>(productViewModel); var validationResult = await _productHandler.CanUpdate(id, product); if (validationResult == null) { product.Id = id; var result = await _productService.Update(product); if (result.ValidationResults.Count == 0) { return(Ok("Product modified successfully!")); } ModelState.AddModelErrors(result.ValidationResults); } ModelState.AddModelError(validationResult); } } catch (Exception ex) { var exceptionMessage = await Helpers.GetErrors(ex); ModelState.AddModelError(new ValidationResult(exceptionMessage)); } return(BadRequest(ModelState.GetErrors())); }
public async Task <IActionResult> GetAllCategory([FromQuery] CategorySearchViewModel searchViewModel) { try { var result = await _categoryService.FindCategories(searchViewModel); return(Ok(result)); } catch (Exception ex) { var exceptionMessage = await Helpers.GetErrors(ex); ModelState.AddModelError(new ValidationResult(exceptionMessage)); } return(BadRequest(ModelState.GetErrors())); }
public async Task <IActionResult> Get(int id) { try { var result = await _categoryService.FindById(id); if (result != null) { return(Ok(result)); } ModelState.AddModelError(new ValidationResult("Category not found.")); } catch (Exception ex) { var exceptionMessage = await Helpers.GetErrors(ex); ModelState.AddModelError(new ValidationResult(exceptionMessage)); } return(BadRequest(ModelState.GetErrors())); }
public async Task <IActionResult> Delete(int id) { try { var validationResult = await _categoryHandler.CanDelete(id); if (validationResult == null) { await _categoryService.DeleteById(id); return(Ok("Category deleted successfully!")); } ModelState.AddModelError(validationResult); } catch (Exception ex) { var exceptionMessage = await Helpers.GetErrors(ex); ModelState.AddModelError(new ValidationResult(exceptionMessage)); } return(BadRequest(ModelState.GetErrors())); }