public async Task <IActionResult> Create(ServiceInputModel model) { this.FillServiceUnifiedModel(); if (!this.ModelState.IsValid) { return(this.View("Create", this.unifiedModel)); } var operatingLocations = new List <OperatingLocationsDropdownServiceModel>(); if (model.OperatingLocationIds != null) { foreach (int operatingLocationId in model.OperatingLocationIds) { operatingLocations.Add(new OperatingLocationsDropdownServiceModel { Id = operatingLocationId, }); } } var documents = new List <DocumentsDropdownServiceModel>(); if (model.DocumentIds != null) { foreach (int documentId in model.DocumentIds) { documents.Add(new DocumentsDropdownServiceModel { Id = documentId, }); } } var service = new CreateServiceServiceModel { ServiceTypeId = model.ServiceTypeId, VehicleTypeId = model.VehicleTypeId, ServiceName = model.ServiceName, ServiceDescription = model.ServiceDescription, IsShownInSubMenu = model.IsShownInSubMenu, Price = model.Price, OperatingLocations = operatingLocations, Documents = documents, }; await this.servicesService.CreateAsync(service); return(this.RedirectToAction("Create")); }
//--------------- METHODS ----------------- /// <summary> /// Creates a new <see cref="Service"/> using the <see cref="CreateServiceServiceModel"/>. /// If such <see cref="Service"/> already exists in the database, fetches it's (int)<c>Id</c> and returns it. /// If such <see cref="Service"/> doesn't exist in the database, adds it and return it's (int)<c>Id</c>. /// </summary> /// <param name="model">Service model with <c>ServiceTypeId</c>, <c>Name</c>, <c>Description</c> and <c>IsShownInMenu</c></param> /// <returns>Service ID</returns> public async Task <int> CreateAsync(CreateServiceServiceModel model) { int serviceId = this.dbContext.Services.Where(x => x.Name == model.ServiceName && x.VehicleTypeId == model.VehicleTypeId) .Select(x => x.Id) .FirstOrDefault(); if (serviceId != 0) // If serviceId is different than 0 (default int value), service with such name and vehicleType already exists, so return it's id. { return(serviceId); } Service service = new Service { ServiceTypeId = model.ServiceTypeId, VehicleTypeId = model.VehicleTypeId, Name = model.ServiceName, Description = model.ServiceDescription, IsShownInSubMenu = model.IsShownInSubMenu, Price = model.Price, }; foreach (var operatingLocationDropdown in model.OperatingLocations) { OperatingLocation operatingLocation = this.dbContext.OperatingLocations.FirstOrDefault(x => x.Id == operatingLocationDropdown.Id); await this.dbContext.ServiceOperatingLocations.AddAsync(new ServiceOperatingLocation { Service = service, OperatingLocation = operatingLocation, }); } foreach (var documentDropdown in model.Documents) { Document document = this.dbContext.Documents.FirstOrDefault(x => x.Id == documentDropdown.Id); await this.dbContext.ServiceDocuments.AddAsync(new ServiceDocument { Service = service, Document = document, }); } await this.dbContext.Services.AddAsync(service); await this.dbContext.SaveChangesAsync(); return(service.Id); }