// GET: Author
        public ActionResult Index()
        {
            AuthorDAL autdal = new AuthorDAL();
            var       models = autdal.GetAll();

            return(View(models));
        }
Exemple #2
0
        public void CreateNewBookAndAddToDB()
        {
            MockAuthors();

            Book book = new Book()
            {
                Title       = "title",
                ReleaseDate = DateTime.Now,
                Description = "desc",
                Note        = "note",
                Authors     = _author_dal.GetAll().Select(x => new Author(x)).ToList()
            };

            _book_dal.Add(book);
            _book_dal.Save();

            Assert.AreEqual(1, _book_dal.GetAll().Count);
        }
        public ActionResult Add() // Pour l'ajout d'un nouveau livre
        {
            // Récupération des informations sur l'auteur via le form + ajout au model
            AuthorDAL     authorDAL = new AuthorDAL((List <Author>)Session["Authors"]);
            List <Author> authors   = authorDAL.GetAll();

            var model = new AddBookViewModel();

            model.authors = GetSelectListItems(authors);
            model.book    = new Book();
            return(View(model));
        }
        [ValidateAntiForgeryToken] // Pour l'édition d'un livre
        public ActionResult Edition([Bind(Include = "book, authorSelected")] EditBookModelView model)
        {
            // Récupération des datas
            BookDAL       dal       = new BookDAL((List <Book>)Session["Books"]);
            AuthorDAL     authorDAL = new AuthorDAL((List <Author>)Session["Authors"]);
            List <Author> authors   = authorDAL.GetAll();

            model.authors = GetSelectListItems(authors);
            Author selectedAuthor = authorDAL.Read(model.authorSelected);

            model.book.Author = selectedAuthor;  // Attribuer l'auteur

            if (ModelState.IsValid)
            {
                dal.Update(model.book.Id, model.book); // Mise à jour des datas du livres
                return(RedirectToAction("Index"));     // Si déroulement OK, retour à la liste de livres
            }

            return(View("Edit", model)); // Si erreur, on reste sur le formulaire d'edition
        }
        [ValidateAntiForgeryToken] // Création d'un livre à partir d'infos récupéré via un form
        public ActionResult Create([Bind(Include = "book, authorSelected")] AddBookViewModel model)
        {
            // Récupération des datas
            BookDAL   dal       = new BookDAL((List <Book>)Session["Books"]);
            AuthorDAL authorDAL = new AuthorDAL((List <Author>)Session["Authors"]);

            List <Author> authors = authorDAL.GetAll();

            model.authors = GetSelectListItems(authors); // Récupération de l'auteur
            Author selectedAuthor = authorDAL.Read(model.authorSelected);

            model.book.Author = selectedAuthor; // Ajout de l'auteur au model

            if (ModelState.IsValid)
            {
                dal.Add(model.book);               // Ajout du livre
                return(RedirectToAction("Index")); // Si tout se passe bien, afficher liste livre
            }

            return(View("Add", model)); // Si erreur, laissez la view d'ajout
        }
        public ActionResult Edit(int id) // Pour l'édition d'un livre
        {
            // Récupération des infos sur l'auteur
            AuthorDAL     authorDAL = new AuthorDAL((List <Author>)Session["Authors"]);
            List <Author> authors   = authorDAL.GetAll();

            // Récupération des infos sur le livre
            BookDAL bookDAL = new BookDAL((List <Book>)Session["Books"]);
            Book    book    = bookDAL.Read(id);

            bookDAL.Update(id, book); // MAJ du livre

            //Données mises à jour renvoyées dans la vue
            var model = new EditBookModelView();

            model.book    = book;
            model.authors = GetSelectListItems(authors);

            // Données retournée à la vue
            return(View(model));
        }
        public void CreateNewAuthorAndAddToDB()
        {
            Author author = new Author()
            {
                FirstName = "Fn",
                LastName  = "Ln"
            };

            _author_dal.Add(author);

            Author author2 = new Author()
            {
                FirstName = "Fn2",
                LastName  = "Ln2"
            };

            _author_dal.Add(author2);

            _author_dal.Save();

            Assert.AreEqual(2, _author_dal.GetAll().Count);
        }