public async Task <ActionResult> AddLike(string username) { var sourceUserId = User.GetUserId(); var likedUser = await _userRepository.GetUserByUsernameAsync(username); var sourceUser = await _likeRepository.GetUserWithLikes(sourceUserId); if (likedUser == null) { return(NotFound()); } if (sourceUser.UserName == username) { return(BadRequest("You can not like yourself")); } var likeUser = await _likeRepository.GetUserLike(sourceUserId, likedUser.Id); if (likeUser != null) { return(BadRequest("You already like this user")); } likeUser = new LikeUser { SourceUserId = sourceUserId, LikedUserId = likedUser.Id }; sourceUser.LikedUsers.Add(likeUser); if (await _userRepository.SaveAllAsync()) { return(Ok()); } return(BadRequest("Failed to like the user")); }
public async Task <IActionResult> ParseProfile(string link) { if (String.IsNullOrEmpty(link)) { return(StatusCode(StatusCodes.Status400BadRequest, "profile link")); } if (_vkApiService.AccessToken == "") { return(StatusCode(StatusCodes.Status500InternalServerError, "5 User authorization failed: access_token has expired.")); } string id = link.Split('/').Last(); string json; Profile profile; List <WallItem> wallItems; try { json = await _vkApiService.ResolveScreenNameAsync(id); dynamic res = JsonConvert.DeserializeObject(json); string type = res.response?.type; if (type == "application") { return(StatusCode(StatusCodes.Status500InternalServerError, "application profile")); } //получаем общие данные о странице json = await _vkApiService.GetProfileAsync(id, type); res = JsonConvert.DeserializeObject(json); string ProfileId = (string)res.response[0].id;; if (type == "group") { ProfileId = "-" + ProfileId; } profile = await _profiles.GetByIdAsync(ProfileId); //если такой профиль уже есть в бд, удаляем, для записи новых данных if (profile != null) { await _profiles.DeleteAsync(profile.Id); } profile = _parseVk.ParseProfile(json, type); //получаем записи со стены wallItems = _parseVk.ParseWall(await _vkApiService.GetWallAsync(profile.Id, 50)); //------------------------- // res = JsonConvert.DeserializeObject(await _vkApiService.GetSubscriptions(profile.Id)); // List<string> SubIds = res.response?.groups.items.ToObject<List<string>>(); // res = JsonConvert.DeserializeObject(await _vkApiService.GetNewsfeed(String.Join(',',SubIds))); //------------------- //полачаем все лайки //для ускорения используется метод execute, который может содержать до 25 запросов var likeUsers = new List <LikeUser>(); for (int i = 0; i < wallItems.Count; i = i + 12) { //делаем выборку по 12 записей, в итоге 24 запроса int count = wallItems.Count - i > 12 ? 12 : wallItems.Count - i; dynamic likes; //ограничение максимум 3 запроса в сек. await Task.Delay(350); //var likesJson = await _vkApiService.GetLikesAsync(profile.Id, item.ItemId, "post"); // var likesJson = await _vkApiService.GetLikeUsersAsync(profile.Id, item.ItemId, "post"); var likesJson = await _vkApiService.GetLikeUsersAsync(profile.Id, String.Join(',', wallItems.GetRange(i, count).Select(wi => wi.ItemId)), "post", 300); likes = JsonConvert.DeserializeObject(likesJson); Debug.WriteLine("wallItemsParseCount: " + i); // записываем в бд о пользователе и количестве лайков на стене foreach (var ul in likes.response) { var likeUser = likeUsers.FirstOrDefault(l => l.OwnerId == ul.id.ToString()); if (likeUser == null) { likeUser = new LikeUser { OwnerId = ul.id.ToString(), ProfileId = profile.Id, FullName = ul.first_name.ToString() + " " + ul.last_name.ToString(), PhotoUrl = ul.photo_100, LikeCount = 1 }; likeUsers.Add(likeUser); } else { likeUser.LikeCount++; Debug.WriteLine("yyyyy:"); } } } //------------------ profile.WallItems = wallItems; profile.LikeUsers = likeUsers; await _profiles.CreateAsync(profile); } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message)); } return(Ok(JsonConvert.SerializeObject(profile))); }