Ejemplo n.º 1
0
        public async Task CreateRent(CarRentBindingModel carRentBindingModel, string userId)
        {
            decimal price = context.Cars
                            .Where(c => c.Id == carRentBindingModel.CarId)
                            .Select(s => s.PricePerDay)
                            .FirstOrDefault();

            decimal fee = ((carRentBindingModel.EndDate.Date - carRentBindingModel.StartDate.Date).Days * price);

            Rent rent = new Rent()
            {
                EndDate   = carRentBindingModel.EndDate,
                StartDate = carRentBindingModel.StartDate,
                CarId     = carRentBindingModel.CarId,
                StatusId  = 1,
                IssuedOn  = DateTime.UtcNow,
                Fee       = fee,
                UserId    = userId
            };

            await this.context.Rents.AddAsync(rent);

            //Change car status to Booked
            var bookedDbCar = this.context.Cars
                              .Where(c => c.Id == carRentBindingModel.CarId)
                              .FirstOrDefault();

            bookedDbCar.CarStatusId = 2;

            this.context.SaveChanges();
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Create(CarRentBindingModel carRentBindingModel)
        {
            string userId = this.User.FindFirst(ClaimTypes.NameIdentifier).Value;

            await this.rentService.CreateRent(carRentBindingModel, userId);

            return(this.Redirect("/"));
        }