public async Task <IActionResult> Register(IdentityModel identityModel) { if (!ModelState.IsValid) { return(BadRequest("Unvalid data")); } try { var user = new GeoPointUser { SecurityStamp = Guid.NewGuid().ToString(), UserName = identityModel.Username, Email = identityModel.Email }; if (await _userManager.FindByNameAsync(user.UserName) == null) { var u = await _userManager.CreateAsync(user, identityModel.Password); if (u.Errors.Count() > 0) { return(BadRequest("Password should be minimum 8 characters and contain a combination of uppercase, lowercase, numbers and special characters")); } return(await Login(identityModel)); } else { return(BadRequest("User already exists!")); } } catch (Exception e) { _logger.LogError($"\r\n\r\nError thrown on AuthController - Register method (" + DateTime.UtcNow.ToString() + ") \r\nException thrown when trying to Register: " + e + "\r\n\r\n"); return(BadRequest("Failed to create account")); } }
public async Task <IActionResult> declineFriendRequest(UserVM userVM) { try { var claimsIdentity = this.User.Identity as ClaimsIdentity; var userId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value; GeoPointUser user = await userManager.FindByIdAsync(userId); GeoPointUser friend = await userManager.FindByNameAsync(userVM.username); if ((user.Friends != null)) { foreach (Friend f in user.Friends.ToList()) { if (f.Username == userVM.username) { user.Friends.Remove(f); } } } await userManager.UpdateAsync(user); return(Ok("friend request declined!")); } catch (Exception e) { logger.LogError($"\r\n\r\nError thrown on UsersController - declineFriendRequest method (" + DateTime.UtcNow.ToString() + ") \r\nException thrown when trying to decline Friend Request: " + e + "\r\n\r\n"); return(BadRequest("Failed to decline friend request")); } }
public async Task <IActionResult> getFriendTopScores([Required] string area, int length = 10) { try { var claimsIdentity = this.User.Identity as ClaimsIdentity; var userId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value; GeoPointUser user = await _userManager.FindByIdAsync(userId); if (user.Friends != null) { IEnumerable <Score> scores = await _scoreRepo.GetFriendTopScoresAsync(user, area.ToUpper(), length); foreach (Score s in scores) { s.User.PasswordHash = null; } return(Ok(scores)); } return(BadRequest("no friends yet")); } catch (Exception e) { _logger.LogError($"\r\n\r\nError thrown on ScoresController - GetFriendTopScores method (" + DateTime.UtcNow.ToString() + ") \r\nException thrown when trying to Get Friend Top Scores: " + e + "\r\n\r\n"); return(BadRequest("Failed to get friend topscores")); } }
public async Task <IActionResult> SendFriendRequest(UserVM userVM) { try { var claimsIdentity = this.User.Identity as ClaimsIdentity; var userId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value; GeoPointUser user = await userManager.FindByIdAsync(userId); GeoPointUser friend = await userManager.FindByNameAsync(userVM.username); if (user.Friends != null) { foreach (Friend f in user.Friends.ToList()) { if (f.Username == friend.UserName) { return(BadRequest("Already friends")); } } } if (friend.Friends != null) { foreach (Friend f in friend.Friends.ToList()) { if (f.Username == user.UserName) { return(BadRequest("Already friends")); } } } Friend newFriend = new Friend { Username = user.UserName }; if (friend.Friends == null) { friend.Friends = new List <Friend>(); } friend.Friends.Add(newFriend); await userManager.UpdateAsync(friend); return(Ok("Friend request sent to " + userVM.username)); } catch (Exception e) { logger.LogError($"\r\n\r\nError thrown on UsersController - SendFriendRequest method (" + DateTime.UtcNow.ToString() + ") \r\nException thrown when trying to Send Friend Request: " + e + "\r\n\r\n"); return(BadRequest("Failed to send friend request")); } }
public async Task <IEnumerable <Score> > GetFriendTopScoresAsync(GeoPointUser user, string area, int length) { List <Score> TopScores = new List <Score>(); foreach (Friend friend in user.Friends.ToList()) { var filter = Builders <Score> .Filter.Where(s => s.User.UserName == friend.Username); filter = filter & Builders <Score> .Filter.Where(s => s.Area == area); TopScores.AddRange(await mongoDBContext.Scores.Find(filter).ToListAsync()); } var filter2 = Builders <Score> .Filter.Where(s => s.User.UserName == user.UserName); filter2 = filter2 & Builders <Score> .Filter.Where(s => s.Area == area); TopScores.AddRange(await mongoDBContext.Scores.Find(filter2).ToListAsync()); return(TopScores.OrderByDescending(s => s.Value).ThenBy(s => s.TimeSpan).Take(length)); }
public async Task <IActionResult> GetMyFriends() { try { var claimsIdentity = this.User.Identity as ClaimsIdentity; var userId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value; GeoPointUser user = await userManager.FindByIdAsync(userId); if (user.Friends == null) { return(BadRequest("no friends yet")); } else { return(Ok(user.Friends)); } } catch (Exception e) { logger.LogError($"\r\n\r\nError thrown on UsersController - getMyFriends method (" + DateTime.UtcNow.ToString() + ") \r\nException thrown when trying to get My friends: " + e + "\r\n\r\n"); return(BadRequest("Failed to get your friendslist")); } }