Exemplo n.º 1
0
        private FilterDefinition <CheckedOutBook> CreateBookHistoryFilter(string studentBarCode, bool fullHistory, int?daysBack
                                                                          , BookSearchParameters bookSearchParameters)
        {
            var builder = Builders <CheckedOutBook> .Filter;
            var filter  = fullHistory ? builder.Empty
                               : builder.Eq(bcr => bcr.CheckedInDate, null);

            if (!string.IsNullOrWhiteSpace(studentBarCode))
            {
                filter &= builder.Eq(bcr => bcr.StudentBarCode, studentBarCode);
            }

            if (daysBack != null)
            {
                filter &= builder.Lte(bcr => bcr.CheckedOutDate, DateTime.UtcNow.Date.AddDays(-daysBack.Value));
            }

            if (bookSearchParameters != null)
            {
                if (!string.IsNullOrWhiteSpace(bookSearchParameters.Title))
                {
                    filter &= builder.Regex(b => b.BookCopy.Title, new Regex($".*{bookSearchParameters.Title}.*", RegexOptions.IgnoreCase));
                }
                if (!string.IsNullOrWhiteSpace(bookSearchParameters.Author))
                {
                    filter &= builder.Regex(b => b.BookCopy.Author, new Regex($".*{bookSearchParameters.Author}.*", RegexOptions.IgnoreCase));
                }
                if (!string.IsNullOrWhiteSpace(bookSearchParameters.BookBarCode))
                {
                    filter &= builder.Eq(b => b.BookCopyBarCode, bookSearchParameters.BookBarCode);
                }
                if (!string.IsNullOrWhiteSpace(bookSearchParameters.TeacherName))
                {
                    filter &= builder.Regex(b => b.Student.TeacherName, new Regex($".*{bookSearchParameters.TeacherName}.*", RegexOptions.IgnoreCase));
                }
                if (!string.IsNullOrWhiteSpace(bookSearchParameters.StudentName))
                {
                    filter &= builder.Regex(b => b.Student.FirstName, new Regex($".*{bookSearchParameters.StudentName}.*", RegexOptions.IgnoreCase))
                              | builder.Regex(b => b.Student.LastName, new Regex($".*{bookSearchParameters.StudentName}.*", RegexOptions.IgnoreCase));
                }
                if (!string.IsNullOrWhiteSpace(bookSearchParameters.Grade))
                {
                    filter &= builder.Eq(b => b.Student.Grade, Convert.ToInt32(bookSearchParameters.Grade));
                }
                if (!string.IsNullOrWhiteSpace(bookSearchParameters.BoxNumber))
                {
                    var readingLevel = bookSearchParameters.BoxNumber.Substring(0, 1);
                    var number       = bookSearchParameters.BoxNumber.Length > 1 ? bookSearchParameters.BoxNumber.Substring(1) : null;
                    return(number != null?builder.Eq(b => b.BookCopy.GuidedReadingLevel, readingLevel) &
                           builder.Eq(b => b.BookCopy.BoxNumber, number)
                               : builder.Eq(b => b.BookCopy.GuidedReadingLevel, readingLevel));
                }
            }

            return(filter);
        }
		private void CreateDataAPI()
		{
			_search = new BookSearch();
			_searchParams = new BookSearchParameters();
			_search.AccessKeyId = "xx";
			_search.AssociateTag = "xx";
			_searchParams.Keywords = SearchTerm;
			_searchParams.MaxResults = Count;
		}
Exemplo n.º 3
0
        public async Task <IActionResult> GetCheckedOutBookCopies([FromQuery] string studentBarCode, [FromQuery] bool fullHistory = false
                                                                  , [FromQuery] int?daysBack       = null
                                                                  , [FromQuery] int?offset         = null, [FromQuery] int?pageSize = null
                                                                  , [FromQuery] string sort        = null, [FromQuery] string order = null
                                                                  , [FromQuery] bool exportAsTab   = false
                                                                  , [FromQuery] bool showMultiples = false
                                                                  , [FromQuery] BookSearchParameters parameters = null)
        {
            var checkedOutBooksCollection = mongoDatabase.GetCollection <CheckedOutBook>(fullHistory ? "bookcheckouthistory" : "bookscheckedout");
            var filter = CreateBookHistoryFilter(studentBarCode, fullHistory, daysBack, parameters);

            var checkedOutBooksFind = checkedOutBooksCollection.Find(filter);
            var totalCount          = await checkedOutBooksFind.CountDocumentsAsync();

            if (!string.IsNullOrWhiteSpace(sort))
            {
                foreach (var sortBy in sort.Split(",", StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).Reverse())
                {
                    checkedOutBooksFind = checkedOutBooksFind.Sort(
                        string.Equals(order, "asc", StringComparison.InvariantCultureIgnoreCase) ?
                        Builders <CheckedOutBook> .Sort.Ascending(sortBy) :
                        Builders <CheckedOutBook> .Sort.Descending(sortBy));
                }
            }
            else
            {
                checkedOutBooksFind = checkedOutBooksFind.SortByDescending(cob => cob.CheckedOutDate);
            }

            if (!exportAsTab && !showMultiples)
            {
                if (offset != null)
                {
                    checkedOutBooksFind = checkedOutBooksFind.Skip(offset);
                }

                if (pageSize != null)
                {
                    checkedOutBooksFind = checkedOutBooksFind.Limit(pageSize);
                }
            }

            var checkedOutBooks = await checkedOutBooksFind.ToListAsync();

            if (showMultiples)
            {
                var studentsWithMultiples = checkedOutBooks.GroupBy(b => b.StudentBarCode)
                                            .Where(g => g.Count() > 1)
                                            .Select(g => g.Key)
                                            .ToHashSet();

                checkedOutBooks.RemoveAll(b => !studentsWithMultiples.Contains(b.StudentBarCode));
                totalCount = checkedOutBooks.Count;
            }

            if (exportAsTab)
            {
                return(CheckedOutBooksAsTabDelimited(checkedOutBooks));
            }
            return(Ok(new { Count = totalCount, Data = checkedOutBooks }));
        }