mirror of
https://github.com/Shchoholiev/shopping-assistant-api.git
synced 2025-04-04 16:49:36 +00:00
SA-14 expanded base repository
This commit is contained in:
parent
547c23f7c0
commit
5679b2837d
@ -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);
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user