Fix a collection of small issues
This commit is contained in:
parent
3b61561df9
commit
af89e037f3
1 changed files with 15 additions and 6 deletions
|
@ -33,15 +33,20 @@ namespace Robware.Data {
|
|||
await _collection.UpdateOneAsync(post => post.Id == id, update);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<BlogPost>> GetAllPostsAsync() => MapStateToPost((await _collection.FindAsync(post => true)).ToEnumerable());
|
||||
public async Task<IEnumerable<BlogPost>> GetAllPostsAsync() => MapStateToPost((await _collection.FindAsync(post => !post.Deleted)).ToEnumerable());
|
||||
|
||||
public async Task<int> GetCountAsync() => (int)await _collection.CountDocumentsAsync(post => !post.Deleted);
|
||||
private async Task<int> GetCountAsync(bool includeAll) => (int)await _collection.CountDocumentsAsync(post => includeAll || (!post.Deleted && string.IsNullOrEmpty(post.Content)));
|
||||
|
||||
public async Task<int> GetCountAsync() => await GetCountAsync(false);
|
||||
|
||||
public async Task<BlogPost> GetLatestPostAsync() => (await GetLatestPostsAsync(1)).FirstOrDefault();
|
||||
|
||||
public async Task<IEnumerable<BlogPost>> GetLatestPostsAsync(int limit, int offset = 0) {
|
||||
var filter = Builders<BlogPostState>.Filter.Eq(nameof(BlogPostState.Deleted), false);
|
||||
var sort = Builders<BlogPostState>.Sort.Descending(nameof(BlogPostState.Timestamp));
|
||||
var filter = Builders<BlogPostState>.Filter.And(
|
||||
Builders<BlogPostState>.Filter.Eq(post => post.Deleted, false),
|
||||
Builders<BlogPostState>.Filter.Ne(post => post.Content, null)
|
||||
);
|
||||
var sort = Builders<BlogPostState>.Sort.Descending(post => post.Timestamp);
|
||||
var options = new FindOptions<BlogPostState> {
|
||||
Sort = sort,
|
||||
Limit = limit,
|
||||
|
@ -64,12 +69,16 @@ namespace Robware.Data {
|
|||
|
||||
public async Task<BlogPost> SavePost(BlogPost post) {
|
||||
if (post.Id == 0) {
|
||||
post.Id = (await GetCountAsync()) + 1;
|
||||
post.Id = (await GetCountAsync(true)) + 1;
|
||||
post.Timestamp = DateTime.Now;
|
||||
}
|
||||
|
||||
var mongoPost = new BlogPostState(post);
|
||||
await _collection.InsertOneAsync(mongoPost);
|
||||
var filter = Builders<BlogPostState>.Filter.Eq(p => p.Id, post.Id);
|
||||
var options = new ReplaceOptions {
|
||||
IsUpsert = true
|
||||
};
|
||||
await _collection.ReplaceOneAsync(filter, mongoPost, options);
|
||||
|
||||
return post;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue