-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentRepository.cs
More file actions
69 lines (56 loc) · 1.76 KB
/
Copy pathCommentRepository.cs
File metadata and controls
69 lines (56 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Model;
using Repository.Interfaces;
namespace Repository
{
public class CommentRepository : ICommentRepository
{
private readonly BlogContext _blogContext;
public CommentRepository(BlogContext blogContext)
{
_blogContext = blogContext;
}
public virtual IEnumerable<Comment> GetAll()
{
return _blogContext.Comments.ToList();
}
public virtual Comment Get(Guid id)
{
return _blogContext.Comments.Find(id);
}
public virtual Comment Create(Comment comment)
{
_blogContext.Comments.Add(comment);
_blogContext.SaveChanges();
return comment;
}
public virtual Comment Update(Comment updatedComment)
{
var existingComment = Get(updatedComment.Id.Value);
if (existingComment != null)
{
_blogContext.Entry(existingComment).CurrentValues.SetValues(updatedComment);
_blogContext.SaveChanges();
}
return existingComment;
}
public virtual bool Delete(Guid id)
{
var comment = this.Get(id);
if (comment == null)
{
return false;
}
_blogContext.Comments.Remove(comment);
_blogContext.SaveChanges();
return true;
}
public virtual IEnumerable<Comment> GetByPostId(Guid postId)
{
return _blogContext.Comments.Where(comment => comment.PostId == postId).ToList();
}
}
}