public async Task <IActionResult> Follow(string userName) { Artist user = await _userManager.GetUserAsync(User); Artist toFollow = await _userManager.FindByNameAsync(userName); FollowRelation followRelation = new FollowRelation { FromArtistId = user.Id, FromArtist = user, ToArtistId = toFollow.Id, ToArtist = toFollow }; FollowRelation duplicate = await _context.FollowRelations.FirstOrDefaultAsync(x => x.FromArtistId == user.Id && x.ToArtistId == toFollow.Id); if (duplicate == null) { _context.Add(followRelation); _context.SaveChanges(); } else { return(NotFound()); } return(RedirectToAction(nameof(Index), toFollow.UserName)); }
public IActionResult FollowUser(int userId) { var myUserId = User.CurrentUserId(); if (db.User.Find(userId) == null) { return(BadRequest($"Cannot find UserId {userId}")); } // FollowRelation follow = new FollowRelation(); var follow = db.FollowRelation.Where(r => r.From == myUserId && r.To == userId).FirstOrDefault(); if (follow != null) { return(BadRequest("User already follows this user.")); } follow = new FollowRelation(); follow.From = myUserId; follow.To = userId; follow.CreateDate = DateTime.Now; db.FollowRelation.Add(follow); db.SaveChanges(); return(Ok("Following user successfully")); }
// Display an artist profile public async Task <IActionResult> Index(string userName) { if (string.IsNullOrEmpty(userName)) { return(NotFound()); } Artist artist = await _userManager.FindByNameAsync(userName); if (artist == null) { return(NotFound()); } // Privacy settings if (_signInManager.IsSignedIn(User)) { Artist user = await _userManager.GetUserAsync(User); if (user != null && (!artist.IsPubliclyVisible && user != artist)) { return(NotFound()); } } else { if (!artist.IsPubliclyVisible) { return(NotFound()); } } ArtistIndexViewModel viewModel = new ArtistIndexViewModel { Artist = artist }; // Check if the viewer is following the artist if (_signInManager.IsSignedIn(User)) { Artist user = await _userManager.GetUserAsync(User); Artist toFollow = await _userManager.FindByNameAsync(userName); FollowRelation followRelation = await _context.FollowRelations.FirstOrDefaultAsync(x => x.FromArtistId == user.Id && x.ToArtistId == toFollow.Id); ViewData["isFollowing"] = followRelation != null ? true : false; } return(View(viewModel)); }
public async Task <IActionResult> Unfollow(string userName) { Artist user = await _userManager.GetUserAsync(User); Artist toFollow = await _userManager.FindByNameAsync(userName); FollowRelation followRelation = await _context.FollowRelations.FirstOrDefaultAsync(x => x.FromArtistId == user.Id && x.ToArtistId == toFollow.Id); if (followRelation != null) { _context.Remove(followRelation); await _context.SaveChangesAsync(); } else { return(NotFound()); } return(RedirectToAction(nameof(Index), toFollow.UserName)); }
public async Task <IActionResult> FollowUser(string Id) { var message = ""; try { var follow = new FollowRelation() { FollowerId = Context.User.GetUserId(), FollowingId = Id }; DbContext.FollowRelations.Add(follow); await DbContext.SaveChangesAsync(); // process your data using the parameter value message = "Successfully processed!"; } catch (Exception ex) { message = ex.Message; // if processing fails, we send the failure message to the view } return(Json(new { message })); }
public async Task <IActionResult> Follow([FromForm] FollowsDto followDto) { // if not, a new follow gets created FollowRelation newFollow = new FollowRelation(followDto.FolloweeId, followDto.FollowerId); try //Create the realation in db { //dbContext.Follows.Add(newFollow); unitOfWork.FollowRelations.Add(newFollow); await unitOfWork.Save(); } catch (Exception e)//Case that userId and followerId inside dto are incorrect { return(BadRequest(e.Message)); } finally { //var follower = dbContext.GamersGridUsers.Single(u => u.ID == followDto.FollowerId); //var follower = unitOfWork.GGUsers.GetUser(followDto.FollowerId); ////var followee = dbContext.GamersGridUsers.Single(u => u.ID == followDto.FolloweeId); //var followee = unitOfWork.GGUsers.GetUser(followDto.FolloweeId); // Creating the personalized Message for followee //var personalNotification = Notification.FollowPersonal(follower); //followee.Notify(personalNotification); ////Creating the list of users with mutual follows with the 2 interacting users //var usersToNotifyBuffer = unitOfWork.GGUsers.GetFollowersOfTwoUsersWithTheirFollowees(followee.ID, follower.ID); //var usersToNotify = new List<User>(); //foreach (var user in usersToNotifyBuffer) //{ // bool condition1 = user.Followees.Contains(new Follow(user.ID, followee.ID)); // bool condition2 = user.Followees.Contains(new Follow(user.ID, follower.ID)); // bool condition3 = user.ID == followee.ID; // if (condition1 && condition2 && !condition3) // usersToNotify.Add(user); //} //var notification = Notification.Follow(followee, follower); //if (usersToNotify.Count != 0) //{ // foreach (var user in usersToNotify) // { //Notify all Users // user.Notify(notification); // } //} //Write Notifications in Db //dbContext.SaveChanges(); //UnitOfWork.Complete(); //signlar logic start //NotificationsHub notificationsHub = new NotificationsHub(); //notificationsHub.SendNotification(usersToNotify, notification); //notificationsHub.SendNotificationPersonal(followee, personalNotification); } return(Ok()); }
public async Task <IActionResult> UnFollow([FromForm] FollowsDto followDto) { //checking for existing relation in db FollowRelation existingFollowInDb = await unitOfWork.FollowRelations.GetFollowRelationOfTwoUsersIncludingUser(followDto.FolloweeId, followDto.FollowerId); // if yes try { unitOfWork.FollowRelations.Remove(existingFollowInDb); await unitOfWork.Save(); } catch //Case that userId and followerId inside dto are incorrect { return(BadRequest()); } finally { //var follower = unitOfWork.GGUsers.GetUser(followDto.FollowerId); //var followee = unitOfWork.GGUsers.GetUser(followDto.FolloweeId); //// Creating the personalized Message for followee //var notificationPersonal = Notification.UnfollowPersonal(follower); //followee.Notify(notificationPersonal); ////Creating the list of users with mutual follows with the 2 interacting users //var usersToNotifyBuffer = UnitOfWork.GGUsers.GetFollowersOfTwoUsersWithTheirFollowees(followee.ID, follower.ID); //var usersToNotify = new List<User>(); //foreach (var user in usersToNotifyBuffer) //{ // bool condition1 = user.Followees.Contains(new Follow(user.ID, followee.ID)); // bool condition2 = user.Followees.Contains(new Follow(user.ID, follower.ID)); // bool condition3 = user.ID == followee.ID; // if (!condition1 && !condition2 && !condition3) // usersToNotify.Add(user); //} //var notification = Notification.Unfollow(followee, follower); //if (usersToNotify.Count != 0) //{ // foreach (var user in usersToNotify) // { //Notify all Users // user.Notify(notification); // } //} ////Write Notifications in Db //UnitOfWork.Complete(); ////NotificationsHub notificationsHub = new NotificationsHub(); ////notificationsHub.SendNotification(usersToNotify, notification); ////notificationsHub.SendNotificationPersonal(followee, notificationPersonal); } return(Ok()); }