Repository navigation
Expand file tree
/
Copy pathBlogProvider.cs
More file actions
99 lines (88 loc) · 2.72 KB
/
Copy pathBlogProvider.cs
File metadata and controls
99 lines (88 loc) · 2.72 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
90
91
92
93
94
95
96
97
98
99
using Blogifier.Core.Data;
using Blogifier.Shared;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Blogifier.Core.Providers
{
public interface IBlogProvider
{
Task<Blog> GetBlog();
Task<ICollection<Category>> GetBlogCategories();
Task<BlogItem> GetBlogItem();
Task<bool> Update(Blog blog);
}
public class BlogProvider : IBlogProvider
{
private readonly AppDbContext _db;
private readonly IStorageProvider _storageProvider;
public BlogProvider(AppDbContext db, IStorageProvider storageProvider)
{
_db = db;
_storageProvider = storageProvider;
}
public async Task<BlogItem> GetBlogItem()
{
var blog = await _db.Blogs.AsNoTracking().OrderBy(b => b.Id).FirstAsync();
blog.Theme = blog.Theme.ToLower();
return new BlogItem
{
Title = blog.Title,
Description = blog.Description,
Theme = blog.Theme,
IncludeFeatured = blog.IncludeFeatured,
ItemsPerPage = blog.ItemsPerPage,
SocialFields = new List<SocialField>(),
Cover = string.IsNullOrEmpty(blog.Cover) ? blog.Cover : Constants.DefaultCover,
Logo = string.IsNullOrEmpty(blog.Logo) ? blog.Logo : Constants.DefaultLogo,
HeaderScript = blog.HeaderScript,
FooterScript = blog.FooterScript,
values = await GetValues(blog.Theme)
};
}
public async Task<Blog> GetBlog()
{
return await _db.Blogs.OrderBy(b => b.Id).AsNoTracking().FirstAsync();
}
public async Task<ICollection<Category>> GetBlogCategories()
{
return await _db.Categories.AsNoTracking().ToListAsync();
}
public async Task<bool> Update(Blog blog)
{
var existing = await _db.Blogs.OrderBy(b => b.Id).FirstAsync();
existing.Title = blog.Title;
existing.Description = blog.Description;
existing.ItemsPerPage = blog.ItemsPerPage;
existing.IncludeFeatured = blog.IncludeFeatured;
existing.Theme = blog.Theme;
existing.Cover = blog.Cover;
existing.Logo = blog.Logo;
existing.HeaderScript = blog.HeaderScript;
existing.FooterScript = blog.FooterScript;
existing.AnalyticsListType = blog.AnalyticsListType;
existing.AnalyticsPeriod = blog.AnalyticsPeriod;
return await _db.SaveChangesAsync() > 0;
}
private async Task<dynamic> GetValues(string theme)
{
var settings = await _storageProvider.GetThemeSettings(theme);
var values = new Dictionary<string, string>();
if (settings != null && settings.Sections != null)
{
foreach (var section in settings.Sections)
{
if (section.Fields != null)
{
foreach (var field in section.Fields)
{
values.Add(field.Id, field.Value);
}
}
}
}
return values;
}
}
}