Example #1
0
        internal Wiki GetWikiByVerslion(WikiName name, WikiVersion ver)
        {
            Wiki wiki = null;

            using (SqlConnection connection = new SqlConnection())
            {
                connection.ConnectionString = connectionString;

                SqlCommand cmd = connection.CreateCommand();

                cmd.CommandText = "SELECT TOP 1 * FROM [Wiki] WHERE [Name]=@name ORDER BY [Version] DESC";
                cmd.Parameters.AddWithValue("@name", name.Text);

                logger.Info(cmd.CommandText);

                connection.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    wiki = new Wiki();
                    wiki.Name = new WikiName(reader.GetString(1));
                    wiki.Content = new WikiContent(reader.GetString(2));
                    wiki.Version = new WikiVersion(reader.GetInt32(3));
                    wiki.CreatedAt = reader.GetDateTime(4);
                }
            }

            if (wiki == null)
            {

                throw new WikiException("The Wiki not found.");
            }

            return wiki;
        }
Example #2
0
        internal WikiVersion GetLatestVersion(WikiName name)
        {
            if (!this.IsExist(name))
            {
                logger.Debug(String.Format("{0} was not found", name.Text));
                throw new WikiException(String.Format("Wiki {0} was not found", name.Text));
            }

            WikiVersion version = null;

            using (SqlConnection connection = new SqlConnection())
            {
                connection.ConnectionString = connectionString;
                connection.Open();
                SqlCommand cmd = connection.CreateCommand();

                cmd.CommandText = "SELECT TOP 1 * FROM [Wiki] WHERE name=@name ORDER BY version DESC";

                cmd.Parameters.AddWithValue("@name", name.Text);

                logger.Info(cmd.CommandText);

                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    version = new WikiVersion(reader.GetInt32(3));
                }
            }

            return version;
        }
Example #3
0
        public virtual ActionResult Edit(long?id, Wiki wiki, WikiVersion entryversion, string tags, bool isNew)
        {
            bool b = Entry.AddVersion(id, wiki, entryversion, tags, WebUser, isNew);

            if (!b)
            {
                throw new ApplicationException("标题已存在");
            }
            return(RedirectToAction("Index", "Entry", new { url = wiki.Url }));
        }
Example #4
0
        public void WikiVersionEqualsTest()
        {
            WikiVersion v1 = new WikiVersion(1);
            WikiVersion v2 = new WikiVersion(1);
            Assert.IsTrue(v1.Equals(v2));
            Assert.IsTrue(v2.Equals(v1));

            WikiVersion v3 = new WikiVersion(2);
            Assert.IsFalse(v1.Equals(v3));
            Assert.IsFalse(v3.Equals(v1));
        }
Example #5
0
        public bool AddVersion(long?id, Wiki wiki, WikiVersion wikiVersion, string tags, IUser user, bool isNew)
        {
            var isDisplayTitle = wiki.IsTitleDisplay;

            using (var db = DbInstance)
            {
                // wiki.ModifiedTime = DateTime.Now;
                if (id.HasValue)
                {
                    wiki = db.Wikis.Where(c => c.Id == id.Value).FirstOrDefault();
                    //  wiki.EditCount += 1;
                    if (!Equals(wiki.IsTitleDisplay, isDisplayTitle))
                    {
                        wiki.IsTitleDisplay = isDisplayTitle;
                    }
                }
                else
                {
                    var old = db.Wikis.Where(c => c.Url == wiki.Url.Trim()).Count();
                    if (old > 0)
                    {
                        return(false);
                    }
                    //   wiki.EditCount = 1;
                    db.Wikis.Add(wiki);
                    db.SaveChanges();
                }

                if (id.HasValue || isNew)
                {
                    wikiVersion.WikiId    = wiki.Id;
                    wikiVersion.AddTime   = DateTime.Now;
                    wikiVersion.Reference = wikiVersion.Reference ?? "";
                    wikiVersion.Reason    = wikiVersion.Reason ?? "";
                    db.WikiVersions.Add(wikiVersion);
                    db.SaveChanges();
                    //wiki.CurrentVersionId = wikiVersion.Id;
                }
                else
                {
                    var ev = db.WikiVersions.FirstOrDefault(c => c.Id == wikiVersion.Id);
                    ev.Description = wikiVersion.Description;
                    ev.Title       = wikiVersion.Title;
                    ev.AddTime     = DateTime.Now;
                }

                db.SaveChanges();
            }
            return(true);
        }
Example #6
0
        public virtual ActionResult History(long versionId)
        {
            KeyValuePair <Wiki, WikiVersion> t = Entry.GetFromVersion(versionId);

            if (t.Key == null || t.Value == null)
            {
                return(View("wait", "site"));
            }
            Wiki        entry   = t.Key;
            WikiVersion version = t.Value;

            ViewData["version"] = version;
            ViewData["entry"]   = entry;
            ViewData["ext"]     = JsonAdapter.Deserialize <EntryExt>(version.Ext);
            Title = entry.Url;
            return(View());
        }
Example #7
0
 public virtual ActionResult Edit(long?id)
 {
     if (id != null)
     {
         //修改
         Tuple <Wiki, WikiVersion> d = Entry.Get(id.Value);
         Wiki entry = d.Item1;
         if ((entry.Status == (int)WikiStatus.Common || HasManageRight() ||
              new[] { 0, WebUser.UserId }.Contains(entry.CreatorId)))
         {
             ViewData["exists"] = true;
             ViewData["entry"]  = entry;
             ViewData["id"]     = entry.Id;
             WikiVersion ev = d.Item2;
             if (ev == null)
             {
                 ev = new WikiVersion();
             }
             ViewData["entryversion"] = ev; //.Url;
             EntryExt ee = JsonAdapter.Deserialize <EntryExt>(ev.Ext) ?? new EntryExt();
             ViewData["tags"] = string.Join(",", ee.Tags.ToNotNull().ToArray());
             Title            = "编辑词条:" + entry.Url;
         }
         else
         {
             return(View("Wait"));
         }
     }
     else
     {
         if (!string.IsNullOrEmpty(Request.QueryString["url"]))
         {
             ViewData["entry.Url"] = Request.QueryString["url"];
         }
         ViewData["entryversion.reason"] = "创建词条";
         Title = "创建词条";
     }
     return(View());
 }
Example #8
0
 public void InitializeDatabase(SqlServerEntities context)
 {
     if (context.Database.Exists())
     {
         if (context.Wikis.Count() == 0)
         {
             var wiki = new Wiki()
             {
                 CreatorId      = 1,
                 CreatedTime    = DateTime.Now,
                 Ext            = "",
                 IsTitleDisplay = true,
                 Status         = (int)WikiStatus.Common,
                 Url            = "index"
             };
             context.Wikis.Add(wiki);
             context.SaveChanges();
             var version = new WikiVersion
             {
                 WikiId      = wiki.Id,
                 AddTime     = DateTime.Now,
                 Description = "content",
                 Ext         = "",
                 Reason      = "",
                 Reference   = "",
                 Status      = (int)WikiVersionStatus.Common,
                 Title       = "index page",
                 UserId      = 1
             };
             context.WikiVersions.Add(version);
             context.SaveChanges();
         }
     }
     else
     {
     }
 }
Example #9
0
        internal void WriteWiki(Wiki wiki)
        {
            if (this.IsExist(wiki.Name))
            {
                WikiVersion oldVer = this.GetLatestVersion(wiki.Name);

                int nv = int.Parse(oldVer.ToString());

                WikiVersion newVer = new WikiVersion(++nv);

                wiki.Version = newVer;
            }
            else
            {
                wiki.Version = new WikiVersion(1);
            }

            wiki.CreatedAt = DateTime.Now;

            using (SqlConnection connection = new SqlConnection())
            {
                connection.ConnectionString = connectionString;
                connection.Open();

                SqlCommand cmd = connection.CreateCommand();
                cmd.CommandText = "INSERT INTO wiki (Name, Content, Version, CreatedAt) VALUES ( @name, @content, @version, @createdAt)";

                cmd.Parameters.AddWithValue("@name", wiki.Name.Text);
                cmd.Parameters.AddWithValue("@content", wiki.Content.Text);
                cmd.Parameters.AddWithValue("@version", wiki.Version.ToString());
                cmd.Parameters.AddWithValue("@createdAt", wiki.CreatedAt);

                logger.Info(cmd.CommandText);

                int result = cmd.ExecuteNonQuery();

                if (result == 0)
                {
                    throw new WikiException("Write wiki faild");
                }
            }
        }
Example #10
0
        internal List<Wiki> GetWikisByName(WikiName name)
        {
            List<Wiki> list = new List<Wiki>();
            var con = ContextFactory.GetNewContext();
            list = (from wiki in con.Wikis
                    where wiki.Name == name.Text
                    select new Wiki()
                    {
                        Content = new WikiContent(wiki.Content),
                        Name = new WikiName(wiki.Name),
                        Version = new WikiVersion(wiki.Version),
                        CreatedAt = wiki.CreatedAt
                    }).ToList();
            return list;

            using (SqlConnection connection = new SqlConnection())
            {
                connection.ConnectionString = connectionString;

                SqlCommand cmd = connection.CreateCommand();

                cmd.CommandText = "SELECT * FROM [Wiki] WHERE name='" + name.Text + "'";

                logger.Info(cmd.CommandText);

                connection.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                try
                {
                    while (reader.Read())
                    {
                        Wiki wiki = new Wiki();
                        WikiName n = new WikiName(reader.GetString(1));
                        WikiContent c = new WikiContent(reader.GetString(2));
                        WikiVersion v = new WikiVersion(reader.GetInt32(3));
                        wiki.Name = n;
                        wiki.Content = c;
                        wiki.Version = v;
                        wiki.CreatedAt = reader.GetDateTime(4);

                        list.Add(wiki);
                    }

                }
                finally
                {
                    if (!reader.IsClosed)
                    {
                        reader.Close();
                    }
                }

            }

            return list;
        }