Example #1
0
        public async Task <IHttpActionResult> Post([FromBody] FuelingViewModel fueling)
        {
            using (var context = new FuelingsContext())
            {
                var vehicle = await context.Vehicles.FirstOrDefaultAsync(b => b.Id == fueling.VehicleId);

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

                var newFueling = context.Fuelings.Add(new Fueling
                {
                    VehicleId = vehicle.Id,
                    Date      = fueling.Date,
                    Fuel      = fueling.Fuel,
                    Mileage   = fueling.Mileage,
                    Price     = fueling.Price,
                    Quantity  = fueling.Quantity
                });

                await context.SaveChangesAsync();

                return(Ok(new FuelingViewModel(newFueling)));
            }
        }
 public async Task <IHttpActionResult> Get()
 {
     using (var context = new FuelingsContext())
     {
         var userId = User.Identity.GetUserId();
         return(Ok(await context.Vehicles.Include(x => x.Fuelings).Include("Fuelings.Fuel").Where(x => x.UserId == userId).ToListAsync()));
     }
 }
Example #3
0
        public async Task <IHttpActionResult> Delete(int id)
        {
            using (var context = new FuelingsContext())
            {
                var fueling = await context.Fuelings.FirstOrDefaultAsync(r => r.Id == id);

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

                context.Fuelings.Remove(fueling);
                await context.SaveChangesAsync();
            }
            return(Ok());
        }