示例#1
0
        public void A_owner_should_be_assignable()
        {
            var  owner = fixture.Create <string>();
            Plan plan  = this.fixture.ConstructPlan();

            plan.AssignOwnership(owner);
            Assert.AreEqual(1, plan.UncommittedChanges().OfType <PlanCollaboratorAdded>().Count(x => x.Name == owner && x.Role == CollaboratorRole.Owner.ToString()));
        }
示例#2
0
        static Bus ConfigureBus()
        {
            var b = new Switchboard();

            // Personal plan
            b.RegisterHandler <CreatePersonalPlan>((c) =>
            {
                var repository = new RepositoryFactory(bus, container).Build <Plan>();
                var plan       = new Plan((PlanId)c.Id, c.Name, c.Description, c.Owner, PlanType.Personal);
                plan.AssignOwnership(c.Owner);
                repository.Save(plan);
            });

            b.RegisterSubscriber <PersonalPlanCreated>((c) =>
            {
                Persist <Plans, PlanDto>(new PlanDto
                {
                    PlanId        = c.Id,
                    Name          = c.Name,
                    Description   = c.Description,
                    Owner         = c.Owner,
                    NumberOfTasks = 0,
                    PlanType      = PlanType.Personal.ToString()
                });
            });



            //
            // Collab plan
            b.RegisterHandler <CreateCollaborativePlan>((c) =>
            {
                var repository = new RepositoryFactory(bus, container).Build <Plan>();
                var plan       = new Plan((PlanId)c.Id, c.Name, c.Description, c.Owner, PlanType.Collaborative);
                plan.AssignOwnership(c.Owner);
                repository.Save(plan);
            });

            b.RegisterSubscriber <CollaborativePlanCreated>((c) =>
            {
                Persist <Plans, PlanDto>(new PlanDto
                {
                    PlanId      = c.Id,
                    Name        = c.Name,
                    Description = c.Description,
                    Owner       = c.Owner, NumberOfTasks = 0,
                    PlanType    = PlanType.Collaborative.ToString()
                });
            });


            //
            // Distributable plan
            b.RegisterHandler <CreateDistributablePlan>((c) =>
            {
                var repository = new RepositoryFactory(bus, container).Build <Plan>();
                var plan       = new Plan((PlanId)c.Id, c.Name, c.Description, c.Owner, PlanType.Distributable);
                plan.AssignOwnership(c.Owner);

                repository.Save(plan);
            });

            b.RegisterSubscriber <DistributablePlanCreated>((c) =>
            {
                Persist <Plans, PlanDto>(new PlanDto
                {
                    PlanId        = c.Id,
                    Name          = c.Name,
                    Description   = c.Description,
                    Owner         = c.Owner,
                    NumberOfTasks = 0,
                    PlanType      = PlanType.Distributable.ToString()
                });
            });



            //
            // Task creation
            b.RegisterHandler <CreateTask>((c) =>
            {
                var repository = new RepositoryFactory(bus, container).Build <Task>();
                var task       = new Task((TaskId)c.TaskId, (PlanId)c.PlanId, c.Name, c.Interval);
                repository.Save(task);
            });

            b.RegisterSubscriber <TaskCreated>((c) =>
            {
                var plan     = FindById <Plans, PlanDto>(c.PlanId);
                var planName = (plan != null) ? plan.Name : "Undefined plan";
                Persist <Tasks, TaskDto>(new TaskDto
                {
                    PlanId      = c.PlanId,
                    TaskTitle   = c.Title,
                    TaskId      = c.Id,
                    Reccurence  = c.Interval.ToString(),
                    Description = "",
                    PlanName    = planName
                });

                // Add number of tasks
                // REVISIT This might nde to be an scalar query.
                Mutate <Plans, PlanDto>(c.PlanId,
                                        (dto) =>
                {
                    dto.NumberOfTasks += 1;
                }
                                        );
            });



            //
            // Task creation
            b.RegisterHandler <MarkTaskCompleted>((c) =>
            {
                var repository = new RepositoryFactory(bus, container).Build <Task>();
                var task       = repository.GetById(c.TaskId);
                task.MarkCompleted(c.CompletedBy, DateTimeOffset.Parse(c.Timestamp));
                repository.Save(task);
            });

            b.RegisterSubscriber <TaskMarkedCompleted>((c) =>
            {
                var task   = FindById <Tasks, TaskDto>(c.Id);
                var planId = (task != null) ? task.PlanId : Guid.Empty;

                Persist <Tasks, CompletedTaskDto>(new CompletedTaskDto
                {
                    CompletedBy   = c.User,
                    LastCompleted = c.Timestamp,
                    PlanId        = planId,
                    TaskId        = c.Id
                });

                // Add number of tasks
                // REVISIT This might nde to be an scalar query.
                Mutate <Tasks, TaskDto>(c.Id,
                                        (dto) =>
                {
                    if (dto.LastCompletion == null ||
                        DateTimeOffset.Parse(dto.LastCompletion) < DateTimeOffset.Parse(c.Timestamp))
                    {
                        dto.LastCompletion = c.Timestamp;
                    }
                }
                                        );
            });


            // Task creation
            b.RegisterHandler <RevokeTaskCompletion>((c) =>
            {
                var repository = new RepositoryFactory(bus, container).Build <Task>();
                var task       = repository.GetById(c.TaskId);
                task.RevokeCompletion(c.RevokedBy, DateTimeOffset.Parse(c.RevocationTimestamp));
                repository.Save(task);
            });



            b.RegisterHandler <AddCollaborator>((c) =>
            {
                var repository = new RepositoryFactory(bus, container).Build <Plan>();
                var plan       = repository.GetById(c.PlanId);
                plan.AddCollaborator(c.Collaborator, (CollaboratorRole)Enum.Parse(typeof(CollaboratorRole), c.Role));
                repository.Save(plan);
            });

            b.RegisterSubscriber <PlanCollaboratorAdded>((c) =>
            {
                Persist <CollaborationRepo, CollaboratorDto>(new CollaboratorDto
                {
                    PlanId         = c.PlanId,
                    Username       = c.Name,
                    Role           = c.Role,
                    DisplayName    = c.Name,
                    CollaboratorId = Guid.NewGuid()
                });
            });

            return(b);
        }