using Dapper; using SolarPower.Helper; using SolarPower.Repository.Interface; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace SolarPower.Repository.Implement { public class RepositoryBase : IRepositoryBase where T : class { protected readonly IDatabaseHelper _databaseHelper; protected string tableName; protected IEnumerable GetProperties = typeof(T).GetProperties(); public RepositoryBase(IDatabaseHelper databaseHelper) { this._databaseHelper = databaseHelper; } /// /// 新增資料 /// /// /// public virtual async Task AddAsync(T entity, List properties) { int count; using (IDbConnection conn = _databaseHelper.GetConnection()) { conn.Open(); try { string sql = GenerateInsertQuery(properties); count = await conn.ExecuteAsync(sql, entity); } catch (Exception exception) { throw exception; } finally { conn.Close(); } return count; } } /// /// 新增單一筆資料 /// /// /// /// public virtual async Task AddOneAsync(T entity, List properties) { int id; using (IDbConnection conn = _databaseHelper.GetConnection()) { conn.Open(); try { string sql = GenerateInsertQuery(properties); sql += "SELECT LAST_INSERT_ID();"; id = (await conn.QueryAsync(sql, entity)).Single(); } catch (Exception exception) { throw exception; } finally { conn.Close(); } return id; } } /// /// 透過Id,軟刪除單一筆資料 /// /// /// public virtual async Task DeleteOne(int id) { using (IDbConnection conn = this._databaseHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { var sql = $"UPDATE {tableName} SET deleted = 1 WHERE id = @Id"; await conn.ExecuteAsync(sql, new { Id = id }, trans); trans.Commit(); } catch (Exception exception) { trans.Rollback(); throw exception; } finally { conn.Close(); } } } } /// /// 透過Id,軟刪除單一筆資料(不同資料表) /// /// /// /// public virtual async Task DeleteOneOtherTable(int id,string table_name) { using (IDbConnection conn = this._databaseHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { var sql = $"UPDATE {table_name} SET deleted = 1 WHERE id = @Id"; await conn.ExecuteAsync(sql, new { Id = id }, trans); trans.Commit(); } catch (Exception exception) { trans.Rollback(); throw exception; } finally { conn.Close(); } } } } /// /// 取得所有資料 /// /// public virtual async Task> GetAllAsync() { List result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { try { var sql = $"SELECT * FROM {tableName}"; result = (await conn.QueryAsync(sql)).ToList(); } catch (Exception exception) { throw exception; } return result; } } /// /// 取得單一筆資料 /// /// /// public virtual async Task GetOneAsync(int id) { T result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { try { var sql = $"SELECT * FROM {tableName} WHERE id = @Id"; result = await conn.QueryFirstOrDefaultAsync(sql, new { Id = id }); } catch (Exception exception) { throw exception; } return result; } } /// /// 透過Id,實際刪除單一筆資料 /// /// /// public virtual async Task PurgeOneAsync(int id) { using (IDbConnection conn = this._databaseHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { var sql = $"DELETE FROM {tableName} WHERE id = @Id"; await conn.ExecuteAsync(sql, new { Id = id }, trans); trans.Commit(); } catch (Exception exception) { trans.Rollback(); throw exception; } finally { conn.Close(); } } } } /// /// 修改資料 /// /// /// public virtual async Task Update(T entity, List properties) { using (IDbConnection conn = this._databaseHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { var sql = GenerateUpdateQuery(properties); await conn.ExecuteAsync(sql, entity, trans); trans.Commit(); } catch (Exception exception) { trans.Rollback(); throw exception; } finally { conn.Close(); } } } } /// /// 產生Insert語句 /// /// protected string GenerateInsertQuery(List properties) { var insertQuery = new StringBuilder($"INSERT INTO {tableName} "); insertQuery.Append("("); properties.ForEach(prop => { insertQuery.Append($"{prop},"); }); insertQuery .Remove(insertQuery.Length - 1, 1) .Append(") VALUES ("); properties.ForEach(prop => { insertQuery.Append($"@{prop},"); }); insertQuery .Remove(insertQuery.Length - 1, 1) .Append(");"); return insertQuery.ToString(); } /// /// 產生Insert語句,可選擇自己要加入資料表 /// /// /// 欲新增至目標資料表 /// protected string GenerateInsertQueryWithCustomTable(List properties, string table_name) { var insertQuery = new StringBuilder($"INSERT INTO {table_name} "); insertQuery.Append("("); properties.ForEach(prop => { insertQuery.Append($"{prop},"); }); insertQuery .Remove(insertQuery.Length - 1, 1) .Append(") VALUES ("); properties.ForEach(prop => { insertQuery.Append($"@{prop},"); }); insertQuery .Remove(insertQuery.Length - 1, 1) .Append(")"); return insertQuery.ToString(); } /// /// 產生Update語句 /// /// /// protected string GenerateUpdateQuery(List properties) { var updateQuery = new StringBuilder($"UPDATE {tableName} SET "); properties.ForEach(property => { if (!property.Equals("Id")) { updateQuery.Append($"{property}=@{property},"); } }); updateQuery.Remove(updateQuery.Length - 1, 1); //remove last comma updateQuery.Append(" WHERE id = @Id"); return updateQuery.ToString(); } } }