//--------------- METHODS ----------------- /// <summary> /// Creates a new <see cref="OperatingLocation"/> using the <see cref="CreateOperatingLocationServiceModel"/>. /// If such <see cref="OperatingLocation"/> already exists in the database, fetches it's (int)<c>Id</c> and returns it. /// If such <see cref="OperatingLocation"/> doesn't exist in the database, adds it and return it's (int)<c>Id</c>. /// </summary> /// <param name="model">Service model with <c>Town</c>, <c>Address</c>, <c>Description</c>, <c>ImageUrl</c> and a collection <c>Departments</c></param> /// <returns>OperatingLocation ID</returns> public async Task <int> CreateAsync(CreateOperatingLocationServiceModel model) { int operatingLocationId = this.dbContext.OperatingLocations.Where(x => x.Town == model.Town && x.Address == model.Address) .Select(x => x.Id) .FirstOrDefault(); if (operatingLocationId != 0) // If operatingLocationId is different than 0 (default int value), operatingLocation with such town and address already exists, so return it's id. { return(operatingLocationId); } OperatingLocation operatingLocation = new OperatingLocation { Town = model.Town, Address = model.Address, Description = model.Description, ImageUrl = model.ImageUrl, }; await this.dbContext.OperatingLocations.AddAsync(operatingLocation); await this.dbContext.SaveChangesAsync(); foreach (var departmentFromModel in model.Departments) { // Set Department's OperatingLocationId, because it is null by default 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; } await this.dbContext.SaveChangesAsync(); } return(operatingLocation.Id); }
public async Task <IActionResult> Create(OperatingLocationInputModel model) { this.FillOperatingLocationInputModel(); if (!this.ModelState.IsValid) { return(this.View("Create", this.operatingLocationInputModel)); } var departments = this.departmentsService.GetAllDepartmentsWithSelectedPhones(model.DepartmentIds); var operatingLocation = new CreateOperatingLocationServiceModel { Town = model.Town, Address = model.Address, Description = model.Description, ImageUrl = model.ImageUrl, Departments = departments, }; await this.operatingLocationsService.CreateAsync(operatingLocation); return(this.RedirectToAction("Create")); }