//--------------- METHODS ----------------- /// <summary> /// Creates a new <see cref="VehicleType"/> using the <see cref="CreateVehicleTypeServiceModel"/>. /// If such <see cref="VehicleType"/> already exists in the database, fetches it's (int)<c>Id</c> and returns it. /// If such <see cref="VehicleType"/> 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>, <c>VehicleCategory</c> and <c>Description</c></param> /// <returns>VehicleType ID</returns> public async Task <int> CreateAsync(CreateVehicleTypeServiceModel model) { VehicleCategory vehicleCategory = (VehicleCategory)model.VehicleCategoryId; int vehicleTypeId = this.dbContext.VehicleTypes.Where(x => x.Name == model.Name && x.VehicleCategory == vehicleCategory) .Select(x => x.Id) .FirstOrDefault(); if (vehicleTypeId != 0) // If vehicleTypeId is different than 0 (int default value), vehicleType with such name already exists, so return it's id. { return(vehicleTypeId); } VehicleType vehicleType = new VehicleType { Name = model.Name, VehicleCategory = vehicleCategory, Description = model.Description, }; await this.dbContext.VehicleTypes.AddAsync(vehicleType); await this.dbContext.SaveChangesAsync(); return(vehicleType.Id); }
public async Task <IActionResult> CreateVehicleType(VehicleTypeInputModel model) { this.FillServiceUnifiedModel(); if (!this.ModelState.IsValid) { return(this.View("Create", this.unifiedModel)); } var vehicleType = new CreateVehicleTypeServiceModel { Name = model.VehicleTypeName, VehicleCategoryId = model.VehicleCategoryId, Description = model.VehicleTypeDescription, }; await this.vehicleTypesService.CreateAsync(vehicleType); return(this.RedirectToAction("Create")); }