Adithyavj_SuggestionApp/SuggestionAppLibrary/DataAccess/MongoUserData.cs
namespace SuggestionAppLibrary.DataAccess;

public class MongoUserData : IUserData
{
    private readonly IMongoCollection<UserModel> _users;

    public MongoUserData(IDbConnection db)
    {
        _users = db.UserCollection; // creating a copy of UserCollection in the _users. Here we are only referencing it, so no extra memory usage.
    }

    // Get all users
    public async Task<List<UserModel>> GetUsersAsync()
    {
        var results = await _users.FindAsync(_ => true); // returns all records (find all)
        return results.ToList();
    }

    // Get user by id
    public async Task<UserModel> GetUser(string id)
    {
        var results = await _users.FindAsync(u => u.Id == id);
        return results.FirstOrDefault();
    }

    // Get user by Azure B2C identifier
    public async Task<UserModel> GetUserFromAuthentication(string objectId)
    {
        var results = await _users.FindAsync(u => u.ObjectIdentifier == objectId);
        return results.FirstOrDefault();
    }

    // Create a user
    public Task CreateUser(UserModel user)
    {
        return _users.InsertOneAsync(user);
    }

    // Update a user
    public Task UpdateUser(UserModel user)
    {
        var filter = Builders<UserModel>.Filter.Eq("Id", user.Id);
        return _users.ReplaceOneAsync(filter, user, new ReplaceOptions { IsUpsert = true });
    }

}
