Esempio n. 1
0
        public void Update(Author entityToUpdate, Author entity)
        {
            entityToUpdate = _booksStoreContext.Author
                             .Include(a => a.BookAuthors)
                             .Include(a => a.AuthorContact)
                             .Single(b => b.Id == entityToUpdate.Id);

            entityToUpdate.Name = entity.Name;

            entityToUpdate.AuthorContact.Address       = entity.AuthorContact.Address;
            entityToUpdate.AuthorContact.ContactNumber = entity.AuthorContact.ContactNumber;

            var deletedBooks = entityToUpdate.BookAuthors.Except(entity.BookAuthors).ToList();
            var addedBooks   = entity.BookAuthors.Except(entityToUpdate.BookAuthors).ToList();

            deletedBooks.ForEach(bookToDelete =>
                                 entityToUpdate.BookAuthors.Remove(
                                     entityToUpdate.BookAuthors
                                     .First(b => b.BookId == bookToDelete.BookId)));

            foreach (var addedBook in addedBooks)
            {
                _booksStoreContext.Entry(addedBook).State = EntityState.Added;
            }

            _booksStoreContext.SaveChanges();
        }
Esempio n. 2
0
        public async Task <IActionResult> PutAuthor(int id, AuthorDTO authorDTO)
        {
            var author = _mapper.Map <Author>(authorDTO);

            if (id != author.AuthorId)
            {
                return(BadRequest());
            }

            _context.Entry(author).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AuthorExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Ok(_mapper.Map <AuthorDTO>(author)));
        }
        public async Task <IActionResult> PutBook(int id, BookDTO bookDTO)
        {
            var book = _mapper.Map <Book>(bookDTO);

            if (id != book.BookId)
            {
                return(BadRequest());
            }

            _context.Entry(book).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BookExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Ok(_mapper.Map <BookDTO>(book)));
        }
Esempio n. 4
0
        public Book Get(long id)
        {
            _booksStoreContext.ChangeTracker.LazyLoadingEnabled = false;

            var book = _booksStoreContext.Book
                       .SingleOrDefault(b => b.Id == id);

            if (book == null)
            {
                return(null);
            }

            _booksStoreContext.Entry(book)
            .Collection(b => b.BookAuthors)
            .Load();

            _booksStoreContext.Entry(book)
            .Reference(b => b.Publisher)
            .Load();

            return(book);
        }
Esempio n. 5
0
        public async Task <IActionResult> Edit(int id, byte[] rowVersion, [Bind("BookId,BookName,Author,Publication,Price,Summary,PictureName,GenreId")] Book book, IFormFile file)
        {
            if (id != book.BookId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                // case the user put new image to update
                if (file != null)
                {
                    // Set full path to file
                    string FileToDelete = _staticImagesRoute + book.PictureName,
                           fileToUpdate = _staticImagesRoute + file.FileName;

                    // put the new picture name to product object
                    book.PictureName = "/img/" + file.FileName;

                    // save the picture to the static path
                    using (var stream = new FileStream(fileToUpdate, FileMode.Create))
                    {
                        await file.CopyToAsync(stream);
                    }
                }


                var bookToUpdate = await _context.Books.FindAsync(id);

                if (bookToUpdate == null)
                {
                    Book deletedBook = new Book();
                    await TryUpdateModelAsync(deletedBook);

                    ModelState.AddModelError(string.Empty,
                                             "Unable to save changes. The book was deleted by another user.");
                    ViewData["GenreId"] = new SelectList(_context.Genres, "GenreId", "GenreName", deletedBook.GenreId);
                    return(View(deletedBook));
                }

                _context.Entry(bookToUpdate).Property("RowVersion").OriginalValue = rowVersion;

                if (await TryUpdateModelAsync <Book>(
                        bookToUpdate,
                        "",
                        s => s.BookName, s => s.Author, s => s.Price, s => s.GenreId, s => s.Publication, s => s.Summary))
                {
                    try
                    {
                        await _context.SaveChangesAsync();

                        return(RedirectToAction(nameof(Index)));
                    }
                    catch (DbUpdateConcurrencyException ex)
                    {
                        var exceptionEntry = ex.Entries.Single();
                        var clientValues   = (Book)exceptionEntry.Entity;
                        var databaseEntry  = exceptionEntry.GetDatabaseValues();
                        if (databaseEntry == null)
                        {
                            ModelState.AddModelError(string.Empty,
                                                     "Unable to save changes. The book was deleted by another user.");
                        }
                        else
                        {
                            var databaseValues = (Book)databaseEntry.ToObject();

                            if (databaseValues.BookName != clientValues.BookName)
                            {
                                ModelState.AddModelError("Name", $"Current value: {databaseValues.BookName}");
                            }
                            if (databaseValues.Price != clientValues.Price)
                            {
                                ModelState.AddModelError("Price", $"Current value: {databaseValues.Price:c}");
                            }

                            if (databaseValues.Publication != clientValues.Publication)
                            {
                                ModelState.AddModelError("Publication", $"Current value: {databaseValues.Publication:e}");
                            }
                            if (databaseValues.Summary != clientValues.Summary)
                            {
                                ModelState.AddModelError("Summary", $"Current value: {databaseValues.Summary:f}");
                            }

                            if (databaseValues.Genre != clientValues.Genre)
                            {
                                Genre databaseGenre = await _context.Genres.FirstOrDefaultAsync(i => i.GenreId == databaseValues.GenreId);

                                ModelState.AddModelError("GenreName", $"Current value: {databaseGenre?.GenreName}");
                            }

                            ModelState.AddModelError(string.Empty, "The record you attempted to edit "
                                                     + "was modified by another user after you got the original value. The "
                                                     + "edit operation was canceled and the current values in the database "
                                                     + "have been displayed. If you still want to edit this record, click "
                                                     + "the Save button again. Otherwise click the Back to List hyperlink.");
                            bookToUpdate.RowVersion = (byte[])databaseValues.RowVersion;
                            ModelState.Remove("RowVersion");
                        }
                    }
                }
                ViewData["GenreId"] = new SelectList(_context.Genres, "GenreId", "GenreName", bookToUpdate.GenreId);
                return(View(bookToUpdate));
            }
            ViewData["GenreId"] = new SelectList(_context.Genres, "GenreId", "GenreName", book.GenreId);
            return(View(book));
        }