コード例 #1
0
        public async Task <IActionResult> UpdateEmployee([FromRoute] string id, [FromBody] BusinessEmployeeViewModel employeeModel)
        {
            var errorMessage = new ErrorMessageViewModel();
            var error        = new { Error = errorMessage };

            // Get the employee from the database
            BusinessEmployees employee = db.BusinessEmployees.Where(e => e.UserId == id).SingleOrDefault();

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

            ApplicationUser user = db.ApplicationUser.Where(e => e.Id == id).SingleOrDefault();

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

            // Update information
            employee.FirstName      = employeeModel.FirstName;
            employee.LastName       = employeeModel.LastName;
            employee.Position       = employeeModel.Position;
            employee.CanEditLibrary = employeeModel.CanEditLibrary;

            user.Email = employeeModel.Email;

            db.Entry(employee).State = EntityState.Modified;

            try
            {
                await _userManager.UpdateNormalizedEmailAsync(user);

                db.SaveChanges();
            }
            catch (Exception exception)
            {
                errorMessage.Message = "Could not update employee information";
                return(Json(error));
            }

            // Model data to return to view
            var result = new BusinessEmployeeViewModel
            {
                Id             = employee.UserId,
                FirstName      = employee.FirstName,
                LastName       = employee.LastName,
                Position       = employee.Position,
                ProfilePicture = employee.ProfilePicture,
                CanEditLibrary = employee.CanEditLibrary,
                Email          = user.Email
            };

            return(Ok(result));
        }
コード例 #2
0
        public async Task <IActionResult> GetCoworkers()
        {
            var errorMessage = new ErrorMessageViewModel();
            var error        = new { Error = errorMessage };
            var id           = "";

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

            // Get user profile
            BusinessEmployees user = db.BusinessEmployees.Where(e => e.UserId == id).SingleOrDefault();

            if (user == null)
            {
                errorMessage.Message = "User profile was not found";
                return(BadRequest(error));
            }

            // Get a list of coworkers
            List <BusinessEmployees> coworkers = db.BusinessEmployees.Where(e => e.BusinessUserId == user.BusinessUserId && e.UserId != user.UserId).ToList();
            List <ContactsViewModel> contacts  = new List <ContactsViewModel>();

            foreach (BusinessEmployees coworker in coworkers)
            {
                ContactsViewModel contact = new ContactsViewModel
                {
                    Name        = String.Format("{0} {1}", coworker.FirstName, coworker.LastName),
                    Position    = coworker.Position,
                    PhoneNumber = coworker.PhoneNumber
                };

                contacts.Add(contact);
            }

            return(Ok(contacts.ToArray()));
        }
コード例 #3
0
        public async Task <IActionResult> DeleteEmployee([FromRoute] string id)
        {
            var errorMessage = new ErrorMessageViewModel();
            var error        = new { Error = errorMessage };

            // Delete the user from the application user list
            ApplicationUser user = db.ApplicationUser.Where(e => e.Id == id).SingleOrDefault();

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

            // Find the employee
            BusinessEmployees employee = db.BusinessEmployees.Where(e => e.UserId == id).SingleOrDefault();

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

            // Remove the employee
            db.Remove(employee);
            db.Remove(user);

            try
            {
                db.SaveChanges();
            }
            catch (Exception exception)
            {
                errorMessage.Message = "Could not remove employee";
                return(Json(error));
            }

            return(Accepted(employee));
        }
コード例 #4
0
        public async Task <IActionResult> GetEmployeeById(string id)
        {
            var errorMessage = new ErrorMessageViewModel();
            var error        = new { Error = errorMessage };

            // Get the employee from the database
            BusinessEmployees employee = db.BusinessEmployees.Where(e => e.UserId == id).SingleOrDefault();

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

            ApplicationUser user = db.ApplicationUser.Where(e => e.Id == id).SingleOrDefault();

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

            // Get the data for the employee
            var employeeModel = new BusinessEmployeeViewModel
            {
                Id             = employee.UserId,
                FirstName      = employee.FirstName,
                LastName       = employee.LastName,
                Email          = user.Email,
                Position       = employee.Position,
                PhoneNumber    = employee.PhoneNumber,
                CanEditLibrary = employee.CanEditLibrary
            };

            return(Ok(employeeModel));
        }
コード例 #5
0
        public async Task <IActionResult> GetUserProfile([FromRoute] string id)
        {
            var errorMessage = new ErrorMessageViewModel();
            var error        = new { Error = errorMessage };

            // Get the user profile
            ApplicationUser user = db.ApplicationUser.Where(e => e.Id == id).SingleOrDefault();

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

            // Find the user type based on the id
            BusinessUsers businessUser = db.BusinessUsers.Where(e => e.UserId == id).SingleOrDefault();

            if (businessUser == null)
            {
                BusinessEmployees employee = db.BusinessEmployees.Where(e => e.UserId == id).SingleOrDefault();

                if (employee == null)
                {
                    PersonalUsers personalUser = db.PersonalUsers.Where(e => e.UserId == id).SingleOrDefault();

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

                    // Get the personal user details
                    ProfileDetailsViewModel personalProfile = new ProfileDetailsViewModel
                    {
                        Id             = personalUser.UserId,
                        FirstName      = personalUser.FirstName,
                        LastName       = personalUser.LastName,
                        Email          = user.Email,
                        AccountType    = "Personal",
                        ProfilePicture = personalUser.ProfilePicture
                    };

                    return(Ok(personalProfile));
                }

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

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

                // Get the employee user details
                EmployeeDetailsViewModel employeeProfile = new EmployeeDetailsViewModel
                {
                    Id             = employee.UserId,
                    FirstName      = employee.FirstName,
                    LastName       = employee.LastName,
                    Email          = user.Email,
                    AccountType    = "Employee",
                    ProfilePicture = employee.ProfilePicture,
                    Organization   = employer.Organization,
                    CanEditLibrary = employee.CanEditLibrary
                };

                return(Ok(employeeProfile));
            }

            // Get the business user details
            ProfileDetailsViewModel businessProfile = new ProfileDetailsViewModel
            {
                Id             = businessUser.UserId,
                FirstName      = businessUser.FirstName,
                LastName       = businessUser.LastName,
                Email          = user.Email,
                AccountType    = "Business",
                ProfilePicture = businessUser.ProfilePicture,
                Organization   = businessUser.Organization
            };

            return(Ok(businessProfile));
        }
コード例 #6
0
        public async Task <IActionResult> UpdateProfile([FromRoute] string id, [FromBody] ProfileDetailsViewModel profile)
        {
            var errorMessage = new ErrorMessageViewModel();
            var error        = new { Error = errorMessage };

            var role = "";

            if (User != null)
            {
                try
                {
                    role = User.Claims.Where(c => c.Type == "http://schemas.microsoft.com/ws/2008/06/identity/claims/role").SingleOrDefault().Value;

                    if (role == null)
                    {
                        errorMessage.Message = "Could not find role for user";
                        return(Json(error));
                    }
                }
                catch (Exception exception)
                {
                    errorMessage.Message = "Could not get role for user";
                    return(Json(error));
                }
            }

            if (profile == null)
            {
                errorMessage.Message = "Model is missing data";
                return(Json(error));
            }

            // Find the type of user based on the role
            if (role == "Personal")
            {
                // Get the personal user in the database
                PersonalUsers personalUser = db.PersonalUsers.Where(e => e.UserId == id).SingleOrDefault();

                // Update the details for the profile
                if (personalUser != null)
                {
                    personalUser.FirstName = profile.FirstName;
                    personalUser.LastName  = profile.LastName;

                    if (!String.IsNullOrWhiteSpace(profile.ProfilePicture))
                    {
                        var fileName = await fileController.UploadImage(profile.ProfilePicture, Request);

                        if (String.IsNullOrWhiteSpace(fileName))
                        {
                            errorMessage.Message = "Image upload encountered an error";
                            return(Json(error));
                        }

                        personalUser.ProfilePicture = fileName;
                    }

                    // Update record in the database
                    db.Entry(personalUser).State = EntityState.Modified;

                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception exception)
                    {
                        errorMessage.Message = "Could update the account information";
                        return(Json(error));
                    }

                    return(Ok(personalUser));
                }
                else
                {
                    errorMessage.Message = "Could not find the user profile";
                    return(Json(error));
                }
            }
            else if (role == "Business")
            {
                // Get the business user in the database
                BusinessUsers businessUser = db.BusinessUsers.Where(e => e.UserId == id).SingleOrDefault();

                // Update the details for the profile
                if (businessUser != null)
                {
                    businessUser.FirstName    = profile.FirstName;
                    businessUser.LastName     = profile.LastName;
                    businessUser.Organization = profile.Organization;
                    businessUser.PhoneNumber  = profile.PhoneNumber;

                    if (!String.IsNullOrWhiteSpace(profile.ProfilePicture))
                    {
                        var fileName = await fileController.UploadImage(profile.ProfilePicture, Request);

                        if (String.IsNullOrWhiteSpace(fileName))
                        {
                            errorMessage.Message = "Image upload encountered an error";
                            return(Json(error));
                        }

                        businessUser.ProfilePicture = fileName;
                    }

                    // Update record in the database
                    db.Entry(businessUser).State = EntityState.Modified;

                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception exception)
                    {
                        errorMessage.Message = "Could update the account information";
                        return(Json(error));
                    }

                    return(Ok(businessUser));
                }
                else
                {
                    errorMessage.Message = "Could not find the user profile";
                    return(Json(error));
                }
            }
            else if (role == "Employee")
            {
                // Get the employee in the database
                BusinessEmployees employee = db.BusinessEmployees.Where(e => e.UserId == id).SingleOrDefault();

                // Update the details for the profile
                if (employee != null)
                {
                    employee.FirstName   = profile.FirstName;
                    employee.LastName    = profile.LastName;
                    employee.PhoneNumber = profile.PhoneNumber;

                    if (!String.IsNullOrWhiteSpace(profile.ProfilePicture))
                    {
                        var fileName = await fileController.UploadImage(profile.ProfilePicture, Request);

                        if (String.IsNullOrWhiteSpace(fileName))
                        {
                            errorMessage.Message = "Image upload encountered an error";
                            return(Json(error));
                        }

                        employee.ProfilePicture = fileName;
                    }

                    // Update record in the database
                    db.Entry(employee).State = EntityState.Modified;

                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception exception)
                    {
                        errorMessage.Message = "Could update the account information";
                        return(Json(error));
                    }

                    return(Ok(employee));
                }
                else
                {
                    errorMessage.Message = "Could not find the user profile";
                    return(Json(error));
                }
            }

            errorMessage.Message = "An error has occurred";
            return(Json(error));
        }
コード例 #7
0
        public async Task <IActionResult> AddEmployee([FromBody] BusinessEmployeeViewModel employee)
        {
            var id = "";

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

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

            // Get the employer info
            BusinessUsers business = db.BusinessUsers.Where(e => e.UserId == id).SingleOrDefault();

            if (business == null)
            {
                errorMessage.Message = "Could not get the employer info";
                return(Json(error));
            }

            // Check if the user exists
            ApplicationUser user = _userManager.Users.SingleOrDefault(e => e.Email.Equals(employee.Email, StringComparison.InvariantCultureIgnoreCase));

            // If not, create the user identity profile
            if (user == null)
            {
                if (employee.Email != null)
                {
                    ApplicationUser applicationUser = new ApplicationUser
                    {
                        UserName = employee.Email,
                        Email    = employee.Email
                    };

                    // Set temporary password
                    string password = String.Format("{0}{1}#{2}",
                                                    employee.Email.First().ToString().ToUpper(), employee.Email.Substring(1), DateTime.Now.Year);


                    IdentityResult result = await _userManager.CreateAsync(applicationUser, password);

                    if (result.Succeeded)
                    {
                        await _userManager.AddToRoleAsync(applicationUser, "Employee");

                        await _signInManager.SignInAsync(applicationUser, false);

                        // Add the new user to the database
                        BusinessEmployees businessEmployee = new BusinessEmployees
                        {
                            FirstName      = employee.FirstName,
                            LastName       = employee.LastName,
                            Position       = employee.Position,
                            CanEditLibrary = employee.CanEditLibrary,
                            PhoneNumber    = employee.PhoneNumber,
                            UserId         = applicationUser.Id,
                            BusinessUserId = business.BusinessUserId
                        };

                        if (!String.IsNullOrWhiteSpace(employee.ProfilePicture))
                        {
                            var fileName = await fileController.UploadImage(employee.ProfilePicture, Request);

                            if (String.IsNullOrWhiteSpace(fileName))
                            {
                                errorMessage.Message = "Image upload encountered an error";
                                return(Json(error));
                            }

                            businessEmployee.ProfilePicture = fileName;
                        }

                        db.BusinessEmployees.Add(businessEmployee);

                        try
                        {
                            db.SaveChanges();
                        }
                        catch (Exception exception)
                        {
                            errorMessage.Message = "Could not create the business employee";
                            return(Json(error));
                        }

                        // model data
                        var modelData = new BusinessEmployeeViewModel
                        {
                            Id             = businessEmployee.UserId,
                            FirstName      = businessEmployee.FirstName,
                            LastName       = businessEmployee.LastName,
                            Position       = businessEmployee.Position,
                            ProfilePicture = businessEmployee.ProfilePicture,
                            PhoneNumber    = businessEmployee.PhoneNumber,
                            CanEditLibrary = businessEmployee.CanEditLibrary,
                            Email          = applicationUser.Email
                        };

                        return(CreatedAtAction("AddEmployee", new { id = modelData.Id }, modelData));
                    }

                    // Send an email with this link ( SET UP SENDGRID TOWARDS END OF PROJECT )

                    //string code = await _userManager.GeneratePasswordResetTokenAsync(applicationUser);
                    //var callbackUrl = Url.Action("ResetPassword", "Accounts", new { userId = applicationUser.Id, code }, protocol: Request.Scheme);

                    //// Send the email
                    //var emailNotification = new EmailNotificationViewModel
                    //{
                    //    Email = applicationUser.Email,
                    //    Subject = "SPINE - Password Reset",
                    //    Content = "",
                    //    Html = "Please reset your password by clicking <a href =\"" + callbackUrl + "\">here</a>"
                    //};

                    //await notificationsController.SendEmailNotification(emailNotification);
                }
                else
                {
                    errorMessage.Message = "Data is missing the Email field";
                    return(Json(error));
                }
            }

            errorMessage.Message = "An error has occurred";
            return(Json(error));
        }
コード例 #8
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));
        }