Friday, March 24, 2023

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();
    }
}


 

 

No comments:

Post a Comment

Please keep your comments relevant.
Comments with external links and adult words will be filtered.