Example #1
0
        public async Task <bool> Handle(CreateSpeciesRequest message, IOutboundPort <NewEntityResponse <int> > outputPort)
        {
            var species = _entityFactory.GetSpeciesBuilder()
                          .SetName(message.Name)
                          .SetScientificName(message.ScientificName)
                          .SetLatitude(message.Latitude)
                          .SetLongitude(message.Longitude)
                          .Build();

            // validate some stuff:
            if (string.IsNullOrWhiteSpace(species.Name) ||
                string.IsNullOrEmpty(species.ScientificName) ||
                Math.Abs(species.DefaultLatitude) > 90.0 ||
                Math.Abs(species.DefaultLongitude) > 180.0)
            {
                return(false);
            }

            var response = new NewEntityResponse <int>
            {
                Id = await _petStore.Create(species)
            };

            outputPort.Handle(response);
            return(true);
        }
Example #2
0
        public async Task <bool> Handle(CreatePetRequest message, IOutboundPort <NewEntityResponse <int> > outputPort)
        {
            var user = await _userStore.GetUserById(message.User);

            if (user == null)
            {
                return(false);
            }

            // Compose a new Pet instance:
            var pet = _entityFactory.GetPetBuilder()
                      .SetName(message.Name)
                      .SetSpecies(await _store.GetSpeciesById(message.SpeciesType))
                      .SetMorph(message.Morph)
                      .SetOwner(user)
                      .Build();

            var id = await _store.Create(pet);

            var response = new NewEntityResponse <int> {
                Id = id
            };

            outputPort.Handle(response);
            return(true);
        }
        public async Task <bool> Handle(RegisterEnvironmentRequest message, IOutboundPort <NewEntityResponse <Guid> > outputPort)
        {
            var response = new NewEntityResponse <Guid>();
            var user     = await _userStore.GetUserById(message.Owner);

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

            switch (message.CreateMode)
            {
            case RegisterEnvironmentRequest.Mode.Touch:
                // Check if user already has it:
                var env = user?.Environments?.FirstOrDefault(e => e.Id == message.MfgId);

                // check if we have this environment already - and that its associated to user
                // if not, we'll fall through to Create logic where we'll attempt creating it - erroring if already
                // present.
                if (env != null)
                {
                    // This next line should be impossible - its just here to keep linting happy.
                    if (env.Id == null)
                    {
                        throw new Exception("Malformed input on this entity");
                    }

                    // we already have it and its associated to user.
                    // should be same as message.MfgId
                    response.Id = env.Id.Value;
                    break;
                }
                else if (user == null)
                {
                    return(false);                       // nothing to do, this is a mis-use.
                }
                else
                {
                    // Apparently we gotta explicitly mimic fallthrough...
                    goto case RegisterEnvironmentRequest.Mode.Create;
                }

            case RegisterEnvironmentRequest.Mode.Create:
                // Environments need an initial user:
                if (user == null)
                {
                    return(false);
                }

                // check if it exists (blind firing a create call
                // may be a pretty hard crash.)
                var searchResult = await _envStore.GetById(message.MfgId);

                if (searchResult != null)
                {
                    return(false);
                }

                // Compose a new entry entity
                var envEntity = _entityFactory.GetEnvironmentBuilder()
                                .SetId(message.MfgId)
                                .SetDescription(message.Description)
                                .SetModelInfo(message.Model)
                                .Build();

                response.Id = await _envStore.Create(envEntity);

                // Associate to user:
                await _userStore.AddAssociationToEnv(user, envEntity);

                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(message));
            }

            outputPort.Handle(response);
            return(true);
        }