Beispiel #1
0
        public async Task <IActionResult> UpdateBook([FromRoute] int id, [FromBody] BookDetailsViewModel book)
        {
            var errorMessage = new ErrorMessageViewModel();
            var error        = new { Error = errorMessage };

            List <DocumentGenres>  dbGenres  = db.DocumentGenres.Where(e => e.DocumentId == id).ToList();
            List <DocumentAuthors> dbAuthors = db.DocumentAuthors.Where(e => e.DocumentId == id).ToList();

            Documents document = db.Documents.Where(e => e.DocumentId == id).SingleOrDefault();

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

            // Remove old entries for the authors
            foreach (var item in dbAuthors)
            {
                db.Remove(item);
            }

            // Remove old entries for the genres
            foreach (var item in dbGenres)
            {
                db.Remove(item);
            }

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

            // Add new document authors and genres
            foreach (var genre in book.Genres)
            {
                // Add the document genre for each genre
                DocumentGenres documentGenre = new DocumentGenres
                {
                    DocumentId = document.DocumentId,
                    GenreId    = genre.GenreId
                };

                db.DocumentGenres.Add(documentGenre);

                try
                {
                    db.SaveChanges();
                }
                catch (Exception exception)
                {
                    errorMessage.Message = "Could not add document genre to database";
                    return(Json(error));
                }
            }

            foreach (var item in book.Authors)
            {
                var dbAuthor = db.Authors.Where(e => e.Name.Equals(item.Name, StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault();

                if (dbAuthor == null)
                {
                    // Add the author
                    Authors author = new Authors
                    {
                        Name = item.Name
                    };

                    db.Authors.Add(author);

                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception exception)
                    {
                        errorMessage.Message = "Could not add author to the database";
                        return(Json(error));
                    }

                    // Add the author to the books list of authors
                    DocumentAuthors documentAuthor = new DocumentAuthors
                    {
                        AuthorId   = author.AuthorId,
                        DocumentId = document.DocumentId
                    };

                    db.DocumentAuthors.Add(documentAuthor);

                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception exception)
                    {
                        errorMessage.Message = "Could not add document author to the database";
                        return(Json(error));
                    }
                }
                else
                {
                    // Add the author to the books list of authors
                    DocumentAuthors documentAuthor = new DocumentAuthors
                    {
                        AuthorId   = dbAuthor.AuthorId,
                        DocumentId = document.DocumentId
                    };

                    db.DocumentAuthors.Add(documentAuthor);

                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception exception)
                    {
                        errorMessage.Message = "Could not add document author to the database";
                        return(Json(error));
                    }
                }
            }

            // Attach values
            document.Title         = book.Title;
            document.ISBN          = book.ISBN;
            document.Pages         = book.Pages;
            document.Publisher     = book.Publisher;
            document.PublishedDate = Convert.ToDateTime(book.PublishedDate);
            document.CheckedOut    = book.CheckedOut;
            document.Edition       = book.Edition;
            document.Description   = book.Description;

            // Add image for book
            if (!String.IsNullOrWhiteSpace(book.Picture))
            {
                var fileName = await fileController.UploadImage(book.Picture, Request);

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

                document.CoverImage = fileName;
            }

            // Update book information
            db.Update(document);

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

            BookDetailsViewModel bookDetails = new BookDetailsViewModel
            {
                Id            = document.DocumentId,
                Title         = document.Title,
                ISBN          = document.ISBN,
                CheckedOut    = document.CheckedOut,
                Picture       = document.CoverImage,
                Pages         = Convert.ToInt32(document.Pages),
                Publisher     = document.Publisher,
                PublishedDate = document.PublishedDate.ToString("yyyy-MM-dd"),
                Edition       = document.Edition,
                Description   = document.Description
            };

            return(Ok(bookDetails));
        }
Beispiel #2
0
        public async Task <IActionResult> AddBook([FromBody] BookDetailsViewModel book)
        {
            var errorMessage = new ErrorMessageViewModel();
            var error        = new { Error = errorMessage };

            var id = "";

            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;
            }
            catch (Exception exception)
            {
                errorMessage.Message = "Id was not found";
                return(Json(error));
            }

            if (book == null)
            {
                errorMessage.Message = "The book does not contain data";
                return(Json(error));
            }

            // Add the book details
            Documents document = new Documents
            {
                Title         = book.Title,
                ISBN          = book.ISBN,
                CheckedOut    = book.CheckedOut,
                Pages         = book.Pages,
                Publisher     = book.Publisher,
                PublishedDate = Convert.ToDateTime(book.PublishedDate),
                Edition       = book.Edition,
                Description   = book.Description,
                UserId        = id
            };

            // Add cover image for the book
            if (!String.IsNullOrWhiteSpace(book.Picture))
            {
                var fileName = await fileController.UploadImage(book.Picture, Request);

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

                document.CoverImage = fileName;
            }

            // Add the book to the database
            db.Documents.Add(document);

            try
            {
                db.SaveChanges();
            }
            catch (Exception exception)
            {
                errorMessage.Message = "Could not add book to the database";
                return(Json(error));
            }

            // Add the genres
            foreach (var genre in book.Genres)
            {
                // Add the document genre for each genre
                DocumentGenres documentGenre = new DocumentGenres
                {
                    DocumentId = document.DocumentId,
                    GenreId    = genre.GenreId
                };

                db.DocumentGenres.Add(documentGenre);

                try
                {
                    db.SaveChanges();
                }
                catch (Exception exception)
                {
                    errorMessage.Message = "Could not add document genre to database";
                    return(Json(error));
                }
            }

            // Add the authors
            foreach (var item in book.Authors)
            {
                var dbAuthor = db.Authors.Where(e => e.Name.Equals(item.Name, StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault();

                if (dbAuthor == null)
                {
                    // Add the author
                    Authors author = new Authors
                    {
                        Name = item.Name
                    };

                    db.Authors.Add(author);

                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception exception)
                    {
                        errorMessage.Message = "Could not add author to the database";
                        return(Json(error));
                    }

                    // Add the author to the books list of authors
                    DocumentAuthors documentAuthor = new DocumentAuthors
                    {
                        AuthorId   = author.AuthorId,
                        DocumentId = document.DocumentId
                    };

                    db.DocumentAuthors.Add(documentAuthor);

                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception exception)
                    {
                        errorMessage.Message = "Could not add document author to the database";
                        return(Json(error));
                    }
                }
                else
                {
                    // Add the author to the books list of authors
                    DocumentAuthors documentAuthor = new DocumentAuthors
                    {
                        AuthorId   = dbAuthor.AuthorId,
                        DocumentId = document.DocumentId
                    };

                    db.DocumentAuthors.Add(documentAuthor);

                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception exception)
                    {
                        errorMessage.Message = "Could not add document author to the database";
                        return(Json(error));
                    }
                }
            }

            BookDetailsViewModel bookDetails = new BookDetailsViewModel
            {
                Id            = document.DocumentId,
                Title         = document.Title,
                ISBN          = document.ISBN,
                CheckedOut    = document.CheckedOut,
                Picture       = document.CoverImage,
                Pages         = Convert.ToInt32(document.Pages),
                Publisher     = document.Publisher,
                PublishedDate = document.PublishedDate.ToString("yyyy-MM-dd"),
                Edition       = document.Edition,
                Description   = document.Description
            };

            return(Ok(bookDetails));
        }