Пример #1
0
        public async Task <IHttpActionResult> UpdateProject(ProjectRequestViewModel project)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

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

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

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

            db.Entry(pro).State  = EntityState.Modified;
            pro.ProjectName      = project.Name;
            pro.Description      = project.Description;
            pro.Tag              = project.Tag;
            pro.Status           = project.Status;
            pro.LastModifiedById = db.ShqUsers.Where(u => u.IdentityUser.UserName == HttpContext.Current.User.Identity.Name).FirstOrDefault().IdentityUserId;
            pro.LastModfiedTime  = DateTime.Now;

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

            await db.SaveChangesAsync();

            ProjectViewModel result = new ProjectViewModel(pro, db);

            result.Privilege = ShqConstants.AllowProjectUpdate;
            (new AuditsController()).AddAuditEntry("api/Projects/Update", JsonConvert.SerializeObject(result));
            return(Ok(result));
        }
Пример #2
0
        public async Task <IHttpActionResult> AddAudit(Audit audit)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.Entry(audit).State = EntityState.Modified;

            try
            {
                db.Audits.Add(audit);
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AuditExists(audit.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public async Task <IHttpActionResult> AddFTAProjectTree(FTATreeRequestViewModel tree)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var docs = db.FTAProjects.Include("Project").Where(item => item.ProjectId == tree.ProjectId).FirstOrDefault();

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

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

            ShqUser shqUser = await db.ShqUsers.Where(item => item.IdentityUser.UserName == HttpContext.Current.User.Identity.Name).Include("IdentityUser").FirstOrDefaultAsync();

            var tr = docs.FTATrees.Where(item => item.Id == tree.Id).FirstOrDefault();

            if (tr == null)
            {
                FTATree ftaTree = new FTATree()
                {
                    Id = tree.Id, FTAProjectId = docs.Id, FTAProject = docs, AnalysisStatus = 0, Content = tree.Content, CreatedById = shqUser.IdentityUserId, CreatedTime = DateTime.Now, LastModifiedById = shqUser.IdentityUserId, LastModfiedTime = DateTime.Now
                };
                docs.FTATrees.Add(ftaTree);
                await db.SaveChangesAsync();

                (new AuditsController()).AddAuditEntry("api/FTAProjects/AddTree", JsonConvert.SerializeObject(new ProjectViewModel(docs.Project, db)));
                return(Ok(new FTATreeViewModel(ftaTree, db)));
            }
            else
            {
                return(Conflict());
            }
        }
        public async Task <IHttpActionResult> AddFMEAProjectTree(FTATreeRequestViewModel tree)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var docs = db.FMEAProjects.Include("Project").Where(item => item.ProjectId == tree.ProjectId).FirstOrDefault();

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

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

            ShqUser shqUser = await db.ShqUsers.Where(item => item.IdentityUser.UserName == HttpContext.Current.User.Identity.Name).Include("IdentityUser").FirstOrDefaultAsync();

            var tr = docs.FMEATrees.Where(item => item.Id == tree.Id).FirstOrDefault();

            if (tr == null)
            {
                FMEATree FMEATree = new FMEATree()
                {
                    Id = tree.Id, FMEAProjectId = docs.Id, FMEAProject = docs, Content = tree.Content, CreatedById = shqUser.IdentityUserId, CreatedTime = DateTime.Now, LastModifiedById = shqUser.IdentityUserId, LastModfiedTime = DateTime.Now
                };
                docs.FMEATrees.Add(FMEATree);
                await db.SaveChangesAsync();

                return(Ok(new FTATreeRequestViewModel()
                {
                    Id = FMEATree.Id, Content = FMEATree.Content, ProjectId = docs.ProjectId
                }));
            }
            else
            {
                return(Conflict());
            }
        }
Пример #5
0
        public async Task <IHttpActionResult> Add(ShqUserRequestViewModel shqUserView)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (string.IsNullOrEmpty(shqUserView.Password) == true)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.PreconditionFailed, "Password is invalide"));
            }

            if (shqUserView.Roles == null || shqUserView.Roles.Count < 1)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.PreconditionFailed, "User role not specified"));
            }

            if (db.Users.Where(u => u.UserName.ToLower() == shqUserView.EmailAddress.ToLower()).FirstOrDefault() != null)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.PreconditionFailed, "Name is duiplicated"));
            }

            var user = db.Users.Add(new IdentityUser(shqUserView.EmailAddress)
            {
                Email = shqUserView.EmailAddress, EmailConfirmed = true
            });

            foreach (var role in shqUserView.Roles)
            {
                string roleid = db.Roles.First(c => c.Name == role).Id;
                user.Roles.Add(new IdentityUserRole {
                    RoleId = roleid
                });
            }

            var shqUser = shqUserView.ToShqUser();

            shqUser.IdentityUserId   = user.Id;
            shqUser.IdentityUser     = user;
            shqUser.CreatedById      = db.ShqUsers.Where(u => u.IdentityUser.UserName == System.Web.HttpContext.Current.User.Identity.Name).FirstOrDefault().IdentityUserId;
            shqUser.LastModifiedById = shqUser.CreatedById;
            db.ShqUsers.Add(shqUser);

            var store = new ShqUserStore();
            await store.SetPasswordHashAsync(user, new ShqUserManager().PasswordHasher.HashPassword(shqUserView.Password));

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (ShqUserExists(shqUser.IdentityUserId))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(Ok(new ShqUserRespondViewModel(shqUser, db)));
        }
        public async Task <IHttpActionResult> AddFMEAProjectTree(FTATreeRequestViewModel tree)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var docs = db.FMEAProjects.Include("Project").Where(item => item.ProjectId == tree.ProjectId).FirstOrDefault();

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

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

            ShqUser shqUser = await db.ShqUsers.Where(item => item.IdentityUser.UserName == HttpContext.Current.User.Identity.Name).Include("IdentityUser").FirstOrDefaultAsync();

            var tr = docs.FMEATrees.Where(item => item.Id == tree.Id).FirstOrDefault();

            if (tr == null)
            {
                FMEATree FMEATree = new FMEATree()
                {
                    Id = tree.Id, FMEAProjectId = docs.Id, FMEAProject = docs, Content = tree.Content, CreatedById = shqUser.IdentityUserId, CreatedTime = DateTime.Now, LastModifiedById = shqUser.IdentityUserId, LastModfiedTime = DateTime.Now
                };
                docs.FMEATrees.Add(FMEATree);
                await db.SaveChangesAsync();

                dynamic jsonSource = JObject.Parse(FMEATree.Content);
                foreach (var structureNode in jsonSource.structureNodes)
                {
                    foreach (var function in structureNode.FunctionSet)
                    {
                        foreach (var failure in function.FailureSet)
                        {
                            foreach (var property in failure.properties)
                            {
                                string key   = property.key;
                                string value = property.value;
                                if (property.previousValue != value)
                                {
                                    property.previousValue = value;
                                    db.ShqKeywordSets.Add(new ShqKeywordSet {
                                        Keyword = string.Format("{0}^{1}^{2}^{3}", structureNode.name, function.name, failure.name, key), KeywordValue = value
                                    });
                                }
                            }
                        }
                    }
                }

                FMEATree.Content = JsonConvert.SerializeObject(jsonSource);

                await db.SaveChangesAsync();

                (new AuditsController()).AddAuditEntry("api/FMEAProjects/AddTree", JsonConvert.SerializeObject(new ProjectViewModel(docs.Project, db)));
                return(Ok(new FTATreeRequestViewModel()
                {
                    Id = FMEATree.Id, Content = FMEATree.Content, ProjectId = docs.ProjectId
                }));
            }
            else
            {
                return(Conflict());
            }
        }
        public async Task <IHttpActionResult> SyncProjectFiles(Guid projectId)
        {
            var pro = db.Projects.FirstOrDefault(item => item.Id == projectId);

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

            string path = ShqConstants.ProjectRootFolder + "\\" + projectId;

            if (Directory.Exists(path) == false)
            {
                Directory.CreateDirectory(path);
            }

            path = path + "\\Root";
            if (Directory.Exists(path) == false)
            {
                Directory.CreateDirectory(path);
            }

            var wp          = db.WorkProjects.Include("WorkProjectTemplate").FirstOrDefault(item => item.ProjectId == projectId);
            var sourceFiles = wp.WorkProjectTemplate.ProjectFiles.Where(item => item.Status == (int)ShqConstants.FileStatus.Ready && (item.Level & wp.Level) == wp.Level).ToList();

            wp.FilesToCopyNum = sourceFiles.Count();
            wp.FilesCopiedNum = 0;

            string sourceRootFolder = Path.Combine(ShqConstants.TemplateRootFolder, wp.WorkProjectTemplate.Id.ToString()) + "\\Root";
            int    start            = sourceRootFolder.Split('\\').Length;

            foreach (var file in sourceFiles)
            {
                wp.FilesCopiedNum++;

                string[] folers   = file.Path.Split('\\');
                string   tempPath = path;
                int      to       = file.IsFolder == true ? folers.Length : folers.Length - 1;
                for (int i = start; i < to; i++)
                {
                    int id = ShqConstants.GetPathId(folers[i]);
                    var tf = db.ProjectFiles.FirstOrDefault(item => item.Id == id).FileId;

                    var f = db.ProjectFiles.FirstOrDefault(item => item.FileId == tf && item.WorkProjectTemplateId == wp.WorkProjectTemplateId && item.WorkProjectId == wp.Id);
                    if (f == null)
                    {
                        var projectFile = db.ProjectFiles.Add(new ProjectFile
                        {
                            FileId   = file.FileId,
                            Name     = file.Name,
                            Level    = file.Level,
                            IsFolder = file.IsFolder,
                            Path     = tempPath,
                            Status   = (int)ShqConstants.FileStatus.Copying,
                            WorkProjectTemplateId = wp.WorkProjectTemplateId,
                            WorkProjectId         = wp.Id,
                            CreatedById           = db.ShqUsers.Where(u => u.IdentityUser.UserName == HttpContext.Current.User.Identity.Name).FirstOrDefault().IdentityUserId,
                            LastModifiedById      = db.ShqUsers.Where(u => u.IdentityUser.UserName == HttpContext.Current.User.Identity.Name).FirstOrDefault().IdentityUserId
                        });

                        wp.ProjectFiles.Add(projectFile);

                        db.SaveChanges();
                        tempPath = Path.Combine(tempPath, file.Name + "." + projectFile.Id.ToString());
                        if (Directory.Exists(tempPath) == false)
                        {
                            Directory.CreateDirectory(tempPath);
                            projectFile.Path   = tempPath;
                            projectFile.Status = (int)ShqConstants.FileStatus.Ready;
                        }
                    }
                    else
                    {
                        if (f.Status != (int)ShqConstants.FileStatus.Deleted)
                        {
                            tempPath = Path.Combine(tempPath, f.Name + "." + f.Id.ToString());
                            if (Directory.Exists(tempPath) == false)
                            {
                                Directory.CreateDirectory(tempPath);
                                f.Status           = (int)ShqConstants.FileStatus.Ready;
                                f.LastModifiedById = db.ShqUsers.Where(u => u.IdentityUser.UserName == HttpContext.Current.User.Identity.Name).FirstOrDefault().IdentityUserId;
                                f.LastModfiedTime  = DateTime.Now;
                            }
                        }
                    }
                }

                if (file.IsFolder == false)
                {
                    var f = db.ProjectFiles.FirstOrDefault(item => item.FileId == file.FileId && item.WorkProjectTemplateId == wp.WorkProjectTemplateId && item.WorkProjectId == wp.Id);
                    if (f == null)
                    {
                        var projectFile = db.ProjectFiles.Add(new ProjectFile
                        {
                            FileId   = file.FileId,
                            Name     = file.Name,
                            Level    = file.Level,
                            IsFolder = file.IsFolder,
                            Path     = tempPath,
                            WorkProjectTemplateId = wp.WorkProjectTemplateId,
                            WorkProjectId         = wp.Id,
                            Status           = (int)ShqConstants.FileStatus.Copying,
                            CreatedById      = db.ShqUsers.Where(u => u.IdentityUser.UserName == HttpContext.Current.User.Identity.Name).FirstOrDefault().IdentityUserId,
                            LastModifiedById = db.ShqUsers.Where(u => u.IdentityUser.UserName == HttpContext.Current.User.Identity.Name).FirstOrDefault().IdentityUserId
                        });

                        wp.ProjectFiles.Add(projectFile);
                        db.SaveChanges();

                        string targetPath = tempPath + "\\" + projectFile.Name + "." + projectFile.Id;

                        if (File.Exists(targetPath) == false)
                        {
                            File.Copy(file.Path, targetPath);
                            projectFile.Status = (int)ShqConstants.FileStatus.Ready;
                            projectFile.Path   = targetPath;
                        }
                    }
                    else
                    {
                        if (f.Status != (int)ShqConstants.FileStatus.Deleted)
                        {
                            string targetPath = tempPath + "\\" + f.Name + "." + f.Id;

                            if (File.Exists(targetPath) == false)
                            {
                                File.Copy(file.Path, targetPath);
                                f.Status           = (int)ShqConstants.FileStatus.Ready;
                                f.Path             = targetPath;
                                f.LastModifiedById = db.ShqUsers.Where(u => u.IdentityUser.UserName == HttpContext.Current.User.Identity.Name).FirstOrDefault().IdentityUserId;
                                f.LastModfiedTime  = DateTime.Now;
                            }
                        }
                    }
                }
            }

            await db.SaveChangesAsync();

            return(Ok());
        }