public ActionResult Search(string username, string firstname, string lastname)
        {
            if (AuthorizationMiddleware.AdminAuthorized(Session))
            {
                var queryClients = new List <Client>();

                foreach (var client in db.Clients)
                {
                    var usernameNeeded  = username != null && username.Length > 0;
                    var firstnameNeeded = firstname != null && firstname.Length > 0;
                    var lastnameNeeded  = lastname != null && lastname.Length > 0;

                    if ((usernameNeeded ? client.ClientName != null && client.ClientName.Contains(username) : true) &&
                        (firstnameNeeded ? client.FirstName != null && client.FirstName.Contains(firstname) : true) &&
                        (lastnameNeeded ? client.LastName != null && client.LastName.Contains(lastname) : true))
                    {
                        queryClients.Add(client);
                    }
                }

                return(View(queryClients.OrderByDescending(x => x.ClientName)));
            }
            else
            {
                return(RedirectToAction("Index", "Home"));
            }
        }
        public ActionResult Create([Bind(Include = "ID,Name")] Category category)
        {
            if (AuthorizationMiddleware.AdminAuthorized(Session))
            {
                if (ModelState.IsValid)
                {
                    // Checking if the category already exist
                    var isExist = db.Categories.Where(x => x.Name == category.Name).FirstOrDefault();

                    if (isExist == null)
                    {
                        db.Categories.Add(category);
                        db.SaveChanges();

                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        return(View(category));
                    }
                }

                return(View(category));
            }
            else
            {
                return(RedirectToAction("Index", "Home"));
            }
        }
        public ActionResult DeleteConfirmed(int id)
        {
            if (AuthorizationMiddleware.AdminAuthorized(Session))
            {
                Category category = db.Categories.Find(id);

                // Getting all the posts of the category
                List <Post> lstPosts = new List <Post>();
                lstPosts = db.Posts.Where(x => x.Category.ID == id).ToList();

                // Removing all the posts of that category
                foreach (Post curPost in lstPosts)
                {
                    Post post = db.Posts.Find(curPost.ID);

                    List <Comment> lstComments = new List <Comment>();
                    lstComments = db.Comments.Where(x => x.PostID == curPost.ID).ToList();

                    foreach (Comment curComm in lstComments)
                    {
                        db.Comments.Remove(curComm);
                    }

                    db.Posts.Remove(post);
                }

                db.Categories.Remove(category);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            else
            {
                return(RedirectToAction("Index", "Home"));
            }
        }
 // GET: Clients
 public ActionResult Index()
 {
     if (AuthorizationMiddleware.AdminAuthorized(Session))
     {
         return(View(db.Clients.ToList()));
     }
     else
     {
         return(RedirectToAction("Index", "Home"));
     }
 }
 public ActionResult Edit([Bind(Include = "ID,Gender,ClientName,FirstName,LastName,Password,isAdmin")] Client client)
 {
     if (AuthorizationMiddleware.AdminAuthorized(Session))
     {
         if (ModelState.IsValid)
         {
             db.Entry(client).State = EntityState.Modified;
             db.SaveChanges();
             return(RedirectToAction("Index"));
         }
         return(View(client));
     }
     else
     {
         return(RedirectToAction("Index", "Home"));
     }
 }
        public ActionResult Edit([Bind(Include = "ID,Name")] Category category)
        {
            if (AuthorizationMiddleware.AdminAuthorized(Session))
            {
                var isExist = db.Categories.Where(x => x.Name == category.Name && x.ID != category.ID).FirstOrDefault();

                if (ModelState.IsValid && isExist == null)
                {
                    db.Entry(category).State = EntityState.Modified;
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                return(View(category));
            }
            else
            {
                return(RedirectToAction("Index", "Home"));
            }
        }
 // GET: Clients/Edit/5
 public ActionResult Edit(int?id)
 {
     if (AuthorizationMiddleware.AdminAuthorized(Session))
     {
         if (id == null)
         {
             return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
         }
         Client client = db.Clients.Find(id);
         if (client == null)
         {
             return(HttpNotFound());
         }
         return(View(client));
     }
     else
     {
         return(RedirectToAction("Index", "Home"));
     }
 }
        public ActionResult DeleteConfirmed(int id)
        {
            if (AuthorizationMiddleware.AdminAuthorized(Session))
            {
                Client client = db.Clients.Find(id);

                List <Post> lstPosts = new List <Post>();

                // Get the posts of the user
                lstPosts = db.Posts.Where(x => x.ClientID == id).ToList();

                foreach (Post currPost in lstPosts)
                {
                    List <Comment> lstComments = new List <Comment>();
                    lstComments = db.Comments.Where(x => x.PostID == currPost.ID).ToList();

                    foreach (Comment currCmt in lstComments)
                    {
                        db.Comments.Remove(currCmt);
                    }

                    db.Posts.Remove(currPost);
                }

                db.Clients.Remove(client);

                db.SaveChanges();

                if (((Client)Session["Client"]).ID == id)
                {
                    Session.Clear();
                }

                return(RedirectToAction("Index"));
            }
            else
            {
                return(RedirectToAction("Index", "Home"));
            }
        }
        // GET: Categories/Delete/5
        public ActionResult Delete(int?id)
        {
            if (AuthorizationMiddleware.AdminAuthorized(Session))
            {
                if (id == null)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
                }
                Category category = db.Categories.Find(id);

                if (category == null)
                {
                    return(HttpNotFound());
                }

                return(View(category));
            }
            else
            {
                return(RedirectToAction("Index", "Home"));
            }
        }