public void AddArticle(Article article)
 {
     using (var db = new BlogContext())
     {
         db.Articles.Add(article);
         db.SaveChanges();
     }
 }
 public void MarkAsRead(int commentId)
 {
     using (var db = new BlogContext())
     {
         var comment = db.Comments.Find(commentId);
         comment.Read = true;
         db.SaveChanges();
     }
 }
 public void AddComment(Comment comment)
 {
     using (var db = new BlogContext())
     {
         comment.CreationDate = DateTime.UtcNow;
         db.Comments.Add(comment);
         db.SaveChanges();
     }
 }
 public void AddUser(User user)
 {
     using (var db = new BlogContext())
     {
         foreach (Role r in user.Roles)
         {
             db.Roles.Attach(r);
         }
         db.Users.Add(user);
         db.SaveChanges();
     }
 }
 public void ModifyArticle(Article article)
 {
     using (var db = new BlogContext())
     {
         var query = (from a in db.Articles
                      where a.Id == article.Id
                      select a).First();
         query.Layout = article.Layout;
         query.ModificationdDate = DateTime.Now;
         query.Name = article.Name;
         query.PicturePath = article.PicturePath;
         query.Text = article.Text;
         query.Type = article.Type;
         db.SaveChanges();
     }
 }
        public void UpdateUserComments(int userId, Comment comment)
        {
            using (var db = new BlogContext())
            {
                db.Comments.Attach(comment);
                var query = (from u in db.Users.Include(u => u.Comments)
                             where u.IsActive == true && u.Id == userId
                             select u).FirstOrDefault<User>();

                query.Comments.Add(comment);
                db.SaveChanges();
            }
        }
 public void RemoveUser(User user)
 {
     using (var db = new BlogContext())
     {
         var query = (from u in db.Users
                      where u.IsActive == true && u.Id == user.Id
                      select u).FirstOrDefault<User>();
         query.IsActive = false;
         db.SaveChanges();
     }
 }
        public void ModifyUser(User user)
        {
            using (var db = new BlogContext())
            {
                var query = (from u in db.Users.Include(r=>r.Roles)
                             where u.IsActive == true && u.Id == user.Id
                             select u).FirstOrDefault<User>();

                var types = user.Roles.Select(r => r.Type);

                var newRoles = db.Roles
                              .Where(r => types.Contains(r.Type)).ToList();
                query.Email = user.Email;
                query.Name = user.Name;
                query.Surname = user.Surname;
                query.Password = user.Password;
                query.PicturePath = user.PicturePath;
                query.Roles.Clear();
                query.Roles.AddRange(newRoles);
                db.SaveChanges();
            }
        }