Exemple #1
0
        public async Task <ActionResult <BikeDTO> > PostBike(BikeDTO bikeDTO)
        {
            var bike = new Bike
            {
                IsRented  = bikeDTO.IsRented,
                Name      = bikeDTO.Name,
                RentPrice = bikeDTO.RentPrice,
                BikeType  = bikeDTO.BikeType
            };

            _context.Bikes.Add(bike);
            await _context.Bikes.BikeContext.SaveChangesAsync();

            return(CreatedAtAction(nameof(Get), new { id = bikeDTO.ID }, bikeDTO));
        }
Exemple #2
0
        public async Task <IActionResult> PutBike(long id, BikeDTO bikeDTO)
        {
            if (id != bikeDTO.ID)
            {
                return(BadRequest());
            }

            Bike bike = await _context.Bikes.FindAsync(id);

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

            bike.IsRented  = bikeDTO.IsRented;
            bike.Name      = bikeDTO.Name;
            bike.RentPrice = bikeDTO.RentPrice;


            _context.Bikes.BikeContext.Entry(bike).State = EntityState.Modified;

            try
            {
                _context.Bikes.BikeContext.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BikeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }