public IActionResult Edit(DetailsClientViewModel model) { if (!ModelState.IsValid) { return(BadRequest()); } try { var updateDetails = new EditClientServiceModel() { Id = model.Id, FirstName = model.FirstName, LastName = model.LastName, EGN = model.EGN, PhoneNumber = model.PhoneNumber, Email = model.Email }; this.client.Edit(updateDetails); return(RedirectToAction("Details", new { egn = updateDetails.EGN })); } catch (Exception ex) { LogExceptionWithMessage(ex); ViewBag.Section = ex.Message; return(View()); } // EGN VALIDATION IS INSIDE SERVICE }
public void Edit(EditClientServiceModel model) { var client = data.Clients.Find(model.Id); if (string.IsNullOrEmpty(model.FirstName) || string.IsNullOrEmpty(model.Email) || string.IsNullOrEmpty(model.LastName) || string.IsNullOrEmpty(model.PhoneNumber) || string.IsNullOrEmpty(model.EGN)) { throw new Exception("Must add a value"); } var allClients = data.Clients.AsNoTracking().ToList(); // check if there is client with same EGN, Email or Phone Number for (int i = 0; i < data.Clients.Count(); i++) { if (allClients[i].EGN == model.EGN && model.Id != allClients[i].Id) { throw new ArgumentException("This EGN is already used by another client"); } else if (allClients[i].PhoneNumber == model.PhoneNumber && model.Id != allClients[i].Id) { throw new ArgumentException("This Phone number is already used by another client"); } else if (allClients[i].Email == model.Email && model.Id != allClients[i].Id) { throw new ArgumentException("This Email is already used by another client"); } } client.FirstName = model.FirstName; client.LastName = model.LastName; client.EGN = model.EGN; client.Email = model.Email; client.PhoneNumber = model.PhoneNumber; this.data.SaveChanges(); }