ปรับปรุงการทำ log

This commit is contained in:
LAPTOP-KB8JC2K2\acer
2021-05-30 12:41:44 +07:00
parent 0666c64024
commit 0dbfb54829
5 changed files with 127 additions and 10 deletions

View File

@@ -8,6 +8,11 @@ using TTSW.Common;
using TodoAPI2.Models;
using Microsoft.AspNetCore.Http;
using IdentityModel;
using Newtonsoft.Json;
using System.Reflection;
using System.Data;
using System.ComponentModel.DataAnnotations;
using TTSW.Utils;
namespace TTSW.EF
{
@@ -16,6 +21,7 @@ namespace TTSW.EF
{
#region Private Variables
private DataContext _context;
private IMyDatabase _db;
public DataContext Context
{
get
@@ -36,11 +42,12 @@ namespace TTSW.EF
}
#endregion
public BaseRepository(DataContext context, IHttpContextAccessor contextAccessor
public BaseRepository(DataContext context, IHttpContextAccessor contextAccessor, IMyDatabase mydatabase
)
{
_context = context;
_contextAccessor = contextAccessor;
_db = mydatabase;
}
#region Private Functions
@@ -255,12 +262,24 @@ namespace TTSW.EF
if (!SaveToDB())
throw new NotificationException($"Unable to insert item to database.");
}
public T Update(Key id, object model)
{
var userLogin = GetLoginProfile();
var existingItem = Get(id);
var old_existingItem = Get(id);
object old_existingItem;
try
{
var x = MyHelper.GetDataRow(id, model, _db);
old_existingItem = MyHelper.GetObject(x, model.GetType().Name.Replace("Entity", ""));
}
catch(Exception ex)
{
old_existingItem = null;
}
if (existingItem == null)
throw new NotificationException($"No item in database.");

View File

@@ -11,6 +11,8 @@ using TodoAPI2.Models;
using Microsoft.AspNetCore.Http;
using IdentityModel;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using TTSW.Utils;
namespace TTSW.EF
{
@@ -25,6 +27,7 @@ namespace TTSW.EF
{
#region Private Variables
private DataContext _context;
private IMyDatabase _db;
public DataContext Context
{
get
@@ -45,11 +48,12 @@ namespace TTSW.EF
}
#endregion
public BaseRepository2(DataContext context, IHttpContextAccessor contextAccessor
public BaseRepository2(DataContext context, IHttpContextAccessor contextAccessor, IMyDatabase mydatabase
)
{
_context = context;
_contextAccessor = contextAccessor;
_db = mydatabase;
}
#region Private Functions
@@ -269,7 +273,17 @@ namespace TTSW.EF
var userLogin = GetLoginProfile();
var existingItem = Get(id);
var old_existingItem = Get(id);
object old_existingItem;
try
{
var x = MyHelper.GetDataRow(id, model, _db);
old_existingItem = MyHelper.GetObject(x, model.GetType().Name.Replace("Entity", ""));
}
catch (Exception ex)
{
old_existingItem = null;
}
if (existingItem == null)
throw new NotificationException($"No item in database.");

View File

@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
@@ -19,6 +21,8 @@ namespace TTSW.Utils
Configuration = configuration;
}
public DataTable ExecuteDataTableNpgsql(string queryString, List<NpgsqlParameter> para)
{
System.Data.DataSet ds = new DataSet();

View File

@@ -9,17 +9,97 @@ using Microsoft.Extensions.Configuration;
using System.Net;
using TodoAPI2.Models;
using Microsoft.AspNetCore.Http;
using System.ComponentModel.DataAnnotations;
using TTSW.Utils;
/// <summary>
/// Summary description for MyHelper
/// </summary>
public class MyHelper
public static class MyHelper
{
public MyHelper()
public static DataRow GetDataRow(object id, object model, IMyDatabase _db)
{
//
// TODO: Add constructor logic here
//
DataRow result = null;
if (id.GetType().Name.Contains("int"))
{
result = _db.ExecuteDataTableNpgsql($"select * from {model.GetType().Name.Replace("Entity", "")} where id={id.ToString()} ", null).Rows[0];
}
else
{
result = _db.ExecuteDataTableNpgsql($"select * from {model.GetType().Name.Replace("Entity", "")} where id='{id.ToString()}' ", null).Rows[0];
}
return result;
}
public static object GetObject(this DataRow dataRow, string data_type)
{
if (data_type == "eva_adjust_postponement") return ToObject<eva_adjust_postponementEntity>(dataRow);
if (data_type == "eva_adjust_postponement_detail") return ToObject<eva_adjust_postponement_detailEntity>(dataRow);
if (data_type == "eva_create_evaluation") return ToObject<eva_create_evaluationEntity>(dataRow);
if (data_type == "eva_create_evaluation_detail") return ToObject<eva_create_evaluation_detailEntity>(dataRow);
if (data_type == "eva_create_evaluation_detail_history") return ToObject<eva_create_evaluation_detail_historyEntity>(dataRow);
if (data_type == "eva_evaluation_achievement") return ToObject<eva_evaluation_achievementEntity>(dataRow);
if (data_type == "eva_evaluation_achievement_attach") return ToObject<eva_evaluation_achievement_attachEntity>(dataRow);
if (data_type == "eva_evaluation_behavior") return ToObject<eva_evaluation_behaviorEntity>(dataRow);
if (data_type == "eva_evaluation_group") return ToObject<eva_evaluation_groupEntity>(dataRow);
if (data_type == "eva_evaluation_group_detail") return ToObject<eva_evaluation_group_detailEntity>(dataRow);
if (data_type == "eva_evaluation_operating_agreement") return ToObject<eva_evaluation_operating_agreementEntity>(dataRow);
if (data_type == "eva_idp_plan") return ToObject<eva_idp_planEntity>(dataRow);
if (data_type == "eva_level_score") return ToObject<eva_level_scoreEntity>(dataRow);
if (data_type == "eva_limit_frame_employee") return ToObject<eva_limit_frame_employeeEntity>(dataRow);
if (data_type == "eva_limit_frame_group") return ToObject<eva_limit_frame_groupEntity>(dataRow);
if (data_type == "eva_limit_frame_plan") return ToObject<eva_limit_frame_planEntity>(dataRow);
if (data_type == "eva_performance_plan") return ToObject<eva_performance_planEntity>(dataRow);
if (data_type == "eva_performance_plan_detail") return ToObject<eva_performance_plan_detailEntity>(dataRow);
if (data_type == "eva_promoted_percentage") return ToObject<eva_promoted_percentageEntity>(dataRow);
if (data_type == "eva_salary_cylinder") return ToObject<eva_salary_cylinderEntity>(dataRow);
return null;
}
public static T ToObject<T>(this DataRow dataRow) where T : new()
{
T item = new T();
foreach (DataColumn column in dataRow.Table.Columns)
{
PropertyInfo property = GetProperty(typeof(T), column.ColumnName);
if (property != null && dataRow[column] != DBNull.Value && dataRow[column].ToString() != "NULL")
{
property.SetValue(item, ChangeType(dataRow[column], property.PropertyType), null);
}
}
return item;
}
private static PropertyInfo GetProperty(Type type, string attributeName)
{
PropertyInfo property = type.GetProperty(attributeName);
if (property != null)
{
return property;
}
return type.GetProperties()
.Where(p => p.IsDefined(typeof(DisplayAttribute), false) && p.GetCustomAttributes(typeof(DisplayAttribute), false).Cast<DisplayAttribute>().Single().Name == attributeName)
.FirstOrDefault();
}
public static object ChangeType(object value, Type type)
{
if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
if (value == null)
{
return null;
}
return Convert.ChangeType(value, Nullable.GetUnderlyingType(type));
}
return Convert.ChangeType(value, type);
}
public static string GetDummyText()

BIN
object_index.xlsx Normal file

Binary file not shown.