public static int NumberOfBooks(string title, DictBookGenre bookGenre)
        {
            using (LibraryContext db = new LibraryContext())
            {
                var result = db.Books.Where(x => (title == null || x.Title.Contains(title)) &&
                                            (bookGenre.BookGenreId == 0 || x.BookGenreId == bookGenre.BookGenreId)
                                            ).Count();

                return(result);
            }
        }
        public ReportBookViewModel()
        {
            BookGenres = DictBookGenreServices.GetAll();
            DictBookGenre filterAll = new DictBookGenre()
            {
                BookGenreId = 0, Name = "All"
            };

            BookGenres.Add(filterAll);
            BookGenres.Move(BookGenres.IndexOf(filterAll), 0);
            numberOfBooks = BookServices.NumberOfBooks(null, new DictBookGenre()
            {
                BookGenreId = 0, Name = null
            });
            NumberOfBorrowsPerTitle = BorrowServices.NumberOfBorrowsPerTitle(SelectedTitle, SelectedBookGenre, FromDate, ToDate, ActualPage, PageSize);
        }
        public static List <BorrowDetailsDTO> NumberOfBorrowsPerTitle(string title, DictBookGenre bookGenre, DateTime?fromDate, DateTime?toDate, int actualPage, int pageSize)
        {
            using (LibraryContext db = new LibraryContext())
            {
                var result = db.Books.
                             Where(x => (title == null || x.Title.Contains(title)) &&
                                   (bookGenre.BookGenreId == 0 || x.BookGenreId == bookGenre.BookGenreId)
                                   ).Select(x => new BorrowDetailsDTO
                {
                    BookTitle    = x.Title,
                    BookAuthor   = x.Author,
                    BookGenre    = x.DictBookGenre.Name,
                    BorrowsCount = x.Borrow.Where(y => (fromDate == null || y.FromDate >= fromDate) &&
                                                  (toDate == null || y.ToDate <= toDate)).Count(),
                }).OrderByDescending(x => x.BorrowsCount).Skip(actualPage * pageSize).Take(pageSize).ToList();

                return(result);
            }
        }
 public AddBookViewModel()
 {
     BookGenres        = DictBookGenreServices.GetAll();
     selectedBookGenre = BookGenres[0];
 }
        public static string Add(string title, string author, DateTime?releaseDate, string isbn, int count, DictBookGenre selectedBookGenre)
        {
            using (LibraryContext db = new LibraryContext())
            {
                string error   = null;
                Book   newBook = new Book();
                newBook.Author = author;
                newBook.Title  = title;

                //if (DateTime.TryParse(releaseDate, out dt) && dt.Year > 1753)
                //{
                //    newBook.ReleaseDate = dt;
                //}
                //else
                //    newBook.ReleaseDate = null;
                newBook.ReleaseDate  = releaseDate;
                newBook.ISBN         = isbn;
                newBook.Count        = count;
                newBook.AddDate      = DateTime.Now;
                newBook.ModifiedDate = DateTime.Now;
                newBook.BookGenreId  = selectedBookGenre.BookGenreId;


                var context = new ValidationContext(newBook, null, null);
                var result  = new List <System.ComponentModel.DataAnnotations.ValidationResult>();
                Validator.TryValidateObject(newBook, context, result, true);

                foreach (var x in result)
                {
                    error = error + x.ErrorMessage + "\n";
                }



                if (error == null)
                {
                    db.Books.Add(newBook);
                    db.SaveChanges();
                }
                return(error);
            }
        }