Skip to content

Commit

Permalink
added synchronous repository methods
Browse files Browse the repository at this point in the history
  • Loading branch information
joshika39 committed Oct 9, 2023
1 parent 29c594c commit 07fbaca
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 17 deletions.
76 changes: 63 additions & 13 deletions _src/Implementation/Repositories/AJsonRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@ protected AJsonRepository(IApplicationSettings applicationSettings, string repos
_filePath = Path.Join(_dataPath, $"{repositoryKey}.json");
}

public async Task<IEnumerable<T>> GetAllEntities()
public async Task<IEnumerable<T>> GetAllEntitiesAsync()
{
return await GetAllContent();
}
public IEnumerable<T> GetAllEntities()
{
return GetAllEntitiesAsync().Result;
}

public async Task<T?> GetEntity(Guid id)
{
var allContent = await GetAllContent();
Expand All @@ -54,8 +59,43 @@ public IRepository<T> Update(T entity)
_updatedEntities.Add(entity);
return this;
}

public IRepository<T> SaveChanges()
{
if (_isLocked)
{
return this;
}
CreateRepository();
_isLocked = true;
var currentContent = GetAllContent().Result.ToList();
currentContent.AddRange(_addedEntities);

foreach (var updatedEntity in _updatedEntities)
{
var target = currentContent.FirstOrDefault(e => e.Id.Equals(updatedEntity.Id));
if (target != null)
{
currentContent.Remove(target);
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 = JsonConvert.SerializeObject(currentContent);
File.WriteAllText(_filePath, newContent);
_isLocked = false;
return this;
}

private async Task CreateRepository()
private async Task CreateRepositoryAsync()
{
if (!Directory.Exists(_dataPath))
{
Expand All @@ -68,21 +108,36 @@ private async Task CreateRepository()
await File.AppendAllTextAsync(_filePath, "[]");
}
}

private void CreateRepository()
{
if (!Directory.Exists(_dataPath))
{
Directory.CreateDirectory(_dataPath);
}

if (!File.Exists(_filePath))
{
File.Create(_filePath).Close();
File.AppendAllText(_filePath, "[]");
}
}

private async Task<IEnumerable<T>> GetAllContent()
{
await SaveChanges();
await SaveChangesAsync();
var allContent = await File.ReadAllTextAsync(_filePath);
return await Task.Factory.StartNew(() => JsonConvert.DeserializeObject<IEnumerable<T>>(allContent));
var list = await Task.Factory.StartNew(() => JsonConvert.DeserializeObject<IEnumerable<T>>(allContent));
return list ?? new List<T>();
}

public async Task<IRepository<T>> SaveChanges()
public async Task<IRepository<T>> SaveChangesAsync()
{
if (_isLocked)
{
return await Task.FromResult<IRepository<T>>(this);
}
await CreateRepository();
await CreateRepositoryAsync();
_isLocked = true;
var currentContent = (await GetAllContent()).ToList();
currentContent.AddRange(_addedEntities);
Expand Down Expand Up @@ -113,17 +168,12 @@ public async Task<IRepository<T>> SaveChanges()

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

public void Dispose()
{
new Task(Action).RunSynchronously();
}

private async void Action()
{
await SaveChanges();
SaveChanges();
}
}
}
6 changes: 4 additions & 2 deletions _src/Infrastructure/Repositories/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ namespace Infrastructure.Repositories
{
public interface IRepository<T> : IAsyncDisposable, IDisposable where T: IEntity
{
Task<IEnumerable<T>> GetAllEntities();
Task<IEnumerable<T>> GetAllEntitiesAsync();
IEnumerable<T> GetAllEntities();
Task<T?> GetEntity(Guid id);
IRepository<T> Create(T entity);
IRepository<T> Delete(Guid id);
IRepository<T> Update(T entity);
Task<IRepository<T>> SaveChanges();
Task<IRepository<T>> SaveChangesAsync();
IRepository<T> SaveChanges();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async Task JRT_0011_Given_EmptyRepository_When_CreateEntityCalled_Then_En
Name = "Peter",
Age = 25
};
await repository.Create(user).SaveChanges();
await repository.Create(user).SaveChangesAsync();
var jsonString = $"[{{\"Id\":\"{user.Id}\",\"Name\":\"{user.Name}\",\"Age\":{user.Age}}}]";

Assert.True(File.Exists(fileName));
Expand All @@ -66,13 +66,43 @@ public async Task JRT_0011_Given_EmptyRepository_When_CreateEntityCalled_Then_En
}
}
}

[Fact]
public void JRT_0011_Given_EmptyRepository_When_CreateEntityCalledSynchronously_Then_EntityCreated()
{
var id = Guid.NewGuid();
var fileName = $@".\data\users-{id}.json";

try
{
using var repository = CreateRepositoryFactory(@".\data").CreateJsonRepository<User>($"users-{id}");
var user = new User
{
Name = "Peter",
Age = 25
};
repository.Create(user);
repository.SaveChanges();
var jsonString = $"[{{\"Id\":\"{user.Id}\",\"Name\":\"{user.Name}\",\"Age\":{user.Age}}}]";

Assert.True(File.Exists(fileName));
Assert.Equal(jsonString, File.ReadAllText(fileName));
}
finally
{
if (File.Exists($@".\data\users-{id}.json"))
{
File.Delete(fileName);
}
}
}

[Fact]
public async Task JRT_0021_Given_JsonFile_When_GetAllEntitiesCalled_Then_AllEntitiesReturns()
{
var fileName = @".\Resources\JRT\0021-users.json";
await using var repository = CreateRepositoryFactory(@".\Resources\JRT").CreateJsonRepository<User>("0021-users");
var allUsers = await repository.GetAllEntities();
var allUsers = await repository.GetAllEntitiesAsync();
Assert.True(File.Exists(fileName));
Assert.Equal(4, allUsers.Count());
}
Expand Down

0 comments on commit 07fbaca

Please sign in to comment.