Commit f4b720d2 authored by Almouhannad's avatar Almouhannad

(B) Add create with entity result to repos

parent 9c1bae96
using Domain.Primitives;
using Domain.Shared;
namespace Domain.Repositories.Base
{
......@@ -13,6 +14,8 @@ namespace Domain.Repositories.Base
public void Create(TEntity entity);
public Task<Result<TEntity>> CreateWithEntityResultAsync (TEntity entity);
#endregion
#region Read operations
......
using Domain.Primitives;
using Domain.Errors;
using Domain.Primitives;
using Domain.Repositories.Base;
using Domain.Shared;
using Microsoft.EntityFrameworkCore;
using Persistence.Context;
using Persistence.Repositories.Specifications.Base;
......@@ -37,6 +39,20 @@ public class Repositroy<TEntity> : IRepository<TEntity> where 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
{
await _context.SaveChangesAsync();
}
catch (Exception)
{
return Result.Failure<TEntity>(PersistenceErrors.UnableToCompleteTransaction);
}
return Result.Success<TEntity>(entry.Entity);
}
#endregion
#region Read operations
......
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