コード例 #1
0
 /// <summary>
 /// Returnez toate Post-urile si Comment-urile corespunzatoare
 /// </summary>
 /// <returns></returns>
 public static List <Post> GetAllPosts()
 {
     using (ModelDesignContainer ctx = new ModelDesignContainer())
     {
         return(ctx.Posts.Include("Comments").ToList <Post>());
     }
 }
コード例 #2
0
 public static int DeletePost(int id)
 {
     using (ModelDesignContainer ctx = new ModelDesignContainer())
     {
         return(ctx.Database.ExecuteSqlCommand("Delete From Post where postid =
        @p0", id));
     }
 }
コード例 #3
0
 public static Comment GetCommentById(int id)
 {
     using (ModelDesignContainer ctx = new ModelDesignContainer())
     {
         var items = from c in ctx.Comments where (c.CommentId == id) select c;
         return(items.Include(p => p.Post).SingleOrDefault());
     }
 }
コード例 #4
0
 /// <summary>
 /// Returnez un Post si toate Comment-urile asociate lui
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 public static Post GetPostById(int id)
 {
     using (ModelDesignContainer ctx = new ModelDesignContainer())
     {
         var items = from p in ctx.Posts where (p.PostId == id) select p;
         if (items != null)
         {
             return(items.Include(c => c.Comments).SingleOrDefault());
         }
         return(null);
     }
 }
コード例 #5
0
 public static bool AddPost(Post post)
 {
     using (ModelDesignContainer ctx = new ModelDesignContainer())
     {
         bool bResult = false;
         if (post.PostId == 0)
         {
             var it = ctx.Entry <Post>(post).State = EntityState.Added;
             ctx.SaveChanges();
             bResult = true;
         }
         return(bResult);
     }
 }
コード例 #6
0
 public static Post UpdatePost(Post newPost)
 {
     using (ModelDesignContainer ctx = new ModelDesignContainer())
     {
         // Ce e in bd. PK nu poate fi modificata
         Post oldPost = ctx.Posts.Find(newPost.PostId);
         if (oldPost == null) // nu exista in bd
         {
             return(null);
         }
         oldPost.Description = newPost.Description;
         oldPost.Domain      = newPost.Domain;
         oldPost.Date        = newPost.Date;
         ctx.SaveChanges();
         return(oldPost);
     }
 }
コード例 #7
0
 public static Comment UpdateComment(Comment newComment)
 {
     using (ModelDesignContainer ctx = new ModelDesignContainer())
     {
         Comment oldComment = ctx.Comments.Find(newComment.CommentId);
         if (newComment.Text != null)
         {
             oldComment.Text = newComment.Text;
         }
         if ((oldComment.PostPostId != newComment.PostPostId) &&
             (newComment.PostPostId != 0))
         {
             oldComment.PostPostId = newComment.PostPostId;
         }
         ctx.SaveChanges();
         return(oldComment);
     }
 }
コード例 #8
0
 // Comment table
 public static bool AddComment(Comment comment)
 {
     using (ModelDesignContainer ctx = new ModelDesignContainer())
     {
         bool bResult = false;
         if (comment == null || comment.PostPostId == 0)
         {
             return(bResult);
         }
         if (comment.CommentId == 0)
         {
             ctx.Entry <Comment>(comment).State = EntityState.Added;
             Post p = ctx.Posts.Find(comment.PostPostId);
             ctx.Entry <Post>(p).State = EntityState.Unchanged;
             ctx.SaveChanges();
             bResult = true;
         }
         return(bResult);
     }
 }