Friday, March 24, 2023

How to write async C# crud methods for Employee model from Apache Cassandra


 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Cassandra;
using Cassandra.Mapping;

public class EmployeeRepository
{
    private readonly ISession _session;
    private readonly IMapper _mapper;

    public EmployeeRepository(string connectionString, string keyspace)
    {
        var cluster = Cluster.Builder()
            .AddContactPoint(connectionString)
            .Build();

        _session = cluster.Connect(keyspace);

        var mappingConfig = new MappingConfiguration()
            .Define<EmployeeMapping>();

        _mapper = new Mapper(_session, mappingConfig);
    }

    public async Task<string> CreateEmployeeAsync(Employee employee)
    {
        employee.Id = Guid.NewGuid().ToString();
        await _mapper.InsertAsync(employee);
        return employee.Id;
    }

    public async Task<IEnumerable<Employee>> GetEmployeesAsync()
    {
        var query = new SimpleStatement("SELECT * FROM employees");
        var result = await _mapper.FetchAsync<Employee>(query);
        return result.ToList();
    }

    public async Task<Employee> GetEmployeeByIdAsync(string id)
    {
        var query = new SimpleStatement("SELECT * FROM employees WHERE id = ?", id);
        var result = await _mapper.FetchAsync<Employee>(query);
        return result.FirstOrDefault();
    }

    public async Task<bool> UpdateEmployeeAsync(Employee employee)
    {
        var query = new SimpleStatement("UPDATE employees SET name = ?, email = ? WHERE id = ?",
            employee.Name, employee.Email, employee.Id);

        var result = await _session.ExecuteAsync(query);
        return result.WasApplied;
    }

    public async Task<bool> DeleteEmployeeAsync(string id)
    {
        var query = new SimpleStatement("DELETE FROM employees WHERE id = ?", id);
        var result = await _session.ExecuteAsync(query);
        return result.WasApplied;
    }
}


 

How to write async C# crud methods for Employee model from ArangoDB

Using Linq:

 

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using ArangoDB.Client;
using ArangoDB.Client.Linq;
using ArangoDB.Client.Query;

public class EmployeeRepository
{
    private readonly string _databaseName;
    private readonly ArangoDatabase _database;

    public EmployeeRepository(string url, string databaseName, string username, string password)
    {
        var connection = new ConnectionSettings(url, databaseName)
        {
            Database = databaseName,
            Credential = new NetworkCredential(username, password)
        };

        var database = new ArangoDatabase(connection);
        _databaseName = databaseName;
        _database = database;
    }

    public async Task<string> CreateEmployeeAsync(Employee employee)
    {
        var collection = _database.Collection<Employee>();
        await collection.InsertAsync(employee);
        return employee.Id;
    }

    public async Task<IEnumerable<Employee>> GetEmployeesAsync()
    {
        var collection = _database.Collection<Employee>();
        return await collection.ToListAsync();
    }

    public async Task<Employee> GetEmployeeByIdAsync(string id)
    {
        var collection = _database.Collection<Employee>();
        var query = from employee in collection.Linq()
                    where employee.Id == id
                    select employee;

        return await query.FirstOrDefaultAsync();
    }

    public async Task<bool> UpdateEmployeeAsync(Employee employee)
    {
        var collection = _database.Collection<Employee>();
        var result = await collection.UpdateAsync(employee.Id, employee);

        return result.Updated == 1;
    }

    public async Task<bool> DeleteEmployeeAsync(string id)
    {
        var collection = _database.Collection<Employee>();
        var result = await collection.RemoveByIdAsync<Employee>(id);

        return result.Removed == 1;
    }
}


 

Using AQL:

 

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using ArangoDB.Client;
using ArangoDB.Client.Query;

public class EmployeeRepository
{
    private readonly string _databaseName;
    private readonly ArangoDatabase _database;

    public EmployeeRepository(string url, string databaseName, string username, string password)
    {
        var connection = new ConnectionSettings(url, databaseName)
        {
            Database = databaseName,
            Credential = new NetworkCredential(username, password)
        };

        var database = new ArangoDatabase(connection);
        _databaseName = databaseName;
        _database = database;
    }

    public async Task<string> CreateEmployeeAsync(Employee employee)
    {
        var query = new AqlQuery()
        {
            Query = $"INSERT {employee.ToJson()} INTO employees RETURN NEW._key",
        };

        var result = await _database.QueryAsync<string>(query);
        return result.FirstOrDefault();
    }

    public async Task<IEnumerable<Employee>> GetEmployeesAsync()
    {
        var query = new AqlQuery()
        {
            Query = "FOR employee IN employees RETURN employee",
        };

        var result = await _database.QueryAsync<Employee>(query);
        return result.ToList();
    }

    public async Task<Employee> GetEmployeeByIdAsync(string id)
    {
        var query = new AqlQuery()
        {
            Query = $"FOR employee IN employees FILTER employee._key == '{id}' RETURN employee",
        };

        var result = await _database.QueryAsync<Employee>(query);
        return result.FirstOrDefault();
    }

    public async Task<bool> UpdateEmployeeAsync(Employee employee)
    {
        var query = new AqlQuery()
        {
            Query = $"UPDATE '{employee.Id}' WITH {employee.ToJson()} IN employees RETURN NEW._id",
        };

        var result = await _database.QueryAsync<string>(query);
        return result.Any();
    }

    public async Task<bool> DeleteEmployeeAsync(string id)
    {
        var query = new AqlQuery()
        {
            Query = $"REMOVE '{id}' IN employees RETURN OLD._id",
        };

        var result = await _database.QueryAsync<string>(query);
        return result.Any();
    }
}


 

 

How to write async C# crud methods for Employee model from MongoDB

 

using System.Collections.Generic;
using System.Threading.Tasks;
using MongoDB.Driver;

public class EmployeeRepository
{
    private readonly IMongoCollection<Employee> _employeeCollection;

    public EmployeeRepository(string connectionString, string databaseName)
    {
        var client = new MongoClient(connectionString);
        var database = client.GetDatabase(databaseName);

        _employeeCollection = database.GetCollection<Employee>("employees");
    }

    public async Task<string> CreateEmployeeAsync(Employee employee)
    {
        await _employeeCollection.InsertOneAsync(employee);
        return employee.Id;
    }

    public async Task<IEnumerable<Employee>> GetEmployeesAsync()
    {
        var employees = await _employeeCollection.FindAsync(_ => true);
        return await employees.ToListAsync();
    }

    public async Task<Employee> GetEmployeeByIdAsync(string id)
    {
        var filter = Builders<Employee>.Filter.Eq(e => e.Id, id);
        var employees = await _employeeCollection.FindAsync(filter);
        return await employees.FirstOrDefaultAsync();
    }

    public async Task<bool> UpdateEmployeeAsync(Employee employee)
    {
        var filter = Builders<Employee>.Filter.Eq(e => e.Id, employee.Id);
        var result = await _employeeCollection.ReplaceOneAsync(filter, employee);
        return result.ModifiedCount > 0;
    }

    public async Task<bool> DeleteEmployeeAsync(string id)
    {
        var filter = Builders<Employee>.Filter.Eq(e => e.Id, id);
        var result = await _employeeCollection.DeleteOneAsync(filter);
        return result.DeletedCount > 0;
    }
}