public async Task <Object> PostVehicleReservation(TOVehicleReservation toVehicleReservation, [FromQuery] int version)
        {
            string role = User.Claims.First(c => c.Type == "Roles").Value;

            if (role != "regular")
            {
                return(BadRequest("You are not authorised to do this action"));
            }

            VehicleReservation vehicleReservation = new VehicleReservation();

            vehicleReservation.FromTO(toVehicleReservation);

            bool    success = false;
            Vehicle vehicle = await _context.Vehicles.Include(v => v.UnavailableDates).FirstOrDefaultAsync(v => v.VehicleId == vehicleReservation.VehicleId);

            if (vehicle.Version != version)
            {
                //Provera da li je zbog edita ili rezervacije duple
                return(Ok(new { success }));
            }

            _context.VehicleReservation.Add(vehicleReservation);
            await _context.SaveChangesAsync();

            //Treba popuniti Unavailable Dates


            for (int i = 0; i < vehicleReservation.NumOfDays; i++)
            {
                vehicle.UnavailableDates.Add(new UnavailableDate()
                {
                    DateId  = 0,
                    Vehicle = vehicle,
                    Date    = vehicleReservation.FromDate.AddDays(i)
                });
            }

            vehicle.Version++;
            await _context.SaveChangesAsync();

            int id = vehicleReservation.ReservationId;

            success = true;
            return(Ok(new { id, success }));
        }
Ejemplo n.º 2
0
        public TOVehicleReservation ToTO()
        {
            TOVehicleReservation toVehicleReservation = new TOVehicleReservation();

            toVehicleReservation.FromDate      = FromDate.ToString();
            toVehicleReservation.FromLocation  = FromLocation;
            toVehicleReservation.NumOfDays     = NumOfDays;
            toVehicleReservation.Price         = Price;
            toVehicleReservation.ReservationId = ReservationId;
            toVehicleReservation.ToDate        = ToDate.ToString();
            toVehicleReservation.ToLocation    = ToLocation;
            toVehicleReservation.Type          = Type;
            //toVehicleReservation.Vehicle = Vehicle.ToTO();
            toVehicleReservation.VehicleId       = VehicleId;
            toVehicleReservation.CreationDate    = CreationDate.ToString();
            toVehicleReservation.IsVehicleRated  = IsVehicleRated;
            toVehicleReservation.IsRentACarRated = IsRentACarRated;

            return(toVehicleReservation);
        }
Ejemplo n.º 3
0
        public void FromTO(TOVehicleReservation toVehicleReservation)
        {
            FromDate      = DateTime.Parse(toVehicleReservation.FromDate);
            FromLocation  = toVehicleReservation.FromLocation;
            NumOfDays     = toVehicleReservation.NumOfDays;
            Price         = toVehicleReservation.Price;
            ReservationId = toVehicleReservation.ReservationId;
            ToDate        = DateTime.Parse(toVehicleReservation.ToDate);
            ToLocation    = toVehicleReservation.ToLocation;
            Type          = toVehicleReservation.Type;
            Vehicle  vehicle  = new Vehicle();
            RentACar rentACar = new RentACar();

            rentACar.FromTO(toVehicleReservation.RentACar);
            //vehicle.FromTO(toVehicleReservation.Vehicle, rentACar);
            VehicleId       = toVehicleReservation.VehicleId;
            UserName        = toVehicleReservation.UserName;
            CreationDate    = DateTime.Now;
            IsVehicleRated  = toVehicleReservation.IsVehicleRated;
            IsRentACarRated = toVehicleReservation.IsRentACarRated;
        }
        public async Task <IActionResult> PutVehicleReservation(int id, TOVehicleReservation toVehicleReservation)
        {
            string role = User.Claims.First(c => c.Type == "Roles").Value;

            if (role != "regular")
            {
                return(BadRequest("You are not authorised to do this action"));
            }

            VehicleReservation vehicleReservation = new VehicleReservation();

            vehicleReservation.FromTO(toVehicleReservation);

            if (id != vehicleReservation.ReservationId)
            {
                return(BadRequest());
            }

            _context.Entry(vehicleReservation).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!VehicleReservationExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }