SA-14 expanded base repository

This commit is contained in:
shchoholiev-opflo 2023-10-14 19:53:47 +00:00
parent 547c23f7c0
commit 5679b2837d
2 changed files with 20 additions and 3 deletions

View File

@ -1,4 +1,5 @@
using ShoppingAssistantApi.Domain.Common;
using MongoDB.Bson;
using ShoppingAssistantApi.Domain.Common;
using System.Linq.Expressions;
namespace ShoppingAssistantApi.Application.IRepositories;
@ -7,6 +8,10 @@ public interface IBaseRepository<TEntity> where TEntity : EntityBase
{
Task<TEntity> AddAsync(TEntity entity, CancellationToken cancellationToken);
Task<TEntity> GetOneAsync(ObjectId id, CancellationToken cancellationToken);
Task<TEntity> GetOneAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken);
Task<List<TEntity>> GetPageAsync(int pageNumber, int pageSize, CancellationToken cancellationToken);
Task<List<TEntity>> GetPageAsync(int pageNumber, int pageSize, Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken);

View File

@ -1,11 +1,13 @@
using MongoDB.Driver;
using MongoDB.Bson;
using MongoDB.Driver;
using ShoppingAssistantApi.Application.IRepositories;
using ShoppingAssistantApi.Domain.Common;
using ShoppingAssistantApi.Persistance.Database;
using System.Linq.Expressions;
namespace ShoppingAssistantApi.Persistance.Repositories;
public abstract class BaseRepository<TEntity> where TEntity : EntityBase
public abstract class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : EntityBase
{
protected MongoDbContext _db;
@ -17,6 +19,16 @@ public abstract class BaseRepository<TEntity> where TEntity : EntityBase
this._collection = _db.Db.GetCollection<TEntity>(collectionName);
}
public async Task<TEntity> GetOneAsync(ObjectId id, CancellationToken cancellationToken)
{
return await this._collection.Find(x => x.Id == id).FirstOrDefaultAsync(cancellationToken);
}
public async Task<TEntity> GetOneAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken)
{
return await this._collection.Find(predicate).FirstOrDefaultAsync(cancellationToken);
}
public async Task<TEntity> AddAsync(TEntity entity, CancellationToken cancellationToken)
{
await this._collection.InsertOneAsync(entity, new InsertOneOptions(), cancellationToken);