public void TestIsBlogTitleUnique()
        {
            Database.SetInitializer(new DatabaseInitializer());
            var context = new BlogDbContext();

            context.Blogs.Add(new Blog() { Title = "abc" });
            context.SaveChanges();

            context.Blogs.Add(new Blog() { Title = "abc" });
            context.SaveChanges();
        }
コード例 #2
0
        public ActionResult DeleteConfirmed(string id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var database = new BlogDbContext())
            {
                //get user from database
                var user = database.Users
                           .Where(u => u.Id.Equals(id))
                           .First();

                //get user articles from database
                var userArticles = database.Articles
                                   .Where(a => a.Author.Id == user.Id);

                //delete user articles
                foreach (var article in userArticles)
                {
                    database.Articles.Remove(article);
                }

                //delete user and save changes
                database.Users.Remove(user);
                database.SaveChanges();

                return(RedirectToAction("List"));
            }
        }
コード例 #3
0
        public ActionResult Edit(string id, EditUserViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                using (var db = new BlogDbContext())
                {
                    var user = db.Users.FirstOrDefault(u => u.Id == id);

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

                    if (!string.IsNullOrEmpty(viewModel.Password))
                    {
                        var hasher       = new PasswordHasher();
                        var passwordHash = hasher.HashPassword(viewModel.Password);
                        user.PasswordHash = passwordHash;
                    }

                    user.Email    = viewModel.User.Email;
                    user.FullName = viewModel.User.FullName;
                    user.UserName = viewModel.User.Email;
                    this.SetUserRoles(viewModel, user, db);

                    db.Entry(user).State = EntityState.Modified;
                    db.SaveChanges();

                    return(RedirectToAction("List"));
                }
            }
            return(View(viewModel));
        }
コード例 #4
0
        public ActionResult Edit(ArticleViewModel model)
        {
            //Check if model state is valid
            if (ModelState.IsValid)
            {
                using (var db = new BlogDbContext())
                {
                    //Get article from database
                    var article = db.Articles.
                                  FirstOrDefault(a => a.Id == model.Id);

                    //Set article properties
                    article.Title   = model.Title;
                    article.Content = model.Content;

                    //Save article state in database
                    db.Entry(article).State = EntityState.Modified;
                    db.SaveChanges();

                    //Redirect to the index page
                    return(RedirectToAction("Index"));
                }
            }

            //If model state is invalid, return the same view
            return(View(model));
        }
コード例 #5
0
        public ActionResult Create(Article article)
        {
            if (ModelState.IsValid)
            {
                using (var database = new BlogDbContext())
                {
                    //Get author id
                    var authorId = database.Users
                                   .Where(u => u.UserName == this.User.Identity.Name)
                                   .Select(x => x.Id)
                                   .FirstOrDefault();

                    if (authorId == null)
                    {
                        return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
                    }

                    //Set articles author
                    article.AuthorId = authorId;

                    //Save article in DB
                    database.Articles.Add(article);
                    database.SaveChanges();

                    return(RedirectToAction("Index"));
                }
            }

            return(View(article));
        }
コード例 #6
0
        public static bool RegisterNewUser(RegisterModel model)
        {
            using (BlogDbContext db = new BlogDbContext())
            {
                if (db.Users.Any(u => u.Username.Equals(model.Login)))
                {
                    return(false);
                }
                User newUser = new User
                {
                    Username  = model.Login,
                    Password  = PasswordMethods.CreateHash(model.Password),
                    Name      = model.Name,
                    Surname   = model.Surname,
                    Email     = model.Email,
                    RoleId    = 1,
                    IsDeleted = false
                };
                db.Users.Add(newUser);
                db.SaveChanges();

                if (db.Users.Any(u => u.Username.Equals(newUser.Username)))
                {
                    return(true);
                }
            }

            return(false);
        }
コード例 #7
0
        public ActionResult Edit(int?id, FormCollection editedArticle)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            if (ModelState.IsValid)
            {
                using (var database = new BlogDbContext())
                {
                    var article = database.Articles
                                  .Where(a => a.Id == id)
                                  .First();

                    if (!IsUserAuthorizedToEdit(article))
                    {
                        return(new HttpStatusCodeResult(HttpStatusCode.Forbidden));
                    }

                    article.Title   = editedArticle["Title"];
                    article.Content = editedArticle["Content"];
                    database.Entry(article).State = EntityState.Modified;
                    database.SaveChanges();

                    return(RedirectToAction("Index"));
                }
            }

            return(View(editedArticle));
        }
コード例 #8
0
        public ActionResult ViewPost(int id)
        {
            var model = new PostViewModel();

            using (var dbContext = new BlogDbContext())
            {
                Post currentPost =
                    dbContext.Posts.Where(p => p.PostId == id).FirstOrDefault();

                //update the selected post
                currentPost.Counter = currentPost.Counter + 1;
                //tell the db that there are updates
                dbContext.Entry(currentPost).State = EntityState.Modified;
                //save changs to the database
                dbContext.SaveChanges();

                model.PostId      = currentPost.PostId;
                model.PostTitle   = currentPost.PostTitle;
                model.DatePost    = currentPost.DatePost;
                model.PostContent = currentPost.PostContent;
                model.Counter     = currentPost.Counter;
            }

            return(View(model));
        }
コード例 #9
0
        public ActionResult Edit(ArticleViewModel model)
        {
            //check if model state is valid
            if (ModelState.IsValid)
            {
                using (var database = new BlogDbContext())
                {
                    //Get article from database
                    var article = database.Articles.FirstOrDefault(a => a.Id == model.Id);

                    //Set article's new values
                    article.Title     = model.Title;
                    article.Content   = model.Content;
                    article.DateAdded = DateTime.Now;

                    //Set the article state to modified
                    //Save the article in database
                    database.Entry(article).State = EntityState.Modified;
                    database.SaveChanges();

                    //Redirect to index page to see the changes
                    return(RedirectToAction("Index"));
                }
            }

            //If model is invalid return the same view
            return(View(model));
        }
コード例 #10
0
        public ActionResult UpdateUser(int userId, string name, string surname, string email, string password,
                                       string username, int roleId, bool isDeleted = false)
        {
            using (var db = new BlogDbContext())
            {
                var user = db.Users.FirstOrDefault(u => u.UserId == userId);
                if (user != null)
                {
                    user.Name     = name;
                    user.Surname  = surname;
                    user.Email    = email;
                    user.Username = username;
                    user.RoleId   = roleId;
                    if (!string.IsNullOrEmpty(password))
                    {
                        user.Password = GetMethods.GetHash(password);
                    }

                    user.IsDeleted = Convert.ToBoolean(isDeleted);
                    if (!isDeleted)
                    {
                        var posts = db.Posts.Where(p => p.UserId == userId).ToList();
                        RestoreUserPosts(posts);
                    }
                }

                db.SaveChanges();

                return(View("~/Views/Admin/AdminMain.cshtml"));
            }
        }
コード例 #11
0
        public ActionResult CreateUser(string name, string surname, string email, string password, string username,
                                       int roleId)
        {
            var isAdmin = CheckMethods.IsCurrentUserAdmin(User.Identity.Name);

            if (User.Identity.IsAuthenticated && isAdmin)
            {
                using (var db = new BlogDbContext())
                {
                    var user = new User();
                    user.Name     = name;
                    user.Surname  = surname;
                    user.Email    = email;
                    user.Username = username;
                    user.Password = GetMethods.GetHash(password);
                    user.RoleId   = roleId;
                    db.Users.Add(user);
                    db.SaveChanges();

                    return(View("~/Views/Admin/AdminMain.cshtml"));
                }
            }

            return(new HttpStatusCodeResult(HttpStatusCode.Forbidden));
        }
コード例 #12
0
        public ActionResult DeleteUser(int userId)
        {
            using (var db = new BlogDbContext())
            {
                var user = db.Users.FirstOrDefault(u => u.UserId == userId);
                if (user != null)
                {
                    user.IsDeleted = true;
                }
                var posts = db.Posts.Where(p => p.UserId == userId).ToList();

                foreach (var p in posts)
                {
                    p.IsDeleted = true;
                }

                var comments = db.Comments.ToList();
                foreach (var c in comments)
                {
                    var postId = posts.Where(p => p.PostId == c.PostId).Select(p => p.PostId).FirstOrDefault();
                    if (c.UserId == userId)
                    {
                        db.Comments.Remove(c);
                    }
                    else if (c.PostId == postId)
                    {
                        db.Comments.Remove(c);
                    }
                }

                db.SaveChanges();
                ViewBag.Users = db.Users.ToList();
                return(View("~/Views/Admin/AdminMain.cshtml"));
            }
        }
コード例 #13
0
        public IActionResult CreatePost([FromBody] PostModel model)
        {
            Post post = new Post()
            {
                Id   = new Guid(),
                Name = model.Name
            };

            _dbContext.Posts.Add(post);

            foreach (var postCategoryModel in model.PostCategories)
            {
                PostCategory postCategory = new PostCategory()
                {
                    PostId     = post.Id,
                    CategoryId = postCategoryModel.Id
                };

                _dbContext.PostCategories.Add(postCategory);
            }

            _dbContext.SaveChanges();

            var postModels = PrepareBlogPostModels();

            return(Ok(postModels));
        }
コード例 #14
0
        public ActionResult DeleteConfirmed(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var database = new BlogDbContext())
            {
                var article = database.Articles
                              .Where(a => a.Id == id)
                              .Include(a => a.Author)
                              .Include(a => a.Category)
                              .First();


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


                database.Articles.Remove(article);
                database.SaveChanges();


                return(RedirectToAction("Index"));
            }
        }
コード例 #15
0
        public ActionResult DeleteAction(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var db = new BlogDbContext())
            {
                var article = db.Articles.Find(id);

                if (!IsAuthorized(article))
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.Forbidden));
                }

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

                db.Articles.Remove(article);
                db.SaveChanges();

                return(RedirectToAction("List"));
            }
        }
コード例 #16
0
        public IActionResult OnPost()
        {
            _db.Blogs.Add(Blog);
            _db.SaveChanges();

            return(Redirect("index"));
        }
コード例 #17
0
        public ActionResult Edit(ArticleViewModel model)
        {
            //check if model state is valid
            if (ModelState.IsValid)
            {
                using (var database = new BlogDbContext())
                {
                    //get article from database
                    var article = database.Articles
                                  .FirstOrDefault(a => a.Id == model.Id);
                    //set article properties
                    article.Title      = model.Title;
                    article.Content    = model.Content;
                    article.CategoryId = model.CategoryId;
                    this.SetArticleTags(article, model, database);

                    //save article state in database
                    database.Entry(article).State = EntityState.Modified;
                    database.SaveChanges();
                    //redirect to the index page
                    return(RedirectToAction("Index"));
                }
            }
            //if model state is invalid return the same view
            return(View(model));
        }
コード例 #18
0
        public ActionResult DeleteConfirmed(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            using (var db = new BlogDbContext())
            {
                var article = db.Articles.Where(a => a.Id == id).Include(a => a.Author).First();

                if (!IsUserAuthorizedToEdit(article))
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.Forbidden));
                }

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

                db.Articles.Remove(article);
                db.SaveChanges();

                return(RedirectToAction("Index"));
            }
        }
コード例 #19
0
        //GET: Article/Details
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            using (var database = new BlogDbContext())
            {
                //Get the article from database
                var article = database.Articles
                              .Where(a => a.Id == id)
                              .Include(a => a.Author)
                              .Include(a => a.Tags)
                              .First();

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

                article.Views++;

                //save article state in database
                database.Entry(article).State = EntityState.Modified;
                database.SaveChanges();

                return(View(article));
            }
        }
コード例 #20
0
        public ActionResult AddCommentary(Comment comment, int ArticleId)
        {
            if (comment == null && _ArticleId != null)
            {
                return(View("~/Views/Home/ErrorView", new { error = "Commentary was not received" }));
            }
            if (comment.Text.Length > 400)
            {
                comment.Text = comment.Text.Substring(0, 400);
            }
            if (ModelState.IsValid)
            {
                comment.ArticleId = (int)_ArticleId;
                comment.ArticleId = ArticleId;
                comment.Id        = 0;
                comment.Date      = DateTime.Now;

                using (BlogDbContext BlogDB = new BlogDbContext())
                {
                    BlogDB.Comments.Add(comment);
                    BlogDB.SaveChanges();
                }

                return(PartialView("PartialAppendComment", comment));
            }
            else
            {
                return(View("~/Views/Home/ErrorView", new { error = "Commentary was not received" }));
            }
        }
コード例 #21
0
        public ActionResult Create(ArticleViewModel model)
        {
            if (ModelState.IsValid)
            {
                //insert article in DB
                using (var database = new BlogDbContext())
                {
                    //Get author id
                    var authorId = database.Users
                                   .Where(u => u.UserName == this.User.Identity.Name)
                                   .First()
                                   .Id;

                    var article = new Article(authorId, model.Title, model.Content, model.CategoryId);

                    this.SetArticleTags(article, model, database);

                    //Save article in DB
                    database.Articles.Add(article);
                    database.SaveChanges();

                    return(RedirectToAction("Index"));
                }
            }
            return(View(model));
        }
コード例 #22
0
        public ActionResult CreateComment(Comment model)
        {
            var blogDb  = new BlogDbContext();
            var user    = User.Identity.GetUserId();
            var author  = blogDb.Profiles.FirstOrDefault(u => u.ProfileID == user);
            var comment = new Comment
            {
                Text             = model.Text,
                Date             = DateTime.Now,
                AuthorOfComments = author,
                ProfileID        = user,
                PostID           = Convert.ToInt32(Session["postId"]),
            };

            blogDb.Comments.Add(comment);
            blogDb.SaveChanges();

            var commentedPost = blogDb.Posts.FirstOrDefault(p => p.PostID == comment.PostID);

            if (commentedPost.PublishedWall.Equals("Formell"))
            {
                return(RedirectToAction("FormalWall", "Wall"));
            }
            else
            {
                return(RedirectToAction("InformalWall", "Wall"));
            }
        }
コード例 #23
0
ファイル: BoxingController.cs プロジェクト: ros1914/Blog
        public ActionResult DeleteConfirmed(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var database = new BlogDbContext())
            {
                // Get Boxing from database
                var boxing = database.Boxings
                             .Where(a => a.Id == id)
                             .Include(a => a.Author)
                             .First();

                //Check if boxing exists
                if (boxing == null)
                {
                    return(HttpNotFound());
                }
                // Delete boxing from database
                //Remove boxing from db
                database.Boxings.Remove(boxing);
                database.SaveChanges();

                // Redirect to index page
                return(RedirectToAction("List"));
            }
        }
コード例 #24
0
        public ActionResult DeleteConfirmed(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(
                           HttpStatusCode.BadRequest));
            }

            using (var db = new BlogDbContext())
            {
                //Get article from database
                var article = db.Articles.
                              Where(a => a.Id == id).
                              Include(a => a.Author).
                              First();

                //Check if article exists
                if (article == null)
                {
                    return(HttpNotFound());
                }

                //Delete article from database
                db.Articles.Remove(article);
                db.SaveChanges();

                //Redirect to index page
                return(RedirectToAction("Index"));
            }
        }
コード例 #25
0
ファイル: BoxingController.cs プロジェクト: ros1914/Blog
        public ActionResult Edit(BoxingViewModel model)
        {
            // Check if model state is valid
            if (ModelState.IsValid)
            {
                using (var database = new BlogDbContext())
                {
                    // Get boxing from database
                    var boxing = database.Boxings
                                 .FirstOrDefault(a => a.Id == model.Id);
                    // Set boxing properties
                    boxing.Title   = model.Title;
                    boxing.Content = model.Content;
                    // boxing.CategoryId = model.CategoryId;
                    // this.SetboxingTags(boxing, model, database);
                    // Save boxing state in database
                    database.Entry(boxing).State = EntityState.Modified;
                    database.SaveChanges();
                    // Redirect to the index page
                    return(RedirectToAction("List"));
                }
            }

            // If model state is invalid, return the same view
            return(View(model));
        }
コード例 #26
0
        public ActionResult Create(Article article)
        {
            if (ModelState.IsValid)
            {
                //Insert article in database
                using (var db = new BlogDbContext())
                {
                    //Get author id;
                    var authorId = db.Users.
                                   Where(u => u.UserName == this.User.Identity.Name).
                                   First().
                                   Id;

                    //Set articles author
                    article.AuthorId = authorId;

                    //Save article in database
                    db.Articles.Add(article);
                    db.SaveChanges();

                    return(RedirectToAction("Index"));
                }
            }
            return(View(article));
        }
コード例 #27
0
        public ActionResult Delete(int?id, Article article)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var db = new BlogDbContext())
            {
                article = db.Articles
                          .Where(a => a.Id == id)
                          .Include(a => a.Author)
                          .First();

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

                db.Articles.Remove(article);
                db.SaveChanges();

                return(RedirectToAction("Index"));
            }
        }
コード例 #28
0
        public ActionResult Edit(ArticleViewModel model)
        {
            //Check if model state is valid
            if (ModelState.IsValid)
            {
                using (var db = new BlogDbContext())
                {
                    // Get article from database
                    var article = db.Articles.FirstOrDefault(a => a.Id == model.Id);

                    // Check if current user is authorized to edit the article
                    if (!IsUserAuthorizedToEdit(article))
                    {
                        return(new HttpStatusCodeResult(HttpStatusCode.Forbidden));
                    }

                    // Set article properties
                    article.Title      = model.Title;
                    article.Content    = model.Content;
                    article.CategoryId = model.CategoryId;
                    this.SetArticleTags(article, model, db);

                    // Save article state in database
                    db.Entry(article).State = EntityState.Modified;
                    db.SaveChanges();

                    // Redirect to index
                    return(RedirectToAction("Index"));
                }
            }

            // If model state is invalid return the same view
            return(View(model));
        }
コード例 #29
0
        public ActionResult Edit(ArticleViewModel model)
        {
            if (ModelState.IsValid)
            {
                using (var db = new BlogDbContext())
                {
                    var article = db.Articles
                                  .FirstOrDefault(a => a.Id == model.Id);

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

                    if (!CanEdit(article))
                    {
                        return(new HttpStatusCodeResult(HttpStatusCode.Forbidden));
                    }

                    article.Title   = model.Title;
                    article.Content = model.Content;

                    db.Entry(article).State = EntityState.Modified;
                    db.SaveChanges();

                    return(RedirectToAction("Index"));
                }
            }

            return(View(model));
        }
コード例 #30
0
        public ActionResult DeleteConfirmed(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var database = new BlogDbContext())
            {
                var publication = database.Publications
                                  .Where(p => p.Id == id)
                                  .Include(p => p.Author)
                                  .Include(p => p.Comments)
                                  .First();

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

                database.Publications.Remove(publication);
                database.SaveChanges();
                return(RedirectToAction("Index"));
            }
        }
コード例 #31
0
        public ActionResult EditAction(ArticleViewModel model)
        {
            if (ModelState.IsValid)
            {
                using (var db = new BlogDbContext())
                {
                    var article = db.Articles.Find(model.Id);

                    if (!IsAuthorized(article))
                    {
                        return(new HttpStatusCodeResult(HttpStatusCode.Forbidden));
                    }

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

                    article.Title   = model.Title;
                    article.Content = model.Content;
                    article.Date    = DateTime.Now;

                    db.Entry(article).State = EntityState.Modified;
                    db.SaveChanges();

                    return(RedirectToAction("List"));
                }
            }

            return(View(model));
        }