Example #1
0
        public void Edit(EndUserId endUserId, string?name, RobotApplication?application)
        {
            var latestRegistration = Registrations.LastOrDefault();

            if (latestRegistration == null)
            {
                throw new ValidationException("Robot is not registered");
            }

            latestRegistration.Apply(
                registration =>
            {
                if (registration.EndUserId != endUserId)
                {
                    throw new ValidationException("Robot is registered to somebody else");
                }

                var e = RobotEdited.Create(name, application);

                Apply(e);
                Append(Guid.NewGuid(), RobotEdited.EventType, e);
            },
                unregistration => throw new ValidationException("Robot is not registered")
                );
        }
Example #2
0
        public void Register(EndUserId endUserId, string?name, RobotApplication?application)
        {
            void AddRegistration()
            {
                var registrationId = new RobotRegistrationId(Guid.NewGuid());
                var e = RobotRegistered.Create(registrationId, endUserId, name, application);

                Apply(e);
                Append(registrationId.Value, RobotRegistered.EventType, e);
            }

            var latestRegistration = Registrations.LastOrDefault();

            if (latestRegistration == null)
            {
                AddRegistration();
            }
            else
            {
                latestRegistration.Apply(
                    registration => throw new ValidationException("Robot is already registered"),
                    unregistration => AddRegistration()
                    );
            }
        }
Example #3
0
        public void Unregister(EndUserId endUserId)
        {
            var latestRegistration = Registrations.LastOrDefault();

            if (latestRegistration == null)
            {
                throw new ValidationException("Robot is not registered");
            }

            latestRegistration.Apply(
                registration =>
            {
                if (registration.EndUserId == endUserId)
                {
                    var e = RobotUnregistered.Create();
                    Apply(e);
                    Append(Guid.NewGuid(), RobotUnregistered.EventType, e);
                }
                else
                {
                    throw new ValidationException("Robot is registered to somebody else");
                }
            },
                unregistration => throw new ValidationException("Robot is not registered")
                );
        }
Example #4
0
        private void Apply(RobotEdited e)
        {
            var latestRegistration = Registrations.LastOrDefault();

            if (latestRegistration == null)
            {
                throw new InvalidOperationException("Robot is not registered");
            }

            latestRegistration.Apply(
                registration => registration.Apply(this, e),
                unregistration => throw new InvalidOperationException("Robot is not registered")
                );
        }
Example #5
0
        private void Apply(RobotUnregistered e)
        {
            var latestRegistration = Registrations.LastOrDefault();

            if (latestRegistration == null)
            {
                throw new InvalidOperationException("Robot is not already registered");
            }

            latestRegistration.Apply(
                registration => { Registrations = Registrations.Add(RobotRegistration.Unregistration.Instance); },
                unregistration => throw new InvalidOperationException("Robot is not already registered")
                );
        }
Example #6
0
        private void ImportRegistrations(RobotImported e)
        {
            void AddRegistration(Guid endUserId)
            {
                var newRegistration = new RobotRegistration.Registration(this, e, endUserId);

                Registrations = Registrations.Add(newRegistration);
            }

            var latestRegistration = Registrations.LastOrDefault();

            if (latestRegistration == null)
            {
                if (e.Entity.C2RurEnduserValue != null)
                {
                    AddRegistration(e.Entity.C2RurEnduserValue.Value);
                }
            }
            else
            {
                latestRegistration.Apply(
                    registration =>
                {
                    if (registration.EndUserId.Value == e.Entity.C2RurEnduserValue)
                    {
                        registration.Apply(e);
                    }
                    else if (e.Entity.C2RurEnduserValue.HasValue)
                    {
                        AddRegistration(e.Entity.C2RurEnduserValue.Value);
                    }
                    else
                    {
                        Registrations = Registrations.Add(RobotRegistration.Unregistration.Instance);
                    }
                },
                    unregistration =>
                {
                    if (e.Entity.C2RurEnduserValue.HasValue)
                    {
                        AddRegistration(e.Entity.C2RurEnduserValue.Value);
                    }
                }
                    );
            }
        }
Example #7
0
        private void Apply(RobotRegistered e)
        {
            void AddRegistration()
            {
                var registration = new RobotRegistration.Registration(this, e);

                Registrations = Registrations.Add(registration);
            }

            var latestRegistration = Registrations.LastOrDefault();

            if (latestRegistration == null)
            {
                AddRegistration();
            }
            else
            {
                latestRegistration.Apply(
                    registration => throw new InvalidOperationException("Robot is already registered"),
                    unregistration => AddRegistration()
                    );
            }
        }