using System.Linq.Expressions; using MongoDB.Bson; using MongoDB.Driver; using ShoppingAssistantApi.Application.IRepositories; using ShoppingAssistantApi.Domain.Entities; using ShoppingAssistantApi.Persistance.Database; namespace ShoppingAssistantApi.Persistance.Repositories; public class WishlistsRepository : BaseRepository, IWishlistsRepository { public WishlistsRepository(MongoDbContext db) : base(db, "Wishlists") { } public async Task GetWishlistAsync(Expression> predicate, CancellationToken cancellationToken) { return await (await _collection.FindAsync(Builders.Filter.Where(predicate) & Builders.Filter.Where(x => !x.IsDeleted))).FirstOrDefaultAsync(cancellationToken); } public async Task UpdateWishlistNameAsync(ObjectId wishlistId, string newName, ObjectId updatedById, CancellationToken cancellationToken) { var filterDefinition = Builders.Filter.Eq(w => w.Id, wishlistId) & Builders.Filter.Where(x => !x.IsDeleted); var updateDefinition = Builders.Update .Set(w => w.Name, newName) .Set(w => w.LastModifiedDateUtc, DateTime.UtcNow) .Set(w => w.LastModifiedById, updatedById); var options = new FindOneAndUpdateOptions { ReturnDocument = ReturnDocument.After }; return await _collection.FindOneAndUpdateAsync(filterDefinition, updateDefinition, options, cancellationToken); } }