read database info from file
This commit is contained in:
63
DbService.cs
63
DbService.cs
@ -1,19 +1,65 @@
|
||||
#pragma warning disable 1591
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MySqlConnector;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
// make sure to adjust the prefix with the PACKAGE_NAME from ENV
|
||||
using com.krohne.genericdatabaseapiservice.Models;
|
||||
|
||||
// make sure to adjust the prefix with the PACKAGE_NAME from ENV
|
||||
namespace com.krohne.genericdatabaseapiservice.Services {
|
||||
public class DbInfoObject {
|
||||
public DbInfoObject(string host, string user, string password, string name) {
|
||||
Host = host;
|
||||
User = user;
|
||||
Password = password;
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public string Host { get; set; }
|
||||
public string User { get; set; }
|
||||
public string Password { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
|
||||
public interface IDbInfoService {
|
||||
DbInfoObject GetInfoByTag(string tag);
|
||||
string GetInfoStringByTag(string tag);
|
||||
}
|
||||
|
||||
public class DbInfoService : IDbInfoService {
|
||||
private readonly IConfiguration Configuration;
|
||||
private Dictionary<string, DbInfoObject> DbInfos;
|
||||
|
||||
public DbInfoService(IConfiguration configuration) {
|
||||
Configuration = configuration;
|
||||
Console.WriteLine("Database Infofile: {0}", Configuration["Database:InfoFile"]);
|
||||
DbInfos = JsonConvert.DeserializeObject<Dictionary<string, DbInfoObject>>(File.ReadAllText(Configuration["Database:InfoFile"]));
|
||||
}
|
||||
|
||||
public DbInfoObject GetInfoByTag(string tag) {
|
||||
return DbInfos[tag];
|
||||
}
|
||||
|
||||
public string GetInfoStringByTag(string tag) {
|
||||
return String.Format(
|
||||
"Server={0};User ID={1};Password={2};Database={3}",
|
||||
DbInfos[tag].Host,
|
||||
DbInfos[tag].User,
|
||||
DbInfos[tag].Password,
|
||||
DbInfos[tag].Name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public interface IDbService {
|
||||
Task<List<TOUT>> ReadBySelect<TIN, TOUT>(string selectStatement, bool justOne, TIN input);
|
||||
Task<List<TOUT>> ReadBySelect<TIN, TOUT>(string databaseTag, string selectStatement, bool justOne, TIN input);
|
||||
}
|
||||
|
||||
public class DbServiceException : Exception {}
|
||||
@ -23,21 +69,18 @@ namespace com.krohne.genericdatabaseapiservice.Services {
|
||||
|
||||
public class DbService : IDbService {
|
||||
private readonly IConfiguration Configuration;
|
||||
private string databaseConnInfo;
|
||||
private readonly IDbInfoService DbInfoService;
|
||||
|
||||
public DbService(IConfiguration configuration) {
|
||||
public DbService(IConfiguration configuration, IDbInfoService dbInfoService) {
|
||||
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"]);
|
||||
DbInfoService = dbInfoService;
|
||||
}
|
||||
|
||||
async public Task<List<TOUT>> ReadBySelect<TIN, TOUT>(string selectStatement, bool justOne, TIN input) {
|
||||
async public Task<List<TOUT>> ReadBySelect<TIN, TOUT>(string databaseTag, string selectStatement, bool justOne, TIN input) {
|
||||
var itemList = new List<TOUT>();
|
||||
|
||||
var databaseConnInfo = DbInfoService.GetInfoStringByTag(databaseTag);
|
||||
|
||||
Console.WriteLine("ConnInfo: {0}", databaseConnInfo);
|
||||
Console.WriteLine("Statement: {0}", selectStatement);
|
||||
|
||||
|
Reference in New Issue
Block a user