/* This file is part of the Notesnook Sync Server project (https://notesnook.com/) Copyright (C) 2023 Streetwriters (Private) Limited This program is free software: you can redistribute it and/or modify it under the terms of the Affero GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Affero GNU General Public License for more details. You should have received a copy of the Affero GNU General Public License along with this program. If not, see . */ using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; using MongoDB.Bson; using MongoDB.Driver; using Streetwriters.Data.Interfaces; namespace Streetwriters.Data.Repositories { public class Repository where TEntity : class { protected readonly IDbContext dbContext; public IMongoCollection Collection { get; set; } public Repository(IDbContext _dbContext, IMongoCollection collection) { dbContext = _dbContext; Collection = collection; } public virtual void Insert(TEntity obj) { dbContext.AddCommand((handle, ct) => Collection.InsertOneAsync(handle, obj, null, ct)); } public virtual Task InsertAsync(TEntity obj) { return Collection.InsertOneAsync(obj); } public virtual void Upsert(TEntity obj, Expression> filterExpression) { dbContext.AddCommand((handle, ct) => Collection.ReplaceOneAsync(handle, filterExpression, obj, new ReplaceOptions { IsUpsert = true }, ct)); } public virtual Task UpsertAsync(TEntity obj, Expression> filterExpression) { return Collection.ReplaceOneAsync(filterExpression, obj, new ReplaceOptions { IsUpsert = true }); } public virtual async Task FindOneAsync(Expression> filterExpression) { var data = await Collection.FindAsync(filterExpression); return data.FirstOrDefault(); } public virtual async Task GetAsync(ObjectId id) { var data = await Collection.FindAsync(Builders.Filter.Eq("_id", id)); return data.FirstOrDefault(); } public virtual async Task> FindAsync(Expression> filterExpression) { var data = await Collection.FindAsync(filterExpression); return data.ToList(); } public virtual async Task> GetAllAsync() { var all = await Collection.FindAsync(Builders.Filter.Empty); return all.ToList(); } public virtual async Task CountAsync(Expression> filterExpression) { return await Collection.CountDocumentsAsync(filterExpression); } public virtual void Update(ObjectId id, TEntity obj) { dbContext.AddCommand((handle, ct) => Collection.ReplaceOneAsync(handle, Builders.Filter.Eq("_id", id), obj, cancellationToken: ct)); } public virtual Task UpdateAsync(ObjectId id, TEntity obj) { return Collection.ReplaceOneAsync(Builders.Filter.Eq("_id", id), obj); } public virtual void DeleteById(ObjectId id) { dbContext.AddCommand((handle, ct) => Collection.DeleteOneAsync(handle, Builders.Filter.Eq("_id", id), cancellationToken: ct)); } public virtual Task DeleteByIdAsync(ObjectId id) { return Collection.DeleteOneAsync(Builders.Filter.Eq("_id", id)); } public virtual void Delete(Expression> filterExpression) { dbContext.AddCommand((handle, ct) => Collection.DeleteOneAsync(handle, filterExpression, cancellationToken: ct)); } public virtual void DeleteMany(Expression> filterExpression) { dbContext.AddCommand((handle, ct) => Collection.DeleteManyAsync(handle, filterExpression, cancellationToken: ct)); } public virtual Task DeleteAsync(Expression> filterExpression) { return Collection.DeleteOneAsync(filterExpression); } public virtual Task DeleteManyAsync(Expression> filterExpression) { return Collection.DeleteManyAsync(filterExpression); } } }