Пример #1
0
        public Operation <Farm> CreateFarm(Guid farmerId, Farm farm)
        => Operation.Try(async() =>
        {
            //verify the parameters
            farmerId
            .ThrowIf(default(Guid), new GaiaException(PolluxErrorCodes.InvalidArgument));

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

            //data access authorization
            await _dataAuth.AuthorizeAccess(typeof(Farm).FullName);

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

            //farm.Id = Guid.NewGuid(); //<-- this is redundant because the StoreTransformer always adds new Guids for objects being added to the store.
            farm.Cooperatives = new Cooperative[0];
            farm.Products     = new ProductCategory[0];
            farm.Harvests     = new HarvestBatch[0];
            farm.Area         = new GeoPosition[0];

            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 <Farmer> CreateFarmer(Guid userId, Farmer farmer)
        => Operation.Try(async() =>
        {
            //verify the parameters
            userId
            .ThrowIf(default(Guid), new GaiaException(PolluxErrorCodes.InvalidArgument));

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

            //data access authorization
            await _dataAuth.AuthorizeAccess(typeof(Farmer).FullName);

            var user = (await _queries
                        .GetUser(userId))
                       .ThrowIfNull(new GaiaException(ErrorCodes.InvalidStoreQueryResult));

            //farmer.Id = Guid.NewGuid(); //<-- this is redundant because the StoreTransformer always adds new Guids for objects being added to the store.
            farmer.Farms  = new Farm[0];
            farmer.Status = FarmerStatus.Active;
            farmer.User   = user;

            var storeCommand = _storeProvider.CommandFor(typeof(Farmer).FullName);
            return((await storeCommand
                    .Add(farmer))
                   .ThrowIfNull(new GaiaException(ErrorCodes.InvalidStoreCommandResult)));
        });
Пример #4
0
        public Operation <UserIdentity> CreateIdentity(string userName, Guid userId)
        => Operation.Try(async() =>
        {
            //verify the parameters
            userId.ThrowIf(
                default(Guid),
                new GaiaException(PolluxErrorCodes.InvalidArgument));

            userName.ThrowIf(
                string.IsNullOrWhiteSpace,
                new GaiaException(PolluxErrorCodes.InvalidArgument));

            //data access authorization
            await _dataAuth.AuthorizeAccess(typeof(UserIdentity).FullName);

            //get the user object
            var user = (await _queries
                        .GetUser(userId))
                       .ThrowIfNull(new GaiaException(ErrorCodes.DomainLogicError));

            //make sure this user doesn't already have an identity object
            (await _queries
             .GetUserIdentity(user.Id))
            .ThrowIfNotNull(new GaiaException(ErrorCodes.DomainLogicError));

            //make sure this user name is unique
            (await _queries
             .GetUserIdentity(userName))
            .ThrowIfNotNull(new GaiaException(ErrorCodes.DomainLogicError));

            var userIdentity = new UserIdentity
            {
                Owner    = user,
                UserName = await _userNameValidator.Validate(userName)
            };

            var storeCommand = _storeProvider.CommandFor(typeof(UserIdentity).FullName);
            return((await storeCommand
                    .Add(userIdentity))
                   .ThrowIfNull(new GaiaException(ErrorCodes.InvalidStoreCommandResult)));
        });
Пример #5
0
        private Operation Persist(PolluxPolicy policy)
        => Operation.Try(async() =>
        {
            var persisted = policy.Id != default(Guid)
                ? (await _query.GetPolicyById(policy.Id)).ThrowIfNull(new GaiaException(ErrorCodes.InvalidStoreQueryResult))
                : new PolluxPolicy();

            persisted.Parent            = policy.Parent;
            persisted.Code              = policy.Code;
            persisted.CombinationClause = policy.CombinationClause;
            persisted.GovernedResources = policy.GovernedResources;
            persisted.Title             = policy.Title;

            var command = _provider.CommandFor(typeof(PolluxPolicy).FullName);

            var op = policy.Id == default(Guid)
                ? command.Add(persisted)
                : command.Update(persisted);

            (await op).ThrowIfNull(new GaiaException(ErrorCodes.InvalidStoreCommandResult));
        });
Пример #6
0
        public Operation <Cooperative> CreateCooperative(Cooperative cooperative)
        => Operation.Try(async() =>
        {
            await cooperative
            .ThrowIfNull(new GaiaException(PolluxErrorCodes.InvalidArgument))
            .Validate();

            //Ensure that the right principal has access to this data
            await _dataAccessAuthorizer.AuthorizeAccess(typeof(Cooperative).FullName);

            cooperative.Id     = Guid.NewGuid();
            cooperative.Admins = null;
            cooperative.Farms  = null;
            cooperative.Status = CooperativeStatus.Active;

            var storeCommand = _storeProvider.CommandFor(typeof(Cooperative).FullName);

            return((await storeCommand
                    .Add(cooperative))
                   .ThrowIfNull(new GaiaException(ErrorCodes.InvalidStoreCommandResult)));
        });