feat : RDB 기능 추가 (라이브러리, base 코드 구현)
This commit is contained in:
31
MMOTestServer/MMOserver/RDB/DbConnectionFactory.cs
Normal file
31
MMOTestServer/MMOserver/RDB/DbConnectionFactory.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System.Data;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MySql.Data.MySqlClient;
|
||||
using ServerLib.RDB.Database;
|
||||
|
||||
namespace MMOserver.RDB;
|
||||
|
||||
public class DbConnectionFactory : IDbConnectionFactory
|
||||
{
|
||||
private readonly string connectionString;
|
||||
|
||||
public DbConnectionFactory(IConfiguration config)
|
||||
{
|
||||
IConfigurationSection db = config.GetSection("Database");
|
||||
connectionString = $"Server={db["Host"]};" +
|
||||
$"Port={db["Port"]};" +
|
||||
$"Database={db["Name"]};" +
|
||||
$"User={db["User"]};" +
|
||||
$"Password={db["Password"]};" +
|
||||
$"Pooling=true;" +
|
||||
$"MinimumPoolSize={db["Pooling:MinimumPoolSize"]};" +
|
||||
$"MaximumPoolSize={db["Pooling:MaximumPoolSize"]};" +
|
||||
$"ConnectionTimeout={db["Pooling:ConnectionTimeout"]};" +
|
||||
$"ConnectionIdleTimeout={db["Pooling:ConnectionIdleTimeout"]};";
|
||||
}
|
||||
|
||||
public IDbConnection CreateConnection()
|
||||
{
|
||||
return new MySqlConnection(connectionString);
|
||||
}
|
||||
}
|
||||
48
MMOTestServer/MMOserver/RDB/Handlers/TestHandlers.cs
Normal file
48
MMOTestServer/MMOserver/RDB/Handlers/TestHandlers.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using MMOserver.RDB.Models;
|
||||
using MMOserver.RDB.Services;
|
||||
using ServerLib.RDB.Handlers;
|
||||
|
||||
namespace MMOserver.RDB.Handlers;
|
||||
|
||||
public class TestHandler
|
||||
{
|
||||
private readonly TestService _testService;
|
||||
|
||||
public TestHandler(TestService testService) => _testService = testService;
|
||||
|
||||
public async Task<string> GetTestAsync(int id)
|
||||
{
|
||||
Test? result = await _testService.GetTestAsync(id);
|
||||
return result is null
|
||||
? HandlerHelper.Error("Test not found")
|
||||
: HandlerHelper.Success(result);
|
||||
}
|
||||
|
||||
public async Task<string> GetTestByUuidAsync(string uuid)
|
||||
{
|
||||
Test? result = await _testService.GetTestByUuidAsync(uuid);
|
||||
return result is null
|
||||
? HandlerHelper.Error("Test not found")
|
||||
: HandlerHelper.Success(result);
|
||||
}
|
||||
|
||||
public async Task<string> GetAllTestsAsync()
|
||||
{
|
||||
return HandlerHelper.Success(await _testService.GetAllTestsAsync());
|
||||
}
|
||||
|
||||
public async Task<string> CreateTestAsync(int testA, string testB, double testC)
|
||||
{
|
||||
return HandlerHelper.Success(await _testService.CreateTestAsync(testA, testB, testC));
|
||||
}
|
||||
|
||||
public async Task<string> UpdateTestAsync(int id, int? testA, string? testB, double? testC)
|
||||
{
|
||||
return HandlerHelper.Success(await _testService.UpdateTestAsync(id, testA, testB, testC));
|
||||
}
|
||||
|
||||
public async Task<string> DeleteTestAsync(int id)
|
||||
{
|
||||
return HandlerHelper.Success(await _testService.DeleteTestAsync(id));
|
||||
}
|
||||
}
|
||||
16
MMOTestServer/MMOserver/RDB/Models/TestM.cs
Normal file
16
MMOTestServer/MMOserver/RDB/Models/TestM.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using Dapper.Contrib.Extensions;
|
||||
|
||||
namespace MMOserver.RDB.Models;
|
||||
|
||||
// 테이블 entity
|
||||
[Table("test")]
|
||||
public class Test
|
||||
{
|
||||
[Key]
|
||||
public int Id { get; set; }
|
||||
public int TestA { get; set; }
|
||||
public string? TestB { get; set; }
|
||||
public double TestC { get; set; }
|
||||
public string? Uuid { get; set; } // UNIQUE
|
||||
}
|
||||
|
||||
18
MMOTestServer/MMOserver/RDB/Repositories/TestRepository.cs
Normal file
18
MMOTestServer/MMOserver/RDB/Repositories/TestRepository.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using MMOserver.RDB.Models;
|
||||
using ServerLib.RDB.Database;
|
||||
using ServerLib.RDB.Repositories;
|
||||
|
||||
namespace MMOserver.RDB.Repositories;
|
||||
|
||||
// 실제 호출할 쿼리들
|
||||
public class TestRepository : ARepository<Test>
|
||||
{
|
||||
public TestRepository(DbConnectionFactory factory) : base(factory)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<Test?> GetByUuidAsync(string uuid)
|
||||
{
|
||||
return await QueryFirstOrDefaultAsync("SELECT * FROM test WHERE uuid = @Uuid", new { Uuid = uuid });
|
||||
}
|
||||
}
|
||||
57
MMOTestServer/MMOserver/RDB/Services/TestService.cs
Normal file
57
MMOTestServer/MMOserver/RDB/Services/TestService.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using MMOserver.RDB.Models;
|
||||
using MMOserver.RDB.Repositories;
|
||||
|
||||
namespace MMOserver.RDB.Services;
|
||||
|
||||
public class TestService
|
||||
{
|
||||
private readonly TestRepository repo;
|
||||
|
||||
public TestService(TestRepository repo)
|
||||
{
|
||||
this.repo = repo;
|
||||
}
|
||||
|
||||
public Task<Test?> GetTestAsync(int id)
|
||||
{
|
||||
return repo.GetByIdAsync(id);
|
||||
}
|
||||
|
||||
public Task<Test?> GetTestByUuidAsync(string uuid)
|
||||
{
|
||||
return repo.GetByUuidAsync(uuid);
|
||||
}
|
||||
|
||||
public Task<IEnumerable<Test>> GetAllTestsAsync()
|
||||
{
|
||||
return repo.GetAllAsync();
|
||||
}
|
||||
|
||||
public async Task<long> CreateTestAsync(int testA, string testB, double testC)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(testB))
|
||||
{
|
||||
throw new ArgumentException("TestB is required");
|
||||
}
|
||||
|
||||
return await repo.InsertAsync(new Test { TestA = testA, TestB = testB, TestC = testC, Uuid = Guid.NewGuid().ToString() });
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateTestAsync(int id, int? testA, string? testB, double? testC)
|
||||
{
|
||||
Test entity = await repo.GetByIdAsync(id) ?? throw new KeyNotFoundException("Test not found");
|
||||
|
||||
entity.TestA = testA ?? entity.TestA;
|
||||
entity.TestB = testB ?? entity.TestB;
|
||||
entity.TestC = testC ?? entity.TestC;
|
||||
|
||||
return await repo.UpdateAsync(entity);
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteTestAsync(int id)
|
||||
{
|
||||
Test entity = await repo.GetByIdAsync(id) ?? throw new KeyNotFoundException("Test not found");
|
||||
|
||||
return await repo.DeleteAsync(entity);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user