public ActionResult Delete(int id = 0)
        {
            BookstoreContext db = new BookstoreContext();
               Posting post = db.Postings.Find(id);
               if (post == null)
               {
               post = new Posting();
               }
               try
               {
               OfficialPosting opost = db.OfficialPostings.Find(id);
               if (opost != null)
               {
                   db.OfficialPostings.Remove(opost);
               }

               db.Postings.Remove(post);
               db.SaveChanges();
               }
               catch (Exception e)
               {

               }
               //Add Audit
               var userIds = WebSecurity.GetUserId(User.Identity.Name);
               var postId = id;
               AuditController.AuditEntry(userIds, postId, AuditController.REMOVEOFFICIAL);
               return RedirectToAction("Manage", "manage");
        }
        /*
         *  METHOD      : AuditEntry
         *  DESCRIPTION :
         *      This method begins an entry into the audit table starting from inactivity
         *  PARAMETERS  :
         *      int userId    : logged in user's ID
         *      int PostID     : ID of post being created
         *      string action : create/modify/delete
         *  RETURNS     :
         *      Audit auditEntry.ID : the ID integer of the entry being recorded
         */
        public static int AuditEntry(int userId, int PostID, string action)
        {
            using (var context = new BookstoreContext())
            {
                var auditEntry = new Audits
                {
                    ActionTime = DateTime.Now,
                    User_ID = userId,
                    Posting_ID = PostID,
                    Action = action,

                };
                context.Audit.Add(auditEntry);
                context.SaveChanges();
                SaveDBcontext(context);
                return auditEntry.ID;
            }
        }
        /*
         *  METHOD      : SaveDBcontext
         *  DESCRIPTION :
         *      Audit was successful, modification/change was valid, now save
         *      this information into the database
         *  PARAMETERS  :
         *      EmployeeDBContext context : stores the employee information
         *  RETURNS     : N/A
         */
        private static void SaveDBcontext(BookstoreContext context)
        {
            try
            {
                context.SaveChanges();
            }
            catch (DbEntityValidationException ex)
            {
                // Retrieve the error messages as a list of strings.
                var errorMessages = ex.EntityValidationErrors
                        .SelectMany(x => x.ValidationErrors)
                        .Select(x => x.ErrorMessage);

                // Join the list to a single string.
                var fullErrorMessage = string.Join("; ", errorMessages);

                // Combine the original exception message with the new one.
                var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

                //// Throw a new DbEntityValidationException with the improved exception message.
                //throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
            }
        }
        /*
         *  METHOD      : AuditEntry
         *  DESCRIPTION :
         *      The end of the audit entry, add it into the audit table.
         *  PARAMETERS  :
         *      int userId        : user logged in
         *      int PostID         : Post ID (Post modified)
         *      string action     : one of static private strings above, denoting which action performed
         *      string attr       : attribute that was changed
         *      string oldValue   : attribute's old value
         *      string newValue   : attribute's new value
         *  RETURNS     :
         *      int auditEntry.ID : The new audit entry's traceable ID
         */
        public static int AuditEntry(int userId, int PostID, string action, string attr, string oldValue, string newValue)
        {
            using (var context = new BookstoreContext())
            {

                var auditEntry = new Audits
                {
                    ActionTime = DateTime.Now,
                    User_ID = userId,
                    Posting_ID = PostID,
                    Action = action,
                    Attribute_Name = attr,
                    Old_value = oldValue,
                    New_Value = newValue
                };

                context.Audit.Add(auditEntry);
                context.SaveChanges();

                return auditEntry.ID;
            }
        }
        public ActionResult UNOfficialBook(ManageViewModel mine, int id = 0)
        {
            BookstoreContext dba = new BookstoreContext();
            OfficialPosting tmp = (from ec in dba.OfficialPostings
                                    where ec.PostingID == id
                                    select ec).FirstOrDefault();

            try
            {
                if (tmp != null)
                {
                    dba.OfficialPostings.Remove(tmp);
                }
                dba.SaveChanges();
            }
            catch (Exception e)
            {

            }
            //Add Audit
            var userIds = WebSecurity.GetUserId(User.Identity.Name);
            var postId = id;
            AuditController.AuditEntry(userIds, postId, AuditController.REMOVEOFFICIAL);
            return RedirectToAction("Index", "manage", mine);
        }
        public ActionResult OfficialBook(ManageViewModel mine, int id = 0)
        {
            BookstoreContext dba = new BookstoreContext();
            if (ModelState.IsValid)
            {
                try
                {
                    if (mine.isOfficial)
                    {
                        mine.Officialpostings.PostingID = id;
                        dba.OfficialPostings.Add(mine.Officialpostings);
                    }

                    dba.SaveChanges();

                }
                catch (DbEntityValidationException e)
                {
                    //display all the validation error on the postingview model
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                            eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                }

                //Add Audit
                var userIds = WebSecurity.GetUserId(User.Identity.Name);
                var postId = id;
                AuditController.AuditEntry(userIds, postId, AuditController.CREATEOFFICIAL);
                return RedirectToAction("Index", "manage", mine );
            }
            return View(mine.postings.ToPostingViewModel());
        }
        public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
        {
            string provider = null;
            string providerUserId = null;

            if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId))
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Insert a new user into the database
                using (BookstoreContext db = new BookstoreContext())
                {
                    User user = db.User.FirstOrDefault(u => u.Email.ToLower() == model.Email.ToLower());
                    // Check if user already exists
                    if (user == null)
                    {
                        // Insert name into the profile table
                        db.User.Add(new User { Email = model.Email });
                        db.SaveChanges();

                        OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.Email);
                        OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);

                        return RedirectToLocal(returnUrl);
                    }
                    else
                    {
                        ModelState.AddModelError("Email", "Email Address already exists. Please enter a different Email Address.");
                    }
                }
            }

            ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }