Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LNK-2159: Shared refactor #89

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
arianamihailescu marked this conversation as resolved.
Show resolved Hide resolved
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
Loading