using MongoDB.Driver;
using System;
using System.Collections.Generic;
using MongoDbGenericRepository.ViewModels;
using System.Threading.Tasks;
using MongoDbGenericRepository.Services;
using MongoDB.Bson;
namespace MongoDbGenericRepository
{
public class MongoRepository : IMongoDbRepository
{
private MongoDbContext _mongoDbContext = null;
public MongoRepository(MongoDbContext mongoDbContext = null)
{
_mongoDbContext = mongoDbContext != null ? mongoDbContext : new MongoDbContext();
}
#region Get
///
/// A generic GetOne method
///
///
///
///
public async Task> GetOne(string id) where TEntity : class, new()
{
var filter = Builders.Filter.Eq("Id", id);
return await GetOne(filter);
}
///
/// A generic GetOne method
///
///
///
///
public async Task> GetOne(FilterDefinition filter) where TEntity : class, new()
{
var res = new GetOneResult();
try
{
var collection = GetCollection();
var entity = await collection.Find(filter).SingleOrDefaultAsync();
if (entity != null)
{
res.Entity = entity;
}
res.Success = true;
return res;
}
catch (Exception ex)
{
res.Message = HelperService.NotifyException("GetOne", "Exception getting one " + typeof(TEntity).Name, ex);
return res;
}
}
///
/// A generic get many method
///
///
///
///
public async Task> GetMany(IEnumerable ids) where TEntity : class, new()
{
try
{
var collection = GetCollection();
var filter = Builders.Filter.Eq("Id", ids);
return await GetMany(filter);
}
catch (Exception ex)
{
var res = new GetManyResult();
res.Message = HelperService.NotifyException("GetMany", "Exception getting many " + typeof(TEntity).Name + "s", ex);
return res;
}
}
///
/// A generic get many method with filter
///
///
///
///
public async Task> GetMany(FilterDefinition filter) where TEntity : class, new()
{
var res = new GetManyResult();
try
{
var collection = GetCollection();
var entities = await collection.Find(filter).ToListAsync();
if (entities != null)
{
res.Entities = entities;
}
res.Success = true;
return res;
}
catch (Exception ex)
{
res.Message = HelperService.NotifyException("GetMany", "Exception getting many " + typeof(TEntity).Name + "s", ex);
return res;
}
}
///
/// FindCursor
///
///
///
/// A cursor for the query
public IFindFluent FindCursor(FilterDefinition filter) where TEntity : class, new()
{
var res = new GetManyResult();
var collection = GetCollection();
var cursor = collection.Find(filter);
return cursor;
}
///
/// A generic get all method
///
///
///
public async Task> GetAll() where TEntity : class, new()
{
var res = new GetManyResult();
try
{
var collection = GetCollection();
var entities = await collection.Find(new BsonDocument()).ToListAsync();
if (entities != null)
{
res.Entities = entities;
}
res.Success = true;
return res;
}
catch (Exception ex)
{
res.Message = HelperService.NotifyException("GetAll", "Exception getting all " + typeof(TEntity).Name + "s", ex);
return res;
}
}
///
/// A generic Exists method
///
///
///
///
public async Task Exists(string id) where TEntity : class, new()
{
var collection = GetCollection();
var query = new BsonDocument("Id", id);
var cursor = collection.Find(query);
var count = await cursor.CountAsync();
return (count > 0);
}
///
/// A generic count method
///
///
///
///
public async Task Count(string id) where TEntity : class, new()
{
var filter = new FilterDefinitionBuilder().Eq("Id", id);
return await Count(filter);
}
///
/// A generic count method
///
///
///
///
public async Task Count(FilterDefinition filter) where TEntity : class, new()
{
var collection = GetCollection();
var cursor = collection.Find(filter);
var count = await cursor.CountAsync();
return count;
}
#endregion Get
#region Create
///
/// A generic Add One method
///
///
///
///
public async Task AddOne(TEntity item) where TEntity : class, new()
{
var res = new Result();
try
{
var collection = GetCollection();
await collection.InsertOneAsync(item);
res.Success = true;
res.Message = "OK";
return res;
}
catch (Exception ex)
{
res.Message = HelperService.NotifyException("AddOne", "Exception adding one " + typeof(TEntity).Name, ex);
return res;
}
}
#endregion Create
#region Delete
///
/// A generic delete one method
///
///
///
///
public async Task DeleteOne(string id) where TEntity : class, new()
{
var filter = new FilterDefinitionBuilder().Eq("Id", id);
return await DeleteOne(filter);
}
///
/// A generic delete one method
///
///
///
///
public async Task DeleteOne(FilterDefinition filter) where TEntity : class, new()
{
var result = new Result();
try
{
var collection = GetCollection();
var deleteRes = await collection.DeleteOneAsync(filter);
result.Success = true;
result.Message = "OK";
return result;
}
catch (Exception ex)
{
result.Message = HelperService.NotifyException("DeleteOne", "Exception deleting one " + typeof(TEntity).Name, ex);
return result;
}
}
///
/// A generic delete many method
///
///
///
///
public async Task DeleteMany(IEnumerable ids) where TEntity : class, new()
{
var filter = new FilterDefinitionBuilder().In("Id", ids);
return await DeleteMany(filter);
}
///
/// A generic delete many method
///
///
///
///
public async Task DeleteMany(FilterDefinition filter) where TEntity : class, new()
{
var result = new Result();
try
{
var collection = GetCollection();
var deleteRes = await collection.DeleteManyAsync(filter);
if (deleteRes.DeletedCount < 1)
{
var ex = new Exception();
result.Message = HelperService.NotifyException("DeleteMany", "Some " + typeof(TEntity).Name + "s could not be deleted.", ex);
return result;
}
result.Success = true;
result.Message = "OK";
return result;
}
catch (Exception ex)
{
result.Message = HelperService.NotifyException("DeleteMany", "Some " + typeof(TEntity).Name + "s could not be deleted.", ex);
return result;
}
}
#endregion Delete
#region Update
///
/// UpdateOne by id
///
///
///
///
///
public async Task UpdateOne(string id, UpdateDefinition update) where TEntity : class, new()
{
var filter = new FilterDefinitionBuilder().Eq("Id", id);
return await UpdateOne(filter, update);
}
///
/// UpdateOne with filter
///
///
///
///
///
public async Task UpdateOne(FilterDefinition filter, UpdateDefinition update) where TEntity : class, new()
{
var result = new Result();
try
{
var collection = GetCollection();
var updateRes = await collection.UpdateOneAsync(filter, update);
if (updateRes.ModifiedCount < 1)
{
var ex = new Exception();
result.Message = HelperService.NotifyException("UpdateOne", "ERROR: updateRes.ModifiedCount < 1 for entity: " + typeof(TEntity).Name, ex);
return result;
}
result.Success = true;
result.Message = "OK";
return result;
}
catch (Exception ex)
{
result.Message = HelperService.NotifyException("UpdateOne", "Exception updating entity: " + typeof(TEntity).Name, ex);
return result;
}
}
///
/// UpdateMany with Ids
///
///
///
///
///
public async Task UpdateMany(IEnumerable ids, UpdateDefinition update) where TEntity : class, new()
{
var filter = new FilterDefinitionBuilder().In("Id", ids);
return await UpdateOne(filter, update);
}
///
/// UpdateMany with filter
///
///
///
///
///
public async Task UpdateMany(FilterDefinition filter, UpdateDefinition update) where TEntity : class, new()
{
var result = new Result();
try
{
var collection = GetCollection();
var updateRes = await collection.UpdateManyAsync(filter, update);
if (updateRes.ModifiedCount < 1)
{
var ex = new Exception();
result.Message = HelperService.NotifyException("UpdateMany", "ERROR: updateRes.ModifiedCount < 1 for entities: " + typeof(TEntity).Name + "s", ex);
return result;
}
result.Success = true;
result.Message = "OK";
return result;
}
catch (Exception ex)
{
result.Message = HelperService.NotifyException("UpdateMany", "Exception updating entities: " + typeof(TEntity).Name + "s", ex);
return result;
}
}
#endregion Update
#region Find And Update
///
/// GetAndUpdateOne with filter
///
///
///
///
///
///
public async Task> GetAndUpdateOne(FilterDefinition filter, UpdateDefinition update, FindOneAndUpdateOptions options) where TEntity : class, new()
{
var result = new GetOneResult();
try
{
var collection = GetCollection();
result.Entity = await collection.FindOneAndUpdateAsync(filter, update, options);
result.Success = true;
result.Message = "OK";
return result;
}
catch (Exception ex)
{
result.Message = HelperService.NotifyException("GetAndUpdateOne", "Exception getting and updating entity: " + typeof(TEntity).Name, ex);
return result;
}
}
#endregion Find And Update
///
/// The private GetCollection method
///
///
///
private IMongoCollection GetCollection()
{
return _mongoDbContext.GetCollection();
}
}
}