public SearchResultViewModel(SearchResultDTO dto) { this.Result = dto; this.Pagination = new PaginationViewModel() { Controller = "Search", Action = "BookSearch", EntityCount = dto.Results.Count, EntityName = "Results", Page = dto.Page, PageSize = 10, TotalPages = (int)(dto.Total / 10) }; }
internal SearchResultDTO Subset(int page, int pageSize) { if (this.Results.Count < (page * pageSize)) { return(SearchResultDTO.Empty()); } return(new SearchResultDTO() { Id = this.Id, SearchTerm = this.SearchTerm, Results = this.Results.Skip(page * pageSize).Take(pageSize).ToArray(), Page = page + 1, Total = this.Total }); }
public SearchResultDTO Search(string term, int? userId = null) { try { term = term.Trim(); if (String.IsNullOrWhiteSpace(term) || term.Length < 3) { return SearchResultDTO.Empty(); } using (var db = new SDCContext()) { UserProfile profile = null; if (userId != null) profile = db.UserProfiles.FirstOrDefault(p => p.UserId == (int)userId); var booksResult = db.Books .Where(b => b.Shelf.IsVisible && b.Title.Contains(term)) .Select(b => new SearchResultBookDTO() { Id = b.Id, OwnerId = b.Shelf.Owner.UserId, OwnerUserName = b.Shelf.Owner.UserName, OwnerRating = 3.5f, OwnerAvatarUrl = b.Shelf.Owner.Avatar.Url, Title = b.Title, Authors = b.Authors.Select(a => new AuthorDTO() { Id = a.Id, Name = a.Name }).ToList(), BookPictures = b.Pictures.Select(p => new BookPictureDTO() { Url = p.Url }).ToList() }).ToArray(); for(int i = 0; i < booksResult.Length; i++) { booksResult[i].Rank = i + 1; } BookSearch search = new BookSearch() { Date = DateTime.Now, Term = term, User = profile }; db.BookSearches.Add(search); db.SaveChanges(); var result = new SearchResultDTO(search.Id, booksResult, search.Term); _cache.Add(result.Id, result); return result.Subset(0, 10); } } catch (Exception ex) { //return empty result return SearchResultDTO.Empty(); } }
public void Add(int id, SearchResultDTO result) { _cache.Add(id, result); }