Beispiel #1
0
        public async Task <IActionResult> PutCar([FromBody] Api.Models.CarInfo carInfo)
        {
            // idempotency
            if (_context.Cars.Any(e => e.Model == carInfo.Model && e.Year == carInfo.Year && e.Price == carInfo.Price &&
                                  e.SerialNumber == carInfo.SerialNumber && e.SoldDateUtc == carInfo.SoldDateUtc && e.Comment == carInfo.Comment &&
                                  e.TransmissionID == (int)carInfo.Transmission && e.MotorID == (int)carInfo.Motor && e.GarageID == carInfo.GarageId &&
                                  e.SellerID == carInfo.SellerId))
            {
                return(NoContent());
            }

            var created = new Car
            {
                Model          = carInfo.Model,
                Price          = carInfo.Price,
                Year           = carInfo.Year,
                SerialNumber   = carInfo.SerialNumber,
                SoldDateUtc    = carInfo.SoldDateUtc,
                Comment        = carInfo.Comment,
                MotorID        = _context.MotorTypes.Find((int)carInfo.Motor).ID,
                TransmissionID = _context.TransmissionTypes.Find((int)carInfo.Transmission).ID,
                GarageID       = carInfo.GarageId,
                SellerID       = carInfo.SellerId
            };

            _context.Cars.Add(created);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetCar", new { id = created.ID }, created));
        }
Beispiel #2
0
        public async Task <IActionResult> PostCar([FromRoute] int id, [FromBody] Api.Models.CarInfo carInfo)
        {
            var toUpdate = await _context.Cars.FindAsync(id);

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

            toUpdate.Model          = carInfo.Model;
            toUpdate.Price          = carInfo.Price;
            toUpdate.Year           = carInfo.Year;
            toUpdate.SerialNumber   = carInfo.SerialNumber;
            toUpdate.SoldDateUtc    = carInfo.SoldDateUtc;
            toUpdate.Comment        = carInfo.Comment;
            toUpdate.MotorID        = _context.MotorTypes.Find((int)carInfo.Motor).ID;
            toUpdate.TransmissionID = _context.TransmissionTypes.Find((int)carInfo.Transmission).ID;
            toUpdate.GarageID       = carInfo.GarageId;
            toUpdate.SellerID       = carInfo.SellerId;

            await _context.SaveChangesAsync();

            return(NoContent());
        }