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))); });
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))); });
public Operation <ArrayPage <HarvestBatch> > GetHarvestBatches(ArrayPageRequest request = null) => Operation.Try(async() => { //data access authorization await _dataAuth.AuthorizeAccess(typeof(HarvestBatch).FullName); return((await _queries .GetHarvestBatches(request)) .ThrowIfNull(new GaiaException(ErrorCodes.InvalidStoreQueryResult))); });
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))); });
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))); });
public Operation RegisterCooperative(CooperativeOnlyRegistrationInfo info) => Operation.Try(async() => { //validate parameters await info .ThrowIfNull(new GaiaException(Axis.Pollux.Common.Exceptions.ErrorCodes.InvalidContractParamState)) .Validate(); //make sure the admin user exists var adminUser = (await _userManager .GetUser(info.AdminUserId)) .ThrowIfNull(new GaiaException(Axis.Pollux.Common.Exceptions.ErrorCodes.InvalidContractParamState)); //authorize 'Cooperative' data access await _dataAccessAuthorizer.AuthorizeAccess(typeof(Cooperative).FullName, adminUser.Id); //create the cooperative await _cooperativeManager.CreateCooperative(new Cooperative { Title = info.Title }); });