示例#1
0
        public async Task <ActionResult <TORentACar> > PostRentACar(TORentACar toRentACar)
        {
            string role = User.Claims.First(c => c.Type == "Roles").Value;

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

            RentACar rentACar = new RentACar();

            rentACar.FromTO(toRentACar);

            _context.RentACars.Add(rentACar);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (RentACarExists(rentACar.Name))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtAction("GetRentACar", new { id = toRentACar.Name }, toRentACar));
        }
示例#2
0
 public void FromTO(TORentACar toRentACar)
 {
     Name        = toRentACar.Name;
     Address     = toRentACar.Address;
     Description = toRentACar.Description;
     Rating      = toRentACar.Rating;
     Vehicles    = new List <Vehicle>();
     Locations   = new List <Location>();
     Prices      = new List <PriceList>();
     toRentACar.Vehicles.ToList().ForEach(vehicle =>
     {
         Vehicle vehicleObj = new Vehicle();
         vehicleObj.FromTO(vehicle, this);
         Vehicles.Add(vehicleObj);
     });
     toRentACar.Locations.ToList().ForEach(location => Locations.Add(
                                               new Location()
     {
         LocationId    = 0,
         LocationValue = location.Value.ToString(),
         RentACar      = this
     }));
     Prices.Add(new PriceList()
     {
         PriceValue   = (Int64)(toRentACar.Prices.ToList()[0].Value),
         PriceService = "Car",
         RentACar     = this
     });
     Prices.Add(new PriceList()
     {
         PriceValue   = (Int64)(toRentACar.Prices.ToList()[1].Value),
         PriceService = "Van",
         RentACar     = this
     });
     Prices.Add(new PriceList()
     {
         PriceValue   = (Int64)(toRentACar.Prices.ToList()[2].Value),
         PriceService = "Truck",
         RentACar     = this
     });
     //toRentACar.Prices.ToList().ForEach(price => Prices.Add(new PriceList()
     //{  }));
 }
示例#3
0
        public TORentACar ToTO()
        {
            TORentACar toRentACar = new TORentACar();

            toRentACar.Name        = Name;
            toRentACar.Address     = Address;
            toRentACar.Description = Description;
            double ratingSum = 0;

            if (Ratings.Count != 0)
            {
                Ratings.ToList().ForEach(rating => ratingSum += rating.RentACarRatingValue);
                toRentACar.Rating = ratingSum / Ratings.Count;
            }
            else
            {
                toRentACar.Rating = 0;
            }
            toRentACar.Vehicles  = new List <TOVehicle>();
            toRentACar.Locations = new List <TOPrimaryObject>();
            toRentACar.Prices    = new List <TOPrimaryObject>();
            Vehicles.ToList().ForEach(vehicle => toRentACar.Vehicles.Add(vehicle.ToTO()));
            Locations.ToList().ForEach(location => toRentACar.Locations.Add(
                                           new TOPrimaryObject()
            {
                Id        = 0,
                Value     = location.LocationValue,
                Reference = this
            }));
            Prices.ToList().ForEach(price => toRentACar.Prices.Add(
                                        new TOPrimaryObject()
            {
                Id        = 0,
                Value     = (Int64)(price.PriceValue),
                Reference = this
            }));

            return(toRentACar);
        }
示例#4
0
        public async Task <IActionResult> PutRentACar(string id, TORentACar toRentACar)
        {
            string role = User.Claims.First(c => c.Type == "Roles").Value;

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

            var rentACar = await _context.RentACars.Include(r => r.Locations).Include(r => r.Prices).FirstOrDefaultAsync(r => r.Name == id);

            RentACar modifiedRentACar = new RentACar();

            modifiedRentACar.FromTO(toRentACar);

            if (id != rentACar.Name)
            {
                return(BadRequest());
            }

            //_context.Entry(rentACar).State = EntityState.Modified;
            _context.Entry(rentACar).CurrentValues.SetValues(modifiedRentACar);

            #region Update Locations

            var locations = rentACar.Locations.ToList(); //Lokacije iz baze
            foreach (var location in locations)
            {
                var loc = toRentACar.Locations.SingleOrDefault(l => l.Value.ToString() == location.LocationValue); //Ako ne postoji u bazi ta lokacija, ukloni je
                if (loc == null)
                {
                    _context.Remove(location);
                }
            }
            // add the new items
            foreach (var location in toRentACar.Locations.ToList())                   //Nove lokacije
            {
                if (locations.All(l => l.LocationValue != location.Value.ToString())) //Ako sve lokacije nisu jednake novoj lokaciji, dodaj je
                {
                    rentACar.Locations.Add(new Location()
                    {
                        LocationId    = 0,
                        LocationValue = location.Value.ToString(),
                        RentACar      = rentACar
                    });
                }
            }

            #endregion

            #region Update Prices

            var prices   = rentACar.Prices.ToList(); //Cene iz baze
            var carPrice = prices.SingleOrDefault(p => p.PriceService == "Car");
            carPrice.PriceValue = (Int64)toRentACar.Prices.ToList()[0].Value;
            var vanPrice = prices.SingleOrDefault(p => p.PriceService == "Van");
            vanPrice.PriceValue = (Int64)toRentACar.Prices.ToList()[1].Value;
            var truckPrice = prices.SingleOrDefault(p => p.PriceService == "Truck");
            truckPrice.PriceValue = (Int64)toRentACar.Prices.ToList()[2].Value;

            _context.Entry(prices[0]).CurrentValues.SetValues(carPrice);
            _context.Entry(prices[1]).CurrentValues.SetValues(vanPrice);
            _context.Entry(prices[2]).CurrentValues.SetValues(truckPrice);

            #endregion

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

            return(NoContent());
        }