public async Task <IActionResult> Edit(OperatingLocationInputModel model) { if (!this.operatingLocationsService.Exists(model.Id)) { return(this.BadRequest()); } if (!ModelState.IsValid) { return(this.RedirectToAction("Error", "Home")); } EditOperatingLocationServiceModel serviceModel = new EditOperatingLocationServiceModel { Id = model.Id, Town = model.Town, Address = model.Address, Description = model.Description, ImageUrl = model.ImageUrl, DepartmentIds = model.DepartmentIds, }; await this.operatingLocationsService.EditAsync(serviceModel); return(this.RedirectToAction("Details", "OperatingLocations", new { id = serviceModel.Id })); }
/// <summary> /// Edits the <see cref="OperatingLocation"/> using <see cref="EditOperatingLocationServiceModel"/>. /// </summary> /// <param name="model">Number of modified entities.</param> /// <returns>Service model with <c>Id</c>, <c>Town</c>, <c>Address</c>, <c>Description</c>, <c>ImageUrl</c> and <c>DepartmentsIds</c> with associated <c>PhoneIds</c>.</returns> public async Task <int> EditAsync(EditOperatingLocationServiceModel model) { int modifiedEntities = 0; OperatingLocation operatingLocation = this.dbContext.OperatingLocations.Find(model.Id); operatingLocation.Town = model.Town; operatingLocation.Address = model.Address; operatingLocation.Description = model.Description; operatingLocation.ImageUrl = model.ImageUrl; // Clear the operatingLocation departments operatingLocation.Departments.Clear(); // First set each Department's OperatingLocationId with this OperatingLocation to null and make corresponding phones internal var deps = this.dbContext.Departments.Where(x => x.OperatingLocationId == operatingLocation.Id).ToList(); for (int i = 0; i < deps.Count; i++) { deps[i].OperatingLocationId = null; foreach (var phone in deps[i].Phones) { bool result = this.phonesService.IsPhoneContainedInOtherDepartments(phone.Phone.PhoneNumber); // Only if the Phone is not contained within other Departments and is Public -> Set Phone to Internal // Just in One Department and is Public -> set Phone to Internal if (result == false && phone.Phone.IsInternal == false) { this.dbContext.Phones.FirstOrDefault(x => x.Id == phone.PhoneId).IsInternal = true; } // In Many Departments and is Internal -> set Phone to Public else if (result == true && phone.Phone.IsInternal == false) { this.dbContext.Phones.FirstOrDefault(x => x.Id == phone.PhoneId).IsInternal = true; } } } modifiedEntities += await this.dbContext.SaveChangesAsync(); var departments = this.departmentsService.GetAllDepartmentsWithSelectedPhones(model.DepartmentIds); foreach (var departmentFromModel in departments) { // Set Department's OperatingLocationId, because it is null Department department = this.dbContext.Departments.FirstOrDefault(x => x.Id == departmentFromModel.Id); department.OperatingLocation = operatingLocation; // Set Department's selected phones from the user to public phones (IsInternal = false) foreach (var phoneFromModel in departmentFromModel.Phones) { department.Phones.Where(p => p.PhoneId == phoneFromModel.Id).FirstOrDefault().Phone.IsInternal = false; } } modifiedEntities += await this.dbContext.SaveChangesAsync(); return(modifiedEntities); }