Example #1
0
        public IActionResult SaveBike(int id, [FromBody] Bikes bike)
        {
            try
            {
                if (id <= 0)
                {
                    _context.Bikes.Add(bike);
                    _context.SaveChangesAsync();
                }
                else
                {
                    Bikes bikeToUpdate = _context.Bikes.Single(s => s.Id == id);
                    bikeToUpdate.Description    = bike.Description;
                    bikeToUpdate.Model          = bike.Model;
                    bikeToUpdate.Price          = bike.Price;
                    bikeToUpdate.QuantityOnHand = bike.QuantityOnHand;
                    bikeToUpdate.BikeType       = bike.BikeType;

                    _context.Bikes.Update(bikeToUpdate);
                    _context.SaveChangesAsync();
                }
            }
            catch (Exception e)
            {
                return(BadRequest("Failed to save bike."));
            }
            return(Ok(bike));
        }