コード例 #1
0
        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"));
            }
        }
コード例 #2
0
        public ActionResult Create([Bind(Include = "ID,Name")] Genre genre)
        {
            if (AuthorizationMiddleware.AdminAuthorized(Session))
            {
                if (ModelState.IsValid)
                {
                    // Checking if the genre already exist
                    var isExist = db.Genres.Where(x => x.Name == genre.Name).FirstOrDefault();

                    if (isExist == null)
                    {
                        db.Genres.Add(genre);
                        db.SaveChanges();

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

                return(View(genre));
            }
            else
            {
                return(RedirectToAction("Index", "Home"));
            }
        }
コード例 #3
0
        public ActionResult Create([Bind(Include = "ID,clientId,GenreID,Title,Content")] Post post)
        {
            if (post.Content != null && post.Title != null && post.GenreID != 0)
            {
                if (AuthorizationMiddleware.Authorized(Session))
                {
                    if (ModelState.IsValid)
                    {
                        post.CreationDate = DateTime.Now;
                        db.Posts.Add(post);
                        db.SaveChanges();
                        return(RedirectToAction("Index"));
                    }

                    ViewBag.ClientID = new SelectList(db.Clients, "ID", "ClientName", post.ClientID);
                    ViewBag.GenreID  = new SelectList(db.Genres, "ID", "Name", post.GenreID);
                    return(View(post));
                }
            }
            else
            {
                return(RedirectToAction("Index", "Home"));
            }

            return(RedirectToAction("Index", "Home"));
        }
コード例 #4
0
        public ActionResult PostComment(int clientId, int postId, string content)
        {
            if (AuthorizationMiddleware.Authorized(Session))
            {
                Comment comment = new Comment
                {
                    Content      = content,
                    ClientID     = clientId,
                    PostID       = postId,
                    CreationDate = DateTime.Now
                };

                if (content != string.Empty)
                {
                    db.Comments.Add(comment);
                    db.SaveChanges();
                }

                return(RedirectToAction("Index"));
            }
            else
            {
                return(RedirectToAction("Index", "Home"));
            }
        }
コード例 #5
0
        public ActionResult DeleteConfirmed(int id)
        {
            if (AuthorizationMiddleware.AdminAuthorized(Session))
            {
                Genre genre = db.Genres.Find(id);

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

                // Removing all the posts of that genre
                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.Genres.Remove(genre);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            else
            {
                return(RedirectToAction("Index", "Home"));
            }
        }
コード例 #6
0
        public ActionResult DeleteConfirmed(int id)
        {
            if (AuthorizationMiddleware.Authorized(Session))
            {
                Post post = db.Posts.Find(id);

                // Getting all the comments of the post
                List <Comment> lstRemove = new List <Comment>();
                lstRemove = db.Comments.Where(x => x.Post.ID == id).ToList();

                // Removing all the comments of that post
                foreach (Comment cur in lstRemove)
                {
                    Comment comment = db.Comments.Find(cur.ID);
                    db.Comments.Remove(comment);
                }

                db.Posts.Remove(post);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            else
            {
                return(RedirectToAction("Index", "Home"));
            }
        }
コード例 #7
0
        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();

                // Delete user posts
                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);
                }

                // Delete user comments
                lstPosts = db.Posts.ToList();
                foreach (Post currPost in lstPosts)
                {
                    if (currPost.Comments != null)
                    {
                        List <Comment> lstCommentsOfCurrPost = currPost.Comments.ToList();

                        foreach (Comment currComment in lstCommentsOfCurrPost)
                        {
                            if (currComment.ClientID == id)
                            {
                                db.Comments.Remove(currComment);
                            }
                        }
                    }
                }

                db.Clients.Remove(client);

                db.SaveChanges();

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

                return(RedirectToAction("Index"));
            }
            else
            {
                return(RedirectToAction("Index", "Home"));
            }
        }
コード例 #8
0
 // GET: Genres
 public ActionResult Index()
 {
     if (AuthorizationMiddleware.AdminAuthorized(Session))
     {
         return(View(db.Genres.ToList()));
     }
     else
     {
         return(RedirectToAction("Index", "Home"));
     }
 }
コード例 #9
0
 // GET: Posts/Create
 public ActionResult Create()
 {
     if (AuthorizationMiddleware.Authorized(Session))
     {
         ViewBag.ClientID = new SelectList(db.Clients, "ID", "ClientName");
         ViewBag.GenreID  = new SelectList(db.Genres, "ID", "Name");
         return(View());
     }
     else
     {
         return(RedirectToAction("Index", "Home"));
     }
 }
コード例 #10
0
 public ActionResult DeleteConfirmed(int id)
 {
     if (AuthorizationMiddleware.Authorized(Session))
     {
         Comment comment = db.Comments.Find(id);
         db.Comments.Remove(comment);
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     else
     {
         return(RedirectToAction("Index", "Home"));
     }
 }
コード例 #11
0
 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"));
     }
 }
コード例 #12
0
 public ActionResult Edit([Bind(Include = "ID,Name")] Genre genre)
 {
     if (AuthorizationMiddleware.AdminAuthorized(Session))
     {
         if (ModelState.IsValid)
         {
             db.Entry(genre).State = EntityState.Modified;
             db.SaveChanges();
             return(RedirectToAction("Index"));
         }
         return(View(genre));
     }
     else
     {
         return(RedirectToAction("Index", "Home"));
     }
 }
コード例 #13
0
 public ActionResult Edit([Bind(Include = "ID,ClientID,PostID,Content,CreationDate")] Comment comment)
 {
     if (AuthorizationMiddleware.Authorized(Session))
     {
         if (ModelState.IsValid)
         {
             db.Entry(comment).State = EntityState.Modified;
             db.SaveChanges();
             return(RedirectToAction("Index"));
         }
         ViewBag.ClientID = new SelectList(db.Clients, "ID", "ClientName", comment.ClientID);
         ViewBag.PostID   = new SelectList(db.Posts, "ID", "Content", comment.PostID);
         return(View(comment));
     }
     else
     {
         return(RedirectToAction("Index", "Home"));
     }
 }
コード例 #14
0
 // GET: Posts/Delete/5
 public ActionResult Delete(int?id)
 {
     if (AuthorizationMiddleware.Authorized(Session))
     {
         if (id == null)
         {
             return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
         }
         Post post = db.Posts.Find(id);
         if (post == null)
         {
             return(HttpNotFound());
         }
         return(View(post));
     }
     else
     {
         return(RedirectToAction("Index", "Home"));
     }
 }
コード例 #15
0
 // 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"));
     }
 }
コード例 #16
0
 // GET: Posts/Edit/5
 public ActionResult Edit(int?id)
 {
     if (AuthorizationMiddleware.Authorized(Session))
     {
         if (id == null)
         {
             return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
         }
         Post post = db.Posts.Find(id);
         if (post == null)
         {
             return(HttpNotFound());
         }
         ViewBag.ClientID = new SelectList(db.Clients, "ID", "ClientName", post.ClientID);
         ViewBag.GenreID  = new SelectList(db.Genres, "ID", "Name", post.GenreID);
         return(View(post));
     }
     else
     {
         return(RedirectToAction("Index", "Home"));
     }
 }
コード例 #17
0
        // GET: Genres/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            Genre genre = db.Genres.Find(id);

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

            if (AuthorizationMiddleware.AdminAuthorized(Session))
            {
                return(View(genre));
            }
            else
            {
                return(RedirectToAction("Index", "Home"));
            }
        }