public async Task OnGet(int index = 1, string query = null) { CurrentQuery = query; IEnumerable <User_Picture> list = new User_Picture[0]; PaginatedPhotos result = null; if (query != null) { result = await _search.PaginatedSearchAsync(query, index); IEnumerable <int> userPics = new int[0]; if (result != null && result.Photos.Count() > 0) { if (User.Identity.IsAuthenticated) { var userId = User.FindFirst("userId"); userPics = await _caching.GetVotesByUserIdAsync(userId.Value); if (userPics == null || userPics.Count() == 0) { var picsVoted = await _dataAccess.GetVotesByUserIdAsync(userId.Value); _caching.InsertVotesAsync(picsVoted); userPics = picsVoted.Select(x => x.Picture_Id); } list = result.Photos.Select(item => new User_Picture { Id = item.PhotoId, Rating = item.Data.Rating, Thumbnail_Url = item.Data.Thumbnail_Url, Url = item.Data.Url, Author = item.User.UserId, Votes = item.Data.Votes, IsVoted = userId.Value == item.User.UserId || userPics.Any(vote => vote == item.PhotoId) }); } else { list = result.Photos.Select(item => new User_Picture { Id = item.PhotoId, Rating = item.Data.Rating, Thumbnail_Url = item.Data.Thumbnail_Url, Url = item.Data.Url, Author = item.User.UserId, Votes = item.Data.Votes, IsVoted = true }); } } } Photos = (result != null) ? new PaginatedList <User_Picture>(list, (int)result.TotalResults, index, 6) : new PaginatedList <User_Picture>(new User_Picture[0], 0, 1, 6); }
public async Task <IActionResult> PaginatedSearch(int index, string query) { var result = await _search.PaginatedSearchAsync(query, index); IEnumerable <User_Picture> tosend = null; IEnumerable <int> userPics = new int[0]; if (result != null && result.Photos.Count() > 0) { if (User.Identity.IsAuthenticated) { var userId = User.FindFirst("userId"); userPics = await _caching.GetVotesByUserIdAsync(userId.Value); if (userPics == null || userPics.Count() == 0) { var picsVoted = await _dataAccess.GetVotesByUserIdAsync(userId.Value); _caching.InsertVotesAsync(picsVoted); userPics = picsVoted.Select(x => x.Picture_Id); } tosend = result.Photos.Select(item => new User_Picture { Id = item.PhotoId, Rating = item.Data.Rating, Thumbnail_Url = item.Data.Thumbnail_Url, Url = item.Data.Url, Author = item.User.UserId, Votes = item.Data.Votes, IsVoted = userId.Value == item.User.UserId || userPics.Any(vote => vote == item.PhotoId) }); } else { tosend = result.Photos.Select(item => new User_Picture { Id = item.PhotoId, Rating = item.Data.Rating, Thumbnail_Url = item.Data.Thumbnail_Url, Url = item.Data.Url, Author = item.User.UserId, Votes = item.Data.Votes, IsVoted = true }); } } return(Ok(new { pageCount = (int)Math.Ceiling(result.TotalResults / 6D), photos = tosend })); }
public async Task OnGet(int index = 1) { IEnumerable <User_Picture> userPictures = null; var Pics = await _caching.GetPhotosByScoreAsync((index - 1) * 6, index * 6); if (Pics == null) { Pics = await _dataAccess.GetPaginatedPicturesAsync(index, 6); _caching.InsertPhotosAsync(Pics); } if (Pics != null) { if (!User.Identity.IsAuthenticated) { userPictures = Pics.Select(elem => new User_Picture { Id = elem.Id, Rating = elem.Rating, Votes = elem.Votes, Url = elem.Url, Thumbnail_Url = elem.Thumbnail_Url, Author = elem.User_Id, IsVoted = true }); } else { var userId = User.FindFirst("userId"); var userPics = await _caching.GetVotesByUserIdAsync(userId.Value); if (userPics == null || userPics.Count() == 0) { var picsVoted = await _dataAccess.GetVotesByUserIdAsync(userId.Value); _caching.InsertVotesAsync(picsVoted); userPics = picsVoted.Select(x => x.Picture_Id); } if (userPics != null) { userPictures = Pics.Select(elem => new User_Picture { Id = elem.Id, Rating = elem.Rating, Votes = elem.Votes, Url = elem.Url, Thumbnail_Url = elem.Thumbnail_Url, Author = elem.User_Id, IsVoted = elem.User_Id == userId.Value || userPics.Any(item => item == elem.Id) }); } else { userPictures = Pics.Select(elem => new User_Picture { Id = elem.Id, Rating = elem.Rating, Votes = elem.Votes, Url = elem.Url, Thumbnail_Url = elem.Thumbnail_Url, Author = elem.User_Id, IsVoted = elem.User_Id == userId.Value }); } } } var picCount = await _dataAccess.GetPictureCountAsync(); Pictures = new PaginatedList <User_Picture>(userPictures, (int)picCount, index, 6); }
public async Task <IEnumerable <User_Picture> > GetPictures() { IEnumerable <User_Picture> user_Pictures = null; var Pics = await _caching.GetPhotosAsync(); if (Pics == null) { Pics = await _dataAccess.GetPicturesAsync(); _caching.InsertPhotosAsync(Pics); } if (Pics != null) { if (!User.Identity.IsAuthenticated) { user_Pictures = Pics.Select(elem => new User_Picture { Id = elem.Id, Rating = elem.Rating, Votes = elem.Votes, Url = elem.Url, Thumbnail_Url = elem.Thumbnail_Url, Author = elem.User_Id, IsVoted = true }); } else { var userId = User.FindFirst("userId"); var userPics = await _caching.GetVotesByUserIdAsync(userId.Value); if (userPics == null || userPics.Count() == 0) { var picsVoted = await _dataAccess.GetVotesByUserIdAsync(userId.Value); _caching.InsertVotesAsync(picsVoted); userPics = picsVoted.Select(x => x.Picture_Id); } if (userPics != null) { return(Pics.Select(elem => new User_Picture { Id = elem.Id, Rating = elem.Rating, Votes = elem.Votes, Url = elem.Url, Thumbnail_Url = elem.Thumbnail_Url, Author = elem.User_Id, IsVoted = elem.User_Id == userId.Value || userPics.Any(item => item == elem.Id) })); } else { return(Pics.Select(elem => new User_Picture { Id = elem.Id, Rating = elem.Rating, Votes = elem.Votes, Url = elem.Url, Thumbnail_Url = elem.Thumbnail_Url, Author = elem.User_Id, IsVoted = elem.User_Id == userId.Value })); } } } return(user_Pictures); }