| | 1 | | using MRA.DTO.Mapper.Interfaces; |
| | 2 | | using MRA.DTO.Models.Interfaces; |
| | 3 | | using MRA.Infrastructure.Database.Documents.Interfaces; |
| | 4 | | using MRA.Infrastructure.Database.Providers.Interfaces; |
| | 5 | |
|
| | 6 | | namespace MRA.Services.Models.Documents; |
| | 7 | |
|
| | 8 | | public abstract class DocumentModelService<Model, Document> : IDocumentModelService<Model> |
| | 9 | | where Model : IModel |
| | 10 | | where Document : IDocument |
| | 11 | | { |
| | 12 | | private readonly string _collectionName; |
| | 13 | | private readonly IDocumentsDatabase _db; |
| | 14 | | protected IDocumentMapper<Model, Document> Converter; |
| | 15 | |
|
| 36 | 16 | | protected DocumentModelService(string collectionName, IDocumentMapper<Model, Document> converter, IDocumentsDatabase |
| | 17 | | { |
| 36 | 18 | | _collectionName = collectionName; |
| 36 | 19 | | Converter = converter; |
| 36 | 20 | | _db = db; |
| 36 | 21 | | } |
| | 22 | |
|
| | 23 | | public async Task<bool> ExistsAsync(string id) |
| | 24 | | { |
| 15 | 25 | | return await _db.DocumentExistsAsync(_collectionName, id); |
| 14 | 26 | | } |
| | 27 | |
|
| | 28 | | public async Task<IEnumerable<Model>> GetAllAsync() |
| | 29 | | { |
| 8 | 30 | | var documentList = (await _db.GetAllDocumentsAsync<Document>(_collectionName)); |
| 7 | 31 | | return documentList.Select(Converter.ConvertToModel); |
| 7 | 32 | | } |
| | 33 | |
|
| | 34 | | public async Task<Model> FindAsync(string id) |
| | 35 | | { |
| 11 | 36 | | var document = await _db.GetDocumentAsync<Document>(_collectionName, id); |
| 10 | 37 | | return Converter.ConvertToModel(document); |
| 9 | 38 | | } |
| | 39 | |
|
| | 40 | | public async Task<bool> SetAsync(string id, Model model) |
| | 41 | | { |
| 10 | 42 | | var document = Converter.ConvertToDocument(model); |
| 9 | 43 | | return await _db.SetDocumentAsync(_collectionName, id, document); |
| 8 | 44 | | } |
| | 45 | |
|
| | 46 | | public async Task<bool> DeleteAsync(string id) |
| | 47 | | { |
| 2 | 48 | | return await _db.DeleteDocumentAsync(_collectionName, id); |
| 1 | 49 | | } |
| | 50 | | } |