public IHttpActionResult PostOnWall(AddPostBindingModel postModel) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var wallOwner = this.SocialNetworkData.Users.All() .FirstOrDefault(u => u.UserName == postModel.Username); if (wallOwner == null) { return this.NotFound(); } var loggedUserId = this.User.Identity.GetUserId(); var loggedUser = this.SocialNetworkData.Users .GetById(loggedUserId); if (loggedUserId == null) { return this.BadRequest("Invalid session token."); } if (wallOwner.Id != loggedUserId) { var isFriendOfWallOwner = wallOwner.Friends .Any(fr => fr.Id == loggedUserId); if (!isFriendOfWallOwner) { return this.BadRequest("Only friends can post on user's wall."); } } var newPost = new Post() { Content = postModel.PostContent, WallOwner = wallOwner, WallOwnerId = wallOwner.Id, Author = loggedUser, AuthorId = loggedUserId, Date = DateTime.Now }; this.SocialNetworkData.Posts.Add(newPost); this.SocialNetworkData.SaveChanges(); return this.Ok(PostViewModel.Create(newPost, loggedUser)); }
protected bool HasAccessToPost(ApplicationUser user, Post post) { if (post.AuthorId != user.Id && post.WallOwnerId != user.Id) { bool postAuthorOrWallOwnerIsFriend = user.Friends .Any(fr => fr.Id == post.AuthorId || fr.Id == post.WallOwnerId); if (!postAuthorOrWallOwnerIsFriend) { return false; } } return true; }
public static PostViewModel Create(Post p, ApplicationUser currentUser) { return new PostViewModel() { Id = p.Id, Author = UserViewModelPreview.Create(p.Author, currentUser), WallOwner = UserViewModelPreview.Create(p.WallOwner, currentUser), PostContent = p.Content, Date = p.Date, LikesCount = p.Likes.Count, Liked = p.Likes .Any(l => l.UserId == currentUser.Id), TotalCommentsCount = p.Comments.Count, Comments = p.Comments .Reverse() .Take(3) .Select(c => CommentViewModel.Create(c, currentUser)) }; }
public static AddPostViewModel ConvertTo(Post post, ApplicationUser currentUser) { AddPostViewModel postViewModel = new AddPostViewModel { Id = post.Id, Author = new UserViewModelMinified { Id = post.AuthorId, Name = post.Author.Name, Username = post.Author.UserName, IsFriend = post.Author.Friends.Any(f => f.Id == currentUser.Id), Gender = post.Author.Gender, ProfileImageData = post.Author.ProfileImageData, }, WallOwner = new UserViewModelMinified { Id = post.WallOwnerId, Name = post.WallOwner.Name, Username = post.WallOwner.UserName, IsFriend = post.WallOwner.Friends.Any(f => f.Id == currentUser.Id), Gender = post.WallOwner.Gender, ProfileImageData = post.WallOwner.ProfileImageData, }, PostContent = post.Content, PostedOn = post.PostedOn, LikesCount = post.Likes.Count, Liked = post.Likes.Any(l => l.UserId == currentUser.Id), TotalCommentsCount = post.Comments.Count, Comments = post.Comments .Reverse() .Take(4) .AsQueryable() .Select(CommentViewModel.Create(currentUser)) }; return postViewModel; }
public IHttpActionResult AddPost(AddNewPostBindingModel bindingModel) { if (bindingModel == null) { return this.BadRequest("Invalid data!"); } if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var existingWallOwner = this.Data .Users .All() .FirstOrDefault(u => u.UserName == bindingModel.Username); if (existingWallOwner == null) { return this.BadRequest("No such user!"); } var currentUserId = this.UserIdProvider.GetUserId(); var currentUser = this.Data.Users.Find(currentUserId); if ((!currentUser.Friends.Contains(existingWallOwner)) && (currentUserId != existingWallOwner.Id)) { return this.BadRequest("You have no permissions to make this post."); } var post = new Post { Content = bindingModel.PostContent, PostedOn = DateTime.Now, AuthorId = currentUserId, WallOwnerId = existingWallOwner.Id }; this.Data.Posts.Add(post); this.Data.SaveChanges(); AddPostViewModel postViewModel = AddPostViewModel.ConvertTo(post, currentUser); return this.Ok(postViewModel); }
private bool HasAuthorization(ApplicationUser currentUser, Post post) { return !currentUser.Friends.Contains(post.Author) && !currentUser.Friends.Contains(post.WallOwner) && post.WallOwnerId != currentUser.Id; }
public async Task<HttpResponseMessage> AddPostToUser(AddPostBindingModel model) { if (!ModelState.IsValid) { return await this.BadRequest(this.ModelState).ExecuteAsync(new CancellationToken()); } if (model == null) { return await this.BadRequest("Post cannot be empty.").ExecuteAsync(new CancellationToken()); } var user = this.Data.Users.FirstOrDefault(u => u.UserName == model.username); if (user == 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 (!user.Friends.Contains(currentUser)) { return await this.BadRequest("Not allowed. Wall-owner must be a friend or user.").ExecuteAsync(new CancellationToken()); } var newPost = new Post() { Content = model.postContent, Author = currentUser, PostedOn = DateTime.Now }; user.Posts.Add(newPost); this.Data.SaveChanges(); var dbPost = this.Data.Posts.FirstOrDefault(p => p.Id == newPost.Id); var postReturnView = new PostViewModel() { Id = dbPost.Id, AuthorId = dbPost.Author.Id, AuthorUsername = dbPost.Author.UserName, AuthorProfileImage = dbPost.Author.ProfilePicture, WallOwnerId = user.Id, PostContent = dbPost.Content, Date = dbPost.PostedOn, LikesCount = dbPost.Likes.Count, Liked = false, TotalCommentsCount = 0, Comments = new List<CommentViewModel>() }; return await this.Ok(new { message = "Post successfully added.", post = postReturnView }).ExecuteAsync(new CancellationToken()); }
private void PostsImport() { var document = new XmlDocument(); document.Load(PostsPath); var root = document.DocumentElement; if (root != null) { var recordsIndex = 0; foreach (XmlNode node in root.ChildNodes) { var post = new Post { Content = node.SelectSingleNode("Content").InnerText, PostingDate = DateTime.Parse(node.SelectSingleNode("PostedOn").InnerText) }; var users = node.SelectSingleNode("Users").InnerText.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); var postUsers = users.Select(user => this.data.Users.First(x => x.UserName == user)).ToList(); if (postUsers.Count > 0) { foreach (var usr in postUsers) { post.Users.Add(usr); } } Console.Write("."); this.data.Posts.Add(post); if (recordsIndex % 100 == 0) { this.data.SaveChanges(); this.data.Dispose(); this.data = new SocialNetworkDbContext(); } recordsIndex++; } this.data.SaveChanges(); } }
public static object CreatePostPreview(ApplicationUser currentUser, Post 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() }; }
protected void Post_Command(object sender, CommandEventArgs e) { using (SocialNetworkDbEntities context = new SocialNetworkDbEntities()) { try { Post post = new Post(); context.Posts.Add(post); post.AuthorId = User.Identity.GetUserId(); post.DateCreated = DateTime.Now; post.Text = Server.HtmlEncode(this.TextBoxNewPostText.Text); context.SaveChanges(); Response.Redirect("UserPost.aspx?postId=" + post.PostId, false); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage("Posting failed! Server error!"); } } }