Example #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));
        }
Example #2
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());
        }