コード例 #1
0
        public async Task <IActionResult> Create([Bind(include: new string[] { "RentalId", "SelectedCustomerId", "SelectedBikeId", "Status", "RentedDate", "ReturnedDate" })] BikeBookingViewModel model)
        {
            if (ModelState.IsValid)
            {
                var response = await _bookingRepo.SaveBooking(model);

                if (response)
                {
                    return(RedirectToAction("Index"));
                }
            }

            return(NoContent());
        }
コード例 #2
0
        public async Task <IActionResult> Edit([Bind(include: new string[] { "RentalId", "SelectedCustomerId", "SelectedBikeId", "Status", "RentedDate", "ReturnedDate" })] BikeBookingViewModel model)
        {
            if (ModelState.IsValid)
            {
                var currentUser = await _user.GetUserAsync(User);

                model.ModifiedDate = DateTime.Now;
                model.ModifiedBy   = currentUser.Email;

                var response = await _bookingRepo.UpdateBooking(model);

                if (response)
                {
                    return(RedirectToAction("Index"));
                }
            }

            return(NoContent());
        }
コード例 #3
0
        public async Task <IEnumerable <BikeBookingViewModel> > GetBookings()
        {
            var bookings = await _context.BicycleRentals.AsNoTracking()
                           .Include(c => c.Customer)
                           .Include(b => b.BicycleInventory)
                           .ToListAsync();

            if (bookings != null)
            {
                var bookingsDisplay = new List <BikeBookingViewModel>();
                foreach (var booking in bookings)
                {
                    var currDate = booking.ReturnedDate.HasValue ? booking.ReturnedDate : DateTime.Now;
                    var status   = booking.ReturnedDate.HasValue ? "Returned" : "Rented";

                    var bookingDisplay = new BikeBookingViewModel
                    {
                        RentalId           = booking.RentalId,
                        RentedDate         = booking.RentedDate,
                        ReturnedDate       = booking.ReturnedDate,
                        Status             = status,
                        SelectedBikeId     = booking.BicycleInventory.BikeId,
                        SelectedBikeModel  = booking.BicycleInventory.ModelNo,
                        BicycleInventory   = booking.BicycleInventory,
                        SelectedCustomerId = booking.Customer.CustomerId,
                        CustomerFullName   = string.Format("{0}, {1}", booking.Customer.LastName, booking.Customer.FirstName),
                        Customer           = booking.Customer,
                        TotalTimeSpent     = currDate.Value.Subtract(booking.RentedDate),
                        Bikes        = _bikes,
                        Customers    = _customers,
                        ModifiedBy   = booking.ModifiedBy,
                        ModifiedDate = booking.ModifiedDate
                    };

                    bookingsDisplay.Add(bookingDisplay);
                }

                return(bookingsDisplay);
            }

            return(null);
        }
コード例 #4
0
        public async Task <bool> SaveBooking(BikeBookingViewModel model)
        {
            if (model != null)
            {
                var booking = new BicycleBooking
                {
                    RentalId         = model.RentalId,
                    RentedDate       = model.RentedDate,
                    BicycleInventory = await _context.BicycleInventories.FindAsync(model.SelectedBikeId),
                    Customer         = await _context.Customers.FindAsync(model.SelectedCustomerId)
                };

                _context.BicycleRentals.Add(booking);
                await _context.SaveChangesAsync();

                return(true);
            }

            return(false);
        }
コード例 #5
0
        public async Task <BikeBookingViewModel> GetBooking(Guid?id)
        {
            var booking = await _context.BicycleRentals.AsNoTracking()
                          .Include(c => c.Customer)
                          .Include(b => b.BicycleInventory)
                          .FirstOrDefaultAsync(x => x.RentalId == id);

            if (booking != null)
            {
                var currDate = booking.ReturnedDate.HasValue ? booking.ReturnedDate : DateTime.Now;
                var status   = booking.ReturnedDate.HasValue ? "Returned" : "Rented";

                var bookingDisplay = new BikeBookingViewModel
                {
                    RentalId           = booking.RentalId,
                    RentedDate         = booking.RentedDate,
                    ReturnedDate       = booking.ReturnedDate,
                    Status             = status,
                    SelectedBikeId     = booking.BicycleInventory.BikeId,
                    SelectedBikeModel  = booking.BicycleInventory.ModelNo,
                    BicycleInventory   = booking.BicycleInventory,
                    SelectedCustomerId = booking.Customer.CustomerId,
                    CustomerFullName   = string.Format("{0}, {1}", booking.Customer.LastName, booking.Customer.FirstName),
                    Customer           = booking.Customer,
                    TotalTimeSpent     = currDate.Value.Subtract(booking.RentedDate),
                    Bikes        = _bikes,
                    Customers    = _customers,
                    ModifiedBy   = booking.ModifiedBy,
                    ModifiedDate = booking.ModifiedDate
                };

                return(bookingDisplay);
            }

            return(null);
        }
コード例 #6
0
        public async Task <bool> UpdateBooking(BikeBookingViewModel model)
        {
            if (model != null)
            {
                var booking = new BicycleBooking
                {
                    RentalId         = model.RentalId,
                    RentedDate       = model.RentedDate,
                    ReturnedDate     = model.ReturnedDate,
                    BicycleInventory = await _context.BicycleInventories.FindAsync(model.SelectedBikeId),
                    Customer         = await _context.Customers.FindAsync(model.SelectedCustomerId),
                    ModifiedBy       = model.ModifiedBy,
                    ModifiedDate     = model.ModifiedDate
                };
                var current = await _context.BicycleRentals.FindAsync(model.RentalId);

                _context.Entry(current).CurrentValues.SetValues(booking);
                await _context.SaveChangesAsync();

                return(true);
            }

            return(false);
        }