-
Notifications
You must be signed in to change notification settings - Fork 64
/
PostCommands.cs
169 lines (137 loc) · 5.67 KB
/
PostCommands.cs
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (c) Source Tree Solutions, LLC. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Author: Joe Audette
// Created: 2016-04-24
// Last Modified: 2018-06-28
//
using cloudscribe.SimpleContent.Models;
using NoDb;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace cloudscribe.SimpleContent.Storage.NoDb
{
public class PostCommands : IPostCommands, IPostCommandsSingleton
{
public PostCommands(
PostCache cache,
IBasicCommands<Post> postCommands,
IBasicQueries<Post> postQueries,
IKeyGenerator keyGenerator
)
{
_cache = cache;
_commands = postCommands;
_query = postQueries;
_keyGenerator = keyGenerator;
}
private PostCache _cache;
private IBasicCommands<Post> _commands;
private IBasicQueries<Post> _query;
private IKeyGenerator _keyGenerator;
public async Task Create(
string projectId,
IPost post,
CancellationToken cancellationToken = default(CancellationToken)
)
{
var p = Post.FromIPost(post);
p.LastModified = DateTime.UtcNow;
// metaweblog sets the id, don't change it if it exists
if(string.IsNullOrWhiteSpace(p.Id))
{
p.Id = _keyGenerator.GenerateKey(p);
}
await _commands.CreateAsync(projectId, p.Id, p).ConfigureAwait(false);
_cache.ClearListCache(projectId);
}
public async Task Update(
string projectId,
IPost post,
CancellationToken cancellationToken = default(CancellationToken)
)
{
var p = Post.FromIPost(post);
p.LastModified = DateTime.UtcNow;
//since we store posts in year month folders we need to check if pubdate changed and move the file if needed by delete and re-reate.
var currentVersion = await _query.FetchAsync(projectId, p.Id);
if(currentVersion.PubDate != p.PubDate)
{
await _commands.DeleteAsync(projectId, p.Id).ConfigureAwait(false);
await _commands.CreateAsync(projectId, p.Id, p).ConfigureAwait(false);
}
else
{
await _commands.UpdateAsync(projectId, p.Id, p).ConfigureAwait(false);
}
_cache.ClearListCache(projectId);
}
//private async Task HandlePubDateAboutToChange(
// string projectId,
// IPost post,
// DateTime newPubDate,
// CancellationToken cancellationToken = default(CancellationToken))
//{
// if (post.PubDate.HasValue)
// {
// if ((post.PubDate.Value.Month == newPubDate.Month) && (post.PubDate.Value.Year == newPubDate.Year))
// {
// // we store posts in /year/month folders, if that didn't change no need to do anything
// return;
// }
// }
// // because with the filesystem storage we are storing posts in a year/month folder
// // if the year or month changes we need to delete the old file and save the updated post to the
// // new year/month folder
// var p = Post.FromIPost(post);
// await _commands.DeleteAsync(projectId, p.Id).ConfigureAwait(false);
// p.PubDate = newPubDate;
// await _commands.CreateAsync(projectId, p.Id, p).ConfigureAwait(false);
//}
public async Task Delete(
string projectId,
string postId,
CancellationToken cancellationToken = default(CancellationToken)
)
{
var post = await _query.FetchAsync(projectId, postId, cancellationToken).ConfigureAwait(false);
if (post != null)
{
//var allPosts = await GetAllPosts(projectId, CancellationToken.None).ConfigureAwait(false);
await _commands.DeleteAsync(projectId, postId).ConfigureAwait(false);
//allPosts.Remove(post);
_cache.ClearListCache(projectId);
}
}
private async Task<List<Post>> GetAllPosts(
string projectId,
CancellationToken cancellationToken)
{
var list = _cache.GetAllPosts(projectId);
if (list != null) return list;
var l = await _query.GetAllAsync(projectId, cancellationToken).ConfigureAwait(false);
list = l.ToList();
_cache.AddToCache(list, projectId);
return list;
}
public async Task<string> CloneToNewProject(
string sourceProjectId,
string targetProjectId,
string postId,
bool includeComments = false,
CancellationToken cancellationToken = default(CancellationToken))
{
var post = await _query.FetchAsync(sourceProjectId, postId, CancellationToken.None);
if (post == null) throw new InvalidOperationException("post not found");
var p = Post.FromIPost(post);
p.LastModified = DateTime.UtcNow;
p.Id = _keyGenerator.GenerateKey(p);
await _commands.CreateAsync(targetProjectId, p.Id, p).ConfigureAwait(false);
// NoDB posts don't have categories or comments so no need to copy those
_cache.ClearListCache(targetProjectId);
return p.Id;
}
}
}