Esempio n. 1
0
 public List <Post> GetAllPosts()
 {
     using (PostContainer context = new PostContainer())
     {
         return(context.Posts.Include("Comments").ToList <Post>());
     }
 }
Esempio n. 2
0
 public int DeletePost(int id)
 {
     using (PostContainer ctx = new PostContainer())
     {
         return(ctx.Database.ExecuteSqlCommand("Delete From Post where postid =@p0", id));
     }
 }
Esempio n. 3
0
 public List <Post> GetAllPosts()
 {
     using (PostContainer ctx = new PostContainer())
     {
         return(ctx.Posts.Include("Comments").ToList <Post>());
         // Obligatoriu de verificat in apelant rezultatul primit.
     }
 }
Esempio n. 4
0
 public Comment GetCommentById(int id)
 {
     using (PostContainer ctx = new PostContainer())
     {
         var items = from c in ctx.Comments where (c.CommentId == id) select c;
         return(items.Include(p => p.Post).SingleOrDefault());
     }
 }
Esempio n. 5
0
 public Post GetPostById(int id)
 {
     using (PostContainer ctx = new PostContainer())
     {
         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); // trebuie verificat in apelant
     }
 }
Esempio n. 6
0
 public bool AddPost()
 {
     using (PostContainer ctx = new PostContainer())
     {
         bool bResult = false;
         if (this.PostId == 0)
         {
             var it = ctx.Entry <Post>(this).State = EntityState.Added;
             ctx.SaveChanges();
             bResult = true;
         }
         return(bResult);
     }
 }
Esempio n. 7
0
 public Post UpdatePost(Post newPost)
 {
     using (PostContainer ctx = new PostContainer())
     {
         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);
     }
 }
Esempio n. 8
0
 public bool AddComment()
 {
     using (PostContainer ctx = new PostContainer())
     {
         bool bResult = false;
         if (this == null || this.PostPostId == 0)
         {
             return(bResult);
         }
         if (this.CommentId == 0)
         {
             ctx.Entry <Comment>(this).State = EntityState.Added;
             Post p = ctx.Posts.Find(this.PostPostId);
             ctx.Entry <Post>(p).State = EntityState.Unchanged;
             ctx.SaveChanges();
             bResult = true;
         }
         return(bResult);
     }
 }
Esempio n. 9
0
 public Comment UpdateComment(Comment newComment)
 {
     using (PostContainer ctx = new PostContainer())
     {
         Comment oldComment = ctx.Comments.Find(newComment.CommentId);
         // Deoarece parametrul este un Comment ar trebui verificata fiecare
         // proprietate din newComment daca are valoare atribuita si
         // daca valoarea este diferita de cea din bd.
         // Acest lucru il fac numai la modificarea asocierii.
         if (newComment.Text != null)
         {
             oldComment.Text = newComment.Text;
         }
         if ((oldComment.PostPostId != newComment.PostPostId) &&
             (newComment.PostPostId != 0))
         {
             oldComment.PostPostId = newComment.PostPostId;
         }
         ctx.SaveChanges();
         return(oldComment);
     }
 }