Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joshika39 committed Oct 4, 2023
1 parent 0a861cf commit 9431e50
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
28 changes: 17 additions & 11 deletions _src/Implementation/Repositories/JsonRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Implementation.Repositories
{
internal class JsonRepository<T> : IJsonRepository<T> where T: IEntity
internal class JsonRepository<T> : IJsonRepository<T> where T : IEntity
{
private readonly string _dataPath;
private readonly string _filePath;
Expand All @@ -18,18 +18,18 @@ internal class JsonRepository<T> : IJsonRepository<T> where T: IEntity
private readonly IList<T> _addedEntities;
private readonly IList<Guid> _removedEntities;
private bool _isLocked = false;

public JsonRepository(string filePath)
{
_updatedEntities = new List<T>();
_addedEntities = new List<T>();
_removedEntities = new List<Guid>();

_id = Guid.NewGuid();
_filePath = filePath ?? throw new ArgumentNullException(nameof(filePath));
_dataPath = "./";
_dataPath = Path.GetDirectoryName(filePath) ?? @".\";
}

public JsonRepository(string directory, string repositoryKey)
{
_updatedEntities = new List<T>();
Expand All @@ -40,9 +40,9 @@ public JsonRepository(string directory, string repositoryKey)
_dataPath = directory ?? throw new ArgumentNullException(nameof(directory));
_filePath = $@"{_dataPath}\{repositoryKey}.json";
}

public async Task<IEnumerable<T>> GetAllEntities()
{
await SaveChanges();
return await GetAllContent();
}
public async Task<T?> GetEntity(Guid id)
Expand Down Expand Up @@ -82,6 +82,7 @@ private async Task CreateRepository()

private async Task<IEnumerable<T>> GetAllContent()
{
await SaveChanges();
var allContent = await File.ReadAllTextAsync(_filePath);
return await Task.Factory.StartNew(() => JsonConvert.DeserializeObject<IEnumerable<T>>(allContent));
}
Expand All @@ -106,29 +107,34 @@ public async Task<IRepository<T>> SaveChanges()
currentContent.Add(updatedEntity);
}
}

foreach (var id in _removedEntities)
{
if (currentContent.Any(e => e.Id.Equals(id)))
{
currentContent.Remove(currentContent.First(e => e.Id.Equals(id)));
}
}

var newContent = await Task.Factory.StartNew(() => JsonConvert.SerializeObject(currentContent));
await File.WriteAllTextAsync(_filePath, newContent);
_isLocked = false;
return await Task.FromResult<IRepository<T>>(this);
}

public async ValueTask DisposeAsync()
{
await SaveChanges();
}

public void Dispose()
{
new Task(async () => { await SaveChanges(); }).RunSynchronously();
new Task(Action).RunSynchronously();
}

private async void Action()
{
await SaveChanges();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ public async Task JRT_0001_Given_InExistentJsonRepository_When_ConstructorCalled
var fileName = $@".\data\users-{id}.json";
try
{
await using (var repository = new JsonRepository<User>(fileName))
{
await repository.SaveChanges();
Assert.True(File.Exists(fileName));
var text = await File.ReadAllTextAsync(fileName);
Assert.Equal("[]", text);
}
await using var repository = new JsonRepository<User>(@".\data", $"users-{id}");
await repository.DisposeAsync();
Assert.True(File.Exists(fileName));
var text = await File.ReadAllTextAsync(fileName);
Assert.Equal("[]", text);
}
finally
{
Expand Down

0 comments on commit 9431e50

Please sign in to comment.