public async Task <bool> Handle(DataAccessRequest <IEnvironment> message, IOutboundPort <GenericDataResponse <IEnvironment> > outputPort) { switch (message.Strategy) { case DataAccessRequest <IEnvironment> .AcquisitionStrategy.All: // Acquire all associated environments, null-cascade throughout var user = await _userStore.GetUserById(message.UserId); if (user != null) { await _userStore.LoadEnvironments(user); } var envs = user?.Environments; foreach (var env in envs) { await _envStore.LoadControllerFor(env); await _envStore.LoadPetFor(env); } // Compose a response. var response = new GenericDataResponse <IEnvironment> { Result = envs?.ToList() }; outputPort.Handle(response); return(envs != null); default: throw new ArgumentOutOfRangeException(nameof(message.Strategy)); } }
public async Task <bool> Handle(DataAccessRequest <ISpecies> message, IOutboundPort <GenericDataResponse <ISpecies> > outputPort) { // no need to validate user - species data not user specific. var response = new GenericDataResponse <ISpecies>(); switch (message.Strategy) { case DataAccessRequest <ISpecies> .AcquisitionStrategy.All: response.Result = await _petStore.GetSpeciesInfo(); break; case DataAccessRequest <ISpecies> .AcquisitionStrategy.Range: response.Result = (await _petStore.GetSpeciesInfo()) .Where(message.SelectionPredicate) .ToArray(); break; case DataAccessRequest <ISpecies> .AcquisitionStrategy.Single: response.Result = new[] { (await _petStore.GetSpeciesInfo()).FirstOrDefault(message.SelectionPredicate) }; break; default: throw new ArgumentOutOfRangeException(nameof(message)); } outputPort.Handle(response); return(true); }
public async Task <bool> Handle(DataAccessRequest <IEnvDataSample> message, IOutboundPort <GenericDataResponse <IEnvDataSample> > outputPort) { // verify the user exists: var user = await _userStore.GetUserById(message.UserId); if (user == null) { return(false); } await _userStore.LoadEnvironments(user); var response = new GenericDataResponse <IEnvDataSample>(); var samples = new List <IEnvDataSample>(); foreach (var env in user.Environments) { await _environmentStore.LoadSamplesFor(env); switch (message.Strategy) { case DataAccessRequest <IEnvDataSample> .AcquisitionStrategy.All: samples.AddRange(env.EnvDataSamples); break; case DataAccessRequest <IEnvDataSample> .AcquisitionStrategy.Range: samples.AddRange(env.EnvDataSamples.Where(message.SelectionPredicate)); break; case DataAccessRequest <IEnvDataSample> .AcquisitionStrategy.Single: // this one is a bit different, if we find a match amongst any environments, // shortcut a return after processing it. var sample = env.EnvDataSamples.FirstOrDefault(message.SelectionPredicate); if (sample != null) { response.Result = new[] { sample }; outputPort.Handle(response); return(true); } break; default: throw new ArgumentOutOfRangeException(nameof(message), "Strategy not recognized."); } } // in the event of strategy being single, samples will be empty, which is a suitable response. response.Result = samples; outputPort.Handle(response); return(true); }
public async Task <bool> Handle(SampleAccessRequest message, IOutboundPort <GenericDataResponse <IEnvDataSample> > outputPort) { var response = new GenericDataResponse <IEnvDataSample>(); var user = await _userStore.GetUserById(message.UserId); switch (message.Selector) { case SampleAccessRequest.SelectionCriteria.User: if (user == null) { return(false); } response.Result = await _sampleStore.GetRangeByUser(user, message.RangeStart, message.RangeEnd); break; case SampleAccessRequest.SelectionCriteria.Environment: // ensure the user has access to see this environment's data: await _userStore.LoadEnvironments(user); var env = user.Environments.FirstOrDefault(e => e.Id == message.EnvId); if (env == null) { return(false); // env doesn't exist or user can't access it. } response.Result = await _sampleStore.GetRangeByEnv(env, message.RangeStart, message.RangeEnd); break; case SampleAccessRequest.SelectionCriteria.Pet: // ensure the user owns the pet in question: await _userStore.LoadPets(user); var pet = user.Pets.FirstOrDefault(p => p.Id == message.PetId); if (pet == null) { return(false); // pet doesn't exist or user can't access its data. } response.Result = await _sampleStore.GetRangeByPet(pet, message.RangeStart, message.RangeEnd); break; default: throw new ArgumentException("Invalid selection mode", nameof(message)); } outputPort.Handle(response); return(true); }
public async Task <bool> Handle(DataAccessRequest <IController> message, IOutboundPort <GenericDataResponse <IController> > outputPort) { // Load user, verify they exist. var user = await _userStore.GetUserById(message.UserId); if (user == null) { return(false); } // pipe on back through the port. var response = new GenericDataResponse <IController> { // Load controllers (Note: userStore impls must include controller.Environments relation here) // associated to that user. Result = await _userStore.LoadControllers(user) }; outputPort.Handle(response); return(true); }
public async Task <bool> Handle(DataAccessRequest <IPet> message, IOutboundPort <GenericDataResponse <IPet> > outputPort) { var user = await _userStore.GetUserById(message.UserId); if (user == null) { return(false); } await _userStore.LoadPets(user); // Load species data - its trivial to include. foreach (var pet in user.Pets) { await _petStore.LoadSpecies(pet); } var response = new GenericDataResponse <IPet>(); switch (message.Strategy) { case DataAccessRequest <IPet> .AcquisitionStrategy.All: response.Result = user.Pets.ToImmutableList(); outputPort.Handle(response); return(true); case DataAccessRequest <IPet> .AcquisitionStrategy.Range: response.Result = user.Pets.Where(message.SelectionPredicate).ToImmutableList(); outputPort.Handle(response); return(true); case DataAccessRequest <IPet> .AcquisitionStrategy.Single: response.Result = new List <IPet>(new[] { user.Pets.FirstOrDefault(message.SelectionPredicate) }); outputPort.Handle(response); return(true); default: throw new ArgumentOutOfRangeException(nameof(message), "Unrecognized strategy"); } }
public async Task <bool> Handle(NodeUpdateRequest message, IOutboundPort <GenericDataResponse <IController> > outputPort) { var user = await _userStore.GetUserById(message.UserId); if (user == null) { return(false); // specified user doesn't exist, do nothing on their behalf. } var controller = await _controllerStore.GetById(message.ControllerId); if (message.Operation == NodeUpdateRequest.Mode.Create) { // if the controller exists - then it was owned by another user // if they haven't relinquished it then we don't want to re-author its owner. if (controller?.Owner != null) { if (controller.Owner != user) { return(false); } } // add controller (or a new instance) to the user: controller ??= _entityFactory.GetControllerBuilder() .SetId(message.ControllerId) .Build(); await _userStore.AddAssociationToController(user, controller); } else if (message.Operation == NodeUpdateRequest.Mode.PeerUpdate) { if (controller == null) { return(false); } // call through to RegisterEnvironmentUseCase? foreach (Guid envId in message.PeerIds) { // try loading the environment, update its association var env = await _environmentStore.GetById(envId); // Behavior here is TBD. Should environment devices just be 'whoever has posession it' // if so, should existing data associated to that environment be dumped? // if not, hos should we permit giving them out? etc. // Point is, I have some system-level planning to do here, but I need *something* // here for the time being: if (env == null) { continue; } if (env.Controller != controller) { await _controllerStore.ReParentEnvironment(controller, env); } // for now: not creating new Env entities in this use case. Expect it to be // handled separately } } var response = new GenericDataResponse <IController> { Result = new[] { controller } }; outputPort.Handle(response); return(true); }