Пример #1
0
        public async Task <IHttpActionResult> AddLinkedProject(Guid linkedProjectId, Guid workProjectId)
        {
            WorkProject workProject = await db.WorkProjects.Include("Project").FirstOrDefaultAsync(item => item.ProjectId == workProjectId);

            if (workProject == null)
            {
                return(NotFound());
            }

            if (ProjectHelper.HasUpdateAccess(workProject.Project) == false)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "No Access"));
            }

            Project project = await db.Projects.FirstOrDefaultAsync(item => item.Id == linkedProjectId);

            if (project == null)
            {
                return(NotFound());
            }
            if (ProjectHelper.HasUpdateAccess(project) == false)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "No Access"));
            }

            var fta = db.FTAProjects.FirstOrDefault(item => item.ProjectId == linkedProjectId);

            if (fta != null)
            {
                if (workProject.FTAProjects.FirstOrDefault(item => item.ProjectId == linkedProjectId) == null)
                {
                    workProject.FTAProjects.Add(fta);
                }
            }
            else
            {
                var fmea = db.FMEAProjects.FirstOrDefault(item => item.ProjectId == linkedProjectId);
                if (fmea != null)
                {
                    if (workProject.FMEAProjects.FirstOrDefault(item => item.ProjectId == linkedProjectId) == null)
                    {
                        workProject.FMEAProjects.Add(fmea);
                    }
                }
            }

            db.SaveChanges();

            var model = new WorkProjectViewModel(workProject, db);
            ProjectFilesController pfc = new ProjectFilesController();
            var obj = (OkNegotiatedContentResult <ProjectFolderViewModel>)pfc.GetProjectFiles(workProjectId, "Root", System.IO.SearchOption.AllDirectories).Result;

            model.ProjectFiles = obj.Content;
            return(Ok(model));
        }
Пример #2
0
        public async Task <IHttpActionResult> GetWorkProject(Guid projectId)
        {
            WorkProject workProject = await db.WorkProjects.Include("Project").FirstOrDefaultAsync(item => item.ProjectId == projectId);

            if (workProject == null)
            {
                return(NotFound());
            }

            if (ProjectHelper.HasReadAccess(workProject.Project) == false)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "No Access"));
            }
            var model = new WorkProjectViewModel(workProject, db);
            ProjectFilesController pfc = new ProjectFilesController();
            var obj = (OkNegotiatedContentResult <ProjectFolderViewModel>)pfc.GetProjectFiles(projectId, "Root", System.IO.SearchOption.AllDirectories).Result;

            model.ProjectFiles = obj.Content;
            return(Ok(model));
        }
Пример #3
0
        public async Task <IHttpActionResult> AddProject(ProjectRequestViewModel projectView)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Project pro = await db.Projects.FindAsync(projectView.Id);

            if (pro != null)
            {
                return(Ok("已存在"));
            }

            pro = await db.Projects.FirstOrDefaultAsync(item => item.ProjectName == projectView.Name);

            if (pro != null)
            {
                return(Ok("已存在"));
            }

            Project project = projectView.ToProject();

            project.CreatedById      = db.ShqUsers.Where(u => u.IdentityUser.UserName == HttpContext.Current.User.Identity.Name).FirstOrDefault().IdentityUserId;
            project.LastModifiedById = project.CreatedById;

            db.Projects.Add(project);

            if (project.Type == "FTAProject")
            {
                db.FTAProjects.Add(new FTAProject()
                {
                    Id = Guid.NewGuid(), ProjectId = projectView.Id, CreatedById = project.CreatedById, LastModifiedById = project.LastModifiedById
                });
            }

            if (project.Type == "FMEAProject")
            {
                db.FMEAProjects.Add(new FMEAProject()
                {
                    Id = Guid.NewGuid(), ProjectId = projectView.Id, CreatedById = project.CreatedById, LastModifiedById = project.LastModifiedById
                });
            }

            if (project.Type == "WorkProject")
            {
                var wp = db.WorkProjects.Add(new WorkProject()
                {
                    Id = Guid.NewGuid(), ProjectId = projectView.Id, Level = projectView.Level, CreatedById = project.CreatedById, LastModifiedById = project.LastModifiedById, WorkProjectTemplateId = ShqConstants.DefaultWorkProjectTemplateId
                });
                db.SaveChanges();
                try
                {
                    ProjectFilesController pfc = new ProjectFilesController();
                    await pfc.SyncProjectFiles(wp.ProjectId);
                }
                catch (Exception)
                {
                }
            }

            int i = 0;

            if (projectView.UsersPrivileges.Count > 0)
            {
                i++;
                foreach (var item in projectView.UsersPrivileges)
                {
                    var newAccess = new ProjectShqUsers()
                    {
                        ProjectId        = project.Id,
                        ShqUserId        = db.ShqUsers.Where(u => u.EmailAddress == item.EmailAddress).FirstOrDefault().IdentityUserId,
                        Privilege        = item.Privilege,
                        CreatedById      = project.CreatedById,
                        LastModifiedById = project.CreatedById
                    };
                    newAccess.LastModifiedById = newAccess.CreatedById;
                    db.ProjectShqUsers.Add(newAccess);
                }
            }

            await db.SaveChangesAsync();

            ProjectViewModel result = new ProjectViewModel(project, db);

            result.Description = i.ToString();
            result.Privilege   = ShqConstants.AllowProjectUpdate;

            (new AuditsController()).AddAuditEntry("api/Projects/Add", JsonConvert.SerializeObject(result));
            return(Ok(result));
        }