Skip to content

Commit

Permalink
Merge pull request #89 from lantanagroup/LNK-2159-Shared-refactor
Browse files Browse the repository at this point in the history
LNK-2159: Shared refactor
  • Loading branch information
arianamihailescu authored Apr 8, 2024
2 parents 0071937 + e9b182a commit 97bbdb3
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace LantanaGroup.Link.Shared.Application.Repositories.Implementations;

public class BaseSqlConfigurationRepo<T> : ISqlPersistenceRepository<T> where T : SqlBaseEntity
public class BaseSqlConfigurationRepo<T> : IPersistenceRepository<T> where T : BaseEntity
{
protected readonly ILogger _logger;

Expand All @@ -18,49 +18,61 @@ public BaseSqlConfigurationRepo(ILogger<BaseSqlConfigurationRepo<T>> logger, DbC
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
}

public async Task<List<T>> GetAsync(CancellationToken cancellationToken = default)
{
return await _dbContext.Set<T>().ToListAsync(cancellationToken);
}

public async Task<T> GetAsyncById(string id, CancellationToken cancellationToken = default)
public async Task<T> GetAsync(string id, CancellationToken cancellationToken)
{
return await _dbContext.Set<T>().FirstOrDefaultAsync(o => o.Id == id, cancellationToken);
}

public async virtual Task<bool> CreateAsync(T entity, CancellationToken cancellationToken = default)
public async virtual Task AddAsync(T entity, CancellationToken cancellationToken)
{

_dbContext.Set<T>().Add(entity);

await _dbContext.SaveChangesAsync(cancellationToken);

return true;
}


public async virtual Task<bool> UpdateAsync(T entity, CancellationToken cancellationToken = default)
public async virtual Task<T> UpdateAsync(T entity, CancellationToken cancellationToken)
{
_dbContext.Set<T>().Update(entity);

await _dbContext.SaveChangesAsync(cancellationToken);

return true;
return entity;

}

public async virtual Task<bool> RemoveAsync(string id, CancellationToken cancellationToken = default)
public async virtual Task DeleteAsync(string id, CancellationToken cancellationToken)
{
var entity = await _dbContext.Set<T>().Where(g => g.Id == id).FirstOrDefaultAsync();

if (entity is null) return false;
if (entity is null) return;

_dbContext.Set<T>().Remove(entity);

await _dbContext.SaveChangesAsync(cancellationToken);

return true;
}

public void Add(T entity)
{
throw new NotImplementedException();
}

public T Get(string id)
{
throw new NotImplementedException();
}


}
public T Update(T entity)
{
throw new NotImplementedException();
}

public void Delete(string id)
{
throw new NotImplementedException();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ public override ValueTask<InterceptionResult<int>> SavingChangesAsync(
return base.SavingChangesAsync(eventData, result, cancellationToken);
}

var entries = context.ChangeTracker.Entries<SqlBaseEntity>();
var entries = context.ChangeTracker.Entries<BaseEntityExtended>();

foreach (var entry in entries)
{
switch (entry.State)
{
case EntityState.Added:
entry.Property(p => p.CreatedOn).CurrentValue = DateTime.UtcNow;
entry.Property(p => p.CreateDate).CurrentValue = DateTime.UtcNow;
break;
case EntityState.Modified:
entry.Property(p => p.LastModifiedOn).CurrentValue = DateTime.UtcNow;
entry.Property(p => p.ModifyDate).CurrentValue = DateTime.UtcNow;
break;
}
}
Expand Down

This file was deleted.

2 changes: 0 additions & 2 deletions Shared/Domain/Entities/BaseEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ namespace LantanaGroup.Link.Shared.Domain.Entities;

public class BaseEntity
{
[BsonId]
[BsonIgnoreIfDefault]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string? Id { get; set; }
}
9 changes: 9 additions & 0 deletions Shared/Domain/Entities/BaseEntityExtended.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Text.Json.Serialization;

namespace LantanaGroup.Link.Shared.Domain.Entities;

public class BaseEntityExtended : BaseEntity
{
public DateTime CreateDate { get; set; }
public DateTime? ModifyDate { get; set; }
}
13 changes: 0 additions & 13 deletions Shared/Domain/Entities/SqlBaseEntity.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Shared/Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<Company>Lantana Consulting Group</Company>
<Description>Shared library for Lantana Link</Description>
<Product>NHSN Link Shared Library</Product>
<VersionPrefix>2.2.2</VersionPrefix>
<VersionPrefix>2.2.3</VersionPrefix>
<PackageId>com.lantanagroup.link.Shared</PackageId>
</PropertyGroup>

Expand Down

0 comments on commit 97bbdb3

Please sign in to comment.