-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathRepository.cs
More file actions
114 lines (96 loc) · 3.48 KB
/
Copy pathRepository.cs
File metadata and controls
114 lines (96 loc) · 3.48 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using LinkDotNet.Blog.Domain;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
namespace LinkDotNet.Blog.Infrastructure.Persistence.MongoDB;
public sealed class Repository<TEntity> : IRepository<TEntity>
where TEntity : Entity
{
private readonly IMongoDatabase database;
private IMongoCollection<TEntity> Collection => database.GetCollection<TEntity>(typeof(TEntity).Name);
public Repository(IMongoDatabase database)
{
this.database = database;
}
public async ValueTask<HealthCheckResult> PerformHealthCheckAsync()
{
try
{
var command = new BsonDocument("ping", 1);
await database.RunCommandAsync<BsonDocument>(command);
return HealthCheckResult.Healthy("A healthy result.");
}
catch (Exception ex)
{
return HealthCheckResult.Unhealthy(exception: ex);
}
}
public async ValueTask<TEntity?> GetByIdAsync(string id)
{
var filter = Builders<TEntity>.Filter.Eq(e => e.Id, id);
using var result = await Collection.FindAsync(filter);
return await result.FirstOrDefaultAsync();
}
public async ValueTask<IPagedList<TEntity>> GetAllAsync(Expression<Func<TEntity, bool>>? filter = null,
Expression<Func<TEntity, object>>? orderBy = null,
bool descending = true,
int page = 1,
int pageSize = int.MaxValue) =>
await GetAllByProjectionAsync(s => s, filter, orderBy, descending, page, pageSize);
public async ValueTask<IPagedList<TProjection>> GetAllByProjectionAsync<TProjection>(
Expression<Func<TEntity, TProjection>> selector,
Expression<Func<TEntity, bool>>? filter = null,
Expression<Func<TEntity, object>>? orderBy = null,
bool descending = true,
int page = 1,
int pageSize = int.MaxValue)
{
var query = Collection.AsQueryable();
if (filter is not null)
{
query = query.Where(filter);
}
if (orderBy is not null)
{
query = descending ? query.OrderByDescending(orderBy) : query.OrderBy(orderBy);
}
return await query
.Select(selector)
.ToPagedListAsync(page, pageSize);
}
public async ValueTask StoreAsync(TEntity entity)
{
ArgumentNullException.ThrowIfNull(entity);
if (string.IsNullOrWhiteSpace(entity.Id))
{
entity.Id = ObjectId.GenerateNewId().ToString();
}
var filter = Builders<TEntity>.Filter.Eq(doc => doc.Id, entity.Id);
var options = new ReplaceOptions { IsUpsert = true };
await Collection.ReplaceOneAsync(filter, entity, options);
}
public async ValueTask DeleteAsync(string id)
{
var filter = Builders<TEntity>.Filter.Eq(doc => doc.Id, id);
await Collection.DeleteOneAsync(filter);
}
public async ValueTask DeleteBulkAsync(IReadOnlyCollection<string> ids)
{
var filter = Builders<TEntity>.Filter.In(doc => doc.Id, ids);
await Collection.DeleteManyAsync(filter);
}
public async ValueTask StoreBulkAsync(IReadOnlyCollection<TEntity> records)
{
ArgumentNullException.ThrowIfNull(records);
if (records.Count != 0)
{
await Collection.InsertManyAsync(records);
}
}
}