Ejemplo n.º 1
0
        public static void Initialize(bool overwrite = false)
        {
            #if DEBUG
            overwrite = true;
            #endif

            using (IDbConnection db = Connect())
            {
                //bool userTableExists = db.TableExists("User");
                bool fullTextSearchExists = db.TableExists("FullTextSearchIssue");
                if (overwrite && fullTextSearchExists)
                {
                    db.ExecuteSql("DROP TABLE FullTextSearchIssue");
                    fullTextSearchExists = false;
                }

                db.CreateTables(overwrite, typeof(User), typeof(Config), typeof(Issue), typeof(IssueComment));
                if (!fullTextSearchExists)
                {
                    db.ExecuteSql("CREATE VIRTUAL TABLE FullTextSearchIssue USING fts4(Title, Text, Comments)");
                }

            #if DEBUG
                Config config = new Config()
                {
                    Areas = new[] { "candle.exe", "light.exe" },
                    Releases = new[] { "v3.7", "v3.8", "v3.x", "v4.x" },
                };
                db.Insert(config);

                var fooUser = UserService.Create("*****@*****.**", "bar.");
                fooUser.FullName = "Foo User";
                fooUser.Role = UserRole.Admin;
                db.Insert(fooUser);
                fooUser.Id = db.GetLastInsertId();

                var barUser = UserService.Create("*****@*****.**", "foo");
                barUser.UserName = "******";
                barUser.FullName = "Bar User";
                db.Insert(barUser);
                barUser.Id = db.GetLastInsertId();

                var bazUser = UserService.Create("*****@*****.**", "foo");
                bazUser.UserName = "******";
                bazUser.FullName = "Baz User";
                bazUser.Role = UserRole.User;
                db.Insert(bazUser);
                bazUser.Id = db.GetLastInsertId();

                var issue = new Issue()
                {
                    AssignedToUserId = fooUser.Id,
                    CreatedByUserId = barUser.Id,
                    CreatedAt = DateTime.UtcNow,
                    Title = "This is the title.",
                    Type = IssueType.Bug,
                    Release = "v3.8",
                    Text = "This is the text of the bug. It is a little longer than the title.",
                    Votes = 4,
                };
                db.Insert(issue);
                issue.Id = db.GetLastInsertId();

                var issueSearch = new FullTextSearchIssue()
                {
                    DocId = issue.Id,
                    Text = issue.Text,
                    Title = issue.Title,
                };
                //db.SqlScalar<int>("INSERT INTO issue_text(docid, title, text) VALUES(@i, @t, @c)",
                //              new { i = db.GetLastInsertId(), t = issue.Title, c = issue.Text });
                db.Insert(issueSearch);

                var issueOld = new Issue()
                {
                    CreatedByUserId = fooUser.Id,
                    CreatedAt = DateTime.UtcNow,
                    Title = "This is the title of old feature.",
                    Type = IssueType.Feature,
                    Release = "v3.7",
                    Text = "This is the text of the feature. It is a little longer than the title and it's for older stuff.",
                    Votes = 1,
                };
                db.Insert(issueOld);
                issueOld.Id = db.GetLastInsertId();

                var issueOldComment = new IssueComment()
                {
                    IssueId = issueOld.Id,
                    CommentByUserId = barUser.Id,
                    CreatedAt = DateTime.UtcNow,
                    Text = "This is the text of the comment. It is a little longer to provide some detail about the feature request.",
                };
                db.Insert(issueOldComment);
                issueOldComment.Id = db.GetLastInsertId();

                var issueOldSearch = new FullTextSearchIssue()
                {
                    DocId = issueOldComment.IssueId,
                    Text = issueOld.Text,
                    Title = issueOld.Title,
                    Comments = issueOldComment.Text,
                };
                //db.SqlScalar<int>("INSERT INTO issue_text(docid, title, text) VALUES(@i, @t, @c)",
                //              new { i = db.GetLastInsertId(), t = issue.Title, c = issue.Text });
                db.Insert(issueOldSearch);
            #endif
            }
        }
Ejemplo n.º 2
0
        public IssueViewModel UpdateIssue(ControllerContext context, Issue issue, long userId, string commentText, Dictionary<string, PopulateResults.UpdatedValue> updates)
        {
            IssueViewModel vm = null;

            IssueComment comment = new IssueComment();
            comment.IssueId = issue.Id;
            comment.CommentByUserId = userId;
            comment.CreatedAt = issue.UpdatedAt;
            comment.Text = commentText;
            foreach (var kvp in updates)
            {
                if (kvp.Key.Equals("UpdatedAt", StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                object oldValue = kvp.Value.FriendlyOld ?? kvp.Value.Old ?? String.Empty;
                object newValue = kvp.Value.FriendlyNew ?? kvp.Value.New ?? String.Empty;

                IssueChange change = new IssueChange();
                change.Column = kvp.Value.FriendlyName ?? kvp.Key;
                change.Old = oldValue.ToString();
                change.New = newValue.ToString();
                comment.Changes.Add(change);
            }

            comment.Changes.Sort();

            using (var db = DataService.Connect())
            using (var tx = db.BeginTransaction())
            {
                // If there is some sort of recognizable change.
                if (!String.IsNullOrEmpty(comment.Text) || comment.Changes.Count > 0)
                {
                    db.UpdateOnly(issue, v => v.Update(updates.Keys.ToArray()).Where(i => i.Id == issue.Id));

                    if (updates.ContainsKey("Text") || updates.ContainsKey("Title"))
                    {
                        db.Update<FullTextSearchIssue>(new { Text = issue.Text, Title = issue.Title }, s => s.DocId == issue.Id);
                    }

                    db.Insert(comment);
                    comment.Id = db.GetLastInsertId();
                }

                if (QueryService.TryGetIssueWithCommentsUsingDb(issue.Id, db, out vm))
                {
                    vm.Location = context.ApplicationPath + vm.Id + "/";
                    var breadcrumbs = new BreadcrumbsViewModel(new Breadcrumb("Issues", context.ApplicationPath), new Breadcrumb("#" + vm.Id + " - " + vm.Title, vm.Location));

                    FileService.WriteIssue(vm, breadcrumbs);
                    tx.Commit();

                    // best effort email about changes to issue.
                    try
                    {
                        MailService.SendIssueComment(vm, comment.Id);
                    }
                    catch (Exception e)
                    {
                        LogManager.GetLogger("error").Error(String.Format("  failed to send update about issue #{0}, comment #{1}", issue.Id, comment.Id), e);
                    }
                }
            }

            return vm;
        }