예제 #1
0
        public Operation <Farm> AddFarm(Guid farmerId, Farm farm)
        => Operation.Try(async() =>
        {
            //validate parameters
            farmerId
            .ThrowIf(default(Guid), new GaiaException(PolluxErrorCodes.InvalidArgument));

            await farm
            .ThrowIfNull(new GaiaException(PolluxErrorCodes.InvalidArgument))
            .Validate();

            var farmer = (await _queries
                          .GetFarmer(farmerId))
                         .ThrowIfNull(new GaiaException(ErrorCodes.InvalidStoreQueryResult));

            //check authorization
            await _dataAuth.AuthorizeCustomAccess(new FarmerDataAccess {
                Farmer = farmer
            });

            //perform the edit
            //farmer.Id = Guid.Empty; //<-- unnecessary since the Transformer for the data store will add a new Guid
            farm.Owner = farmer;

            var storeCommand = _storeProvider.CommandFor(typeof(Farm).FullName);
            return((await storeCommand
                    .Add(farm))
                   .ThrowIfNull(new GaiaException(ErrorCodes.InvalidStoreCommandResult)));
        });
예제 #2
0
        public Operation <HarvestBatch> CreateHarvestBatch(Guid farmId, HarvestBatch batch)
        => Operation.Try(async() =>
        {
            //verify the parameters
            farmId
            .ThrowIf(default(Guid), new GaiaException(PolluxErrorCodes.InvalidArgument));

            await batch
            .ThrowIfNull(new GaiaException(PolluxErrorCodes.InvalidArgument))
            .Validate();

            var farm = (await _queries
                        .GetFarm(farmId))
                       .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 = farm.Owner,
                Farm   = farm
            });

            //batch.Id = Guid.NewGuid(); //<-- this is redundant because the StoreTransformer always adds new Guids for objects being added to the store.
            batch.Harvests = new Harvest[0];
            batch.Farm     = farm;
            batch.Status   = HarvestBatchStatus.Draft;

            var storeCommand = _storeProvider.CommandFor(typeof(HarvestBatch).FullName);
            return((await storeCommand
                    .Add(batch))
                   .ThrowIfNull(new GaiaException(ErrorCodes.InvalidStoreCommandResult)));
        });
예제 #3
0
        public Operation RemoveAdmin(Guid cooperativeId, Guid userId)
        => Operation.Try(async() =>
        {
            //validate argument
            cooperativeId.ThrowIf(
                default(Guid),
                new GaiaException(PolluxErrorCodes.InvalidArgument));

            //validate argument
            userId.ThrowIf(
                default(Guid),
                new GaiaException(PolluxErrorCodes.InvalidArgument));

            //make sure the cooperative exists
            (await _queries
             .GetCooperative(cooperativeId))
            .ThrowIfNull(new GaiaException(ErrorCodes.InvalidStoreQueryResult));

            //make sure the user exists
            (await _queries
             .GetUser(userId))
            .ThrowIfNull(new GaiaException(ErrorCodes.InvalidStoreQueryResult));

            var admins = await _GetAllAdmins(cooperativeId);

            //ensure that the current user is an admin for the Cooperative
            await _dataAccessAuthorizer.AuthorizeCustomAccess(new CooperativeAdminDescriptor
            {
                Admins = admins
            });

            //get the admin to be removed
            var coopAdmin = admins
                            .FirstOrDefault(admin => admin.User.Id == userId);

            if (coopAdmin == null)
            {
                return; //ignore situations where the admin to be removed does not belong to the list of admins.
            }
            var storeCommand = _storeProvider.CommandFor(typeof(CooperativeAdmin).FullName);

            (await storeCommand
             .Delete(coopAdmin))
            .ThrowIfNull(new GaiaException(ErrorCodes.InvalidStoreCommandResult));
        });
예제 #4
0
        public Operation AddProductCategory(Guid farmId, Guid produceId)
        => Operation.Try(async() =>
        {
            //validate parameters
            farmId
            .ThrowIf(default(Guid), new GaiaException(PolluxErrorCodes.InvalidArgument));

            produceId
            .ThrowIf(default(Guid), new GaiaException(PolluxErrorCodes.InvalidArgument));

            var farm = (await _queries
                        .GetFarm(farmId))
                       .ThrowIfNull(new GaiaException(ErrorCodes.InvalidStoreQueryResult));

            var produce = (await _queries
                           .GetFarmProduce(produceId))
                          .ThrowIfNull(new GaiaException(ErrorCodes.InvalidStoreQueryResult));

            //check authorization
            await _dataAuth.AuthorizeCustomAccess(new FarmerDataAccess
            {
                Farmer = farm.Owner,
                Farm   = farm
            });

            //filter out duplicates
            (await _queries
             .GetProductCategory(farmId, produceId))
            .ThrowIfNotNull(new GaiaException(ErrorCodes.DomainLogicError));

            //perform the edit
            var category = new ProductCategory
            {
                FarmProduce = produce,
                Farm        = farm
            };

            var storeCommand = _storeProvider.CommandFor(typeof(ProductCategory).FullName);
            (await storeCommand
             .Add(category))
            .ThrowIfNull(new GaiaException(ErrorCodes.InvalidStoreCommandResult));
        });