Skip to content

Commit

Permalink
Start to refactor database functions to it's separate
Browse files Browse the repository at this point in the history
class so Postgres support can be added.

Move database configuration to the appsetttings.json
file
  • Loading branch information
香風智乃 authored and 香風智乃 committed Apr 30, 2019
1 parent 36af8fd commit 180ab95
Show file tree
Hide file tree
Showing 16 changed files with 490 additions and 196 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -259,4 +259,5 @@ paket-files/
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
hato/Helpers/ConnectionConfig.cs
hato/appsettings.json
hato/appsettings.Development.json
6 changes: 4 additions & 2 deletions hato/Controllers/MappingsController.cs
Expand Up @@ -21,10 +21,12 @@ namespace hato.Controllers
public class MappingsController : ControllerBase
{
private readonly UserAgentControl allowedclients;
private readonly dbsettings dbsettings;

public MappingsController(IOptions<UserAgentControl> allowedclients)
public MappingsController(IOptions<UserAgentControl> allowedclients, IOptions<dbsettings> settings)
{
this.allowedclients = allowedclients.Value;
this.dbsettings = settings.Value;
}
// GET api/mappings/(service)/(type)/(id)
[HttpGet("{service}/{type}/{id}")]
Expand All @@ -34,7 +36,7 @@ public ActionResult<JsonResult> Get(string service, string type, string id)
{
return Unauthorized();
}
TitleIdMapping mapping = new TitleIdMapping(service, type, id);
TitleIdMapping mapping = new TitleIdMapping(service, type, id, this.dbsettings);
if (!mapping.errored)
{
mapping.performLookup();
Expand Down
Expand Up @@ -8,22 +8,20 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;

namespace hato.Helpers
{
public class ConnectionConfig
{
// Specify database settings
// Note: You should execute setupschema.sql before setting up this script.
private const String mysqlserver = "localhost";
private const String mysqldatabase = "hato";
private const String mysqlusername = "hato";
private const String mysqlpassword = "";

// Do not modify anything below this line.
public static String connectionstring ()
{
return "SERVER =" + mysqlserver + ";DATABASE=" + mysqldatabase + ";UID=" + mysqlusername + ";PASSWORD=" + mysqlpassword + ";";
}
//dbsettings settings = settings.Values;
//return "SERVER =" + settings.dbhost + ";DATABASE=" + settings.dbname + ";UID=" + settings.dbuser + ";PASSWORD=" + settings.dbpassword + ";";
return "";
}
}
}
108 changes: 108 additions & 0 deletions hato/Helpers/ConnectionManager.cs
@@ -0,0 +1,108 @@
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace hato.Helpers
{
public class ConnectionManager
{
private readonly dbsettings settings;
private MySQLDBManager mysqldbmgr;
private PostgresDBManager postgresdbmgr;

public ConnectionManager(dbsettings settings)
{
this.settings = settings;
String connectionString;
switch (this.settings.dbengine)
{
case "mysql":
connectionString = "SERVER =" + this.settings.dbhost + ";DATABASE=" + this.settings.dbname + ";UID=" + this.settings.dbuser + ";PASSWORD=" + this.settings.dbpassword + ";";
mysqldbmgr = new MySQLDBManager(connectionString);
break;
case "postgres":
connectionString = "SERVER=" + this.settings.dbhost + ";User Id=" + this.settings.dbuser + ";Password=" + this.settings.dbpassword + ";Database=" + this.settings.dbname;
break;
default:
throw new System.ArgumentException("Invalid engine type" + this.settings.dbengine);
}
}

public void Dispose()
{
switch (this.settings.dbengine)
{
case "mysql":
mysqldbmgr.Dispose();
break;
case "postgres":
break;
}
}

public bool isInitalized()
{
switch (this.settings.dbengine)
{
case "mysql":
return mysqldbmgr.initalized;
case "postgres":
return false;
}
return false;
}

public Dictionary<string, object> RetreiveSavedIDsFromServiceID(Service listService, object titleid, MediaType type)
{
switch (this.settings.dbengine)
{
case "mysql":
return mysqldbmgr.RetreiveSavedIDsFromServiceID(listService, titleid, type);
}
return null;
}

public object RetreiveSavedTargetIDFromServiceID(Service targetservice, Service listService, object titleid, MediaType type)
{
switch (this.settings.dbengine)
{
case "mysql":
return mysqldbmgr.RetreiveSavedTargetIDFromServiceID(targetservice, listService, titleid, type);
}
return null;
}

public void SaveIDtoDatabase(Service targetservice, Service listservice, object targettitleid, object servicetitleid, MediaType type)
{
switch (this.settings.dbengine)
{
case "mysql":
mysqldbmgr.SaveIDtoDatabase(targetservice, listservice, targettitleid, servicetitleid, type);
break;
}
}

public object CheckIfEntryExists(Service targetservice, object targetid, MediaType type)
{
switch (this.settings.dbengine)
{
case "mysql":
return mysqldbmgr.CheckIfEntryExists(targetservice, targetid, type);
}
return null;
}

public object CheckIfEntryExists(Service targetservice, object targetid, Service sourceservice, object sourceid, MediaType type)
{
switch (this.settings.dbengine)
{
case "mysql":
return mysqldbmgr.CheckIfEntryExists(targetservice, targetid, sourceservice, sourceid, type);
}
return null;
}

}
}

0 comments on commit 180ab95

Please sign in to comment.