Ejemplo n.º 1
0
        public async Task <IHttpActionResult> PutCar(int id, Car car)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != car.Id)
            {
                return(BadRequest());
            }

            _db.Entry(car).State = EntityState.Modified;

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> PutCarsForSale(int id, CarsForSale carsForSale)
        {
            if (id != carsForSale.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
Ejemplo n.º 3
0
        public async Task <ActionResult <Car> > PostCar(Car car)
        {
            _context.Car.Add(car);
            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetCarId), new { id = car.CarId }, car));
        }
        public async Task <ActionResult> Cars_Create([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")] IEnumerable <CarViewModel> cars)
        {
            foreach (var car in cars)
            {
                if (car != null && ModelState.IsValid)
                {
                    var newCar = new Car {
                        Name = car.Name
                    };
                    if (car.CategoryId.HasValue)
                    {
                        newCar.CategoryId = car.CategoryId.Value;
                    }
                    else
                    {
                        var category = new Category {
                            Name = car.CategoryName
                        };
                        db.Entry(category).State = EntityState.Added;
                        newCar.Category          = category;
                    }

                    db.Cars.Add(newCar);
                    await db.SaveChangesAsync();

                    car.Id = newCar.Id;
                }
            }
            await db.SaveChangesAsync();

            return(Json(cars.ToDataSourceResult(request, ModelState)));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Create([Bind("Id,Name")] CarDealer carDealer)
        {
            if (ModelState.IsValid)
            {
                _context.Add(carDealer);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(carDealer));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> Create([Bind("Id,Model,Motor,EngineType,Year,Price,ManufacturerId,CarDealerId")] Car car)
        {
            if (ModelState.IsValid)
            {
                _context.Add(car);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CarDealerId"]    = new SelectList(_context.CarDealers, "Id", "Id", car.CarDealerId);
            ViewData["ManufacturerId"] = new SelectList(_context.Manufacturers, "Id", "Id", car.ManufacturerId);
            return(View(car));
        }
Ejemplo n.º 7
0
        public async Task <ActionResult <Cars> > AddCar(Cars newCar)
        {
            if (ModelState.IsValid)
            {
                _context.Cars.Add(newCar);
                await _context.SaveChangesAsync();

                return(CreatedAtAction(nameof(GetCar), new { id = newCar.Id }, newCar));
            }
            else
            {
                return(BadRequest());
            }
        }
        public async Task <IActionResult> Post(Car car)
        {
            _context.Cars.Add(car);
            await _context.SaveChangesAsync();

            return(Created($"api/cars/{car.CarId}", car));
        }
        public async Task <ActionResult> DeleteCar(int id)
        {
            var car = await _context.Cars.FindAsync(id);

            if (car == null)
            {
                return(NotFound());
            }
            else
            {
                _context.Cars.Remove(car);
                await _context.SaveChangesAsync();

                return(NoContent());
            }
        }
Ejemplo n.º 10
0
        public async Task <Car> AddCar(Car car)
        {
            await _dbContext.Cars.AddAsync(car);

            await _dbContext.SaveChangesAsync();

            return(await Task.FromResult(car));
        }
Ejemplo n.º 11
0
        // GET: odata/Odata(5)
        //[Queryable]
        //public SingleResult<Car> GetCar([FromODataUri] int key)
        //{
        //    return SingleResult.Create(db.Cars.Where(car => car.Id == key));
        //}

        // PUT: odata/Odata(5)
        public async Task <IHttpActionResult> Put([FromODataUri] int key, Delta <Car> patch)
        {
            Validate(patch.GetEntity());

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Car car = await db.Cars.FindAsync(key);

            if (car == null)
            {
                return(NotFound());
            }

            patch.Put(car);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CarExists(key))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Updated(car));
        }
        public async Task <IActionResult> CreateVehicle([FromBody] VehicleResources vehicleResources)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var model = await context.Models.FindAsync(vehicleResources.ModelId);

            if (model == null)
            {
                ModelState.AddModelError("ModelId", "Invalid ModelId");
                return(BadRequest(ModelState));
            }

            var vehicle = mapper.Map <VehicleResources, Vehicle>(vehicleResources);

            vehicle.LastUpdate = DateTime.Now;
            context.Vehicles.Add(vehicle);
            await context.SaveChangesAsync();

            var result = mapper.Map <Vehicle, VehicleResources>(vehicle);

            return(Ok(result));
        }