Repository navigation
Expand file tree
/
Copy pathBlogService.cs
More file actions
89 lines (74 loc) · 2.81 KB
/
Copy pathBlogService.cs
File metadata and controls
89 lines (74 loc) · 2.81 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
namespace Xv.Blog
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using Xv.Blog.Data;
using Xv.Blog.Model;
public class BlogService
{
private readonly PostRepository postRepository;
private readonly SubscriberRepository subscriberRepository;
private readonly CategoryRepository categoryRepository;
////public BlogService()
//// : this(new PostRepository(), new SubscriberRepository(), new CategoryRepository())
////{
////}
public BlogService()
{
var ctx = new BlogContext();
this.postRepository = new PostRepository(ctx);
this.categoryRepository = new CategoryRepository(ctx);
this.subscriberRepository = new SubscriberRepository(ctx);
}
public BlogService(PostRepository postRepository, SubscriberRepository subscriberRepository, CategoryRepository categoryRepository)
{
this.postRepository = postRepository;
this.subscriberRepository = subscriberRepository;
this.categoryRepository = categoryRepository;
}
public IEnumerable<Post> GetPosts(int take)
{
return this.postRepository.All().OrderByDescending(x => x.DatePublished).Take(take);
}
public Post CreatePost(Post post)
{
this.postRepository.Add(post);
this.postRepository.SaveChanges();
if (post.DatePublished <= DateTime.UtcNow)
{
this.NotifySubscribers(post);
}
return post;
}
public Post GetPostById(int id)
{
return this.postRepository.GetById(id);
}
public Post CreatePost(string title, string body, int categoryId, DateTime dateTime)
{
var category = this.categoryRepository.GetById(categoryId);
var post = new Post() { Title = title, Body = body, Category = category, DatePublished = dateTime };
return this.CreatePost(post);
}
public IEnumerable<Category> GetCategories()
{
return this.categoryRepository.All().ToList();
}
private void NotifySubscribers(Post post)
{
using (var client = new SmtpClient())
{
foreach (var subscriber in this.subscriberRepository.All())
{
client.Send(
subscriber.Email,
"New Blog Post",
"A new blog has been posted. Title: " + post.Title);
}
}
}
}
}