Commit 9ed541c5 authored by Almouhannad's avatar Almouhannad

(B) Update repos

parent 825ca353
...@@ -7,6 +7,15 @@ public static class PersistenceErrors ...@@ -7,6 +7,15 @@ public static class PersistenceErrors
public static Error UnableToCompleteTransaction => public static Error UnableToCompleteTransaction =>
new("Persistence.UnableToCompleteTransaction", "حدثت مشكلة عند الاتصال مع قاعدة البيانات"); new("Persistence.UnableToCompleteTransaction", "حدثت مشكلة عند الاتصال مع قاعدة البيانات");
public static Error UnableToCreate =>
new("Persistence.UnableToCreate", "فشلت عملية الإضافة");
public static Error UnableToUpdate =>
new("Persistence.UnableToUpdate", "فشلت عملية التعديل");
public static Error UnableToDelete =>
new("Persistence.UnableToDelete", "فشلت عملية الحذف");
public static Error NotFound => public static Error NotFound =>
new("Persistence.NotFound", "الغرض المطلوب غير موجود"); new("Persistence.NotFound", "الغرض المطلوب غير موجود");
} }
...@@ -3,8 +3,6 @@ using Domain.Shared; ...@@ -3,8 +3,6 @@ using Domain.Shared;
namespace Domain.Repositories.Base namespace Domain.Repositories.Base
{ {
// Note that queries are async, but commands are NOT
// Since the persist operation is done by UnitOfWork
public interface IRepository<TEntity> public interface IRepository<TEntity>
where TEntity : Entity where TEntity : Entity
{ {
...@@ -12,29 +10,27 @@ namespace Domain.Repositories.Base ...@@ -12,29 +10,27 @@ namespace Domain.Repositories.Base
#region Create operation #region Create operation
public void Create(TEntity entity); public Task<Result<TEntity>> CreateAsync(TEntity entity);
public Task<Result<TEntity>> CreateWithEntityResultAsync (TEntity entity);
#endregion #endregion
#region Read operations #region Read operations
public Task<TEntity?> GetByIdAsync(int id); public Task<Result<TEntity>> GetByIdAsync(int id);
public Task<ICollection<TEntity>> GetAllAsync(); public Task<Result<ICollection<TEntity>>> GetAllAsync();
#endregion #endregion
#region Update oprtation #region Update oprtation
public void Update(TEntity entity); public Task<Result> UpdateAsync(TEntity entity);
#endregion #endregion
#region Delete operation #region Delete operation
public void Delete(TEntity entity); public Task<Result> DeleteAsync(TEntity entity);
#endregion #endregion
} }
......
...@@ -24,7 +24,7 @@ public class Repositroy<TEntity> : IRepository<TEntity> where TEntity : Entity ...@@ -24,7 +24,7 @@ public class Repositroy<TEntity> : IRepository<TEntity> where TEntity : Entity
#endregion #endregion
#region Apply specification #region Apply specification
protected IQueryable<TEntity> ApplySpecification (Specification<TEntity> specification) protected IQueryable<TEntity> ApplySpecification(Specification<TEntity> specification)
{ {
return SpecificationEvaluator.GetQuery(_context.Set<TEntity>(), specification); return SpecificationEvaluator.GetQuery(_context.Set<TEntity>(), specification);
} }
...@@ -34,57 +34,84 @@ public class Repositroy<TEntity> : IRepository<TEntity> where TEntity : Entity ...@@ -34,57 +34,84 @@ public class Repositroy<TEntity> : IRepository<TEntity> where TEntity : Entity
#region Create operation #region Create operation
public virtual void Create(TEntity entity) public async Task<Result<TEntity>> CreateAsync(TEntity entity)
{ {
_context.Set<TEntity>().Add(entity);
}
public virtual async Task<Result<TEntity>> CreateWithEntityResultAsync (TEntity entity)
{
var entry = await _context.Set<TEntity>().AddAsync(entity);
try try
{ {
var createdEntity = await _context.Set<TEntity>().AddAsync(entity);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
return Result.Success<TEntity>(createdEntity.Entity);
} }
catch (Exception) catch (Exception)
{ {
return Result.Failure<TEntity>(PersistenceErrors.UnableToCompleteTransaction); return Result.Failure<TEntity>(PersistenceErrors.UnableToCreate);
} }
return Result.Success<TEntity>(entry.Entity);
} }
#endregion #endregion
#region Read operations #region Read operations
public virtual async Task<TEntity?> GetByIdAsync(int id) public async Task<Result<TEntity>> GetByIdAsync(int id)
{
try
{ {
return await _context.Set<TEntity>().FindAsync(id); var entity = await _context.Set<TEntity>().FirstAsync(e => e.Id == id);
return Result.Success<TEntity>(entity);
}
catch (Exception)
{
return Result.Failure<TEntity>(PersistenceErrors.NotFound);
}
} }
public virtual async Task<ICollection<TEntity>> GetAllAsync() public async Task<Result<ICollection<TEntity>>> GetAllAsync()
{
try
{ {
return await _context.Set<TEntity>().ToListAsync(); var entites = await _context.Set<TEntity>().ToListAsync();
return Result.Success<ICollection<TEntity>>(entites);
}
catch (Exception)
{
return Result.Failure<ICollection<TEntity>>(PersistenceErrors.NotFound);
}
} }
#endregion #endregion
#region Update operation #region Update operation
public async Task<Result> UpdateAsync(TEntity entity)
public virtual void Update(TEntity entity) {
try
{ {
_context.Set<TEntity>().Update(entity); _context.Set<TEntity>().Update(entity);
await _context.SaveChangesAsync();
return Result.Success();
}
catch (Exception)
{
return Result.Failure(PersistenceErrors.UnableToUpdate);
}
} }
#endregion #endregion
#region Delete operation #region Delete operation
public async Task<Result> DeleteAsync(TEntity entity)
public virtual void Delete(TEntity entity) {
try
{ {
_context.Set<TEntity>().Remove(entity); _context.Set<TEntity>().Remove(entity);
await _context.SaveChangesAsync();
return Result.Success();
}
catch (Exception)
{
return Result.Failure(PersistenceErrors.UnableToDelete);
}
} }
#endregion #endregion
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment