public async Task <bool> SharePostAsync(string username, ShareInputModel model) { var user = await identityService.GetUserByName(username) .SingleOrDefaultAsync(); Wall wall = null; if (model.ShareLocationType == ShareLocation.FriendWall && await relationsService.AreFriendsWithAsync(username, model.ShareLocation)) { wall = await wallService.GetUserWallAsync(model.ShareLocation); } else if (model.ShareLocationType == ShareLocation.GroupWall && await groupService.IsUserConfirmedMemberAsync(model.ShareLocation, username)) { wall = await wallService.GetGroupWallAsync(model.ShareLocation); } else if (model.ShareLocationType == ShareLocation.MyWall) { wall = await wallService.GetUserWallAsync(username); } if (user != null && wall != null && await IsUserCanSeePostAsync(username, model.PostId) && await IsUserCanPostToWallAsync(username, wall.Id)) { var sharedPost = new Post() { Content = model.PostModel.Content, Tags = GetPostTags(model.PostModel.Tags), SharedPostId = model.PostId, Location = wall, User = user }; var share = new Share() { PostId = model.PostId, User = user }; if (model.ShareLocationType == ShareLocation.MyWall) { sharedPost.VisibilityType = model.PostModel.VisibilityType; } else { sharedPost.VisibilityType = PostVisibilityType.Public; } await this.dbContext.Posts.AddAsync(sharedPost); await this.dbContext.Shares.AddAsync(share); await this.dbContext.SaveChangesAsync(); return(true); } return(false); }
public async Task <IActionResult> Share(ShareInputModel model) { if (ModelState.IsValid) { if (await this.postService.SharePostAsync(this.User.Identity.Name, model)) { if (model.ShareLocationType == ShareLocation.FriendWall) { return(RedirectToAction("Profile", "User", new { username = model.ShareLocation })); } if (model.ShareLocationType == ShareLocation.GroupWall) { return(RedirectToAction("Open", "Group", new { name = model.ShareLocation })); } return(RedirectToAction("Profile", "User", new { username = this.User.Identity.Name })); } } var viewModel = await postService.GetShareViewAsync(model.PostId); TempData["ShareResult"] = "Failed! Please check where are you trying to share the post: did you spelled the name correctly?, do you have rights to post there?"; return(View(viewModel)); }
public async Task SharePostAsync_ShouldShare_Correctly() { var user = new OurTraceUser() { UserName = "******", BirthDate = DateTime.Now, Country = "Bulgaria", Email = "*****@*****.**", NormalizedEmail = "*****@*****.**", NormalizedUserName = "******", PasswordHash = "00000", Wall = new Wall() }; var user2 = new OurTraceUser() { UserName = "******", BirthDate = DateTime.Now, Country = "Bulgaria", Email = "*****@*****.**", NormalizedEmail = "*****@*****.**", NormalizedUserName = "******", PasswordHash = "00000", Wall = new Wall() }; var user3 = new OurTraceUser() { UserName = "******", BirthDate = DateTime.Now, Country = "Bulgaria", Email = "*****@*****.**", NormalizedEmail = "*****@*****.**", NormalizedUserName = "******", PasswordHash = "00000", Wall = new Wall() }; var friendShipBetweenUser2AndUser1 = new Friendship() { Sender = user2, Recipient = user, AcceptedOn = DateTime.Now }; var post = new Post() { Content = "Post content", Location = user.Wall, User = user, VisibilityType = PostVisibilityType.FriendsOnly }; await this.dbContext.Users.AddAsync(user); await this.dbContext.Users.AddAsync(user2); await this.dbContext.Users.AddAsync(user3); await this.dbContext.Posts.AddAsync(post); await this.dbContext.Friendships.AddAsync(friendShipBetweenUser2AndUser1); await this.dbContext.SaveChangesAsync(); var shareInputModel = new ShareInputModel() { PostId = post.Id, PostModel = new CreatePostInputModel() { Content = "Shared Content", Location = user2.WallId, VisibilityType = PostVisibilityType.Public }, ShareLocation = user.UserName, ShareLocationType = ShareLocation.FriendWall }; var actual1 = await this.postService.SharePostAsync(user2.UserName, shareInputModel); Assert.True(actual1); var actual3 = await this.postService.SharePostAsync(user3.UserName, shareInputModel); Assert.False(actual3); shareInputModel.ShareLocation = user.UserName; shareInputModel.ShareLocationType = ShareLocation.MyWall; var actual2 = await this.postService.SharePostAsync(user.UserName, shareInputModel); Assert.True(actual2); }