public async Task <IActionResult> Index(AddRentViewModel viewModel) { if (ModelState.IsValid) { //Default manager var creatorId = 1; var vehicle = await _repository.FindVehicleBy(viewModel.VehicleName); if (vehicle != null) { var price = vehicle.Price * viewModel.Day; await _repository.Rent(viewModel, creatorId, price); return(Content("اجاره نامه شما با موفقیت ثبت شد. از رانندگی لذت ببرید")); } ModelState.AddModelError(nameof(AddRentViewModel.VehicleName), "خطا"); } return(View(viewModel)); }
public async Task Rent(AddRentViewModel viewModel, int creatorId, double price) { var vehicle = await _dbContext.Vehicles.FirstOrDefaultAsync(x => x.Name == viewModel.VehicleName); var user = await _dbContext.Users.FindAsync(creatorId); if (vehicle != null && user != null) { var rent = await _dbContext.Rents.FirstOrDefaultAsync(x => x.VehicleId == vehicle.Id); if (rent == null) { rent = new Rent(viewModel.Day, user.Id, vehicle.Id, price); rent.Number = CodeGenerator.GetNewRentNumber(); vehicle.IsRented = true; await _dbContext.AddAsync(rent); await _dbContext.SaveChangesAsync(); } } }