public async Task<IHttpActionResult> GetProduct(int id) { Product product = await _unitOfWork.ProductRepository.GetByIdAsync(id); if (product == null) { return NotFound(); } ProductDTO productDTO = new ProductDTO() { CategoryId = product.CategoryId, Description = product.Description, Id = product.Id, ImageUrl = product.ImageUrl, Name = product.Name }; return Ok(productDTO); }
public async Task<IHttpActionResult> PutProduct(ProductDTO productDTO) { SaveImage(productDTO); if (!ModelState.IsValid) { return BadRequest(ModelState); } Product product = new Product() { CategoryId = productDTO.CategoryId, Description = productDTO.Description, Id = productDTO.Id, ImageUrl = productDTO.ImageUrl, Name = productDTO.Name }; _unitOfWork.ProductRepository.Update(product); try { await _unitOfWork.CommitAsync(); } catch (DbBusinessException ex) { ModelState.AddModelError("customMessage", ex.Message); return BadRequest(ModelState); } catch (Exception) { ModelState.AddModelError("customMessage", ErrorMessages.GenericErrorMessage); return BadRequest(ModelState); } return Ok(product); }
private void SaveImage(ProductDTO productDTO) { try { if (!string.IsNullOrEmpty(productDTO.ImageData)) { string imgAsBase64 = productDTO.ImageData; //removing the not needed start information: Regex rgx = new Regex("data:image/.*;base64,"); imgAsBase64 = rgx.Replace(imgAsBase64, string.Empty); productDTO.ImageUrl = ImageHelper.SaveBase64Image( imgAsBase64, productDTO.ImageExtension); } } catch (Exception) { ModelState.AddModelError("customMessage", ErrorMessages.GenericErrorMessage); } }