Skip to content

Commit

Permalink
fixed async
Browse files Browse the repository at this point in the history
  • Loading branch information
joshika39 committed Oct 9, 2023
1 parent 07fbaca commit 726025d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
30 changes: 22 additions & 8 deletions _src/Implementation/Repositories/AJsonRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,22 @@ protected AJsonRepository(IApplicationSettings applicationSettings, string repos

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

public async Task<T?> GetEntity(Guid id)
public async Task<T?> GetEntityAsync(Guid id)
{
var allContent = await GetAllContentAsync();
return allContent.FirstOrDefault(e => e.Id.Equals(id));

}

public T? GetEntity(Guid id)
{
var allContent = await GetAllContent();
var allContent = GetAllContent();
return allContent.FirstOrDefault(e => e.Id.Equals(id));
}
public IRepository<T> Create(T entity)
Expand All @@ -68,7 +74,7 @@ public IRepository<T> SaveChanges()
}
CreateRepository();
_isLocked = true;
var currentContent = GetAllContent().Result.ToList();
var currentContent = GetAllContent().ToList();
currentContent.AddRange(_addedEntities);

foreach (var updatedEntity in _updatedEntities)
Expand Down Expand Up @@ -123,13 +129,21 @@ private void CreateRepository()
}
}

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

private IEnumerable<T> GetAllContent()
{
SaveChanges();
var allContent = File.ReadAllText(_filePath);
var list = JsonConvert.DeserializeObject<IEnumerable<T>>(allContent);
return list ?? new List<T>();
}

public async Task<IRepository<T>> SaveChangesAsync()
{
Expand All @@ -139,7 +153,7 @@ public async Task<IRepository<T>> SaveChangesAsync()
}
await CreateRepositoryAsync();
_isLocked = true;
var currentContent = (await GetAllContent()).ToList();
var currentContent = (await GetAllContentAsync()).ToList();
currentContent.AddRange(_addedEntities);

foreach (var updatedEntity in _updatedEntities)
Expand Down
3 changes: 2 additions & 1 deletion _src/Infrastructure/Repositories/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public interface IRepository<T> : IAsyncDisposable, IDisposable where T: IEntity
{
Task<IEnumerable<T>> GetAllEntitiesAsync();
IEnumerable<T> GetAllEntities();
Task<T?> GetEntity(Guid id);
Task<T?> GetEntityAsync(Guid id);
T? GetEntity(Guid id);
IRepository<T> Create(T entity);
IRepository<T> Delete(Guid id);
IRepository<T> Update(T entity);
Expand Down

0 comments on commit 726025d

Please sign in to comment.