private async Task <Rental> RentalOrderToRental(RentalOrderDTO order)
        {
            if (!checkRentalValidity(order))
            {
                return(null);
            }
            string userId  = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value;
            var    _rental = new Rental
            {
                Payment = await(from p in context.Payments
                                where p.Name == order.PaymentMethod
                                select p).FirstOrDefaultAsync(),
                StartDate   = order.StartDate,
                EndDate     = order.EndDate,
                OrderDate   = DateTime.Now,
                EquipmentId = order.EquipmentId,
                Equipment   = await(from e in context.Equipments
                                    where e.ID == order.EquipmentId
                                    select e).FirstOrDefaultAsync(),

                Address = await(from uaddr in context.UserAddresses
                                where uaddr.ID == order.AddressId && uaddr.UserID == userId
                                select uaddr).FirstOrDefaultAsync(),
                Status = await(from s in context.Statuses
                               where s.Name == "Processing"
                               select s).FirstOrDefaultAsync()
            };

            _rental.PaymentID = _rental.Payment.ID;
            _rental.StatusID  = _rental.Status.ID;
            _rental.AddressID = _rental.Address.ID;
            return(_rental);
        }
        public async Task <ActionResult <int> > PostEquipment(RentalOrderDTO order)
        {
            Rental _rental = await RentalOrderToRental(order);

            if (_rental == null)
            {
                return(BadRequest("Order not valid!"));
            }
            context.Rentals.Add(_rental);
            await context.SaveChangesAsync();

            return(CreatedAtAction("rentalId", _rental.ID));
        }
        private bool checkRentalValidity(RentalOrderDTO o)
        {
            if (!context.UserAddresses.Any(ua => ua.ID == o.AddressId) ||
                !context.Payments.Any(p => p.Name == o.PaymentMethod) ||
                !context.Equipments.Any(e => e.ID == o.EquipmentId))
            {
                return(false);
            }
            var qRentals = (from r in context.Rentals
                            where r.EquipmentId == o.EquipmentId
                            select r);

            foreach (var r in qRentals)
            {
                if ((o.StartDate >= r.StartDate && o.StartDate <= r.EndDate) ||
                    (o.EndDate >= r.StartDate && o.EndDate <= r.EndDate) ||
                    (o.StartDate <= r.StartDate && o.EndDate >= r.EndDate))
                {
                    return(false);
                }
            }
            return(true);
        }