public vmInstructor()
 {
     Person              = new vmPerson();
     Department          = new vmDepartment();
     Departments         = new List <Department>();
     Textbook            = new vmTextbook();
     Textbooks           = new List <Textbook>();
     InstructorTextbooks = new List <vmTextbook>();
 }
 public vmInstructor()
 {
     Person = new vmPerson();
     Department = new vmDepartment();
     Departments = new List<Department>();
     Textbook = new vmTextbook();
     Textbooks = new List<Textbook>();
     InstructorTextbooks = new List<vmTextbook>();
 }
 public ActionResult Delete(int id)
 {
     using (InstManager)
     {
         using (PublishersManager)
         {
             using (AuthorsManager)
             {
                 using (PeopleManager)
                 {
                     var tb = TBManager.GetTextbookbyID(id);
                     var disp = Mapper.Map<vmTextbook>(tb);
                     if (disp != null)
                     {
                         disp.Publisher = Mapper.Map<vmPublisher>(PublishersManager.GetPublisherbyID(tb.PublisherID));
                         disp.Author = Mapper.Map<vmAuthor>(AuthorsManager.GetAuthorbyID(tb.AuthorID));
                         disp.Author.Person = Mapper.Map<vmPerson>(PeopleManager.GetPersonbyID(disp.Author.PersonID));
                     }
                     else
                     {
                         disp = new vmTextbook();
                         ModelState.AddModelError("", "Failed to load details for requested item.");
                     }
                     return View(disp);
                 }
             }
         }
     }
 }
 public ActionResult Create(vmTextbook tb)
 {
     try
     {
         using (InstManager)
         {
             using (PublishersManager)
             {
                 using (AuthorsManager)
                 {
                     using (PeopleManager)
                     {
                         var person = Mapper.Map<Person>(tb.Author.Person);
                         var success = PeopleManager.AddPerson(person);
                         if (success)
                         {
                             var author = Mapper.Map<Author>(tb.Author);
                             author.PersonID = person.ID;
                             success = AuthorsManager.AddAuthor(author);
                             if (success)
                             {
                                 var pub = Mapper.Map<Publisher>(tb.Publisher);
                                 success = PublishersManager.AddPublisher(pub);
                                 if (success)
                                 {
                                     var text = Mapper.Map<Textbook>(tb);
                                     text.AuthorID = author.ID;
                                     text.PublisherID = pub.ID;
                                     success = TBManager.AddTextbook(text);
                                     if (success)
                                     {
                                         RedirectToAction("Details", new { id = text.ID });
                                     }
                                     else
                                     {
                                         throw new DataException("Failed to save textbook. Please try again.");
                                     }
                                 }
                                 else
                                 {
                                     throw new DataException("Failed to save publisher. Please try again.");
                                 }
                             }
                             else
                             {
                                 throw new DataException("Failed to save author. Please try again.");
                             }
                         }
                         else
                         {
                             throw new DataException("Failed to save person. Please try again.");
                         }
                     }
                 }
             }
         }
     }
     catch (DataException ex)
     {
         //Log the error (add a variable name after DataException)
         ModelState.AddModelError("", ex.Message);
     }
     return View(tb);
 }
        public ActionResult Edit(vmTextbook tb)
        {
            try
            {
                using (InstManager)
                {
                    using (PublishersManager)
                    {
                        using (AuthorsManager)
                        {
                            using (PeopleManager)
                            {
                                var text = TBManager.GetTextbookbyID(tb.ID);
                                text.Name = tb.Name;
                                text.PublishDate = tb.PublishDate;
                                var success = TBManager.UpdateTextbook(text);
                                if (success)
                                {
                                    var auth = AuthorsManager.GetAuthorbyID(text.AuthorID);
                                    var pers = PeopleManager.GetPersonbyID(auth.PersonID);
                                    pers.FirstMidName = tb.Author.Person.FirstMidName;
                                    pers.LastName = tb.Author.Person.LastName;
                                    success = PeopleManager.UpdatePerson(pers);
                                    if (success)
                                    {
                                        var pub = PublishersManager.GetPublisherbyID(text.PublisherID);
                                        pub.Name = tb.Publisher.Name;
                                        pub.City = tb.Publisher.City;
                                        pub.State = tb.Publisher.State;
                                        success = PublishersManager.UpdatePublisher(pub);
                                        if (success)
                                        {
                                            return RedirectToAction("Details", new { id = tb.ID });
                                        }
                                        else
                                        {
                                            throw new DataException("Failed to save publisher. Please try again.");
                                        }
                                    }
                                    else
                                    {
                                        throw new DataException("Failed to save author. Please try again.");
                                    }
                                }
                                else
                                {
                                    throw new DataException("Failed to save textbook. Please try again.");
                                }

                            }
                        }
                    }
                }
            }
            catch (DataException ex)
            {
                //Log the error (add a variable name after DataException)
                ModelState.AddModelError("", ex.Message);
            }
            tb.Publisher = Mapper.Map<vmPublisher>(PublishersManager.GetPublisherbyID(tb.PublisherID));
            tb.Author = Mapper.Map<vmAuthor>(AuthorsManager.GetAuthorbyID(tb.AuthorID));
            tb.Author.Person = Mapper.Map<vmPerson>(PeopleManager.GetPersonbyID(tb.Author.PersonID));
            return View(tb);
        }
 public ActionResult Create()
 {
     var disp = new vmTextbook();
     return View(disp);
 }
 public ActionResult Delete(vmTextbook tb)
 {
     try
     {
         using (TBManager)
         {
             var item = TBManager.GetTextbookbyID(tb.ID);
             var success = TBManager.RemoveTextbook(item);
             if (success)
             {
                 return RedirectToAction("Index");
             }
             throw new DataException("Enable to delete textbook " + tb.Name + ". Please try again.");
         }
     }
     catch (DataException ex)
     {
         ModelState.AddModelError("", ex.Message);
     }
     return View(tb);
 }