public Operation <Harvest> UpdateHarvest(Harvest harvest) => Operation.Try(async() => { //verify the await harvest .ThrowIfNull(new GaiaException(PolluxErrorCodes.InvalidArgument)) .Validate(); var persisted = (await _queries .GetHarvest(harvest.Id)) .ThrowIfNull(new GaiaException(ErrorCodes.InvalidStoreQueryResult)); var farm = (await _queries .GetFarm(persisted.Batch.Farm.Id)) .ThrowIfNull(new GaiaException(ErrorCodes.InvalidStoreQueryResult)); //data access authorization await _dataAuth.AuthorizeCustomAccess(new FarmerDataAccess { Farmer = farm.Owner, Farm = farm }); //cannot update harvest of a published batch if (persisted.Batch.Status == HarvestBatchStatus.Published) { throw new GaiaException(ErrorCodes.DomainLogicError); } //make sure the new harvest product exists var produce = (await _queries .GetFarmProduce(harvest.Produce.Id)) .ThrowIfNull(new GaiaException(ErrorCodes.DomainLogicError)); //copy values persisted.Produce = produce; persisted.Unit = harvest.Unit; persisted.UnitAmount = harvest.UnitAmount; var storeCommand = _storeProvider.CommandFor(typeof(HarvestBatch).FullName); return((await storeCommand .Update(persisted)) .ThrowIfNull(new GaiaException(ErrorCodes.InvalidStoreCommandResult))); });
public Operation <Harvest> CreateHarvest(Guid batchId, Harvest harvest) => Operation.Try(async() => { //verify the parameters batchId.ThrowIf( default(Guid), new GaiaException(PolluxErrorCodes.InvalidArgument)); await harvest .ThrowIfNull(new GaiaException(PolluxErrorCodes.InvalidArgument)) .Validate(); var batch = (await _queries .GetBatch(batchId)) .ThrowIfNull(new GaiaException(ErrorCodes.InvalidStoreQueryResult)); var farmer = (await _queries .GetFarmer(batch.Farm.Owner.Id)) .ThrowIfNull(new GaiaException(ErrorCodes.InvalidStoreQueryResult)); //data access authorization: make sure the current user is the farmer that owns the farm to which the batch is being added await _dataAuth.AuthorizeCustomAccess(new FarmerDataAccess { Farmer = farmer, Farm = batch.Farm }); //cannot add a harvest to an already published batch if (batch.Status == HarvestBatchStatus.Published) { throw new GaiaException(ErrorCodes.DomainLogicError); } //harvest.Id = Guid.NewGuid(); //<-- this is redundant because the StoreTransformer always adds new Guids for objects being added to the store. harvest.Batch = batch; var storeCommand = _storeProvider.CommandFor(typeof(Harvest).FullName); return((await storeCommand .Add(harvest)) .ThrowIfNull(new GaiaException(ErrorCodes.InvalidStoreCommandResult))); });