Exemple #1
0
        private void ModifyPublisher(Book book, SimpleBook sBook, LibraryDataBase db)
        {
            if (sBook.Publisher == book.Publisher.PublisherName)
            {
                return;
            }

            if (book.Publisher.PublisherName != "") // si no es un libro nuevo
            {
                book.Publisher.QuantityBooks--;     // Le resto uno a la cant de libros del autor anterior
            }


            bool existingPublisher = false;

            foreach (Publisher p in db.Publishers)
            {
                if (p.PublisherName == sBook.Publisher)
                {
                    existingPublisher = true;
                    book.Publisher    = p;
                    p.QuantityBooks++;
                    break;
                }
            }

            if (!existingPublisher)
            {
                Publisher newPublisher = new Publisher();     //Creo el publisher nuevo
                newPublisher.PublisherName = sBook.Publisher; //Le cambio el valor del Name
                newPublisher.QuantityBooks = 1;               // Le asigno 1 a la cant de libros del publisher actual
                db.Publishers.Add(newPublisher);              //Lo agrego a la tabla publishers
                book.Publisher = newPublisher;
            }
        }
Exemple #2
0
        // GET: Biblioteca
        public ActionResult ViewAll()
        {
            LibraryDataBase db    = new LibraryDataBase();
            List <Book>     books = db.Books.OrderBy(book => book.Title).ToList();

            return(View(books));
        }
Exemple #3
0
        private void ModifyAuthor(Book book, SimpleBook sBook, LibraryDataBase db)
        {
            if (sBook.Author == book.Author.AuthorName)
            {
                return;
            }

            if (book.Author.AuthorName != "") // Si no es un libro nuevo
            {
                book.Author.QuantityBooks--;  // Le resto uno a la cant de libros del autor anterior
            }


            bool existingAuthor = false;

            foreach (Author a in db.Authors)
            {
                if (a.AuthorName == sBook.Author)
                {
                    existingAuthor = true;
                    book.Author    = a;
                    a.QuantityBooks++;
                    break;
                }
            }
            if (!existingAuthor)
            {
                Author newAuthor = new Author();        //Creo el autor nuevo
                newAuthor.AuthorName    = sBook.Author; //Le cambio el Name
                newAuthor.QuantityBooks = 1;            //Le asigno la cant de libros del autor actual
                db.Authors.Add(newAuthor);              //Lo agrego a la tabla de Authors
                book.Author = newAuthor;
            }
        }
Exemple #4
0
        public ActionResult Search()
        {
            LibraryDataBase db    = new LibraryDataBase();
            SimpleBook      sBook = NewEmptySimpleBook(db);

            return(View(sBook));
        }
Exemple #5
0
        public ActionResult AddNew()
        {
            LibraryDataBase db = new LibraryDataBase();

            SimpleBook sBook = NewEmptySimpleBook(db);

            ViewBag.Description = "Agregar Nuevo";
            return(View("Edit", sBook));
        }
Exemple #6
0
        private SimpleBook NewEmptySimpleBook(LibraryDataBase db)
        {
            SimpleBook sBook = new SimpleBook();

            sBook.Title     = "";
            sBook.Author    = "";
            sBook.GenreType = db.Genres.Find(db.Genres.Max(gen => gen.ID)).GenreType;
            sBook.Publisher = "";
            sBook.BookRef   = null;
            return(sBook);
        }
Exemple #7
0
 private void ModifyGenre(Book book, SimpleBook sBook, LibraryDataBase db)
 {
     foreach (Genre g in db.Genres)
     {
         if (g.GenreType == sBook.GenreType)
         {
             book.ID_GenreType = g.ID;
             break;
         }
     }
 }
Exemple #8
0
        public ActionResult Delete(int id)
        {
            LibraryDataBase db   = new LibraryDataBase();
            Book            book = db.Books.Find(id);

            book.Author.QuantityBooks--;
            book.Publisher.QuantityBooks--;
            db.Books.Remove(book);
            db.SaveChanges();

            FreeUnusedMemory(db);
            return(RedirectToAction("ViewAll"));
        }
Exemple #9
0
        public ActionResult Search(SimpleBook sBook)
        {
            LibraryDataBase db       = new LibraryDataBase();
            List <Book>     bookList = db.Books.ToList();

            List <Book> filteredList = FilterByTitle(sBook, bookList);

            filteredList = FilterByGenre(sBook, filteredList);
            filteredList = FilterByPublisher(sBook, filteredList);
            filteredList = FilterByAuthor(sBook, filteredList);

            return(View("ViewAll", filteredList.OrderBy(book => book.Title)));
        }
Exemple #10
0
        public ActionResult Edit(SimpleBook sBook)
        {
            LibraryDataBase db   = new LibraryDataBase();
            Book            book = db.Books.Find(sBook.BookRef);

            ModifyAuthor(book, sBook, db);
            ModifyGenre(book, sBook, db);
            ModifyPublisher(book, sBook, db);
            ModifyTitle(book, sBook);

            db.Entry(book).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();

            FreeUnusedMemory(db);
            return(RedirectToAction("ViewAll"));
        }
Exemple #11
0
        public ActionResult Edit(int id)
        {
            LibraryDataBase db   = new LibraryDataBase();
            Book            book = db.Books.Find(id);

            SimpleBook sBook = new SimpleBook
            {
                Title     = book.Title,
                Author    = book.Author.AuthorName,
                GenreType = book.Genre.GenreType,
                Publisher = book.Publisher.PublisherName,
                BookRef   = book.ID
            };

            ViewBag.Description = "Editar";
            return(View(sBook));
        }
Exemple #12
0
        private void FreeUnusedMemory(LibraryDataBase db)
        {
            foreach (Author a in db.Authors)
            {
                if (a.QuantityBooks == 0)
                {
                    db.Authors.Remove(a);
                }
            }

            foreach (Publisher p in db.Publishers)
            {
                if (p.QuantityBooks == 0)
                {
                    db.Publishers.Remove(p);
                }
            }
            db.SaveChanges();
        }
Exemple #13
0
        public ActionResult AddNew(SimpleBook sBook)
        {
            LibraryDataBase db   = new LibraryDataBase();
            Book            book = new Book();

            book.Author = new Author {
                AuthorName = ""
            };
            book.Publisher = new Publisher {
                PublisherName = ""
            };

            ModifyAuthor(book, sBook, db);
            ModifyGenre(book, sBook, db);
            ModifyPublisher(book, sBook, db);
            ModifyTitle(book, sBook);

            db.Books.Add(book);
            db.SaveChanges();

            FreeUnusedMemory(db);
            return(RedirectToAction("ViewAll"));
        }