Exemplo n.º 1
0
        public ActionResult <Automobile> Patch([FromBody] Automobile auto)
        {
            //check to make sure this automobile exists in the database
            var existingAutomobile = _context.Automobiles.FirstOrDefault(a => a.Id == auto.Id);

            if (existingAutomobile == null || existingAutomobile.Id == 0)
            {
                return(BadRequest("Automobile does not exist in database."));
            }

            //map properties to be updated
            existingAutomobile.Make               = auto.Make;
            existingAutomobile.Model              = auto.Model;
            existingAutomobile.NumberOfAxels      = auto.NumberOfAxels;
            existingAutomobile.NumberOfPassengers = auto.NumberOfPassengers;
            existingAutomobile.RideHigh           = auto.RideHigh;
            existingAutomobile.Weight             = auto.Weight;
            existingAutomobile.Year               = auto.Year;

            //add to database
            _context.Entry(existingAutomobile).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            _context.SaveChanges();

            return(Ok(auto));
        }
Exemplo n.º 2
0
        public ActionResult <Automobile> GetCars()
        {
            Automobile myVehicles = new Automobile();

            myVehicles.Make               = "Kia";
            myVehicles.Model              = "Optima";
            myVehicles.Year               = 2015;
            myVehicles.NumberOfAxels      = 2;
            myVehicles.NumberOfPassengers = 5;
            myVehicles.Weight             = 50;
            myVehicles.Color              = "black";
            myVehicles.RideHigh           = 6;

            return(myVehicles);
        }
Exemplo n.º 3
0
        public ActionResult <Automobile> Post([FromBody] Automobile auto)
        {
            //check to make sure this automobile doesn't already exist in the database


            var isExistingAutomobile = _context.Automobiles.Any(a => a.Id == auto.Id);

            if (isExistingAutomobile)
            {
                return(BadRequest("Duplicate. Automobile already exists in database."));
            }

            //add to database
            _context.Automobiles.Add(auto);
            _context.Entry(auto).State = Microsoft.EntityFrameworkCore.EntityState.Added;
            _context.SaveChanges();

            return(Ok(auto));
        }