Example #1
0
        public async Task <bool> Handle(NewSampleRequest message, IOutboundPort <BlankResponse> outputPort)
        {
            // Verify that the specified user maps to the sample's environment. (e.g., can this user add samples to that
            // environment?)
            var user = await _userStore.GetUserById(message.UserId);

            if (user != null)
            {
                await _userStore.LoadEnvironments(user);
            }

            var env = user?.Environments.FirstOrDefault(e => e.Id == message.Sample.Environment);

            if (env == null)
            {
                return(false);             // environment not owned by user or user not found.
            }
            await _envStore.LoadPetFor(env);

            // Fill in EnvDataSample entity instance.
            var sample = _entityFactory.GetSampleBuilder()
                         .AddHotGlassMeasurement(message.Sample.HotGlass ?? 0)
                         .AddHotMatMeasurement(message.Sample.HotMat ?? 0)
                         .AddMidGlassMeasurement(message.Sample.MidGlass ?? 0)
                         .AddColdGlassMeasurement(message.Sample.ColdGlass ?? 0)
                         .AddColdMatMeasurement(message.Sample.ColdMat ?? 0)
                         .SetInhabitant(env.Inhabitant)
                         .SetEnvironment(env)
                         .Build();

            await _envStore.AddSample(sample);

            if (env.Inhabitant != null)
            {
                await _petStore.UpdateLatestSample(env.Inhabitant, sample);
            }

            return(true);
        }