Exemplo n.º 1
0
        public IEnumerable <IEvent> HandleCreateCourse(CreateCourse command)
        {
            // Validate the command.
            if (State != null)
            {
                return(new IEvent[] { new Failed("Can't create an existing course.") });
            }
            if (command == null)
            {
                return(new IEvent[] { new Failed("Can handle a null command.") });
            }
            var validationResult = CourseValidator.Validate(command);

            if (!validationResult.IsValid)
            {
                return(new IEvent[] { new Failed(GetFormattedErrorMessage(validationResult)) });
            }

            return(new IEvent[] { new CourseCreated(command) });
        }
Exemplo n.º 2
0
        public IEnumerable <IEvent> HandleUpdateCourse(UpdateCourse command)
        {
            // Validate the command.
            if (State == null)
            {
                return(new IEvent[] { new Failed("Course doesn't exist.") });
            }
            if (Deleted)
            {
                return(new IEvent[] { new Failed("The course was previously deleted.") });
            }
            var validationResult = CourseValidator.Validate(command);

            if (!validationResult.IsValid)
            {
                return(new IEvent[] { new Failed(GetFormattedErrorMessage(validationResult)) });
            }

            // Validate that the command doesn't conflict with the current state.
            if (command.Id != State.Id)
            {
                validationResult.Errors.Add(new ValidationFailure("Id", "Id mismatch."));
            }
            if (command.Holes.Length != State.Holes.Length)
            {
                validationResult.Errors.Add(new ValidationFailure("Holes", "Cannot change the number of holes."));
            }
            if (command.PlayerId != State.PlayerId)
            {
                validationResult.Errors.Add(new ValidationFailure("PlayerId", "Cannot change a course's player."));
            }
            if (!validationResult.IsValid)
            {
                return(new IEvent[] { new Failed(GetFormattedErrorMessage(validationResult)) });
            }

            return(new IEvent[] { new CourseUpdated(command) });
        }