コード例 #1
0
ファイル: ServiceLayer.cs プロジェクト: teo-mateo/sdc
        public SearchResultDTO Search(string term, int? userId)
        {
            term = term.Trim();
            if(String.IsNullOrWhiteSpace(term) || term.Length < 3)
            {
                //return empty result
                return new SearchResultDTO()
                {
                    Id = -1,
                    Results = new SearchResultEntryDTO[0],
                    SearchTerm = null
                };
            }

            using (var db = new SDCContext())
            {
                //simple stuff:
                //return books that contain the term in their title.

                UserProfile profile = null;
                if (userId != null)
                    profile = db.UserProfiles.FirstOrDefault(p => p.UserId == (int)userId);

                var booksResult = (from b in db.Books
                                   where b.Shelf.IsVisible && b.Title.Contains(term)
                                   select new SearchResultBookDTO()
                                   {
                                       Id = b.Id,
                                       OwnerId = b.Shelf.Owner.UserId,
                                       OwnerUserName = b.Shelf.Owner.UserName,
                                       Title = b.Title,
                                       Authors = b.Authors.Select(a => new AuthorDTO()
                                       {
                                           Id = a.Id,
                                           Name = a.Name
                                       }).ToList()
                                   }).ToArray();

                BookSearch search = new BookSearch()
                {
                    Date = DateTime.Now,
                    Term = term,
                    User = profile
                };

                db.BookSearches.Add(search);

                return new SearchResultDTO()
                {
                    Id = search.Id,
                    Results = booksResult,
                    SearchTerm = term
                };
            }
        }
コード例 #2
0
ファイル: SDCService.cs プロジェクト: teo-mateo/sdc
        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();
            }
        }