public async Task <IActionResult> CreateServiceType(ServiceTypeInputModel model) { this.FillServiceUnifiedModel(); if (!this.ModelState.IsValid) { return(this.View("Create", this.unifiedModel)); } var serviceType = new CreateServiceTypeServiceModel { Name = model.ServiceTypeName, Description = model.ServiceTypeDescription, IsInDevelopment = model.IsShownInMainMenu, }; await this.serviceTypesService.CreateAsync(serviceType); return(this.RedirectToAction("Create", this.unifiedModel)); }
//--------------- METHODS ----------------- /// <summary> /// Creates a new <see cref="ServiceType"/> using the <see cref="CreateServiceTypeServiceModel"/>. /// If such <see cref="ServiceType"/> already exists in the database, fetches it's (int)<c>Id</c> and returns it. /// If such <see cref="ServiceType"/> doesn't exist in the database, adds it and return it's (int)<c>Id</c>. /// </summary> /// <param name="model">Service model with <c>Name</c> and <c>Description</c></param> /// <returns>ServiceType ID</returns> public async Task <int> CreateAsync(CreateServiceTypeServiceModel model) { int serviceTypeId = this.dbContext.ServiceTypes.Where(d => d.Name == model.Name) .Select(x => x.Id) .FirstOrDefault(); if (serviceTypeId != 0) // If serviceTypeId is different than 0 (int default value), serviceType with such name already exists, so return it's id. { return(serviceTypeId); } ServiceType serviceType = new ServiceType { Name = model.Name, Description = model.Description, IsShownInMainMenu = model.IsInDevelopment, }; await this.dbContext.ServiceTypes.AddAsync(serviceType); await this.dbContext.SaveChangesAsync(); return(serviceType.Id); }