Example #1
0
        public Task <ResourceCreationResult <Tap, int> > CreateAsync(Tap resource, IRequestContext context, CancellationToken cancellation)
        {
            int officeId     = ServiceHelper.GetUrlParameters <Office>(context, "OfficeId");
            int officeInfoId = ServiceHelper.GetUrlParameters <OfficeInfo>(context, "OfficeInfoId");
            var kegId        = _repository.Get(officeInfoId);

            decimal tapMilliliter       = resource.Milliliter;
            decimal remainingMilliliter = kegId.Milliliter;

            if (kegId.Milliliter <= 0)
            {
                throw context.CreateHttpResponseException <Tap>("This keg is empty", HttpStatusCode.BadRequest);
            }

            if (tapMilliliter > remainingMilliliter)
            {
                kegId.Milliliter = 0;
            }
            else
            {
                kegId.Milliliter = remainingMilliliter - tapMilliliter;
            }

            _repository.Update(kegId);
            _repository.SaveChanges();

            context.LinkParameters.Set(new LinksParametersSource(officeId, officeInfoId));
            return(Task.FromResult(new ResourceCreationResult <Tap, int>(resource)));
        }
        public Task <ResourceCreationResult <ReplaceKeg, int> > CreateAsync(ReplaceKeg resource, IRequestContext context, CancellationToken cancellation)
        {
            int officeId     = ServiceHelper.GetUrlParameters <Office>(context, "OfficeId");
            int officeInfoId = ServiceHelper.GetUrlParameters <OfficeInfo>(context, "OfficeInfoId");

            var oldKeg = _repository.Get(officeInfoId);

            oldKeg.Milliliter = oldKeg.Milliliter + resource.Milliliter;
            oldKeg.Beertype   = resource.Beertype ?? oldKeg.Beertype;

            _repository.Update(oldKeg);
            _repository.SaveChanges();

            context.LinkParameters.Set(new LinksParametersSource(officeId, officeInfoId));
            return(Task.FromResult(new ResourceCreationResult <ReplaceKeg, int>(resource)));
        }