Update 20211218

This commit is contained in:
2021-12-18 10:02:42 +01:00
parent 1254540139
commit 3ef1971def
842 changed files with 199916 additions and 17112 deletions

View File

@@ -0,0 +1,52 @@
using APP.Utils;
using BWPMModels;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;
namespace APP.Dal
{
public class AccountDAL : IAccountDAL
{
private IDbHelper _db;
public AccountDAL(IDbHelper db)
{
this._db = db;
}
public async Task<UserModel> GetById(int id)
{
try
{
string sp = "Employee_GetById";
List<SqlParameter> parms = new List<SqlParameter>();
parms.Add(new SqlParameter("Id", id));
var data = await _db.ExecuteToTableAsync<UserModel>(sp, parms, DbHelperEnum.StoredProcedure);
return data != null ? data.FirstOrDefault() : null;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
public async Task<UserModel> GetByEmail(string email)
{
try
{
string sp = "Employee_GetByEmail";
List<SqlParameter> parms = new List<SqlParameter>();
parms.Add(new SqlParameter("Email", email));
var data = await _db.ExecuteToTableAsync<UserModel>(sp, parms, DbHelperEnum.StoredProcedure);
return data != null ? data.FirstOrDefault() : null;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
}

View File

@@ -0,0 +1,244 @@
using APP.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;
namespace APP.Dal
{
public class DbHelper: IDbHelper
{
private SqlConnection _con;
private SqlCommand _cmd;
private SqlDataAdapter _adapter;
private readonly int _connectDBTimeOut = 120;
private static string _connectionString = "";
public DbHelper()
{
_connectionString = AppSettings.Instance.GetConnection(Const.ConnectionString);
}
public async Task<bool> ExecuteNonQuery(string query, List<SqlParameter> parameters, DbHelperEnum type)
{
using (var con = new SqlConnection(_connectionString))
{
using (var cmd = new SqlCommand(query, con))
{
await con.OpenAsync();
cmd.Connection = con;
cmd.CommandType = type == DbHelperEnum.StoredProcedure ? CommandType.StoredProcedure : CommandType.Text;
cmd.CommandText = query;
cmd.CommandTimeout = _connectDBTimeOut;
if (parameters != null)
cmd.Parameters.AddRange(parameters.ToArray());
int result = await cmd.ExecuteNonQueryAsync().ConfigureAwait(false);
con.Dispose();
if (con.State == ConnectionState.Open)
con.Close();
return result > 0;
}
}
}
public async Task<T> ExecuteScalarFunction<T>(string query, List<SqlParameter> parameters, DbHelperEnum type, string outParams)
{
using (var con = new SqlConnection(_connectionString))
{
using (var cmd = new SqlCommand(query, con))
{
await con.OpenAsync();
cmd.Connection = con;
cmd.Parameters.Clear();
cmd.CommandType = type == DbHelperEnum.StoredProcedure ? CommandType.StoredProcedure : CommandType.Text;
cmd.CommandText = query;
cmd.CommandTimeout = _connectDBTimeOut;
if (parameters != null)
cmd.Parameters.AddRange(parameters.ToArray());
SqlParameter returnValue = cmd.Parameters.Add(new SqlParameter(outParams, 0));
returnValue.Direction = ParameterDirection.Output;
await cmd.ExecuteNonQueryAsync();
con.Dispose();
if (con.State == ConnectionState.Open) con.Close();
return (T)returnValue.Value;
}
}
}
public async Task<IEnumerable<T>> ExecuteToTableAsync<T>(string query, List<SqlParameter> parameters, DbHelperEnum type) where T : class
{
try
{
IEnumerable<T> result = new List<T>();
using (var con = new SqlConnection(_connectionString))
{
using (var cmd = new SqlCommand(query, con))
{
Console.WriteLine("Open connecting ......");
var watch = System.Diagnostics.Stopwatch.StartNew();
await con.OpenAsync();
watch.Stop();
Console.WriteLine(watch.ElapsedMilliseconds);
Console.WriteLine("Open connected");
cmd.Parameters.Clear();
cmd.CommandType = type == DbHelperEnum.StoredProcedure ? CommandType.StoredProcedure : CommandType.Text;
cmd.CommandTimeout = _connectDBTimeOut;
if (parameters != null) cmd.Parameters.AddRange(parameters.ToArray());
using (SqlDataReader reader = await cmd.ExecuteReaderAsync())
{
if (reader.HasRows)
{
result = await Mapper<T>(reader);
reader.Close();
}
}
if (con.State == ConnectionState.Open) con.Close();
}
}
return result;
}
catch (Exception ex)
{
throw ex;
}
}
#region Private func
public async Task<IList<T>> Mapper<T>(SqlDataReader reader, bool close = true) where T : class
{
try
{
IList<T> entities = new List<T>();
if (reader != null && reader.HasRows)
{
while (await reader.ReadAsync())
{
T item = default(T);
if (item == null)
item = Activator.CreateInstance<T>();
Mapper(reader, item);
entities.Add(item);
}
if (close)
{
reader.Close();
}
}
return entities;
}
catch (Exception ex)
{
throw ex;
}
}
private bool Mapper<T>(IDataRecord reader, T entity) where T : class
{
Type type = typeof(T);
if (entity != null)
{
for (var i = 0; i < reader.FieldCount; i++)
{
var fieldName = reader.GetName(i);
try
{
var propertyInfo = type.GetProperties().FirstOrDefault(info => info.Name.Equals(fieldName, StringComparison.InvariantCultureIgnoreCase));
if (propertyInfo != null)
{
var value = reader[i];
if ((reader[i] != null) && (reader[i] != DBNull.Value))
{
propertyInfo.SetValue(entity, reader[i], null);
}
else
{
if (propertyInfo.PropertyType == typeof(System.DateTime) ||
propertyInfo.PropertyType == typeof(System.DateTime?))
{
propertyInfo.SetValue(entity, System.DateTime.MinValue, null);
}
else if (propertyInfo.PropertyType == typeof(string))
{
propertyInfo.SetValue(entity, string.Empty, null);
}
else if (propertyInfo.PropertyType == typeof(bool) ||
propertyInfo.PropertyType == typeof(bool?))
{
propertyInfo.SetValue(entity, false, null);
}
else if (propertyInfo.PropertyType == typeof(decimal) ||
propertyInfo.PropertyType == typeof(decimal?))
{
propertyInfo.SetValue(entity, decimal.Zero, null);
}
else if (propertyInfo.PropertyType == typeof(double) ||
propertyInfo.PropertyType == typeof(double?))
{
propertyInfo.SetValue(entity, double.Parse("0"), null);
}
else if (propertyInfo.PropertyType == typeof(float) ||
propertyInfo.PropertyType == typeof(float?))
{
propertyInfo.SetValue(entity, 0, null);
}
else if (propertyInfo.PropertyType == typeof(short) ||
propertyInfo.PropertyType == typeof(short?))
{
propertyInfo.SetValue(entity, short.Parse("0"), null);
}
else if (propertyInfo.PropertyType == typeof(long) ||
propertyInfo.PropertyType == typeof(long?))
{
propertyInfo.SetValue(entity, long.Parse("0"), null);
}
else if (propertyInfo.PropertyType == typeof(int) ||
propertyInfo.PropertyType == typeof(int?))
{
propertyInfo.SetValue(entity, int.Parse("0"), null);
}
else
{
propertyInfo.SetValue(entity, 0, null);
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
return true;
}
else
{
return false;
}
}
#endregion
}
}

View File

@@ -0,0 +1,15 @@
using BWPMModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace APP.Dal
{
public interface IAccountDAL
{
Task<UserModel> GetById(int id);
Task<UserModel> GetByEmail(string email);
}
}

View File

@@ -0,0 +1,18 @@
using APP.Utils;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;
namespace APP.Dal
{
public interface IDbHelper
{
Task<bool> ExecuteNonQuery(string query, List<SqlParameter> parameters, DbHelperEnum type);
Task<T> ExecuteScalarFunction<T>(string query, List<SqlParameter> parameters, DbHelperEnum type, string outParams);
Task<IEnumerable<T>> ExecuteToTableAsync<T>(string query, List<SqlParameter> parameters, DbHelperEnum type) where T : class;
}
}