Пример #1
0
        public string GetMostRecentlyOpenedDiagram(string anonymousId)
        {
            using (var db = new CloudDiagramContext())
            {
                var anon = db.AnonymousByToken(anonymousId);
                if (anon == null)
                {
                    return(null);
                }

                var lastVisitedPublicId = anon.User.Visits.OrderByDescending(v => v.Id).Select(v => v.Diagram.PublicId).FirstOrDefault();
                return(lastVisitedPublicId);
            }
        }
Пример #2
0
        public void CreateNewDiagram(string anonymousId, object model)
        {
            using (var db = new CloudDiagramContext())
            {
                var       anon = db.AnonymousByToken(anonymousId);
                User      user;
                Workgroup workgroup;
                if (anon == null)
                {
                    var account = new Account();
                    db.Accounts.Add(account);

                    user = new User
                    {
                        Account = account
                    };
                    db.Users.Add(user);

                    workgroup = new Workgroup
                    {
                        Account = account,
                        Users   = new[] { user }
                    };
                    db.Workgroups.Add(workgroup);

                    anon = new Anonymous {
                        Token = anonymousId, TokenHash = anonymousId.GetHashCode(), User = user
                    };
                    db.Anonymouses.Add(anon);
                }
                else
                {
                    user      = anon.User;
                    workgroup = user.Workgroups.First();
                }

                var diagram = new Diagram
                {
                    CreatedBy = user,
                    PublicId  = db.GeneratePublicId(),
                    Workgroup = workgroup,
                    Content   = XmlMarshal.WriteContext(model)
                };
                db.Diagrams.Add(diagram);

                db.DiagramVisits.Add(new DiagramVisit
                {
                    Diagram   = diagram,
                    VisitedBy = user,
                    When      = DateTime.UtcNow,
                    Version   = 1
                });

                db.DiagramChanges.Add(new DiagramChange
                {
                    Diagram       = diagram,
                    ChangedBy     = user,
                    When          = DateTime.UtcNow,
                    VersionBefore = 0,
                    VersionAfter  = 1
                });
                db.SaveChanges();
            }
        }