Ejemplo n.º 1
0
        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());
            }
        }
        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());
            }
        }