Exemple #1
0
        public ActionResult Edit([Bind(Include = "BookReadID, Name,Author,Genre, DateEntered, PersonBookID")] DisplayBook displayBook)
        {
            if (ModelState.IsValid)
            {
                var us   = User.Identity.Name;
                var user = db.Users.FirstOrDefault(x => x.Email == us);


                var books      = db.PersonBooks.Where(x => x.Id == user.Id);
                var personBook = (from PB in db.PersonBooks
                                  where PB.PersonBookID == displayBook.PersonBookID
                                  select PB).First();
                personBook.BookDateEntered = displayBook.DateEntered;


                var bookReads = (from BR in db.BooksReads
                                 where BR.BookReadID == displayBook.BookReadID
                                 select BR).First();
                bookReads.BookReadName    = displayBook.Name;
                bookReads.BookReadAuthor  = displayBook.Author;
                bookReads.BookReadGenre   = displayBook.Genre;
                db.Entry(bookReads).State = EntityState.Modified;

                db.Entry(personBook).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            return(View(displayBook));
        }
Exemple #2
0
        public ActionResult Create([Bind(Include = "BookReadID, Name,Author,Genre, DateEntered")] DisplayBook displayBook)
        {
            var us   = User.Identity.Name;
            var user = db.Users.FirstOrDefault(x => x.Email == us);

            if (ModelState.IsValid)
            {
                var addBook = (from a in db.BooksReads
                               where a.BookReadName == displayBook.Name
                               select a);


                if (addBook.Count() == 0)
                {
                    var newBook = new BooksRead();
                    newBook.BookReadName   = displayBook.Name;
                    newBook.BookReadGenre  = displayBook.Genre;
                    newBook.BookReadAuthor = displayBook.Author;

                    db.BooksReads.Add(newBook);
                    db.SaveChanges();


                    var newPB = new PersonBook();
                    newPB.BookDateEntered = displayBook.DateEntered;
                    newPB.BookReadID      = newBook.BookReadID;
                    newPB.Id = user.Id;


                    db.PersonBooks.Add(newPB);
                    db.SaveChanges();
                }
                else
                {
                    var newPB = new PersonBook();
                    newPB.BookDateEntered = displayBook.DateEntered;
                    newPB.BookReadID      = addBook.First().BookReadID;
                    newPB.Id = user.Id;

                    db.PersonBooks.Add(newPB);
                    db.SaveChanges();
                }
                return(RedirectToAction("Index"));
            }

            return(View(displayBook));
        }