Exemplo n.º 1
0
        // GET: Books
        public async Task <IActionResult> Index(string bookGenre, string searchString, string currUserId)
        {
            var books = from b in _context.Book
                        select b;

            var userId = _userManager.GetUserId(HttpContext.User);

            //var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);

            books = books.Where(x => x.UserId == userId);

            IQueryable <string> genreQuery = from b in books
                                             orderby b.Genre
                                             select b.Genre;

            if (!string.IsNullOrEmpty(searchString))
            {
                books = books.Where(s => s.Title.Contains(searchString));
            }

            if (!string.IsNullOrEmpty(bookGenre))
            {
                books = books.Where(x => x.Genre == bookGenre);
            }

            var bookGenreVM = new BookGenreViewModel
            {
                Genres = new SelectList(await genreQuery.Distinct().ToListAsync()),
                Books  = await books.ToListAsync()
            };

            return(View(bookGenreVM));
        }
Exemplo n.º 2
0
        public ActionResult Create(BookGenreViewModel vm)
        {
            vm.Genres = genreRepo.GetAll();
            if (ModelState.IsValid)
            {
                var book  = vm.Book;
                var genre = genreRepo.GetById(vm.SelectedGenreId);
                book.Genre = genre;

                var existingBook = bookRepo.GetAll().Where(b => b.Name.ToLower() == book.Name.ToLower()).SingleOrDefault();
                if (existingBook != null)
                {
                    ViewBag.message = string.Format("Već postoji knjiga sa imenom: {0}", book.Name);
                    return(View(vm));
                }


                if (bookRepo.Create(book))
                {
                    return(RedirectToAction("Index"));
                }
            }

            return(View(vm));
        }
Exemplo n.º 3
0
        // GET: Books
        public async Task <IActionResult> Index(string searchString, string bookGenre)
        {
            // Lambda Query
            var books = await _context.Book.ToListAsync();

            // Use LINQ to get list of genres.
            IQueryable <string> genreQuery = from m in _context.Book
                                             orderby m.Genre
                                             select m.Genre;

            if (!String.IsNullOrEmpty(searchString))
            {
                books = books.Where(s => s.Title.Contains(searchString)).ToList();
            }

            if (!string.IsNullOrEmpty(bookGenre))
            {
                books = books.Where(x => x.Genre == bookGenre).ToList();
            }

            //Create BookGenreViewModel
            var bookGenreVM = new BookGenreViewModel
            {
                Genres = new SelectList(await genreQuery.Distinct().ToListAsync()),
                Books  = books
            };

            return(View(bookGenreVM));
        }
Exemplo n.º 4
0
        // GET: Books
        public async Task <IActionResult> Index(string bookGenre, string SearchTitle, string SearchAuthor)
        {
            // Use LINQ to get list of genres.
            IQueryable <string> genreQuery = from b in _context.Book
                                             orderby b.Genre
                                             select b.Genre;

            var books = from b in _context.Book
                        select b;

            if (!string.IsNullOrEmpty(SearchTitle))
            {
                books = books.Where(s => s.Title.Contains(SearchTitle));
            }

            if (!string.IsNullOrEmpty(SearchAuthor))
            {
                books = books.Where(s => s.Author.Contains(SearchAuthor));
            }

            if (!string.IsNullOrEmpty(bookGenre))
            {
                books = books.Where(x => x.Genre == bookGenre);
            }

            var BookGenreVM = new BookGenreViewModel
            {
                Genres = new SelectList(await genreQuery.Distinct().ToListAsync()),
                Books  = await books.ToListAsync()
            };

            return(View(BookGenreVM));
        }
Exemplo n.º 5
0
        // GET: Books
        public async Task <IActionResult> Index(string bookGenre, string searchString)
        {
            // Use LINQ to get list of genres.
            IQueryable <string> genreQuery = from m in _context.Book
                                             orderby m.Genre
                                             select m.Genre;

            var books = from m in _context.Book
                        select m;

            if (!string.IsNullOrEmpty(searchString))
            {
                books = books.Where(s => s.JudulBuku.Contains(searchString));
            }

            if (!string.IsNullOrEmpty(bookGenre))
            {
                books = books.Where(x => x.Genre == bookGenre);
            }

            var bookGenreVM = new BookGenreViewModel
            {
                Genres = new SelectList(await genreQuery.Distinct().ToListAsync()),
                Books  = await books.ToListAsync()
            };

            return(View(bookGenreVM));
        }
Exemplo n.º 6
0
        public ActionResult Create()
        {
            BookGenreViewModel vm = new BookGenreViewModel();
            var genres            = genreRepo.GetAll();

            vm.Genres = genres;
            return(View(vm));
        }
        // GET: Books
        public async Task <IActionResult> Index(string bookGenre, string searchString)
        {
            // Use LINQ to get list of genres.
            IQueryable <string> genreQuery = from m in _context.Book
                                             orderby m.Genre
                                             select m.Genre;


            /* MvcBookUser user = await _userManager.GetUserAsync(HttpContext.User);*/



            var books = from book in _context.Book
                        join bookMvcBookuser in _context.BookMvcBookUser
                        on book.Id equals bookMvcBookuser.BookId

                        where bookMvcBookuser.MvcBookUserId == User.FindFirstValue(ClaimTypes.NameIdentifier)
                        select book;


            /*  from person in _dbContext.Person
             * join detail in _dbContext.PersonDetails on person.Id equals detail.PersonId into Details
             * from m in Details.DefaultIfEmpty()
             * select new
             * {
             *    id = person.Id,
             *    firstname = person.Firstname,
             *    lastname = person.Lastname,
             *    detailText = m.DetailText
             * };*/



            if (!string.IsNullOrEmpty(searchString))
            {
                books = books.Where(s => s.Title.Contains(searchString));
            }

            if (!string.IsNullOrEmpty(bookGenre))
            {
                books = books.Where(x => x.Genre == bookGenre);
            }

            var bookGenreVM = new BookGenreViewModel
            {
                Genres = new SelectList(await genreQuery.Distinct().ToListAsync()),
                Books  = await books.ToListAsync()
            };

            return(View(bookGenreVM));
        }
Exemplo n.º 8
0
        public ActionResult Edit(int id)
        {
            BookGenreViewModel vm = new BookGenreViewModel();
            var genres            = genreRepo.GetAll();

            vm.Genres = genres;
            vm.Book   = bookRepo.GetById(id);

            //bez ovoga ne prosledi selektovanu opciju iz HTML select liste
            // kada se koristi @Html.DropDownListFor helper!
            vm.SelectedGenreId = vm.Book.Genre.Id;

            return(View(vm));
        }
Exemplo n.º 9
0
        //Фильтрация книг
        public async Task <BookGenreViewModel> FilterBooksAsync(string searchString, string bookAutor, string bookGenre, string bookPublisher, string sortedString)
        {
            IQueryable <string> autorQuery = bookRepository.GetAll()
                                             .OrderByExp("Autor")
                                             .Select(b => b.Autor);
            IQueryable <string> genreQuery = bookRepository.GetAll()
                                             .OrderByExp("Genre")
                                             .Select(b => b.Genre);
            IQueryable <string> publisherQuery = bookRepository.GetAll()
                                                 .OrderByExp("Publisher")
                                                 .Select(b => b.Publisher);

            var books = bookRepository.GetAll();

            if (!String.IsNullOrEmpty(searchString))
            {
                books = books.WhereExp("Name", "Contains", searchString);
            }

            if (!string.IsNullOrEmpty(bookAutor))
            {
                books = books.WhereExp("Autor", "Contains", bookAutor);
            }

            if (!string.IsNullOrEmpty(bookGenre))
            {
                books = books.WhereExp("Genre", "Contains", bookGenre);
            }

            if (!string.IsNullOrEmpty(bookPublisher))
            {
                books = books.WhereExp("Publisher", "Contains", bookPublisher);
            }
            if (sortedString != null)
            {
                books = books.OrderByExp(sortedString);
            }

            var bookGenreVM = new BookGenreViewModel
            {
                Autor     = new SelectList(await autorQuery.Distinct().ToListAsync()),
                Genres    = new SelectList(await genreQuery.Distinct().ToListAsync()),
                Publisher = new SelectList(await publisherQuery.Distinct().ToListAsync()),
                Books     = await books.ToListAsync()
            };

            return(bookGenreVM);
        }
Exemplo n.º 10
0
        public async Task <IActionResult> GetBooks()
        {
            var genreList = db.Genres.ToList().OrderBy(e => e.Title);
            List <BookGenreViewModel> genres = new List <BookGenreViewModel>();

            foreach (var genre in genreList)
            {
                var bookGenre = new BookGenreViewModel
                {
                    GenreId = genre.GenreId,
                    Name    = genre.Title
                };

                genres.Add(bookGenre);
            }

            return(Ok(genres));
        }
Exemplo n.º 11
0
        public ActionResult Edit(BookGenreViewModel vm)
        {
            var genres = genreRepo.GetAll();

            if (ModelState.IsValid)
            {
                Book  book  = bookRepo.GetById(vm.Book.Id);
                Genre genre = genreRepo.GetById(vm.SelectedGenreId);

                book.Name  = vm.Book.Name;
                book.Price = vm.Book.Price;
                book.Genre = genre;
                bookRepo.Update(book);
                return(RedirectToAction("Index"));
            }

            return(View(vm));
        }
Exemplo n.º 12
0
        // GET: Books
        public ActionResult Index()
        {
            var books  = _context.Book.ToList();
            var genres = _context.Genre.ToList();

            var viewModel = new BookGenreViewModel()
            {
                books  = books,
                genres = genres
            };

            if (User.IsInRole("Admin"))
            {
                return(View("Index", viewModel));
            }
            else
            {
                return(View("ReadOnlyList", viewModel));
            }
        }
Exemplo n.º 13
0
        public async Task <IActionResult> GetBookById([FromRoute] int id)
        {
            var errorMessage = new ErrorMessageViewModel();
            var error        = new { Error = errorMessage };

            // Get the book by id
            Documents book = db.Documents.Where(e => e.DocumentId == id).SingleOrDefault();

            if (book == null)
            {
                errorMessage.Message = "Could not find book";
                return(Json(error));
            }

            // Get the genres for the book
            List <DocumentGenres>     genreList = db.DocumentGenres.Where(e => e.DocumentId == book.DocumentId).ToList();
            List <BookGenreViewModel> genres    = new List <BookGenreViewModel>();

            foreach (var dbGenre in genreList)
            {
                Genres genre = db.Genres.Where(e => e.GenreId == dbGenre.GenreId).SingleOrDefault();

                if (genre == null)
                {
                    errorMessage.Message = "Could not find the genre for the book";
                    return(Json(error));
                }

                var bookGenre = new BookGenreViewModel
                {
                    GenreId = genre.GenreId,
                    Name    = genre.Title
                };

                genres.Add(bookGenre);
            }

            // Get the authors for the book
            List <DocumentAuthors>     authorList = db.DocumentAuthors.Where(e => e.DocumentId == book.DocumentId).ToList();
            List <BookAuthorViewModel> authors    = new List <BookAuthorViewModel>();

            foreach (var documentAuthor in authorList)
            {
                Authors author = db.Authors.Where(e => e.AuthorId == documentAuthor.AuthorId).SingleOrDefault();

                if (author == null)
                {
                    errorMessage.Message = "Could not find the author for the book";
                    return(Json(error));
                }

                var bookAuthor = new BookAuthorViewModel
                {
                    AuthorId = author.AuthorId,
                    Name     = author.Name
                };

                authors.Add(bookAuthor);
            }

            // Get the insurance information for the book
            InsuranceInformation          bookInsurance        = db.InsuranceInformation.Where(e => e.InsuranceInformationId == book.InsuranceInformationId).SingleOrDefault();
            InsuranceInformationViewModel insuranceInformation = new InsuranceInformationViewModel();

            if (bookInsurance != null)
            {
                insuranceInformation.Cost          = bookInsurance.Cost;
                insuranceInformation.DatePurchased = bookInsurance.DatePurchased.ToString("yyyy-MM-dd");
                insuranceInformation.ReceiptImage  = bookInsurance.ReceiptImage;
                insuranceInformation.IssueDate     = bookInsurance.IssueDate.ToString("yyyy-MM-dd");
            }

            BookDetailsViewModel bookDetails = new BookDetailsViewModel
            {
                Id                   = book.DocumentId,
                Title                = book.Title,
                Authors              = authors.ToArray(),
                Genres               = genres.ToArray(),
                ISBN                 = book.ISBN,
                CheckedOut           = book.CheckedOut,
                Picture              = book.CoverImage,
                Pages                = Convert.ToInt32(book.Pages),
                Publisher            = book.Publisher,
                PublishedDate        = book.PublishedDate.ToString("yyyy-MM-dd"),
                Edition              = book.Edition,
                Description          = book.Description,
                InsuranceInformation = insuranceInformation
            };

            return(Ok(bookDetails));
        }
Exemplo n.º 14
0
        public async Task <IActionResult> GetMyBooks()
        {
            var id   = "";
            var role = "";

            var errorMessage = new ErrorMessageViewModel();
            var error        = new { Error = errorMessage };

            if (User == null)
            {
                errorMessage.Message = "Could not find user for claims";
                return(Json(error));
            }

            try
            {
                id   = User.Claims.Where(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier").SingleOrDefault().Value;
                role = User.Claims.Where(c => c.Type == "http://schemas.microsoft.com/ws/2008/06/identity/claims/role").SingleOrDefault().Value;
            }
            catch (Exception exception)
            {
                errorMessage.Message = "Id or role was not found";
                return(Json(error));
            }

            if (role == "Personal")
            {
                PersonalUsers personalUser = db.PersonalUsers.Where(e => e.UserId == id).SingleOrDefault();

                if (personalUser == null)
                {
                    errorMessage.Message = "Could not find user profile";
                    return(Json(error));
                }

                // Get books for the user
                List <BookDetailsViewModel> books    = new List <BookDetailsViewModel>();
                List <Documents>            bookList = db.Documents.Where(e => e.UserId == id).ToList();

                foreach (Documents item in bookList)
                {
                    // Get the genres for the book
                    List <DocumentGenres>     genreList = db.DocumentGenres.Where(e => e.DocumentId == item.DocumentId).ToList();
                    List <BookGenreViewModel> genres    = new List <BookGenreViewModel>();

                    foreach (var dbGenre in genreList)
                    {
                        Genres genre = db.Genres.Where(e => e.GenreId == dbGenre.GenreId).SingleOrDefault();

                        if (genre == null)
                        {
                            errorMessage.Message = "Could not find the genre for the book";
                            return(Json(error));
                        }

                        var bookGenre = new BookGenreViewModel
                        {
                            GenreId = genre.GenreId,
                            Name    = genre.Title
                        };

                        genres.Add(bookGenre);
                    }

                    // Get the authors for the book
                    List <DocumentAuthors>     authorList = db.DocumentAuthors.Where(e => e.DocumentId == item.DocumentId).ToList();
                    List <BookAuthorViewModel> authors    = new List <BookAuthorViewModel>();

                    foreach (var documentAuthor in authorList)
                    {
                        Authors author = db.Authors.Where(e => e.AuthorId == documentAuthor.AuthorId).SingleOrDefault();

                        if (author == null)
                        {
                            errorMessage.Message = "Could not find the author for the book";
                            return(Json(error));
                        }

                        var bookAuthor = new BookAuthorViewModel
                        {
                            AuthorId = author.AuthorId,
                            Name     = author.Name
                        };

                        authors.Add(bookAuthor);
                    }

                    // Get the insurance information for the book
                    InsuranceInformation          bookInsurance        = db.InsuranceInformation.Where(e => e.InsuranceInformationId == item.InsuranceInformationId).SingleOrDefault();
                    InsuranceInformationViewModel insuranceInformation = new InsuranceInformationViewModel();

                    if (bookInsurance != null)
                    {
                        insuranceInformation.Cost          = bookInsurance.Cost;
                        insuranceInformation.DatePurchased = bookInsurance.DatePurchased.ToString("yyyy-MM-dd");
                        insuranceInformation.ReceiptImage  = bookInsurance.ReceiptImage;
                        insuranceInformation.IssueDate     = bookInsurance.IssueDate.ToString("yyyy-MM-dd");
                    }

                    BookDetailsViewModel book = new BookDetailsViewModel
                    {
                        Id                   = item.DocumentId,
                        Title                = item.Title,
                        Authors              = authors.ToArray(),
                        Genres               = genres.ToArray(),
                        ISBN                 = item.ISBN,
                        CheckedOut           = item.CheckedOut,
                        Picture              = item.CoverImage,
                        Pages                = Convert.ToInt32(item.Pages),
                        Publisher            = item.Publisher,
                        PublishedDate        = item.PublishedDate.ToString("yyyy-MM-dd"),
                        Edition              = item.Edition,
                        Description          = item.Description,
                        InsuranceInformation = insuranceInformation
                    };

                    books.Add(book);
                }

                return(Ok(books));
            }

            else if (role == "Business")
            {
                BusinessUsers businessUser = db.BusinessUsers.Where(e => e.UserId == id).SingleOrDefault();

                if (businessUser == null)
                {
                    errorMessage.Message = "Could not find user profile";
                    return(Json(error));
                }

                // Get books for the user
                List <BookDetailsViewModel> books    = new List <BookDetailsViewModel>();
                List <Documents>            bookList = db.Documents.Where(e => e.UserId == id).ToList();

                foreach (Documents item in bookList)
                {
                    // Get the genres for the book
                    List <DocumentGenres>     genreList = db.DocumentGenres.Where(e => e.DocumentId == item.DocumentId).ToList();
                    List <BookGenreViewModel> genres    = new List <BookGenreViewModel>();

                    foreach (var dbGenre in genreList)
                    {
                        Genres genre = db.Genres.Where(e => e.GenreId == dbGenre.GenreId).SingleOrDefault();

                        if (genre == null)
                        {
                            errorMessage.Message = "Could not find the genre for the book";
                            return(Json(error));
                        }

                        var bookGenre = new BookGenreViewModel
                        {
                            GenreId = genre.GenreId,
                            Name    = genre.Title
                        };

                        genres.Add(bookGenre);
                    }

                    // Get the authors for the book
                    List <DocumentAuthors>     authorList = db.DocumentAuthors.Where(e => e.DocumentId == item.DocumentId).ToList();
                    List <BookAuthorViewModel> authors    = new List <BookAuthorViewModel>();

                    foreach (var documentAuthor in authorList)
                    {
                        Authors author = db.Authors.Where(e => e.AuthorId == documentAuthor.AuthorId).SingleOrDefault();

                        if (author == null)
                        {
                            errorMessage.Message = "Could not find the author for the book";
                            return(Json(error));
                        }

                        var bookAuthor = new BookAuthorViewModel
                        {
                            AuthorId = author.AuthorId,
                            Name     = author.Name
                        };

                        authors.Add(bookAuthor);
                    }

                    // Get the insurance information for the book
                    InsuranceInformation          bookInsurance        = db.InsuranceInformation.Where(e => e.InsuranceInformationId == item.InsuranceInformationId).SingleOrDefault();
                    InsuranceInformationViewModel insuranceInformation = new InsuranceInformationViewModel();

                    if (bookInsurance != null)
                    {
                        insuranceInformation.Cost          = bookInsurance.Cost;
                        insuranceInformation.DatePurchased = bookInsurance.DatePurchased.ToString("yyyy-MM-dd");
                        insuranceInformation.ReceiptImage  = bookInsurance.ReceiptImage;
                        insuranceInformation.IssueDate     = bookInsurance.IssueDate.ToString("yyyy-MM-dd");
                    }

                    BookDetailsViewModel book = new BookDetailsViewModel
                    {
                        Id                   = item.DocumentId,
                        Title                = item.Title,
                        Authors              = authors.ToArray(),
                        Genres               = genres.ToArray(),
                        ISBN                 = item.ISBN,
                        CheckedOut           = item.CheckedOut,
                        Picture              = item.CoverImage,
                        Pages                = Convert.ToInt32(item.Pages),
                        Publisher            = item.Publisher,
                        PublishedDate        = item.PublishedDate.ToString("yyyy-MM-dd"),
                        Edition              = item.Edition,
                        Description          = item.Description,
                        InsuranceInformation = insuranceInformation
                    };

                    books.Add(book);
                }

                return(Ok(books));
            }

            else if (role == "Employee")
            {
                BusinessEmployees employee = db.BusinessEmployees.Where(e => e.UserId == id).SingleOrDefault();

                if (employee == null)
                {
                    errorMessage.Message = "Could not find user profile";
                    return(Json(error));
                }

                // Get the employer
                BusinessUsers businessUser = db.BusinessUsers.Where(e => e.BusinessUserId == employee.BusinessUserId).SingleOrDefault();

                // Get books for the user
                List <BookDetailsViewModel> books    = new List <BookDetailsViewModel>();
                List <Documents>            bookList = db.Documents.Where(e => e.UserId == businessUser.UserId).ToList();

                foreach (Documents item in bookList)
                {
                    // Get the genres for the book
                    List <DocumentGenres>     genreList = db.DocumentGenres.Where(e => e.DocumentId == item.DocumentId).ToList();
                    List <BookGenreViewModel> genres    = new List <BookGenreViewModel>();

                    foreach (var dbGenre in genreList)
                    {
                        Genres genre = db.Genres.Where(e => e.GenreId == dbGenre.GenreId).SingleOrDefault();

                        if (genre == null)
                        {
                            errorMessage.Message = "Could not find the genre for the book";
                            return(Json(error));
                        }

                        var bookGenre = new BookGenreViewModel
                        {
                            GenreId = genre.GenreId,
                            Name    = genre.Title
                        };

                        genres.Add(bookGenre);
                    }

                    // Get the authors for the book
                    List <DocumentAuthors>     authorList = db.DocumentAuthors.Where(e => e.DocumentId == item.DocumentId).ToList();
                    List <BookAuthorViewModel> authors    = new List <BookAuthorViewModel>();

                    foreach (var documentAuthor in authorList)
                    {
                        Authors author = db.Authors.Where(e => e.AuthorId == documentAuthor.AuthorId).SingleOrDefault();

                        if (author == null)
                        {
                            errorMessage.Message = "Could not find the author for the book";
                            return(Json(error));
                        }

                        var bookAuthor = new BookAuthorViewModel
                        {
                            AuthorId = author.AuthorId,
                            Name     = author.Name
                        };

                        authors.Add(bookAuthor);
                    }

                    // Get the insurance information for the book
                    InsuranceInformation          bookInsurance        = db.InsuranceInformation.Where(e => e.InsuranceInformationId == item.InsuranceInformationId).SingleOrDefault();
                    InsuranceInformationViewModel insuranceInformation = new InsuranceInformationViewModel();

                    if (bookInsurance != null)
                    {
                        insuranceInformation.Cost          = bookInsurance.Cost;
                        insuranceInformation.DatePurchased = bookInsurance.DatePurchased.ToString("yyyy-MM-dd");
                        insuranceInformation.ReceiptImage  = bookInsurance.ReceiptImage;
                        insuranceInformation.IssueDate     = bookInsurance.IssueDate.ToString("yyyy-MM-dd");
                    }

                    BookDetailsViewModel book = new BookDetailsViewModel
                    {
                        Id                   = item.DocumentId,
                        Title                = item.Title,
                        Authors              = authors.ToArray(),
                        Genres               = genres.ToArray(),
                        ISBN                 = item.ISBN,
                        CheckedOut           = item.CheckedOut,
                        Picture              = item.CoverImage,
                        Pages                = Convert.ToInt32(item.Pages),
                        Publisher            = item.Publisher,
                        PublishedDate        = item.PublishedDate.ToString("yyyy-MM-dd"),
                        Edition              = item.Edition,
                        Description          = item.Description,
                        InsuranceInformation = insuranceInformation
                    };

                    books.Add(book);
                }

                return(Ok(books));
            }

            errorMessage.Message = "An error has occurred";
            return(Ok(error));
        }