public async Task <ProductDTO> SaveProduct(ProductDTO product) { ProductValidation validator = new ProductValidation(); ValidationResult results = validator.Validate(product); if (!results.IsValid) { throw new System.ComponentModel.DataAnnotations.ValidationException("Validation failed"); } var p = await _context.Products.FindAsync(product.ID); if (p == null) { p = new Product(); _context.Products.Add(p); } p.Name = product.Name; p.Category = product.Category; p.Active = product.Active; p.Price = product.Price; await _context.SaveChangesAsync(); return(_mapper.Map <ProductDTO>(p)); }
private void btnInserAndUpdate_Click(object sender, EventArgs e) { product.name = tbxName.Text; product.descripition = tbxDescripition.Text; product.quantity = tbxQuantity.Text.Trim() == "" ? 0 : Convert.ToInt32(tbxQuantity.Text); product.price = tbxPrice.Text.Trim() == "" ? 0 : Convert.ToDecimal(tbxPrice.Text); ValidationResult result = productValidation.Validate(product); if (result.IsValid) { if (product.productId == 0) { productRepository.Insert(product); } else { productRepository.Update(product); } Clear(); PopulateGridView(); } else { foreach (ValidationFailure failure in result.Errors) { MessageBox.Show(failure.ErrorMessage, "Advise", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } }
public string Add(Product entity) { result = validator.Validate(entity); if (result.IsValid) { productDal.Add(entity); JObject jsonObject = new JObject(); jsonObject.Add("Status", "success"); jsonObject.Add("ErrorMessage", "Kayıt başarıyla oluşturuldu"); JArray array = new JArray(); array.Add(jsonObject); return(JsonConvert.SerializeObject(array)); } else { return(JsonConvert.SerializeObject(result.Errors)); } }