public DbResponse InsertUpdate(Common.Common.Product model) { var response = new DbResponse(); if (string.IsNullOrEmpty(model.PId.ToString()))// insert { try { bool productCodeExists = Context.Product.Any(x => x.ProductCode == model.ProductCode); if (productCodeExists) { response.SetError(1, "Error! Product Code Already Exists!"); return(response); } model.CreatedOn = DateTime.Now; model.IsActive = true; Context.Product.Add(model); Context.SaveChanges(); response.SetError(0, "Data Inserted Succesfully!"); } catch (Exception ex) { response.SetError(1, "Error! while Inserting Data!"); } } else //update { try { bool productCodeExists = Context.Product.Any(x => x.ProductCode == model.ProductCode && x.PId != model.PId); if (productCodeExists) { response.SetError(1, "Error! Product Code Already Exists!"); return(response); } var data = Context.Product.Where(t => t.IsActive).FirstOrDefault(t => t.PId.Equals(model.PId)); if (data == null) { response.SetError(1, "Error! while Updating Data!"); } else { data.ProductCode = model.ProductCode; data.ProductName = model.ProductName; data.Manufacturer = model.Manufacturer; data.Quantity = model.Quantity; data.Rate = model.Rate; data.LastModifiedOn = DateTime.Now; data.IsActive = true; Context.Product.Update(data); Context.SaveChanges(); response.SetError(0, "Data Updated Succesfully!"); } } catch (Exception ex) { response.SetError(1, "Error! while Updating Data!"); } } return(response); }
public DbResponse InsertUpdate(Common.Common.Product model) { return(repository.InsertUpdate(model)); }