public async Task <UserFollower> Update(UserFollower userFollower) { var result = dataContext.UserFollowers.Update(userFollower); await dataContext.SaveChangesAsync(); return(result.Entity); }
public async Task FollowAsync(string followingUserId, string followedUserId) { var doesFollowingAndFollowerExist = this.userFollowerRepository .AllWithDeleted() .Any(x => x.FollowingUserId == followingUserId && x.FollowedUserId == followedUserId && x.IsDeleted == true); if (doesFollowingAndFollowerExist == true) { var existingUserFollower = this.userFollowerRepository .AllWithDeleted() .FirstOrDefault(x => x.FollowingUserId == followingUserId && x.FollowedUserId == followedUserId); this.userFollowerRepository.Undelete(existingUserFollower); } else { var userFollower = new UserFollower() { FollowingUserId = followingUserId, FollowedUserId = followedUserId, }; await this.userFollowerRepository.AddAsync(userFollower); } await this.userFollowerRepository.SaveChangesAsync(); }
public static string ImportFollowers(InstagraphProfile context, string jsonString, IMapper mapper) { var followersString = JsonConvert.DeserializeObject <ImportFollowersDto[]>(jsonString); List <UserFollower> followersList = new List <UserFollower>(); StringBuilder sb = new StringBuilder(); foreach (ImportFollowersDto followerDto in followersString) { if (!IsValid(followerDto)) { sb.AppendLine(errorMessage); continue; } var userExist = context.Users.Any(x => x.Username == followerDto.User); var followerExist = context.Users.Any(x => x.Username == followerDto.Follower); if (!userExist || !followerExist) { sb.AppendLine(errorMessage); continue; } int?followerId = context.Users .FirstOrDefault(x => x.Username == followerDto.Follower)?.Id; int?userId = context.Users .FirstOrDefault(x => x.Username == followerDto.User)?.Id; bool alreadyFollowed = followersList.Any(f => f.UserId == userId && f.FollowerId == followerId); if (alreadyFollowed) { sb.AppendLine(errorMessage); continue; } var newEntity = new UserFollower() { FollowerId = followerId.Value, UserId = userId.Value }; followersList.Add(newEntity); sb.AppendLine($"Successfully imported Follower {followerDto.Follower} to User {followerDto.User}."); } context.UserFollowers.AddRange(followersList); context.SaveChanges(); string result = sb.ToString(); return(result); }
public static string ImportFollowers(InstagraphContext context, string jsonString) { var deserializedFollowers = JsonConvert.DeserializeObject <FollowerDto[]>(jsonString); var sb = new StringBuilder(); var validFollowers = new List <UserFollower>(); foreach (var dto in deserializedFollowers) { if (!IsValid(dto)) { sb.AppendLine(ErrorMessage); continue; } var user = context.Users .SingleOrDefault(u => u.Username.Equals(dto.User, StringComparison.OrdinalIgnoreCase)); if (user == null) { sb.AppendLine(ErrorMessage); continue; } var follower = context.Users .SingleOrDefault(u => u.Username.Equals(dto.Follower, StringComparison.OrdinalIgnoreCase)); if (follower == null) { sb.AppendLine(ErrorMessage); continue; } var isDuplicateEntry = validFollowers .Any(uf => uf.UserId == user.Id && uf.FollowerId == follower.Id); if (isDuplicateEntry) { sb.AppendLine(ErrorMessage); continue; } var mapping = new UserFollower { UserId = user.Id, FollowerId = follower.Id }; validFollowers.Add(mapping); sb.AppendLine(string.Format(SuccessFollowerMessage, dto.Follower, dto.User)); } context.UsersFollowers.AddRange(validFollowers); context.SaveChanges(); var result = sb.ToString(); return(result); }
public async Task Follow(string followerId, string followingId) { var userFollower = new UserFollower { UserId = followerId, FollowingUserId = followingId }; await userFollowerRepository.Create(userFollower); }
public static string ImportFollowers(InstagraphContext context, string jsonString) { UserFollowerDTO[] usersFollowerDTOs = JsonConvert.DeserializeObject <UserFollowerDTO[]>(jsonString); Dictionary <string, User> users = context.Users.ToDictionary(u => u.Username); StringBuilder sb = new StringBuilder(); HashSet <string> alreadyAddedPairs = new HashSet <string>(); foreach (UserFollowerDTO userFollowerDTO in usersFollowerDTOs) { bool validInput = !string.IsNullOrWhiteSpace(userFollowerDTO.User) && !string.IsNullOrWhiteSpace(userFollowerDTO.Follower); // bool userAndFollowerAreDifferent = !userFollowerDTO.Follower.Equals(userFollowerDTO.User); if (validInput //&& userAndFollowerAreDifferent ) { bool userExists = users.ContainsKey(userFollowerDTO.User); bool followerExists = users.ContainsKey(userFollowerDTO.Follower); if (userExists && followerExists) { User user = users[userFollowerDTO.User]; User follower = users[userFollowerDTO.Follower]; bool pairAlreadyExists = alreadyAddedPairs.Contains(userFollowerDTO.User + userFollowerDTO.Follower); if (!pairAlreadyExists) { UserFollower newUserFollower = new UserFollower() { UserId = user.Id, User = user, FollowerId = follower.Id, Follower = follower }; context.UsersFollowers.Add(newUserFollower); sb.AppendLine($"Successfully imported Follower {userFollowerDTO.Follower} to User {userFollowerDTO.User}."); alreadyAddedPairs.Add(userFollowerDTO.User + userFollowerDTO.Follower); continue; } } } sb.AppendLine("Error: Invalid data."); } context.SaveChanges(); string result = sb.ToString().Trim(); return(result); }
private void RemoveUser(Guid userId, Guid followerUserId) { var userFollower = new UserFollower() { UserId = userId, FollowerUserId = followerUserId, }; DBContext.Delete(userFollower.GetDeleteQuery()); }
public static string ImportFollowers(InstagraphContext context, string jsonString) { var sb = new StringBuilder(); var jsonUsers = JsonConvert.DeserializeObject <UserFollowerDto[]>(jsonString); foreach (var user in jsonUsers) { string usernameToFollow = user.User; string usernameFollower = user.Follower; if (usernameToFollow == null || usernameFollower == null) { sb.AppendLine(ReturnMessages.Error); continue; } int userFollowerId = context.Users .Where(u => u.Username == usernameFollower) .Select(u => u.Id) .FirstOrDefault(); int userFollowedId = context.Users .Where(u => u.Username == usernameToFollow) .Select(u => u.Id) .FirstOrDefault(); bool followedWithFollowerExist = context.UsersFollowers .Any(uf => uf.UserId == userFollowedId && uf.FollowerId == userFollowerId); if (userFollowerId >= 1 && userFollowedId >= 1 && !followedWithFollowerExist) { sb.AppendLine(String.Format(ReturnMessages.UserFollower, usernameFollower, usernameToFollow)); UserFollower userFollower = new UserFollower { UserId = userFollowedId, FollowerId = userFollowerId }; context.Users .FirstOrDefault(u => u.Username == usernameToFollow) .Followers .Add(userFollower); context.SaveChanges(); } else { sb.AppendLine(ReturnMessages.Error); } } return(sb.ToString()); }
private void AddUser(Guid userId, Guid followerUserId) { var userFollower = new UserFollower() { UserId = userId, FollowerUserId = followerUserId, CreatedDate = DateTime.Now }; DBContext.Create(userFollower.GetInsertQuery()); }
public static string ImportFollowers(InstagraphContext context, string jsonString) { var deserializedUserFollowers = JsonConvert.DeserializeObject <UserFollowerDto[]>(jsonString); var validUserFollowers = new List <UserFollower>(); var sb = new StringBuilder(); foreach (var ufDto in deserializedUserFollowers) { if (!IsValid(ufDto)) { sb.AppendLine(ErrorMessage); continue; } bool userExists = context.Users.Any(u => u.Username == ufDto.User); bool followerExists = context.Users.Any(u => u.Username == ufDto.Follower); if (!userExists || !followerExists) { sb.AppendLine(ErrorMessage); continue; } User user = FindUser(context, ufDto.User); User follower = FindUser(context, ufDto.Follower); UserFollower userFollower = new UserFollower { User = user, Follower = follower, }; bool userFollowerExists = validUserFollowers.Any(a => a.Follower == follower && a.User == user); if (userFollowerExists) { sb.AppendLine(ErrorMessage); continue; } validUserFollowers.Add(userFollower); sb.AppendLine(string.Format(SuccessfullyImportedUsersFollowersMessage, follower.Username, user.Username)); } context.UsersFollowers.AddRange(validUserFollowers); context.SaveChanges(); string result = sb.ToString().TrimEnd(); return(result); }
public async Task AddFollowee(Guid followerId, Guid followeeId) { var userFollower = new UserFollower(followerId, followeeId); if (!await _twitterLikeDbContext.Users.AnyAsync(u => u.Id == userFollower.FolloweeId) && !await _twitterLikeDbContext.Users.AnyAsync(u => u.Id == userFollower.FollowerId)) { throw new NotFoundException(nameof(Tweet)); } _twitterLikeDbContext.Entry(userFollower).State = EntityState.Added; await _twitterLikeDbContext.SaveChangesAsync(); }
public async Task FollowUser(string followerId, string id) { var userFollower = new UserFollower { UserId = id, FollowerId = followerId, }; await this.userFollowerRepository.AddAsync(userFollower); await this.userFollowerRepository.SaveChangesAsync(); }
public void GetPlayerFollowers(string username, int pageIndex, int prePage, bool isDynamicLoad = false) { //start processbar _processBarHelper.StartProcessBar(); //register pages RegisterPaginationInfo(PagintaionType.ProfileFollowers, pageIndex, prePage); UserRequestHelper userProfileHelper = new UserRequestHelper(); userProfileHelper.GetUserFollowerData(username, pageIndex, prePage); userProfileHelper.AsyncUserProfileComplated += (responseData, ex) => { //end processbar _processBarHelper.EndProcessBar(); if (ex == null) { #region get user follower data UserFollower userFollowerData = null; if (responseData != null) { userFollowerData = responseData as UserFollower; } if (userFollowerData != null) { if (!isDynamicLoad) { _userFollowersCol.Clear(); } //update total pages UpdateTotalPage(PagintaionType.ProfileFollowers, userFollowerData.Pages); if (userFollowerData.Players.Count > 0) { userFollowerData.Players.ForEach(queryEntity => { this._userFollowersCol.Add(queryEntity); }); } } #endregion } else { string errorMessage = responseData.ToString(); if (!string.IsNullOrEmpty(errorMessage)) { new ToastNotifyHelper().ShowCoding4FunToastNotify(errorMessage, "Tip"); } } }; }
public static string ImportFollowers(InstagraphContext context, string jsonString) { UserFollowerDto[] usersFollowersFromJson = ImportFromJson <UserFollowerDto>(jsonString, false); StringBuilder sb = new StringBuilder(); List <UserFollower> resultUsersFollowers = new List <UserFollower>(); foreach (UserFollowerDto userFollowerDto in usersFollowersFromJson) { if (!IsValid(userFollowerDto)) { sb.AppendLine(FailureMsg); continue; } User user = context.Users.SingleOrDefault(u => u.Username == userFollowerDto.User); User follower = context.Users.SingleOrDefault(f => f.Username == userFollowerDto.Follower); if (user == null || follower == null) { sb.AppendLine(FailureMsg); continue; } bool userFollowerAlreadyExists = resultUsersFollowers.Any(uf => uf.User.Username == userFollowerDto.User && uf.Follower.Username == userFollowerDto.Follower); if (userFollowerAlreadyExists) { sb.AppendLine(FailureMsg); continue; } UserFollower currentUserFollower = new UserFollower { User = user, Follower = follower }; resultUsersFollowers.Add(currentUserFollower); sb.AppendLine(string.Format(SuccessfullyImportedUserFollowerMsg, currentUserFollower.Follower.Username, currentUserFollower.User.Username)); } context.UsersFollowers.AddRange(resultUsersFollowers); context.SaveChanges(); string result = sb.ToString().Trim(); return(result); }
public static string ImportFollowers(InstagraphContext context, string jsonString) { UserFollowerDto[] deserializedFollowers = JsonConvert.DeserializeObject <UserFollowerDto[]>(jsonString); var sb = new StringBuilder(); var userFollowers = new List <UserFollower>(); foreach (var userFollowerDto in deserializedFollowers) { if (!IsValid(userFollowerDto)) { sb.AppendLine(errorMsg); continue; } var user = context.Users.SingleOrDefault(u => u.Username == userFollowerDto.User); var follower = context.Users.SingleOrDefault(u => u.Username == userFollowerDto.Follower); if (user == null || follower == null) { sb.AppendLine(errorMsg); continue; } bool alreadyFollowed = userFollowers.Any(uf => uf.User == user && uf.Follower == follower); if (alreadyFollowed) { sb.AppendLine(errorMsg); continue; } var userFollower = new UserFollower() { User = user, Follower = follower }; userFollowers.Add(userFollower); sb.AppendLine(String.Format(successMsg, $"Follower {userFollowerDto.Follower} to User {userFollowerDto.User}")); } context.UsersFollowers.AddRange(userFollowers); context.SaveChanges(); string result = sb.ToString().TrimEnd(); return(result); }
public static string ImportFollowers(InstagraphContext context, string jsonString) { var userFollowers = JsonConvert.DeserializeObject <FollowerDto[]>(jsonString); var sb = new StringBuilder(); var validFollowers = new List <UserFollower>(); foreach (var userFollDto in userFollowers) { if (!IsValid(userFollDto)) { sb.AppendLine(ErrorMsg); continue; } var user = context.Users.FirstOrDefault(u => u.Username == userFollDto.User); var follower = context.Users.FirstOrDefault(u => u.Username == userFollDto.Follower); if (user == null || follower == null) { sb.AppendLine(ErrorMsg); continue; } var userFollower = new UserFollower { User = user, Follower = follower }; var alreadyFollowers = validFollowers .Where(vf => vf.User == user) .ToArray(); if (alreadyFollowers.Any(af => af.Follower == follower)) { sb.AppendLine(ErrorMsg); continue; } validFollowers.Add(userFollower); sb.AppendLine(string.Format(SuccessUserFollwerMsg, follower.Username, user.Username)); } context.UsersFollowers.AddRange(validFollowers); context.SaveChanges(); var result = sb.ToString().TrimEnd(); return(result); }
public static string ImportFollowers(InstagraphContext context, string jsonString) { var sb = new StringBuilder(); var deserializedUserFollowers = JsonConvert.DeserializeObject <UserFollowerDto[]>(jsonString); var validUserFollowers = new List <UserFollower>(); foreach (var userFollowerDto in deserializedUserFollowers) { if (!IsValid(userFollowerDto)) { sb.AppendLine(FailureMessage); continue; } var userExists = context.Users.Any(u => u.Username == userFollowerDto.User); var followerExists = context.Users.Any(u => u.Username == userFollowerDto.Follower); if (userExists && followerExists) { var userId = context.Users.SingleOrDefault(u => u.Username == userFollowerDto.User).Id; var followerId = context.Users.FirstOrDefault(u => u.Username == userFollowerDto.Follower).Id; var isUFAlreadyAdded = validUserFollowers.Any(uf => uf.UserId == userId && uf.FollowerId == followerId); if (isUFAlreadyAdded) { sb.AppendLine(FailureMessage); continue; } var userFollower = new UserFollower() { UserId = userId, FollowerId = followerId }; validUserFollowers.Add(userFollower); sb.AppendLine(String.Format(SuccessMessage, $"Follower {userFollowerDto.Follower} to User {userFollowerDto.User}")); } else { sb.AppendLine(FailureMessage); continue; } } context.UsersFollowers.AddRange(validUserFollowers); context.SaveChanges(); return(sb.ToString()); }
public static string ImportFollowers(InstagraphContext context, string jsonString) { var sb = new StringBuilder(); var serializer = JsonConvert.DeserializeObject <List <ImportFollowerDto> >(jsonString); var followersToAdd = new List <UserFollower>(); foreach (var followerDto in serializer) { if (!IsValid(followerDto)) { sb.AppendLine(ErrorMessage); continue; } var user = context.Users.FirstOrDefault(u => u.Username == followerDto.User); var follower = context.Users.FirstOrDefault(u => u.Username == followerDto.Follower); var isFollowing = followersToAdd.Any(u => u.User == user && u.Follower == follower); if (user == null || follower == null || isFollowing) { sb.AppendLine(ErrorMessage); continue; } var userFollower = new UserFollower() { User = user, Follower = follower, }; followersToAdd.Add(userFollower); sb.AppendLine($"Successfully imported Follower {follower.Username}to User {user.Username}."); } context.UsersFollowers.AddRange(followersToAdd); context.SaveChanges(); return(sb.ToString().Trim()); }
public bool RemoveFollowee(Guid followerKey, Guid followeeKey) { UserFollower userFollower = _userFollowerRepository.GetAll().FirstOrDefault(uf => uf.Follower.Key == followerKey && uf.Followee.Key == followeeKey); _userFollowerRepository.Delete(userFollower); try { _userFollowerRepository.Save(); return(true); } catch (Exception e) { return(false); } }
private async Task <bool> FollowOrUnFollowByAction(FollowActions followAction, string userToFollowOrUnFollowId) { User loggedUser = await _userManager.GetUserAsync(User); _logger.LogInfo($"Controller : ManageFollowers | Action : Follow | User : {loggedUser.Id} "); List <UserFollower> followedUsers = await GetExistingRecordForUserFollowed(loggedUser.Id, userToFollowOrUnFollowId); UserFollower followedUser = new UserFollower() { UserId = loggedUser.Id, FollowerId = userToFollowOrUnFollowId, FollowAction = followAction }; if (followedUsers.Count == 0) { _unitofWork.UserFollowerRepository.Add(followedUser); } else { followedUser.Id = followedUsers.Find(p => p.UserId == loggedUser.Id && p.FollowerId == userToFollowOrUnFollowId).Id; _unitofWork.UserFollowerRepository.Update(followedUser); } if (await _unitofWork.SaveAsync()) { var following = await _userManager.FindByIdAsync(userToFollowOrUnFollowId); if (followAction == FollowActions.Following) { TempData["FollowerActionMsg"] = $"You are now following {following.FirstName} {following.LastName}."; } else { TempData["FollowerActionMsg"] = $"You have un-followed {following.FirstName} {following.LastName}."; } return(true); } else { _logger.LogError("Error in operation"); } return(false); }
public static string ImportFollowers(InstagraphContext context, string jsonString) { UserFollowerDto[] deserializedFollowers = JsonConvert.DeserializeObject <UserFollowerDto[]>(jsonString); StringBuilder sb = new StringBuilder(); var userFollowers = new List <UserFollower>(); foreach (var dto in deserializedFollowers) { int?userId = context.Users .FirstOrDefault(u => u.Username == dto.User)?.Id; // Така се взима директно Id-то на намерения user и се дори ако е null !!! int?followerId = context.Users .FirstOrDefault(u => u.Username == dto.Follower)?.Id; if (userId == null || followerId == null) { sb.AppendLine(errorMsg); continue; } // проверка имаме ли такива в кол-та: bool alreadyFollowed = userFollowers.Any(f => f.UserId == userId && f.FollowerId == followerId); if (alreadyFollowed) { sb.AppendLine(errorMsg); continue; } // Тук няма да ползваме AutoMapper защото не е добре той да бърка в базата, затова на ръка: var userFollower = new UserFollower() { UserId = userId.Value, FollowerId = followerId.Value }; userFollowers.Add(userFollower); sb.AppendLine(String.Format(successMsg, $"Follower {dto.Follower} to User {dto.User}")); } context.UsersFollowers.AddRange(userFollowers); context.SaveChanges(); string result = sb.ToString().TrimEnd(); return(result); }
public static string ImportFollowers(InstagraphContext context, string jsonString) { var deserializedUF = JsonConvert.DeserializeObject <ImportUserFollowerDto[]>(jsonString); StringBuilder sb = new StringBuilder(); var validUserFollowers = new List <UserFollower>(); foreach (var userFollowDto in deserializedUF) { var user = context.Users .FirstOrDefault(x => x.Username == userFollowDto.User); var follower = context.Users .FirstOrDefault(x => x.Username == userFollowDto.Follower); bool bothExist = user != null && follower != null; if (!IsValid(userFollowDto) || !bothExist) { sb.AppendLine(errorMessage); continue; } bool alreadyFollower = validUserFollowers.Any(x => x.UserId == user.Id && x.FollowerId == follower.Id); if (alreadyFollower) { sb.AppendLine(errorMessage); continue; } var userFollow = new UserFollower() { UserId = user.Id, FollowerId = follower.Id }; validUserFollowers.Add(userFollow); sb.AppendLine(string.Format(successMessage, $"Follower {follower.Username} to User {user.Username}")); } context.UsersFollowers.AddRange(validUserFollowers); context.SaveChanges(); string result = sb.ToString().Trim(); return(result); }
private UserFollower GetUserFollower(Guid userId, Guid followerUserId) { var queryString = string.Format("SELECT * FROM [dbo].[UserFollowers] WHERE UserId='{0}' AND FollowerUserId={1}", userId, followerUserId); using (SqlConnection sqlConn = DatabaseHelper.GetConnection()) { var reader = SqlHelper.ExecuteReader(sqlConn, CommandType.Text, queryString); while (reader.Read()) { return(UserFollower.From(reader)); } } return(null); }
public ResponseResult <UserFollowerVM> FollowUser(UserFollowerVM userFollowerVM, bool follow) { ResponseResult <UserFollowerVM> responseResult = new ResponseResult <UserFollowerVM> { Success = true, Data = userFollowerVM }; try { UserFollowerRepository userFollowerRepository = new UserFollowerRepository(dbContext); UserFollower userFollower = userFollowerRepository.GetFollower(userFollowerVM.FollowByUserId, userFollowerVM.FollowedUserId); if (follow) { if (userFollower == null) { userFollower = new UserFollower(); Mapper.Map(userFollowerVM, userFollower); userFollowerRepository.Insert(userFollower); userFollowerRepository.SaveChanges(); //Add User Action Log new LogsSerivce().RunAddLogTask(_LogActionType.Follow, userFollowerVM.FollowByUserId, userFollowerVM.FollowedUserId); } } else { if (userFollower != null) { userFollowerRepository.Delete(userFollower); userFollowerRepository.SaveChanges(); //Add User Action Log new LogsSerivce().RunAddLogTask(_LogActionType.UnFollow, userFollowerVM.FollowByUserId, userFollowerVM.FollowedUserId); } } userFollowerVM.Id = userFollower.Id; } catch { responseResult.Success = false; responseResult.Message = "Oops! You are unable to follow this user."; } return(responseResult); }
public static string ImportFollowers(InstagraphContext context, string jsonString) { var userFollowerDtos = JsonConvert.DeserializeObject <List <UserFollowerDto> >(jsonString); List <UserFollower> userFollowers = new List <UserFollower>(); StringBuilder stringBuilder = new StringBuilder(); foreach (var userFollowerDto in userFollowerDtos) { if (!IsValid(userFollowerDto)) { stringBuilder.AppendLine("Error: Invalid data."); continue; } var user = context.Users.FirstOrDefault(x => x.Username == userFollowerDto.User); var follower = context.Users.FirstOrDefault(x => x.Username == userFollowerDto.Follower); if (user == null || follower == null) { stringBuilder.AppendLine("Error: Invalid data."); continue; } var isUserFollowerExist = context.UserFollowers.Any(x => x.UserId == user.Id && x.FollowerId == follower.Id); if (isUserFollowerExist || userFollowers.Any(x => x.User.Username == user.Username && x.Follower.Username == follower.Username)) { stringBuilder.AppendLine("Error: Invalid data."); continue; } var userFollower = new UserFollower { Follower = follower, User = user }; userFollowers.Add(userFollower); stringBuilder.AppendLine($"Successfully imported Follower {follower.Username} to User {user.Username}."); } context.UserFollowers.AddRange(userFollowers); context.SaveChanges(); return(stringBuilder.ToString().TrimEnd()); }
public static string ImportFollowers(InstagraphContext context, string jsonString) { var jsonFollowers = JsonConvert.DeserializeObject <UserFollowDto[]>(jsonString); var sb = new StringBuilder(); var userFollowers = new List <UserFollower>(); foreach (var jsf in jsonFollowers) { bool isValidUsers = context.Users.Any(u => u.Username == jsf.User) && context.Users.Any(u => u.Username == jsf.Follower); bool areSame = jsf.User == jsf.Follower; if (!isValidUsers || areSame) { sb.AppendLine("Error: Invalid data."); continue; } var user = context.Users.Where(u => u.Username == jsf.User).SingleOrDefault(); var follower = context.Users.Where(u => u.Username == jsf.Follower).SingleOrDefault(); bool areAdded = userFollowers.Any(uf => uf.UserId == user.Id && uf.FollowerId == follower.Id); if (areAdded) { sb.AppendLine("Error: Invalid data."); continue; } var userFollower = new UserFollower() { UserId = user.Id, User = user, FollowerId = follower.Id, Follower = follower }; sb.AppendLine($"Successfully imported Follower {jsf.Follower} to User {jsf.User}."); userFollowers.Add(userFollower); } context.UsersFollowers.AddRange(userFollowers); context.SaveChanges(); return(sb.ToString().Trim()); }
public static string ImportFollowers(InstagraphContext context, string jsonString) { var deserializedFollowers = JsonConvert.DeserializeObject <UserFollowerDto[]>(jsonString); StringBuilder sb = new StringBuilder(); var followers = new List <UserFollower>(); foreach (var dto in deserializedFollowers) { int?userId = context.Users.FirstOrDefault(u => u.Username == dto.User)?.Id; int?followerId = context.Users.FirstOrDefault(f => f.Username == dto.Follower)?.Id; if (userId == null || followerId == null) { sb.AppendLine(errorMsg); continue; } bool alreadyFollowed = followers.Any(f => f.UserId == userId && f.FollowerId == followerId); if (alreadyFollowed) { sb.AppendLine(errorMsg); continue; } var userFollower = new UserFollower() { UserId = userId.Value, FollowerId = followerId.Value }; followers.Add(userFollower); sb.AppendLine(String.Format(successMsg, $"Follower {dto.Follower} to User {dto.User}")); } context.UsersFollowers.AddRange(followers); context.SaveChanges(); string result = sb.ToString().TrimEnd(); return(result); }
// MakeFollowers skráir niður í UserFollowers, venslin þegar þú fylgir einnhverjum public void MakeFollowers(string myId, string otherId) { using (var dataContext = new ApplicationDbContext()) { var myRepo = new UserRepository <UserFollower>(dataContext); UserFollower followRelation = new UserFollower() { userID = otherId, followerID = myId }; myRepo.Insert(followRelation); dataContext.SaveChanges(); } }
public async Task Follow(string userToFollowId, string currentUserId) { var userFollower = new UserFollower() { UserId = userToFollowId, FollowerId = currentUserId, }; if (this.IsBeingFollowedBy(userToFollowId, currentUserId)) { return; } await this.dbContext.UserFollowers.AddAsync(userFollower); await this.dbContext.SaveChangesAsync(); }
public static string ImportFollowers(InstagraphContext context, string jsonString) { var followerDtos = JsonConvert.DeserializeObject <List <FollowerImporrtDto> >(jsonString); var followersToAdd = new HashSet <UserFollower>(); var sb = new StringBuilder(); foreach (var followerDto in followerDtos) { User user = context.Users .FirstOrDefault(u => u.Username == followerDto.User); User follower = context.Users .FirstOrDefault(u => u.Username == followerDto.Follower); UserFollower userFollower = context.UsersFollowers .FirstOrDefault(uf => uf.User.Username == followerDto.User && uf.Follower.Username == followerDto.Follower); bool isValid = user != null && follower != null && userFollower == null; if (!isValid) { sb.AppendLine(FailureMsg); continue; } bool alreadyFollowed = followersToAdd.Any(f => f.UserId == user.Id && f.FollowerId == follower.Id); if (alreadyFollowed) { sb.AppendLine(FailureMsg); continue; } followersToAdd.Add(new UserFollower { UserId = user.Id, FollowerId = follower.Id }); sb.AppendLine(string.Format(SuccessMsg, $"Follower {follower.Username} to User {user.Username}")); } context.UsersFollowers.AddRange(followersToAdd); context.SaveChanges(); return(sb.ToString().Trim()); }