public ActionResult AddFriend(int id) { FriendConnection connection = null; using (UserContext db = new UserContext()) { connection = db.FriendConnections.FirstOrDefault(f => f.FirstUser.Email == User.Identity.Name && f.SecondUserId == id); if (connection != null) { ModelState.AddModelError("", "You are friends"); ViewBag.Message = $"{User.Identity.Name} has user with this id in friend list"; } else { var user = db.Users.FirstOrDefault(u => u.Email == User.Identity.Name); user.Friends.Add(new FriendConnection { FirstUserId = user.Id, SecondUserId = id }); db.SaveChanges(); ViewBag.Message = $"{User.Identity.Name} added new friend!"; } } return(View()); }
public async Task <IActionResult> SwitchFriendStatus([FromQuery] string userId, [FromQuery] bool positive) { string username = HttpContext?.User?.Identity?.Name; if (username is default(string)) { throw new ArgumentNullException("user"); } if (!_accountService.TryGetIdentifierByUsername(username, out var selfUserId)) { throw new ArgumentNullException("user"); } if (userId == selfUserId) { throw new Exception("Cannot friend yourself."); } var connection = _context.FriendConnections.FirstOrDefault(f => (f.UserOne == userId && f.UserTwo == selfUserId) || (f.UserOne == selfUserId && f.UserTwo == userId)); if (connection is default(FriendConnection)) { var newConnection = new FriendConnection() { Identifier = Guid.NewGuid().ToString(), UserOne = selfUserId, UserTwo = userId, Verified = false }; await _context.FriendConnections.AddAsync(newConnection); await _context.SaveChangesAsync(); return(Ok()); } if (positive && connection.Verified == false) { var newConn = new FriendConnection() { Identifier = Guid.NewGuid().ToString(), UserOne = connection.UserOne, UserTwo = connection.UserTwo, Verified = true }; _context.FriendConnections.Remove(connection); await _context.FriendConnections.AddAsync(newConn); await _context.SaveChangesAsync(); return(Ok()); } _context.FriendConnections.Remove(connection); await _context.SaveChangesAsync(); return(Ok()); }
public void AcceptRequest(int requestId) { var request = _networkRepository.GetRequestById(requestId); var friendConnection = new FriendConnection { FirstUserId = request.FromUserId, SecondUserId = request.ToUserId }; var areFriends = CheckIfFriends(request.FromUserId, request.ToUserId); if (!areFriends) { _networkRepository.AddFriendConnection(friendConnection); _networkRepository.DeleteFriendRequest(request); } return; }
public void Initialize() { // Set up our mock database. In this case, // we only have to worry about one table // with 3 records: var mockDb = new MockDataContext(); var f1 = new FriendConnection { ID = 1, Friend1 = "dabs", Friend2 = "nonni" }; mockDb.FriendConnections.Add(f1); var f2 = new FriendConnection { ID = 2, Friend1 = "gunna", Friend2 = "dabs" }; mockDb.FriendConnections.Add(f2); var f3 = new FriendConnection { ID = 3, Friend1 = "gunna", Friend2 = "nonni" }; mockDb.FriendConnections.Add(f3); // Note: you only have to add data necessary for this // particular service (FriendService) to run properly. // There will be more tables in your DB, but you only // need to provide the data for the methods you are // actually testing here. _service = new FriendService(mockDb); }
//public ActionResult Requests() //{ // ApplicationDbContext context = new ApplicationDbContext(); // string userId = User.Identity.GetUserId(); // List<FriendConnection> pendingRequests = context.FriendConnections.Where(t => t.UserId1 == userId && t.Status == "Pending").OrderByDescending(t => t.ConnectedDate).ToList(); // return View(pendingRequests); //} public ActionResult UpdateRequest(string Status, string UserId) { if (User.Identity.IsAuthenticated) { ApplicationDbContext context = new ApplicationDbContext(); string cuserId = User.Identity.GetUserId(); //List<FriendConnection> friends = context.FriendConnections.Where(t => t.UserId1 == userId && t.Status == "Approved").OrderBy(t => t.User2.FirstName).ToList(); FriendConnection friend = context.FriendConnections.Where(t => t.UserId1 == cuserId && t.UserId2 == UserId).FirstOrDefault(); if (friend != null) { friend.Status = Status; context.SaveChanges(); } return(RedirectToAction("Index")); } else { return(RedirectToAction("Login", "Account")); } //Rejected // Approved }
public EntityEntry <FriendConnection> Remove(FriendConnection obj) => base.RemoveFriend(obj);
public EntityEntry <FriendConnection> Add(FriendConnection obj) => base.AddFriend(obj);
public EntityEntry <FriendConnection> Remove(FriendConnection obj) => base.Set <FriendConnection>().Remove(obj);
public void RemoveFriendConnection(FriendConnection friendConnection) { _context.FriendConnections.Remove(friendConnection); _context.SaveChanges(); }
public void AddFriendConnection(FriendConnection friendConnection) { _context.FriendConnections.Add(friendConnection); _context.SaveChanges(); }