public async Task<HttpResponseMessage> PostToGroupById(AddGroupPostBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return await this.BadRequest(this.ModelState).ExecuteAsync(new CancellationToken());
            }

            var group = this.Data.Groups.Find(model.groupId);

            if (group == null)
            {
                return await this.NotFound().ExecuteAsync(new CancellationToken());
            }

            var currentUserId = User.Identity.GetUserId();
            var currentUser = this.Data.Users.FirstOrDefault(x => x.Id == currentUserId);
            if (currentUser == null)
            {
                return await this.BadRequest("Invalid user token! Please login again!").ExecuteAsync(new CancellationToken());
            }

            if (!group.Members.Contains(currentUser) && group.Owner != currentUser)
            {
                return await this.BadRequest("Not allowed. User is not member of group.")
                    .ExecuteAsync(new CancellationToken());
            }

            var newPost = new GroupPost()
            {
                Content = model.postContent,
                Author = currentUser,
                PostedOn = DateTime.Now
            };

            group.Posts.Add(newPost);
            this.Data.SaveChanges();

            var dbPost = this.Data.GroupPosts.FirstOrDefault(p => p.Id == newPost.Id);
            var postReturnView = new
            {
                Id = dbPost.Id,
                AuthorId = dbPost.Author.Id,
                AuthorUsername = dbPost.Author.UserName,
                AuthorProfileImage = dbPost.Author.ProfilePicture,
                GroupId = group.Id,
                PostContent = dbPost.Content,
                Date = dbPost.PostedOn,
                LikesCount = 0,
                Liked = false,
                TotalCommentsCount = 0,
                Comments = new List<CommentViewModel>()
            };

            return await this.Ok(postReturnView).ExecuteAsync(new CancellationToken());
        }
Пример #2
0
 public static object CreateGroupPostPreview(ApplicationUser currentUser, GroupPost post)
 {
     return new
     {
         Id = post.Id,
         AuthorId = post.Author.Id,
         AuthorUsername = post.Author.UserName,
         AuthorProfileImage = post.Author.ProfilePicture,
         WallOwnerId = post.Owner.Id,
         PostContent = post.Content,
         Date = post.PostedOn,
         LikesCount = post.Likes.Count,
         Liked = post.Likes.Any(l => l.Author == currentUser),
         TotalCommentsCount = post.Comments.Count,
         Comments = post.Comments
             .OrderByDescending(c => c.PostedOn)
             .AsQueryable()
             .Select(c => CommentViewModel.CreatePreview(currentUser, c))
             .ToList()
     };
 }
        public static void PostToGroup(int fromUserId, int toGroupId, string content)
        {
            var context = new SocialNetworkContext();

            var sender = context.Users.Find(fromUserId);
            var recipient = context.Groups.Find(toGroupId);
            var post = new GroupPost()
            {
                Content = content,
                Author = sender,
                PostedOn = DateTime.Now,
            };
            recipient.Posts.Add(post);
            context.SaveChanges();
        }