Esempio n. 1
0
        public ProjectModule(IDispatcherFactory dispatcherFactory,
            IProjectRepository projectRepository, IMappingEngine mappingEngine)
        {
            Post["/projects/{id}/run"] =
                p =>
                {
                    Guid id = Guid.Parse((string) p.id);
                    var command = new QueueProject(id);
                    dispatcherFactory.Create(this.UserLoginSession(), command)
                        .Dispatch(this.UserLoginSession(), command);
                    return null;
                };

            Get["/projects"] =
                _ =>
                {
                    User user = this.UserLoginSession().User;
                    IEnumerable<Project> projects = projectRepository.GetForUser(user);
                    IEnumerable<ProjectListingResponse> projectListingResponses =
                        mappingEngine.Map<IEnumerable<Project>, IEnumerable<ProjectListingResponse>>(projects);
                    return new ProjectListResponse(projectListingResponses);
                };

            Get["/projects/{projectId}"] =
                p =>
                {
                    Guid projectId = Guid.Parse((string) p.projectId);
                    Guid userId = this.UserLoginSession().User.Id;
                    Project project = projectRepository.GetById(projectId);
                    ProjectDetailsResponse response = mappingEngine.Map<Project, ProjectDetailsResponse>(project);
                    if (response == null) throw new ItemNotFoundException<Project>(projectId);
                    return response;
                };

            Get["/projects/{id}/backup"] =
                p =>
                {
                    throw new NotImplementedException();
                    //Guid id = Guid.Parse((string) p.id);
                    //Project project = projectRepository.GetById(id);

                    //if (project.Collaborators.All(x => x.UserId != this.UserLoginSession().User.Id))
                    //{
                    //    throw new UnauthorizedAccessException("That's not your project!");
                    //}

                    //if (project == null) throw new ItemNotFoundException<Project>(id);

                    //var projectBackupResponse = new ProjectBackupResponse
                    //                            {
                    //                                Name = project.Name,
                    //                                Id = project.Id,
                    //                                History = GetFilteredEventd(project)
                    //                            };
                    //return projectBackupResponse;
                };

            Post["/projects/restore"] =
                p =>
                {
                    throw new NotImplementedException();
                    //string body = Request.Body.AsString();
                    //var projectBackupResponse = JsonConvert.DeserializeObject<ProjectBackupResponse>(body,
                    //    new JsonSerializerSettings {TypeNameHandling = TypeNameHandling.Objects});

                    //var command = new RestoreProject(projectBackupResponse.History);
                    //dispatcherFactory.Create(this.UserLoginSession(), command)
                    //    .Dispatch(this.UserLoginSession(), command);
                    //return null;
                };

            Post["/projects"] =
                _ =>
                {
                    var projectData = this.Bind<NewProjectRequest>();
                    var command = new CreateNewProject(Guid.NewGuid(), projectData.Name);
                    dispatcherFactory.Create(this.UserLoginSession(), command)
                        .Dispatch(this.UserLoginSession(), command);
                    return null;
                };

            Put["/projects/{projectId}"] =
                p =>
                {
                    Guid projectId = Guid.Parse((string) p.projectId);
                    var projectData = this.Bind<ChangeNameRequest>();
                    var command = new ChangeProjectName(projectId, projectData.Name);
                    dispatcherFactory.Create(this.UserLoginSession(), command)
                        .Dispatch(this.UserLoginSession(), command);
                    return null;
                };

            Delete["/projects/{id}"] =
                p =>
                {
                    Guid projectId = Guid.Parse((string) p.id);
                    var command = new DeleteProject(projectId);
                    dispatcherFactory.Create(this.UserLoginSession(), command)
                        .Dispatch(this.UserLoginSession(), command);
                    return null;
                };
        }