87 lines
4.1 KiB
C#
87 lines
4.1 KiB
C#
#pragma warning disable 1591
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.Serialization;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.Configuration;
|
|
using MySqlConnector;
|
|
using de.hottis.genericdatabaseapiservice.Models;
|
|
|
|
namespace de.hottis.genericdatabaseapiservice.Services {
|
|
public interface IDbService {
|
|
Task<List<TOUT>> ReadBySelect<TIN, TOUT>(string selectStatement, TIN input);
|
|
}
|
|
|
|
public class DbServiceException : Exception {}
|
|
public class NotDataFoundException : DbServiceException {}
|
|
|
|
public class DbService : IDbService {
|
|
private readonly IConfiguration Configuration;
|
|
private string databaseConnInfo;
|
|
|
|
public DbService(IConfiguration configuration) {
|
|
Configuration = configuration;
|
|
databaseConnInfo = String.Format(
|
|
"Server={0};User ID={1};Password={2};Database={3}",
|
|
Configuration["Database:Host"],
|
|
Configuration["Database:User"],
|
|
Configuration["Database:Password"],
|
|
Configuration["Database:Name"]);
|
|
}
|
|
|
|
async public Task<List<TOUT>> ReadBySelect<TIN, TOUT>(string selectStatement, TIN input) {
|
|
var itemList = new List<TOUT>();
|
|
|
|
// Console.WriteLine("ConnInfo: {0}", databaseConnInfo);
|
|
using (var conn = new MySqlConnection(databaseConnInfo)) {
|
|
await conn.OpenAsync();
|
|
|
|
using (var cmd = conn.CreateCommand()) {
|
|
cmd.CommandText = selectStatement;
|
|
if (input != null){
|
|
foreach (var propertyInfo in typeof(TIN).GetProperties()) {
|
|
Console.WriteLine("Input Property name: {0} {1} ", propertyInfo.Name, propertyInfo.PropertyType);
|
|
var attributes = propertyInfo.GetCustomAttributes(typeof(DataMemberAttribute), true);
|
|
var dma = (DataMemberAttribute)attributes[0];
|
|
Console.WriteLine("Input DataMember name: {0} {1} ", dma.Name, dma.TypeId);
|
|
var value = propertyInfo.GetValue(input);
|
|
Console.WriteLine("Input Value: {0}", value);
|
|
cmd.Parameters.AddWithValue(dma.Name, propertyInfo.GetValue(input));
|
|
}
|
|
} else {
|
|
Console.WriteLine("no input data");
|
|
}
|
|
using (var reader = await cmd.ExecuteReaderAsync()) {
|
|
while (await reader.ReadAsync()) {
|
|
var item = Activator.CreateInstance<TOUT>();
|
|
foreach (var propertyInfo in typeof(TOUT).GetProperties()) {
|
|
Console.WriteLine("Output Property name: {0} {1} ", propertyInfo.Name, propertyInfo.PropertyType);
|
|
var attributes = propertyInfo.GetCustomAttributes(typeof(DataMemberAttribute), true);
|
|
var dma = (DataMemberAttribute)attributes[0];
|
|
Console.WriteLine("Output DataMember name: {0} {1} ", dma.Name, dma.TypeId);
|
|
if (propertyInfo.PropertyType == typeof(System.String)) {
|
|
propertyInfo.SetValue(item, reader.GetString(dma.Name));
|
|
Console.WriteLine("Output Value:{0}", reader.GetString(dma.Name));
|
|
} else if (propertyInfo.PropertyType == typeof(System.Int32)) {
|
|
propertyInfo.SetValue(item, reader.GetInt32(dma.Name));
|
|
Console.WriteLine("Output Value:{0}", reader.GetInt32(dma.Name));
|
|
}
|
|
}
|
|
itemList.Add(item);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (itemList.Count == 0) {
|
|
throw new NotDataFoundException();
|
|
}
|
|
|
|
return itemList;
|
|
}
|
|
}
|
|
}
|
|
|
|
#pragma warning restore 1591
|