diff --git a/EF/_BaseRepository.cs b/EF/_BaseRepository.cs index 97a77f1..fd74f9c 100644 --- a/EF/_BaseRepository.cs +++ b/EF/_BaseRepository.cs @@ -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."); diff --git a/EF/_BaseRepository2.cs b/EF/_BaseRepository2.cs index 3450b6e..1d23f38 100644 --- a/EF/_BaseRepository2.cs +++ b/EF/_BaseRepository2.cs @@ -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."); diff --git a/Utils/MyDatabase.cs b/Utils/MyDatabase.cs index 0dae08f..a24d3b8 100644 --- a/Utils/MyDatabase.cs +++ b/Utils/MyDatabase.cs @@ -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 para) { System.Data.DataSet ds = new DataSet(); diff --git a/Utils/MyHelper.cs b/Utils/MyHelper.cs index f82548b..e44471f 100644 --- a/Utils/MyHelper.cs +++ b/Utils/MyHelper.cs @@ -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 description for MyHelper /// -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(dataRow); + if (data_type == "eva_adjust_postponement_detail") return ToObject(dataRow); + if (data_type == "eva_create_evaluation") return ToObject(dataRow); + if (data_type == "eva_create_evaluation_detail") return ToObject(dataRow); + if (data_type == "eva_create_evaluation_detail_history") return ToObject(dataRow); + if (data_type == "eva_evaluation_achievement") return ToObject(dataRow); + if (data_type == "eva_evaluation_achievement_attach") return ToObject(dataRow); + if (data_type == "eva_evaluation_behavior") return ToObject(dataRow); + if (data_type == "eva_evaluation_group") return ToObject(dataRow); + if (data_type == "eva_evaluation_group_detail") return ToObject(dataRow); + if (data_type == "eva_evaluation_operating_agreement") return ToObject(dataRow); + if (data_type == "eva_idp_plan") return ToObject(dataRow); + if (data_type == "eva_level_score") return ToObject(dataRow); + if (data_type == "eva_limit_frame_employee") return ToObject(dataRow); + if (data_type == "eva_limit_frame_group") return ToObject(dataRow); + if (data_type == "eva_limit_frame_plan") return ToObject(dataRow); + if (data_type == "eva_performance_plan") return ToObject(dataRow); + if (data_type == "eva_performance_plan_detail") return ToObject(dataRow); + if (data_type == "eva_promoted_percentage") return ToObject(dataRow); + if (data_type == "eva_salary_cylinder") return ToObject(dataRow); + + return null; + } + public static T ToObject(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().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() diff --git a/object_index.xlsx b/object_index.xlsx new file mode 100644 index 0000000..dd3144b Binary files /dev/null and b/object_index.xlsx differ