コード例 #1
0
        /// <summary>
        /// Deletes a group comment from the database
        /// </summary>
        /// <param name="groupCommentId"></param>
        public void DeleteGroupComment(int groupCommentId)
        {
            var groupComment = _context.GroupComment.FirstOrDefault(g => g.GroupCommentId == groupCommentId);

            if (groupComment != null)
            {
                var childComments = _context.GroupComment.Where(g => g.ParentId == groupCommentId).ToList();

                if (childComments.Count > 0)
                {
                    DeleteChildren(childComments);
                }

                foreach (var comment in childComments)
                {
                    if (comment != null)
                    {
                        _context.GroupComment.Remove(comment);
                    }
                }

                _context.GroupComment.Remove(groupComment);

                if (!_context.GroupComment.Any(gc => gc.ParentId == groupComment.ParentId))
                {
                    var entity = _context.GroupComment.FirstOrDefault(gc => gc.GroupCommentId == groupComment.ParentId);

                    entity.HasChildren = false;

                    _context.Update(entity);
                }

                _context.SaveChangesAsync();
            }
        }
コード例 #2
0
ファイル: GroupService.cs プロジェクト: Sturminator/MonAmie
        /// <summary>
        /// Adds a user to a group
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="groupId"></param>
        public int AddUserToGroup(int userId, int groupId)
        {
            var entity = _context.GroupHasUser.FirstOrDefault(ghu => ghu.UserId == userId && ghu.GroupId == groupId);

            if (entity == null)
            {
                _context.GroupHasUser.Add(new GroupHasUser
                {
                    UserId   = userId,
                    GroupId  = groupId,
                    JoinDate = DateTime.UtcNow
                });

                var activity = new GroupHasActivity
                {
                    GroupId      = groupId,
                    UserId       = userId,
                    Type         = "JOIN",
                    CreationDate = DateTime.UtcNow
                };

                _context.GroupHasActivity.Add(activity);
                _context.SaveChangesAsync();

                return(activity.GroupHasActivityId);
            }

            return(0);
        }