Add shared packages

This commit is contained in:
2025-06-02 08:58:34 -03:00
parent 6ebfde013a
commit 5b2588b47f
32 changed files with 981 additions and 7 deletions

View File

@ -0,0 +1,55 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
using Microsoft.Extensions.Configuration;
using System;
namespace Tribufu.Database
{
public class DatabaseConfiguration
{
public DatabaseDriver Driver { get; set; }
public string? Version { get; set; }
public string? Host { get; set; }
public string? Port { get; set; }
public string? User { get; set; }
public string? Password { get; set; }
public string? Schema { get; set; }
/// <summary>
/// Loads the <see cref="DatabaseConfiguration"/> from the "database" section or from root-level keys prefixed with "database_".
/// </summary>
/// <param name="configuration">The configuration source.</param>
/// <returns>The populated <see cref="DatabaseConfiguration"/> instance.</returns>
public static DatabaseConfiguration Load(IConfiguration configuration)
{
var section = configuration.GetSection("database");
var useRootFallback = !section.Exists();
string? GetConfig(string key) => useRootFallback ? configuration[$"database_{key}"] : section[key];
var driverString = GetConfig("driver") ?? throw new Exception("Missing database driver");
if (!Enum.TryParse<DatabaseDriver>(driverString, true, out var driver))
{
throw new Exception($"Unsupported database driver: {driverString}");
}
return new DatabaseConfiguration
{
Driver = driver,
Version = GetConfig("version"),
Host = GetConfig("host"),
Port = GetConfig("port"),
User = GetConfig("user"),
Password = GetConfig("password"),
Schema = GetConfig("schema")
};
}
}
}

View File

@ -0,0 +1,12 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
namespace Tribufu.Database
{
public static class DatabaseConstants
{
public const uint DEFAULT_PAGINATION = 20;
public const uint MAX_PAGINATION = 100;
}
}

View File

@ -0,0 +1,22 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
namespace Tribufu.Database
{
public enum DatabaseDriver : byte
{
MySql = 0,
Postgres = 1,
SqlServer = 2,
Oracle = 3,
Firebird = 4,
Sqlite = 5,
MongoDb = 6,
}
}

View File

@ -0,0 +1 @@
# Tribufu

View File

@ -0,0 +1,39 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Tribufu.Database.Repositories
{
public interface IRepository<T, K> where T : class
{
IList<T> GetAll();
Task<IList<T>> GetAllAsync();
IList<T> GetPage(uint page, uint limit);
Task<IList<T>> GetPageAsync(uint page, uint limit);
T? GetOne(K key);
Task<T?> GetOneAsync(K key);
T? Create(T entity);
Task<T?> CreateAsync(T entity);
T? Update(T entity);
Task<T?> UpdateAsync(T entity);
void Delete(K key);
Task DeleteAsync(K key);
void Delete(T entity);
Task DeleteAsync(T entity);
}
}

View File

@ -0,0 +1,113 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Tribufu.Database.Repositories
{
public class Repository<C, T, K> : IRepository<T, K> where C : DbContext where T : class
{
protected readonly C _context;
protected readonly DbSet<T> _dbSet;
public Repository(C context)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
_dbSet = context.Set<T>();
}
public virtual IList<T> GetAll()
{
return [.. _dbSet];
}
public virtual async Task<IList<T>> GetAllAsync()
{
return await _dbSet.ToListAsync();
}
public virtual IList<T> GetPage(uint page, uint limit)
{
return _dbSet.Skip((int)((page < 1 ? 0 : page - 1) * limit)).Take((int)limit).ToList();
}
public virtual async Task<IList<T>> GetPageAsync(uint page, uint limit)
{
return await _dbSet.Skip((int)((page < 1 ? 0 : page - 1) * limit)).Take((int)limit).ToListAsync();
}
public virtual T? GetOne(K key)
{
return _dbSet.Find(key);
}
public virtual async Task<T?> GetOneAsync(K key)
{
return await _dbSet.FindAsync(key);
}
public virtual T? Create(T entity)
{
_dbSet.Add(entity);
var result = _context.SaveChanges();
return result > 0 ? entity : null;
}
public virtual async Task<T?> CreateAsync(T entity)
{
await _dbSet.AddAsync(entity);
var result = await _context.SaveChangesAsync();
return result > 0 ? entity : null;
}
public virtual T? Update(T entity)
{
_dbSet.Update(entity);
var result = _context.SaveChanges();
return result > 0 ? entity : null;
}
public virtual async Task<T?> UpdateAsync(T entity)
{
_dbSet.Update(entity);
var result = await _context.SaveChangesAsync();
return result > 0 ? entity : null;
}
public virtual void Delete(K key)
{
var entity = _dbSet.Find(key);
if (entity != null)
{
Delete(entity);
}
}
public virtual async Task DeleteAsync(K key)
{
var entity = await _dbSet.FindAsync(key);
if (entity != null)
{
await DeleteAsync(entity);
}
}
public virtual void Delete(T entity)
{
_dbSet.Remove(entity);
_context.SaveChanges();
}
public virtual async Task DeleteAsync(T entity)
{
_dbSet.Remove(entity);
await _context.SaveChangesAsync();
}
}
}

View File

@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<PackageId>Tribufu.Database</PackageId>
<Description>Tribufu Database Extensions</Description>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>
<PropertyGroup>
<AppDesignerFolder>Properties</AppDesignerFolder>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Nullable>enable</Nullable>
<OutputType>Library</OutputType>
<TargetFrameworks>net8.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<None Include="README.md" Pack="true" PackagePath="\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Tribufu.Configuration\Tribufu.Configuration.csproj" />
<ProjectReference Include="..\Tribufu.Logging\Tribufu.Logging.csproj" />
<ProjectReference Include="..\Tribufu.Runtime\Tribufu.Runtime.csproj" />
</ItemGroup>
</Project>