public ActionResult PutState(string uid, [FromBody] StateRequest req)
        {
            var userUid = _service.GetUid(HttpContext.User.Identity as ClaimsIdentity);

            if (!hasPermission(userUid, uid))
            {
                return(Unauthorized(new { message = "User is not authorized" }));
            }

            var proj = _context.Project.Where(p => p.Uid == uid).FirstOrDefault <Project>();

            if (proj == null)
            {
                return(BadRequest(new { message = "Invalid Project." }));
            }

            ProjectStimulus stim = JsonConvert.DeserializeObject <ProjectStimulus>(proj.Stimulus);

            if (stim.state.version != req.version)
            {
                HttpContext.Response.StatusCode = 403;
                return(Json(stim.state));
            }

            stim.state = req;
            stim.state.version++;
            proj.Stimulus = JsonConvert.SerializeObject(stim);
            _context.SaveChanges();
            return(Ok(stim.state));
        }
        public ActionResult GetState(string uid)
        {
            var userUid = _service.GetUid(HttpContext.User.Identity as ClaimsIdentity);

            if (!hasPermission(userUid, uid))
            {
                return(Unauthorized(new { message = "User is not authorized" }));
            }

            var proj = _context.Project.Where(p => p.Uid == uid).FirstOrDefault <Project>();

            if (proj == null)
            {
                return(BadRequest(new { message = "Invalid Project." }));
            }

            ProjectStimulus stim = JsonConvert.DeserializeObject <ProjectStimulus>(proj.Stimulus);

            return(Ok(stim.state));
        }
        public ActionResult CreateProject([FromBody] CreateProjectRequest req)
        {
            var uid  = _service.GetUid(HttpContext.User.Identity as ClaimsIdentity);
            var user = _context.User.Where(u => u.Uid == uid).FirstOrDefault <User>();

            Project proj = new Project();

            proj.Title       = req.Title;
            proj.Uid         = Guid.NewGuid().ToString();
            proj.Description = req.Description;
            proj.Definition  = req.Definition;
            proj.DateCreated = DateTime.Now;
            proj.OwnerId     = user.Id;

            var stimulus = new ProjectStimulus();

            stimulus.state = new StateRequest();

            stimulus.state.state = new State();

            List <string> rootSubKeys = new List <string>();

            for (int index = 1; index <= req.InitStimulus.Count; index++)
            {
                rootSubKeys.Add("init" + index);
            }

            stimulus.state.state.rootTopicKey = "root";
            stimulus.state.state.topics       = new List <object>()
            {
                new {
                    key      = "root",
                    subKeys  = rootSubKeys,
                    collapse = false,
                    style    = "{\"background\":\"#e2e2e2\"}",
                    blocks   = new List <object>()
                    {
                        new {
                            type = "CONTENT",
                            data = req.ProblemStatement.Content
                        }
                    }
                },
            };
            stimulus.state.state.config = new
            {
                readOnly  = false,
                allowUndo = true,
                layoutDir = 2,
                theme     = new
                {
                    name           = "theme2",
                    randomColor    = false,
                    background     = "#f8f8f8",
                    highlightColor = "#50C9CE",
                    marginH        = 45,
                    marginV        = 10,
                    fontFamily     = "",
                    bold           = false,
                    italic         = false,
                    textAlign      = "left",
                    rootTopic      = new
                    {
                        background   = "#50C18A",
                        color        = "#fff",
                        fontSize     = "34px",
                        borderRadius = "5px",
                        padding      = "16px 18px 16px 18px",
                        linkStyle    = new
                        {
                            lineType  = "curve",
                            lineWidth = 2,
                            lineColor = "rgb(113, 203, 45)"
                        }
                    },
                    primaryTopic = new
                    {
                        background   = "#ffffff",
                        borderWidth  = "1px",
                        borderStyle  = "solid",
                        borderColor  = "rgb(221, 170, 68)",
                        borderRadius = "5px",
                        color        = "rgb(103,103,103)",
                        fontSize     = "14px",
                        padding      = "6px 10px 5px 10px",
                        linkStyle    = new
                        {
                            lineType  = "curve",
                            lineWidth = 2,
                            lineColor = "#43a9ff"
                        }
                    },
                    normalTopic = new
                    {
                        background   = "#fff",
                        borderRadius = "5px",
                        color        = "rgb(103,103,103)",
                        fontSize     = "13px",
                        padding      = "3px 9px 4px",
                        boxShadow    = "1px 1px 1px #ccc",
                        linkStyle    = new
                        {
                            lineType   = "round",
                            lineRadius = 5,
                            lineWidth  = 1,
                            lineColor  = "#43a9ff"
                        }
                    }
                }
            };
            stimulus.related       = new List <StateItemRequest>();
            stimulus.unrelated     = new List <StateItemRequest>();
            stimulus.state.version = 0;

            int i = 1;

            foreach (StimulusRequest stim in req.InitStimulus)
            {
                string key = "init" + i;

                string desc = stim.Description;
                if (stim.Link != null)
                {
                    desc += " " + stim.Link.HrefName + ": (" + stim.Link.Href + ")";
                }

                var init = new
                {
                    key       = key,
                    parentKey = "root",
                    subKeys   = new List <string>(),
                    collapse  = false,
                    style     = "{\"borderColor\":\"#d67416\"}",
                    blocks    = new List <object>()
                    {
                        new {
                            type = "CONTENT",
                            data = stim.Content
                        },
                        new {
                            type = "DESC",
                            data = desc
                        }
                    }
                };

                stimulus.state.state.topics.Add(init);
                i++;
            }

            foreach (StimulusRequest stim in req.RelatedStimulus)
            {
                var related = new StateItemRequest();
                related.content = stim.Content;
                related.desc    = stim.Description;
                if (stim.Link != null)
                {
                    related.desc += stim.Link.HrefName + ": (" + stim.Link.Href + ")";
                }
                stimulus.related.Add(related);
            }

            foreach (StimulusRequest stim in req.UnrelatedStimulus)
            {
                var unrelated = new StateItemRequest();
                unrelated.content = stim.Content;
                unrelated.desc    = stim.Description;
                if (stim.Link != null)
                {
                    unrelated.desc += " " + stim.Link.HrefName + ": (" + stim.Link.Href + ")";
                }
                stimulus.related.Add(unrelated);
            }

            proj.Stimulus = JsonConvert.SerializeObject(stimulus);

            _context.Project.Add(proj);
            _context.SaveChanges();


            List <ProjectParameters> parameters = new List <ProjectParameters>();

            foreach (ProjectParametersRequest param in req.Exclusions)
            {
                ProjectParameters p = new ProjectParameters();

                if (param.Link != null)
                {
                    Links l = new Links();
                    l.Href = param.Link.Href;
                    l.Name = param.Link.HrefName;
                    l.Uid  = Guid.NewGuid().ToString();
                    _context.Links.Add(l);
                    _context.SaveChanges();
                    p.LinkId = l.Id;
                }
                p.Uid       = Guid.NewGuid().ToString();
                p.Content   = param.Content;
                p.Type      = "e";
                p.ProjectId = proj.Id;
                parameters.Add(p);
            }

            foreach (ProjectParametersRequest param in req.AreasOfResearch)
            {
                ProjectParameters p = new ProjectParameters();

                if (param.Link != null)
                {
                    Links l = new Links();
                    l.Href = param.Link.Href;
                    l.Name = param.Link.HrefName;
                    l.Uid  = Guid.NewGuid().ToString();
                    _context.Links.Add(l);
                    _context.SaveChanges();
                    p.LinkId = l.Id;
                }
                p.Uid       = Guid.NewGuid().ToString();
                p.Content   = param.Content;
                p.Type      = "a";
                p.ProjectId = proj.Id;
                parameters.Add(p);
            }
            _context.ProjectParameters.AddRange(parameters);
            _context.SaveChanges();

            return(Ok(proj));
        }