Beispiel #1
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));
        }
Beispiel #2
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));
        }