public void Update(DalArticle entity)
        {
            //context.Set<Article>().AddOrUpdate(entity.GetORMEntity());
            var article = context.Set <Article>().Where(a => a.Id == entity.Id).FirstOrDefault();

            context.Set <Article>().Attach(article);
            //if (article != null)
            //{
            //context.Set<Article>().AddOrUpdate();
            if (entity.Title != null)
            {
                article.Title = entity.Title;
            }
            if (entity.Text != null)
            {
                article.Text = entity.Text;
            }
            if (entity.PublicationDate != null)
            {
                article.PublicationDate = entity.PublicationDate;
            }
            if (entity.CountLikes != null)
            {
                article.CountLikes = entity.CountLikes;
            }


            //article = entity.GetORMEntity();
            //   context.Entry(article).State = EntityState.Modified;
            context.SaveChanges();
            //}
        }
Example #2
0
        /// <summary>
        /// Removing article
        /// </summary>
        /// <param name="entity">article for removing</param>
        public void Delete(DalArticle entity)
        {
            var article = context.Set <Article>().Where(a => a.Id == entity.Id).FirstOrDefault();

            if (article != null)
            {
                context.Set <Article>().Remove(article);
            }
            var tagArticle = context.Set <TagArticle>().Where(ta => ta.ArticleId == article.Id);

            if (tagArticle != null)
            {
                foreach (var entry in tagArticle)
                {
                    context.Set <TagArticle>().Remove(entry);
                }
            }
            var comments = context.Set <Comment>().Where(a => a.ArticleId == entity.Id);

            if (comments != null)
            {
                foreach (var comment in comments)
                {
                    context.Set <Comment>().Remove(comment);
                }
            }

            context.SaveChanges();
        }
Example #3
0
        /// <summary>
        /// Updates article entity with specified tag string
        /// </summary>
        /// <param name="entity">Base entity for updating</param>
        /// <param name="tags">String that contains tags</param>
        public void Update(DalArticle entity, string tags = null)
        {
            var article = context.Set <Article>().Where(a => a.Id == entity.Id).FirstOrDefault();

            context.Set <Article>().Attach(article);

            if (entity.Title != null)
            {
                article.Title = entity.Title;
            }
            if (entity.Content != null)
            {
                article.Content = entity.Content;
            }
            if (entity.PublicationDate != null)
            {
                article.PublicationDate = entity.PublicationDate;
            }
            article.Tags.Clear();
            article.Rating = entity.Rating;

            if (tags != null)
            {
                article.Tags = new List <Tag>();
                FromTagStringToTaggs(article, tags);
            }
        }
Example #4
0
 public Article GetArticleEntity(int id)
 {
     using (var connection = ProcessConnection.OpenGungnirReadOnly)
     {
         var dal = new DalArticle();
         return(DalArticle.GetArticleEntity(connection, id));
     }
 }
        public void Delete(DalArticle e)
        {
            var article = _context.Articles.FirstOrDefault(x => x.Id == e.Id);

            if (article != null)
            {
                _context.Articles.Remove(article);
            }
        }
        /// <summary>
        /// Remove the article from the database.
        /// </summary>
        /// <param name="article"></param>
        public void Delete(DalArticle article)
        {
            Article ormArticle = context.Set <Article>().Find(article.Id);

            if (ormArticle != null)
            {
                context.Set <Article>().Remove(ormArticle);
            }
        }
        public List <SeekHotWord> GetSeekKeyWord(string sqlStr, int pageSize, int pageIndex, out int recordCount)
        {
            string strConn = ConfigurationManager.ConnectionStrings["Aliyun"].ConnectionString;

            strConn = SecurityHelp.IsBase64Formatted(strConn) ? SecurityHelp.DecryptAES(strConn) : strConn;
            SqlConnection conn = new SqlConnection(strConn);

            return(DalArticle.GetSeekKeyWord(conn, sqlStr, pageSize, pageIndex, out recordCount));
        }
Example #8
0
        /// <summary>
        /// Deletes article entity
        /// </summary>
        /// <param name="entity">Base entity for removing</param>
        public void Delete(DalArticle e)
        {
            var article = context.Set <Article>().Where(a => a.Id == e.Id).Include(a => a.Tags).FirstOrDefault();

            if (article != null)
            {
                context.Set <Article>().Remove(article);
            }
            //context.Set<Article>().Remove(e.ToOrmArticle());
        }
        public void Add(Article article, out string contentUrl, out int id, string locationAddress)
        {
            string _contentUrl            = "";
            int    _id                    = 0;
            Action <SqlConnection> action = (connection) => DalArticle.Add(connection, article, out _contentUrl, out _id, locationAddress);

            dbManager.Execute(action);
            contentUrl = _contentUrl;
            id         = _id;
        }
        public void Delete(DalArticle e)
        {
            var article = context.Set <Article>().Where(a => a.Id == e.Id).FirstOrDefault();

            if (article != null)
            {
                context.Set <Article>().Remove(article);
            }
            context.SaveChanges();
        }
        public void Update(DalArticle entity)
        {
            var article = _context.Articles.FirstOrDefault(x => x.Id == entity.Id);

            if (article != null)
            {
                article.Title      = entity.Title;
                article.Text       = entity.Text;
                article.TitleImage = entity.TitleImage;
            }
        }
Example #12
0
        /// <summary>
        /// Creates new article entity with specified tag string
        /// </summary>
        /// <param name="e">Base entity for new article entity</param>
        /// <param name="tags">String that contains tags</param>
        public void Create(DalArticle e, string tags = null)
        {
            var article = e.ToOrmArticle();

            if (tags != null)
            {
                article.Tags = new List <Tag>();
                FromTagStringToTaggs(article, tags);
            }

            context.Set <Article>().Add(article);
        }
 public void Add(Article article)
 {
     try
     {
         Action <SqlConnection> action = (connection) => DalArticle.Add(connection, article);
         dbManager.Execute(action);
         logger.Log(Level.Info, "Add:" + JsonHelper.Serialize <Article>(article), null);
     }
     catch (Exception ex)
     {
         logger.Log(Level.Error, "Add 错误:" + ex.ToString() + "参数:" + JsonHelper.Serialize <Article>(article), null);
     }
 }
Example #14
0
 public static ArticleEntity ToBllArticle(this DalArticle article)
 {
     return(new ArticleEntity
     {
         Id = article.Id,
         Title = article.Title,
         CreationTime = article.CreationTime,
         Text = article.Text,
         TitleImage = article.TitleImage,
         UserId = article.UserId,
         BlogId = article.BlogId
     });
 }
        /// <summary>
        /// Change properties for specified article and set new tags.
        /// </summary>
        /// <param name="article"></param>
        /// <param name="tags"></param>
        public void Edit(DalArticle article, string[] tags)
        {
            if (article == null)
            {
                throw new ArgumentNullException(nameof(article));
            }
            if (tags == null)
            {
                tags = new string[0];
            }
            Article ormArticle = context.Set <Article>().Find(article.Id);

            if (article.Content != null)
            {
                ormArticle.Content = article.Content;
            }
            if (article.Title != null)
            {
                ormArticle.Title = article.Title;
            }
            DbSet <Tag> tagSet = context.Set <Tag>();

            foreach (Tag tag in ormArticle.Tags)
            {
                //TODO optimize here
                //tag.Articles.Remove(ormArticle);
                var t = tag.Articles.ToList();
                t.Remove(ormArticle);
                tag.Articles = t;
            }
            ormArticle.Tags = new List <Tag>();

            foreach (string tag in tags)
            {
                Tag ormTag = tagSet.FirstOrDefault(x => x.Value == tag);
                if (ormTag == null)
                {
                    ormTag = new Tag()
                    {
                        Value = tag
                    };
                    tagSet.Add(ormTag);
                }
                //TODO optimize here
                var list = ormTag.Articles.ToList();
                list.Add(ormArticle);
                ormTag.Articles = list;
                //tagSet.Add(ormTag);
                ormArticle.Tags.Add(ormTag);
            }
        }
 public static BllArticle ToBllArticle(this DalArticle article)
 {
     return(new BllArticle()
     {
         ArticleId = article.Id,
         CreationDate = article.CreationDate,
         Tag1 = article.Tag1,
         Tag2 = article.Tag2,
         Tag3 = article.Tag3,
         Title = article.Title,
         Text = article.Text,
         Author = article.Author?.ToBllUser()
     });
 }
 public static Article ToOrmArticle(this DalArticle article)
 {
     if (article == null)
     {
         throw new ArgumentNullException(nameof(article));
     }
     return(new Article()
     {
         AuthorId = article.AuthorId,
         Content = article.Content,
         CreationDateTime = article.CreationDateTime,
         Id = article.Id,
         Title = article.Title,
     });
 }
Example #18
0
        /// <summary>
        /// Adding article to database
        /// </summary>
        /// <param name="entity"></param>
        public void Create(DalArticle entity)
        {
            var article = new Article()
            {
                Id              = entity.Id,
                Title           = entity.Title,
                Content         = entity.Content,
                CountLikes      = entity.CountLikes,
                CountShows      = entity.CountShows,
                BloggerId       = entity.BloggerId,
                DatePublication = entity.DatePublication,
                SectionId       = entity.SectionId
            };

            context.Set <Article>().Add(article);
            context.SaveChanges();
        }
Example #19
0
        /// <summary>
        /// Update information about article
        /// </summary>
        /// <param name="entity">article to update</param>
        public void Update(DalArticle entity)
        {
            var article = context.Set <Article>().Where(a => a.Id == entity.Id).FirstOrDefault();

            if (article != null)
            {
                article.Id              = entity.Id;
                article.Title           = entity.Title;
                article.Content         = entity.Content;
                article.CountLikes      = entity.CountLikes;
                article.CountShows      = entity.CountShows;
                article.BloggerId       = entity.BloggerId;
                article.DatePublication = entity.DatePublication;
                article.SectionId       = entity.SectionId;
                context.SaveChanges();
            }
        }
Example #20
0
 public static ArticleEntity ToBllArticle(this DalArticle dalArticle)
 {
     if (dalArticle != null)
     {
         return(new ArticleEntity()
         {
             Id = dalArticle.Id,
             Title = dalArticle.Title,
             Content = dalArticle.Content,
             CountLikes = dalArticle.CountLikes,
             CountShows = dalArticle.CountShows,
             BloggerId = dalArticle.BloggerId,
             DatePublication = dalArticle.DatePublication,
             SectionId = dalArticle.SectionId
         });
     }
     return(null);
 }
        public bool EditCategory(List <ArticleCategory> categoryModelList)
        {
            StringBuilder strSql = new StringBuilder();
            Dictionary <string, object> dicParams = new Dictionary <string, object>();

            for (int i = 0; i < categoryModelList.Count; i++)
            {
                var data = categoryModelList[i];
                //判断ID是否为空(为空则Add 反之则Update)
                if (!string.IsNullOrEmpty(data.id.ToString()) && data.id > 0)
                {
                    string sqlSet   = string.Format(" CategoryName = @CategoryName_{0},Sort = @Sort_{0},Color = @Color_{0} ", i);
                    string sqlWhere = string.Format(" id = @id_{0} ", i);
                    strSql.AppendFormat(" UPDATE [Marketing].[dbo].[tbl_NewCategoryList] WITH(rowlock) SET {0} WHERE {1} ", sqlSet, sqlWhere);

                    dicParams.Add("@CategoryName_" + i, data.CategoryName);
                    dicParams.Add("@Sort_" + i, data.Sort);
                    dicParams.Add("@id_" + i, data.id);
                    dicParams.Add("@Color_" + i, data.Color);
                }
                else
                {
                    string sqlSet   = string.Format("CategoryName,Sort,Color");
                    string sqlWhere = string.Format("@CategoryName_{0},@Sort_{0},@Color_{0} ", i);
                    strSql.AppendFormat(" INSERT into [Marketing].[dbo].[tbl_NewCategoryList]({0}) VALUES({1}) ", sqlSet, sqlWhere);

                    dicParams.Add("@CategoryName_" + i, data.CategoryName);
                    dicParams.Add("@Sort_" + i, data.Sort);
                    dicParams.Add("@Color_" + i, data.Color);
                }
            }

            SqlParameter[] sqlParams = new SqlParameter[dicParams.Count];//参数值
            //修改
            for (int i = 0; i < dicParams.Count; i++)
            {
                var dicKey   = dicParams.ElementAt(i).Key;
                var dicValue = dicParams.ElementAt(i).Value;
                sqlParams[i] = new SqlParameter(dicKey, dicValue);
            }
            Func <SqlConnection, bool> action = (connection) => DalArticle.EditCategory(connection, strSql.ToString(), sqlParams);

            return(dbManager.Execute(action));
        }
Example #22
0
 public static Article ToOrmArticle(this DalArticle dalEntity)
 {
     //if (dalEntity == null)
     //    return null;
     return(new Article()
     {
         Id = dalEntity.Id,
         Title = dalEntity.Title,
         Content = dalEntity.Content,
         PublicationDate = dalEntity.PublicationDate,
         UserId = dalEntity.UserId,
         Rating = dalEntity.Rating,
         Comments = dalEntity.Comments != null
                 ? dalEntity.Comments.Select(r => r.ToOrmComment()).ToList()
                 : null,
         Tags = dalEntity.Tags != null
                 ? dalEntity.Tags.Select(r => r.ToOrmTag()).ToList()
                 : null
     });
 }
        /// <summary>
        /// Create new article with specified tags and add it to database.
        /// </summary>
        /// <param name="article"></param>
        /// <param name="tags"></param>
        public void Create(DalArticle article, string[] tags)
        {
            if (article == null)
            {
                throw new ArgumentNullException(nameof(article));
            }
            article.Id = 0;
            article.CreationDateTime = DateTime.Now;
            Article ormArticle = article.ToOrmArticle();

            if (tags != null)
            {
                var tagsDbSet = context.Set <Tag>();
                foreach (string tag in tags)
                {
                    Tag tagEntity = tagsDbSet.FirstOrDefault(x => x.Value == tag);
                    if (tagEntity == null)//if tag not in DB -> add it
                    {
                        tagsDbSet.Add(new Tag()
                        {
                            Articles = new Article[] { ormArticle },
                            Value    = tag
                        });
                    }
                    else
                    {
                        //TODO optimize here
                        var list = tagEntity.Articles.ToList();
                        list.Add(ormArticle);
                        tagEntity.Articles = list;
                        //tagEntity.Articles.Add(ormArticle);
                    }
                } //foreach
            }     //if
            context.Set <Article>().Add(ormArticle);
        }
 public static Article GetORMEntity(this DalArticle dalEntity)
 {
     if (dalEntity == null)
     {
         return(null);
     }
     return(new Article()
     {
         Id = dalEntity.Id,
         Title = dalEntity.Title,
         Text = dalEntity.Text,
         PublicationDate = dalEntity.PublicationDate,
         AuthorId = dalEntity.AuthorId,
         CountLikes = dalEntity.CountLikes,
         Comments =
             dalEntity.Comments != null
                 ? dalEntity.Comments.Select(r => r.GetORMEntity()).ToList()
                 : null,
         Tags =
             dalEntity.Tags != null
                 ? dalEntity.Tags.Select(r => r.GetORMEntity()).ToList()
                 : null
     });
 }
 public void Update(DalArticle entity)
 {
     var article = _context.Articles.FirstOrDefault(x => x.Id == entity.Id);
     if (article != null)
     {
         article.Title = entity.Title;
         article.Text = entity.Text;
         article.TitleImage = entity.TitleImage;
     }
 }
        public void Delete(int PKID)
        {
            Action <SqlConnection> action = (connection) => DalArticle.Delete(connection, PKID);

            dbManager.Execute(action);
        }
        public List <Article> SelectAll()
        {
            Func <SqlConnection, List <Article> > action = (connection) => DalArticle.SelectAll(connection);

            return(dbManager.Execute(action));
        }
        public Article GetByUrl(string url)
        {
            Func <SqlConnection, Article> action = (connection) => DalArticle.GetByUrl(connection, url);

            return(dbManager.Execute(action));
        }
 public int DeleteQuestion(int PKID)
 {
     return(dbManager.Execute(conn => DalArticle.DeleteQuestion(conn, PKID)));
 }
 public bool DeleteArticleNewList(int Id)
 {
     return(dbManager.Execute(conn => DalArticle.DeleteArticleNewList(conn, Id)));
 }
 public void Delete(DalArticle e)
 {
     var article = _context.Articles.FirstOrDefault(x => x.Id == e.Id);
     if (article != null)
         _context.Articles.Remove(article);
 }
        public void Update(Article article)
        {
            Action <SqlConnection> action = (connection) => DalArticle.Update(connection, article);

            dbManager.Execute(action);
        }
 public void Create(DalArticle e)
 {
     _context.Articles.Add(e.ToOrmArticle());
 }