/*USED FOR DEVELOPMENT*/
 public IEnumerable <Comment> GetAllFromTopic()
 {
     /*
      * List<Comment> comments = new List<Comment>();
      * if (topic == null)
      * {
      *  return BadRequest();
      * }
      * using (var db = new CommentDbContext()) //Is iot more effective to crate as variable instead?
      * {
      *  return Ok(db.Comments.All(c => c.Topic == topic));
      * }
      */
     /*TEST*/
     using (var db = new CommentDbContext())
     {
         var comment = (from c in db.Comments
                        where c.Topic == "Cars"
                        select c).ToArray();
         return(comment);
     }
     //using (var db = new CommentDbContext())
     //{
     //    return db.Comments.ToArray();
     //}
 }
Пример #2
0
        public List <Models.Comment> LoadByPage(Guid sourceId, IBntWebModule module, string sourceType, out int totalCount, int pageIndex = 1, int pageSize = 10)
        {
            var checkModule = module != null;
            var moduleKey   = checkModule ? module.InnerKey : null;
            Expression <Func <Models.Comment, bool> > expression =
                c => c.SourceId.Equals(sourceId) &&
                c.SourceType.Equals(sourceType, StringComparison.OrdinalIgnoreCase) &&
                (c.ParentId == null || c.ParentId == Guid.Empty) &&
                (!checkModule || (checkModule && c.ModuleKey.Equals(moduleKey, StringComparison.OrdinalIgnoreCase)));
            List <Models.Comment> list;

            using (var dbContext = new CommentDbContext())
            {
                totalCount = dbContext.Comments.AsExpandable().Where(expression).Count();

                list = dbContext.Comments.AsExpandable().Where(expression).OrderByDescending(c => c.CreateTime).Include(c => c.Files).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            }

            foreach (var item in list)
            {
                BindMemberInfo(item);
                item.ChildComments = GetAllChilds(item.Id);
            }
            return(list);
        }
Пример #3
0
 public Models.Comment GetComment(Guid commentId)
 {
     using (var dbContext = new CommentDbContext())
     {
         var comment = dbContext.Comments.FirstOrDefault(c => c.Id == commentId);
         return(BindMemberInfo(comment));
     }
 }
Пример #4
0
 public void DeleteComment(Guid commentId)
 {
     using (var dbContext = new CommentDbContext())
     {
         var comment = dbContext.Comments.FirstOrDefault(c => c.Id == commentId);
         DeleteCommentAndChild(comment, dbContext);
         dbContext.SaveChanges();
     }
 }
 public IHttpActionResult LikeComment(int ID)
 {
     using (var db = new CommentDbContext())
     {
         db.Comments.FirstOrDefault(c => c.ID == ID).Likes++;
         db.SaveChanges();
         return(Ok());
     }
 }
Пример #6
0
 private PagedList <Comment> GetParentCommentsByEntityId(CommentSearchCondition commentSearchCondition)
 {
     using (var context = new CommentDbContext())
     {
         var parentCommentQuery = GetComments(context, commentSearchCondition);
         var parentComments     = parentCommentQuery.OrderByDescending(p => p.CreatedOn).ToPagedList(commentSearchCondition.PageNumber, commentSearchCondition.PageSize);
         FetchAndIncludeReplies(context, parentComments, commentSearchCondition, false);
         return(parentComments);
     }
 }
 public IEnumerable <Comment> GetChild(int parent)
 {
     using (var db = new CommentDbContext())
     {
         var comments = (from c in db.Comments
                         where c.Parent == parent
                         select c).ToArray();
         return(comments);
     }
 }
Пример #8
0
 private static IQueryable<Comment> GetComments(CommentDbContext context, CommentSearchCondition commentSearchCondition)
 {
     if (CommonHelpers.IsNotEmptyGuid(commentSearchCondition.ParentId))
     {
         return context.Comments.Where(x => x.ParentId == commentSearchCondition.ParentId);
     }
     else
     {
         return context.Comments.Where(x => x.PostId == commentSearchCondition.PostId && (x.ParentId == null || x.ParentId == ""));
     }
 }
Пример #9
0
 private static IQueryable <Comment> GetComments(CommentDbContext context, CommentSearchCondition commentSearchCondition)
 {
     if (CommonHelpers.IsNotEmptyGuid(commentSearchCondition.ParentId))
     {
         return(context.Comments.Where(x => x.ParentId == commentSearchCondition.ParentId));
     }
     else
     {
         return(context.Comments.Where(x => x.PostId == commentSearchCondition.PostId && (x.ParentId == null || x.ParentId == "")));
     }
 }
 public IEnumerable <Comment> Get(string topic)
 {
     using (var db = new CommentDbContext())
     {
         //var Test = db.Comments.Where(s => s.Topic == topic).ToArray();
         var comment = (from c in db.Comments
                        where c.Topic == topic
                        select c).ToArray();
         return(comment);
     }
 }
Пример #11
0
 public void DeleteCommentBySourceId(Guid sourceId)
 {
     using (var dbContext = new CommentDbContext())
     {
         var comments = dbContext.Comments.Where(c => c.SourceId == sourceId).ToList();
         foreach (var comment in comments)
         {
             DeleteCommentAndChild(comment, dbContext);
         }
         dbContext.SaveChanges();
     }
 }
 public IHttpActionResult Post(Comment comment)
 {
     if (comment == null)
     {
         return(BadRequest());
     }
     using (var db = new CommentDbContext())
     {
         var returnValue = db.Comments.Add(comment);
         db.SaveChanges();
         return(Ok(returnValue)); //returns the comment that ahs been added
     }
 }
Пример #13
0
        private void DeleteCommentAndChild(Models.Comment comment, CommentDbContext dbContext)
        {
            if (comment != null)
            {
                var childs = dbContext.Comments.Where(c => c.ParentId == comment.Id).ToList();
                foreach (var child in childs)
                {
                    DeleteCommentAndChild(child, dbContext);
                }

                dbContext.Comments.Remove(comment);
                _storageFileService.DisassociateFile(comment.Id, CommentModule.Key);
            }
        }
Пример #14
0
        private List <Models.Comment> GetAllChilds(Guid parentId)
        {
            List <Models.Comment> list;

            using (var dbContext = new CommentDbContext())
            {
                list = dbContext.Comments.AsExpandable().Where(c => c.ParentId == parentId).OrderByDescending(c => c.CreateTime).Include(c => c.Files).ToList();
            }
            foreach (var item in list)
            {
                BindMemberInfo(item);
                item.ChildComments = GetAllChilds(item.Id);
            }
            return(list);
        }
Пример #15
0
 public void SaveComment(Models.Comment comment)
 {
     using (var dbContext = new CommentDbContext())
     {
         dbContext.Comments.Add(comment);
         if (dbContext.SaveChanges() > 0 && comment.Files != null)
         {
             foreach (var file in comment.Files)
             {
                 _storageFileService.AssociateFile(comment.Id, CommentModule.Key, CommentModule.DisplayName,
                                                   file.Id);
             }
         }
     }
 }
        public IHttpActionResult GetRandom()
        {
            using (var db = new CommentDbContext())
            {
                Random rnd = new Random();

                var comment = (from c in db.Comments
                               where c.Topic != null
                               group c by new { c.Topic }
                               into topics
                               select topics.FirstOrDefault()).ToArray();

                return(Ok(comment[rnd.Next(0, comment.Count())]));
            }
        }
        public IEnumerable <Comment> Get()
        {
            using (var db = new CommentDbContext())
            {
                if (db.Comments.Count() <= 0)
                {
                    return(null);
                }
                var comment = (from c in db.Comments
                               where c.Topic != null
                               group c by new { c.Topic }
                               into topics
                               select topics.FirstOrDefault()).ToArray();

                return(comment);
            }
        }
        public IHttpActionResult PostAnswer(string author, string answer, int parent)
        {
            if (answer == null)
            {
                return(BadRequest());
            }
            Comment comment = new Comment();

            comment.Parent  = parent;
            comment.Author  = author;
            comment.Message = answer;
            using (var db = new CommentDbContext())
            {
                var returnValue = db.Comments.Add(comment);
                db.SaveChanges();
                return(Ok(returnValue)); //returns the comment that ahs been added
            }
        }
Пример #19
0
        private void FetchAndIncludeReplies(CommentDbContext dbContext, PagedList <Comment> parentComments, CommentSearchCondition commentSearchCondition, bool fetchCountOnly)
        {
            if (parentComments == null || !parentComments.Any())
            {
                return;
            }
            var parentCommentIds = parentComments.Select(x => x.Id).ToArray();
            var childComments    = dbContext.Comments.Where(x => parentCommentIds.Contains(x.ParentId));

            foreach (var parent in parentComments)
            {
                if (fetchCountOnly)
                {
                    parent.Replies = childComments.Where(child => child.ParentId == parent.Id).OrderByDescending(p => p.CreatedOn).ToPagedList(true);
                }
                else
                {
                    parent.Replies = childComments.Where(child => child.ParentId == parent.Id).OrderByDescending(p => p.CreatedOn).ToPagedList(commentSearchCondition.PageNumber, commentSearchCondition.PageSize);
                    FetchAndIncludeReplies(dbContext, parent.Replies, commentSearchCondition, true);
                }
            }
        }
Пример #20
0
        private void FetchAndIncludeReplies(CommentDbContext dbContext, PagedList<Comment> parentComments, CommentSearchCondition commentSearchCondition, bool fetchCountOnly)
        {
            if (parentComments == null || !parentComments.Any()) return;
            var parentCommentIds = parentComments.Select(x => x.Id).ToArray();
            var childComments = dbContext.Comments.Where(x => parentCommentIds.Contains(x.ParentId));

            foreach (var parent in parentComments)
            {
                if (fetchCountOnly)
                {
                    parent.Replies = childComments.Where(child => child.ParentId == parent.Id).OrderByDescending(p => p.CreatedOn).ToPagedList(true);
                }
                else
                {
                    parent.Replies = childComments.Where(child => child.ParentId == parent.Id).OrderByDescending(p => p.CreatedOn).ToPagedList(commentSearchCondition.PageNumber, commentSearchCondition.PageSize);
                    FetchAndIncludeReplies(dbContext, parent.Replies, commentSearchCondition, true);
                }
            }
        }
Пример #21
0
 public CommentRepository(CommentDbContext context)
 {
     this.context = context;
 }
Пример #22
0
 public CommentRepository(CommentDbContext context)
 {
     this.context = context;
 }
Пример #23
0
 public CommentController(CommentDbContext dbContext)
 {
     _dbContext = dbContext;
 }
Пример #24
0
 private PagedList<Comment> GetParentCommentsByEntityId(CommentSearchCondition commentSearchCondition)
 {
     using (var context = new CommentDbContext())
     {
         var parentCommentQuery = GetComments(context, commentSearchCondition);
         var parentComments = parentCommentQuery.OrderByDescending(p => p.CreatedOn).ToPagedList(commentSearchCondition.PageNumber, commentSearchCondition.PageSize);
         FetchAndIncludeReplies(context, parentComments, commentSearchCondition, false);
         return parentComments;
     }
 }