-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0909ab8
commit a720277
Showing
7 changed files
with
145 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
using LiteDB; | ||
|
||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace WitsmlExplorer.Api.Repositories | ||
{ | ||
public class LiteDbRepository<TDocument, TDocumentId> : IDocumentRepository<TDocument, TDocumentId> where TDocument : DbDocument<TDocumentId> | ||
{ | ||
private readonly ILiteCollection<TDocument> _collection; | ||
|
||
public LiteDbRepository(IConfiguration configuration) | ||
{ | ||
var filename = configuration["LiteDb:Name"]; | ||
var db = new LiteDatabase(@$"Filename={filename};Connection=shared"); | ||
var collectionName = $"{typeof(TDocument).Name}s"; | ||
_collection = db.GetCollection<TDocument>(collectionName); | ||
} | ||
|
||
public Task InitClientAsync() | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task<TDocument> GetDocumentAsync(TDocumentId id) | ||
{ | ||
var document = _collection.FindById(new BsonValue(id)); | ||
return Task.FromResult(document); | ||
} | ||
|
||
public Task<ICollection<TDocument>> GetDocumentsAsync() | ||
{ | ||
var documents = _collection.FindAll(); | ||
return Task.FromResult<ICollection<TDocument>>(new List<TDocument>(documents)); | ||
} | ||
|
||
public Task<TDocument> UpdateDocumentAsync(TDocumentId id, TDocument document) | ||
{ | ||
_collection.Update(document); | ||
return GetDocumentAsync(id); | ||
} | ||
|
||
public Task<TDocument> CreateDocumentAsync(TDocument document) | ||
{ | ||
_collection.Insert(document); | ||
return GetDocumentAsync(document.Id); | ||
} | ||
|
||
public Task DeleteDocumentAsync(TDocumentId id) | ||
{ | ||
_collection.Delete(new BsonValue(id)); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
Tests/WitsmlExplorer.IntegrationTests/Api/Repositories/LiteDbRepositoryTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
using Microsoft.Extensions.Configuration; | ||
|
||
using WitsmlExplorer.Api.Models; | ||
using WitsmlExplorer.Api.Repositories; | ||
|
||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace WitsmlExplorer.IntegrationTests.Api.Repositories | ||
{ | ||
public class LiteDbServerRepositoryTests | ||
{ | ||
private readonly IDocumentRepository<Server, Guid> _repo; | ||
private readonly ITestOutputHelper _output; | ||
|
||
public LiteDbServerRepositoryTests(ITestOutputHelper output) | ||
{ | ||
IConfiguration configuration = ConfigurationReader.GetConfig(); | ||
_repo = new LiteDbRepository<Server, Guid>(configuration); | ||
_output = output; | ||
} | ||
|
||
[Fact(Skip = "Should only be run manually")] | ||
public async Task GetAllServers() | ||
{ | ||
IEnumerable<Server> servers = await _repo.GetDocumentsAsync(); | ||
foreach (Server server in servers) | ||
{ | ||
_output.WriteLine(server.ToString()); | ||
} | ||
} | ||
|
||
[Fact(Skip = "Should only be run manually")] | ||
public async Task AddServer() | ||
{ | ||
Server newServer = new() | ||
{ | ||
Name = "<insert servername>", | ||
Url = new Uri("<insert url>"), | ||
Description = "" | ||
}; | ||
Server server = await _repo.CreateDocumentAsync(newServer); | ||
_output.WriteLine($"Inserted server {server}"); | ||
} | ||
|
||
[Fact(Skip = "Should only be run manually")] | ||
public async Task RemoveServer() | ||
{ | ||
Guid serverId = new("<insert server id>"); | ||
await _repo.DeleteDocumentAsync(serverId); | ||
_output.WriteLine($"Removed server"); | ||
} | ||
} | ||
} |