public Task <ResourceCreationResult <Office, int> > CreateAsync(Office resource, IRequestContext context, CancellationToken cancellation) { db.Offices.Add(resource); db.SaveChangesAsync(cancellation); var result = new ResourceCreationResult <Office, int>(resource); return(Task.FromResult(result)); }
public async Task <ResourceCreationResult <BeerTap, int> > CreateAsync(BeerTap resource, IRequestContext context, CancellationToken cancellation) { db.BeerTaps.Add(resource); await db.SaveChangesAsync(cancellation); db.Entry(resource).Reference(x => x.Office).Load(); var result = new ResourceCreationResult <BeerTap, int>(resource); return(result); }
public Task <ResourceCreationResult <ReplaceKeg, int> > CreateAsync(ReplaceKeg resource, IRequestContext context, CancellationToken cancellation) { var officeId = context.UriParameters.GetByName <int>("OfficeId") .EnsureValue( () => context.CreateHttpResponseException <BeerTap>( "The officeId must be supplied in the URI", HttpStatusCode.BadRequest)); var beerTapId = context.UriParameters.GetByName <int>("BeerTapId") .EnsureValue( () => context.CreateHttpResponseException <BeerTap>( "The BeerTapId must be supplied in the URI", HttpStatusCode.BadRequest)); var beerTap = db.BeerTaps.Include(b => b.Office).SingleOrDefault(b => b.Id == beerTapId); if (beerTap == null) { throw context.CreateNotFoundHttpResponseException <BeerTap>(); } beerTap.Volume = resource.ML; if (beerTap.Volume == 0) { beerTap.BeerTapState = BeerTapState.Dry; } else if (beerTap.Volume < 50) { beerTap.BeerTapState = BeerTapState.AlmostGone; } else if (beerTap.Volume < 100) { beerTap.BeerTapState = BeerTapState.NotSoNew; } else { beerTap.BeerTapState = BeerTapState.New; } db.Entry(beerTap).State = EntityState.Modified; try { db.SaveChangesAsync(cancellation); } catch (DbUpdateConcurrencyException) { if (!BeerTapExists(beerTap.Id)) { throw context.CreateNotFoundHttpResponseException <BeerTap>(); } else { throw; } } return(Task.FromResult(new ResourceCreationResult <ReplaceKeg, int>(resource))); }