public async Task <IActionResult> AddBike(AddMotorBikeInputModel input) { if (!this.ModelState.IsValid) { return(this.View()); } if (input.Image != null) { using (FileStream fs = new FileStream(this.webHostEnvironment.WebRootPath + ("/images/bikes/" + input.Model + input.EngineCapacity + "-1" + ".jpg"), FileMode.Create)) { await input.Image.CopyToAsync(fs); } } await this.motorBikesService.AddBikeAsync(input); return(this.RedirectToAction(nameof(this.Success))); }
public async Task AddBikeAsync(AddMotorBikeInputModel input) { var motorBike = new MotorBike { Name = input.Name, Model = input.Model, Price = input.Price, Weight = input.Weight, Length = input.Length, Height = input.Height, SeatHeight = input.SeatHeight, Descrition = input.Descrition, }; if (this.bikeTypeRepository.All().Any(x => x.Name == input.BikeType)) { motorBike.BikeType = this.bikeTypeRepository.All().Where(x => x.Name == input.BikeType).First(); } else { motorBike.BikeType = new BikeType { Name = input.BikeType, Descrition = input.BikeTypeDesctiption }; } if (this.transmissionRepozitory.All().Any(x => x.Name == input.Transmission)) { motorBike.Transmission = this.transmissionRepozitory.All().Where(x => x.Name == input.Transmission).First(); } else { motorBike.Transmission = new Transmission { Name = input.Transmission }; } if (this.frontSuspensionRepository.All().Any(x => x.Name == input.FrontSuspension)) { motorBike.FrontSuspension = this.frontSuspensionRepository.All().Where(x => x.Name == input.FrontSuspension).First(); } else { motorBike.FrontSuspension = new FrontSuspension { Name = input.FrontSuspension }; } if (this.rearSuspensionRepository.All().Any(x => x.Name == input.RearSuspension)) { motorBike.RearSuspension = this.rearSuspensionRepository.All().Where(x => x.Name == input.RearSuspension).First(); } else { motorBike.RearSuspension = new RearSuspension { Name = input.RearSuspension }; } if (this.frontBreakRepository.All().Any(x => x.Name == input.FrontBrakes)) { motorBike.FrontBrakes = this.frontBreakRepository.All().Where(x => x.Name == input.FrontBrakes).First(); } else { motorBike.FrontBrakes = new FrontBrake { Name = input.FrontBrakes }; } if (this.rearBreakRepository.All().Any(x => x.Name == input.RearBrakes)) { motorBike.RearBrakes = this.rearBreakRepository.All().Where(x => x.Name == input.RearBrakes).First(); } else { motorBike.RearBrakes = new RearBrake { Name = input.RearBrakes }; } ////if (!this.coolingSystemRepository.All().Any(x => x.Name == input.CoolingSystem)) ////{ //// await this.coolingSystemRepository.AddAsync(new CoolingSystem { Name = input.CoolingSystem }); ////} if (this.engineRepositiry.All().Any(x => x.Name == input.EngineName)) { motorBike.Engine = this.engineRepositiry.All().Where(x => x.Name == input.EngineName).First(); } else { motorBike.Engine = new Engine { Name = input.EngineName, EngineCapacity = input.EngineCapacity, EnginePower = input.EnginePower, CoolingSystem = new CoolingSystem { Name = input.CoolingSystem }, }; } await this.motorBikeRepository.AddAsync(motorBike); await this.motorBikeRepository.SaveChangesAsync(); }