First Initial
This commit is contained in:
56
EF/DataContext.cs
Normal file
56
EF/DataContext.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
using Microsoft.EntityFrameworkCore.Internal;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using System;
|
||||
using TodoAPI2.Models;
|
||||
using TTSW.EF;
|
||||
|
||||
namespace TTSW.EF {
|
||||
public class DataContext : DbContext {
|
||||
public DataContext (DbContextOptions<DataContext> options) : base (options) { }
|
||||
|
||||
public DbSet<eva_create_evaluationEntity> eva_create_evaluation { get; set; }
|
||||
public DbSet<eva_create_evaluation_detailEntity> eva_create_evaluation_detail { get; set; }
|
||||
//public DbSet<eva_create_evaluation_detail_agreementEntity> eva_create_evaluation_detail_agreement { get; set; }
|
||||
public DbSet<eva_evaluation_achievementEntity> eva_evaluation_achievement { get; set; }
|
||||
public DbSet<eva_evaluation_behaviorEntity> eva_evaluation_behavior { get; set; }
|
||||
public DbSet<eva_evaluation_groupEntity> eva_evaluation_group { get; set; }
|
||||
public DbSet<eva_evaluation_group_detailEntity> eva_evaluation_group_detail { get; set; }
|
||||
public DbSet<eva_level_scoreEntity> eva_level_score { get; set; }
|
||||
//public DbSet<rep_eva01Entity> rep_eva01 { get; set; }
|
||||
//public DbSet<rep_eva02Entity> rep_eva02 { get; set; }
|
||||
//public DbSet<rep_eva03Entity> rep_eva03 { get; set; }
|
||||
//public DbSet<rep_familyEntity> rep_family { get; set; }
|
||||
//public DbSet<rep_leave_summaryEntity> rep_leave_summary { get; set; }
|
||||
//public DbSet<rep_leave_totalEntity> rep_leave_total { get; set; }
|
||||
//public DbSet<rep_position_salaryEntity> rep_position_salary { get; set; }
|
||||
//public DbSet<rep_samanaEntity> rep_samana { get; set; }
|
||||
//public DbSet<rep_study_historyEntity> rep_study_history { get; set; }
|
||||
//public DbSet<rep_working_typeEntity> rep_working_type { get; set; }
|
||||
public DbSet<eva_performance_planEntity> eva_performance_plan { get; set; }
|
||||
public DbSet<eva_performance_plan_detailEntity> eva_performance_plan_detail { get; set; }
|
||||
public DbSet<eva_promoted_percentageEntity> eva_promoted_percentage { get; set; }
|
||||
public DbSet<eva_salary_cylinderEntity> eva_salary_cylinder { get; set; }
|
||||
public DbSet<eva_adjust_postponementEntity> eva_adjust_postponement { get; set; }
|
||||
//public DbSet<eva_adjust_postponement_normalEntity> eva_adjust_postponement_normal { get; set; }
|
||||
public DbSet<eva_adjust_postponement_detailEntity> eva_adjust_postponement_detail { get; set; }
|
||||
//public DbSet<eva_adjust_postponement_detail_normalEntity> eva_adjust_postponement_detail_normal { get; set; }
|
||||
|
||||
protected override void OnModelCreating (ModelBuilder modelBuilder) {
|
||||
|
||||
base.OnModelCreating (modelBuilder);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//public class DataContextFactory : IDesignTimeDbContextFactory<DataContext> {
|
||||
// public DataContext CreateDbContext (string[] args) {
|
||||
// var optionsBuilder = new DbContextOptionsBuilder<DataContext> ();
|
||||
// optionsBuilder.UseNpgsql ("Server=115.31.162.93;Database=poc;User ID=poc;Password=project0*;");
|
||||
|
||||
// return new DataContext (optionsBuilder.Options);
|
||||
// }
|
||||
//}
|
||||
}
|
||||
|
||||
49
EF/_BaseEntity.cs
Normal file
49
EF/_BaseEntity.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TTSW.EF
|
||||
{
|
||||
public class BaseEntity<T>: IBaseEntity<T>
|
||||
{
|
||||
public BaseEntity()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public T id { get; set; }
|
||||
|
||||
//public string CreatorId { get; set; }
|
||||
//[ForeignKey("CreatorId")]
|
||||
//public virtual User Creator { get; set; }
|
||||
|
||||
//public string UpdatorId { get; set; }
|
||||
//[ForeignKey("UpdatorId")]
|
||||
//public virtual User Updator { get; set; }
|
||||
|
||||
public DateTime created { get; set; }
|
||||
public DateTime updated { get; set; }
|
||||
public bool isActive { get; set; }
|
||||
|
||||
//public string CreatorFullname
|
||||
//{
|
||||
// get
|
||||
// {
|
||||
// return Creator.FullName;
|
||||
// }
|
||||
//}
|
||||
|
||||
//public string UpdatorFullname
|
||||
//{
|
||||
// get
|
||||
// {
|
||||
// return Updator.FullName;
|
||||
// }
|
||||
//}
|
||||
|
||||
}
|
||||
}
|
||||
24
EF/_BaseEntity2.cs
Normal file
24
EF/_BaseEntity2.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TTSW.EF
|
||||
{
|
||||
public class BaseEntity2<T>: IBaseEntity2<T>
|
||||
{
|
||||
public BaseEntity2()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
|
||||
public T id { get; set; }
|
||||
|
||||
public DateTime created { get; set; }
|
||||
public DateTime updated { get; set; }
|
||||
public bool isActive { get; set; }
|
||||
}
|
||||
}
|
||||
359
EF/_BaseRepository.cs
Normal file
359
EF/_BaseRepository.cs
Normal file
@@ -0,0 +1,359 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Transactions;
|
||||
using AutoMapper;
|
||||
using System.Linq.Expressions;
|
||||
using TTSW.Common;
|
||||
using TodoAPI2.Models;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using IdentityModel;
|
||||
|
||||
namespace TTSW.EF
|
||||
{
|
||||
public class BaseRepository<T, Key> : IBaseRepository<T, Key>
|
||||
where T : class, IBaseEntity<Key>
|
||||
{
|
||||
#region Private Variables
|
||||
private DataContext _context;
|
||||
public DataContext Context
|
||||
{
|
||||
get
|
||||
{
|
||||
return _context;
|
||||
}
|
||||
}
|
||||
|
||||
private IHttpContextAccessor _contextAccessor;
|
||||
private LoginProfile _loginProfile;
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
private DbSet<T> _entities;
|
||||
public DbSet<T> Entities {
|
||||
get {
|
||||
return _entities ?? (_entities = _context.Set<T>()); }
|
||||
}
|
||||
#endregion
|
||||
|
||||
public BaseRepository(DataContext context, IHttpContextAccessor contextAccessor
|
||||
)
|
||||
{
|
||||
_context = context;
|
||||
_contextAccessor = contextAccessor;
|
||||
}
|
||||
|
||||
#region Private Functions
|
||||
private bool SaveToDB()
|
||||
{
|
||||
return (_context.SaveChanges() >= 0);
|
||||
}
|
||||
private T SetCreatorProperties(T entity)
|
||||
{
|
||||
//entity.CreatorId = _userRepository.LoginProfile.Id;
|
||||
entity.created = DateTime.Now;
|
||||
|
||||
return entity;
|
||||
}
|
||||
private T SetUpdatorProperties(T entity)
|
||||
{
|
||||
//entity.UpdatorId = _userRepository.LoginProfile.Id;
|
||||
entity.updated = DateTime.Now;
|
||||
|
||||
return entity;
|
||||
}
|
||||
private T SetActiveProperty(T entity)
|
||||
{
|
||||
entity.isActive = true;
|
||||
return entity;
|
||||
}
|
||||
private T SetInActiveProperty(T entity)
|
||||
{
|
||||
entity.isActive = false;
|
||||
return entity;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Public Functions
|
||||
|
||||
public LoginProfile GetLoginProfile()
|
||||
{
|
||||
// Set login profile in the 1st accessing time only
|
||||
// The user has to login to the system too.
|
||||
if (_loginProfile == null && _contextAccessor.HttpContext.User.Identity.IsAuthenticated)
|
||||
{
|
||||
var dict = new Dictionary<string, string>();
|
||||
|
||||
// A list to keep list of all keys for getting user user claims
|
||||
var userProfileClaimsKey = new List<string>();
|
||||
userProfileClaimsKey.Add(JwtClaimTypes.GivenName);
|
||||
userProfileClaimsKey.Add(JwtClaimTypes.FamilyName);
|
||||
userProfileClaimsKey.Add(JwtClaimTypes.Email);
|
||||
userProfileClaimsKey.Add("customer_guid");
|
||||
userProfileClaimsKey.Add("user_guid");
|
||||
userProfileClaimsKey.Add("permission_level");
|
||||
userProfileClaimsKey.Add("base_system");
|
||||
userProfileClaimsKey.Add(JwtClaimTypes.Name);
|
||||
userProfileClaimsKey.Add(JwtClaimTypes.PreferredUserName);
|
||||
userProfileClaimsKey.Add(JwtClaimTypes.PhoneNumber);
|
||||
|
||||
// Fetch only claims related to user profile
|
||||
_contextAccessor.HttpContext.User.Claims
|
||||
.Where(c=> userProfileClaimsKey.Contains(c.Type)).ToList()
|
||||
.ForEach(item => dict.Add(item.Type, item.Value));
|
||||
|
||||
// Map claims to model
|
||||
_loginProfile = new LoginProfile();
|
||||
|
||||
//if (dict.ContainsKey(JwtClaimTypes.Subject))
|
||||
// _loginProfile.Id = dict[JwtClaimTypes.Subject];
|
||||
|
||||
if (dict.ContainsKey(JwtClaimTypes.GivenName) && !string.IsNullOrEmpty(dict[JwtClaimTypes.GivenName]))
|
||||
_loginProfile.FirstName = dict[JwtClaimTypes.GivenName];
|
||||
|
||||
if (dict.ContainsKey(JwtClaimTypes.FamilyName) && !string.IsNullOrEmpty(dict[JwtClaimTypes.FamilyName]))
|
||||
_loginProfile.LastName = dict[JwtClaimTypes.FamilyName];
|
||||
|
||||
if (dict.ContainsKey(JwtClaimTypes.Email) && !string.IsNullOrEmpty(dict[JwtClaimTypes.Email]))
|
||||
_loginProfile.Email = dict[JwtClaimTypes.Email];
|
||||
|
||||
if (dict.ContainsKey("customer_guid") && !string.IsNullOrEmpty(dict["customer_guid"]))
|
||||
_loginProfile.CustomerGUID = new Guid(dict["customer_guid"]);
|
||||
|
||||
if (dict.ContainsKey("user_guid") && !string.IsNullOrEmpty(dict["user_guid"]))
|
||||
_loginProfile.UserGUID = new Guid(dict["user_guid"]);
|
||||
|
||||
if (dict.ContainsKey("permission_level") && !string.IsNullOrEmpty(dict["permission_level"]))
|
||||
_loginProfile.permission_level = dict["permission_level"];
|
||||
|
||||
if (dict.ContainsKey("base_system") && !string.IsNullOrEmpty(dict["base_system"]))
|
||||
_loginProfile.base_system = dict["base_system"];
|
||||
|
||||
if (string.IsNullOrEmpty(_loginProfile.base_system))
|
||||
{
|
||||
_loginProfile.base_system = "";
|
||||
}
|
||||
|
||||
if (dict.ContainsKey(JwtClaimTypes.Name) && !string.IsNullOrEmpty(dict[JwtClaimTypes.Name]))
|
||||
_loginProfile.UserName = dict[JwtClaimTypes.Name];
|
||||
else if (dict.ContainsKey(JwtClaimTypes.PreferredUserName) && !string.IsNullOrEmpty(dict[JwtClaimTypes.PreferredUserName]))
|
||||
_loginProfile.UserName = dict[JwtClaimTypes.PreferredUserName];
|
||||
|
||||
if (dict.ContainsKey(JwtClaimTypes.PhoneNumber) && !string.IsNullOrEmpty(dict[JwtClaimTypes.PhoneNumber]))
|
||||
_loginProfile.PhoneNumber = dict[JwtClaimTypes.PhoneNumber];
|
||||
}
|
||||
|
||||
return _loginProfile;
|
||||
}
|
||||
|
||||
#region Query Functions
|
||||
public T Get(Key id)
|
||||
{
|
||||
return Entities
|
||||
//.Include(c => c.Creator)
|
||||
//.Include(c => c.Updator)
|
||||
.FirstOrDefault(c=>c.id.Equals(id));
|
||||
}
|
||||
public T Get(Key id, params Expression<Func<T, object>>[] includes)
|
||||
{
|
||||
var query = Entities.AsQueryable();
|
||||
|
||||
//Dynamic include columns
|
||||
foreach (var include in includes)
|
||||
query = query.Include(include);
|
||||
|
||||
return query
|
||||
//.Include(c => c.Creator)
|
||||
//.Include(c => c.Updator)
|
||||
.FirstOrDefault(c => c.id.Equals(id));
|
||||
}
|
||||
|
||||
public IQueryable<T> GetQuery()
|
||||
{
|
||||
return Entities.AsNoTracking();
|
||||
}
|
||||
public List<T> GetList(Expression<Func<T, bool>> predicate)
|
||||
{
|
||||
var query = Entities
|
||||
.AsNoTracking()
|
||||
//.Include(c => c.Creator)
|
||||
//.Include(c => c.Updator)
|
||||
.Where(predicate);
|
||||
|
||||
return query.ToList();
|
||||
}
|
||||
public List<T> GetList(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includes)
|
||||
{
|
||||
var query = Entities.AsQueryable();
|
||||
|
||||
//Dynamic include columns
|
||||
foreach (var include in includes)
|
||||
query = query.Include(include);
|
||||
|
||||
query = query
|
||||
.AsNoTracking()
|
||||
//.Include(c => c.Creator)
|
||||
//.Include(c => c.Updator)
|
||||
.Where(predicate);
|
||||
|
||||
return query.ToList();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Manipulation Functions
|
||||
public T Insert(T entity)
|
||||
{
|
||||
var userLogin = GetLoginProfile();
|
||||
|
||||
entity = SetCreatorProperties(entity);
|
||||
entity = SetUpdatorProperties(entity);
|
||||
entity = SetActiveProperty(entity);
|
||||
|
||||
var result = Entities.Add(entity);
|
||||
|
||||
if(!SaveToDB())
|
||||
throw new NotificationException($"Unable to add new item to database.");
|
||||
|
||||
return Get(result.Entity.id);
|
||||
}
|
||||
public void Insert(IEnumerable<T> list)
|
||||
{
|
||||
var userLogin = GetLoginProfile();
|
||||
|
||||
var insertedList = new List<T>();
|
||||
foreach(var entity in list)
|
||||
{
|
||||
var newItem = entity;
|
||||
newItem = SetCreatorProperties(newItem);
|
||||
newItem = SetUpdatorProperties(newItem);
|
||||
newItem = SetActiveProperty(newItem);
|
||||
|
||||
insertedList.Add(newItem);
|
||||
}
|
||||
|
||||
Entities.AddRange(insertedList);
|
||||
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);
|
||||
|
||||
if(existingItem == null)
|
||||
throw new NotificationException($"No item in database.");
|
||||
|
||||
Mapper.Map(model, existingItem);
|
||||
|
||||
existingItem = SetUpdatorProperties(existingItem);
|
||||
|
||||
if (!SaveToDB())
|
||||
throw new NotificationException($"Unable to save item to database.");
|
||||
|
||||
return existingItem;
|
||||
}
|
||||
public T SetAsInActive(Key id)
|
||||
{
|
||||
var userLogin = GetLoginProfile();
|
||||
|
||||
var existingItem = Get(id);
|
||||
|
||||
if (existingItem == null)
|
||||
throw new NotificationException($"No item in database.");
|
||||
|
||||
existingItem = SetUpdatorProperties(existingItem);
|
||||
existingItem = SetInActiveProperty(existingItem);
|
||||
|
||||
if (!SaveToDB())
|
||||
throw new NotificationException($"Unable to set item as inactive to database.");
|
||||
|
||||
return existingItem;
|
||||
}
|
||||
public T SetAsActive(Key id)
|
||||
{
|
||||
var userLogin = GetLoginProfile();
|
||||
|
||||
var existingItem = Get(id);
|
||||
|
||||
if (existingItem == null)
|
||||
throw new NotificationException($"No item in database.");
|
||||
|
||||
existingItem = SetUpdatorProperties(existingItem);
|
||||
existingItem = SetActiveProperty(existingItem);
|
||||
|
||||
if (!SaveToDB())
|
||||
throw new NotificationException($"Unable to set item as active to database.");
|
||||
|
||||
return existingItem;
|
||||
}
|
||||
public void Delete(Key id)
|
||||
{
|
||||
var userLogin = GetLoginProfile();
|
||||
|
||||
var existingItem = Get(id);
|
||||
|
||||
if (existingItem == null)
|
||||
throw new NotificationException($"No item in database.");
|
||||
|
||||
Entities.Remove(existingItem);
|
||||
|
||||
if (!SaveToDB())
|
||||
throw new NotificationException($"Unable to delete item from database.");
|
||||
|
||||
return;
|
||||
}
|
||||
public void Delete(Expression<Func<T, bool>> predicate)
|
||||
{
|
||||
var userLogin = GetLoginProfile();
|
||||
|
||||
Entities.RemoveRange(Entities.Where(predicate));
|
||||
if (!SaveToDB())
|
||||
throw new NotificationException($"Unable to delete items from database.");
|
||||
}
|
||||
|
||||
public void InsertWithoutCommit(T entity)
|
||||
{
|
||||
var userLogin = GetLoginProfile();
|
||||
|
||||
entity = SetCreatorProperties(entity);
|
||||
entity = SetUpdatorProperties(entity);
|
||||
entity = SetActiveProperty(entity);
|
||||
|
||||
var result = Entities.Add(entity);
|
||||
}
|
||||
|
||||
public void UpdateWithoutCommit(Key id, object model)
|
||||
{
|
||||
var userLogin = GetLoginProfile();
|
||||
|
||||
var existingItem = Get(id);
|
||||
|
||||
if (existingItem == null)
|
||||
throw new NotificationException($"No item in database.");
|
||||
|
||||
Mapper.Map(model, existingItem);
|
||||
|
||||
existingItem = SetUpdatorProperties(existingItem);
|
||||
}
|
||||
|
||||
public void DeleteWithoutCommit(Key id)
|
||||
{
|
||||
var userLogin = GetLoginProfile();
|
||||
|
||||
var existingItem = Get(id);
|
||||
|
||||
if (existingItem == null)
|
||||
throw new NotificationException($"No item in database.");
|
||||
|
||||
Entities.Remove(existingItem);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
359
EF/_BaseRepository2.cs
Normal file
359
EF/_BaseRepository2.cs
Normal file
@@ -0,0 +1,359 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Transactions;
|
||||
using AutoMapper;
|
||||
using System.Linq.Expressions;
|
||||
using TTSW.Common;
|
||||
using TodoAPI2.Models;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using IdentityModel;
|
||||
|
||||
namespace TTSW.EF
|
||||
{
|
||||
public class BaseRepository2<T, Key> : IBaseRepository2<T, Key>
|
||||
where T : class, IBaseEntity2<Key>
|
||||
{
|
||||
#region Private Variables
|
||||
private DataContext _context;
|
||||
public DataContext Context
|
||||
{
|
||||
get
|
||||
{
|
||||
return _context;
|
||||
}
|
||||
}
|
||||
|
||||
private IHttpContextAccessor _contextAccessor;
|
||||
private LoginProfile _loginProfile;
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
private DbSet<T> _entities;
|
||||
public DbSet<T> Entities {
|
||||
get {
|
||||
return _entities ?? (_entities = _context.Set<T>()); }
|
||||
}
|
||||
#endregion
|
||||
|
||||
public BaseRepository2(DataContext context, IHttpContextAccessor contextAccessor
|
||||
)
|
||||
{
|
||||
_context = context;
|
||||
_contextAccessor = contextAccessor;
|
||||
}
|
||||
|
||||
#region Private Functions
|
||||
private bool SaveToDB()
|
||||
{
|
||||
return (_context.SaveChanges() >= 0);
|
||||
}
|
||||
private T SetCreatorProperties(T entity)
|
||||
{
|
||||
//entity.CreatorId = _userRepository.LoginProfile.Id;
|
||||
entity.created = DateTime.Now;
|
||||
|
||||
return entity;
|
||||
}
|
||||
private T SetUpdatorProperties(T entity)
|
||||
{
|
||||
//entity.UpdatorId = _userRepository.LoginProfile.Id;
|
||||
entity.updated = DateTime.Now;
|
||||
|
||||
return entity;
|
||||
}
|
||||
private T SetActiveProperty(T entity)
|
||||
{
|
||||
entity.isActive = true;
|
||||
return entity;
|
||||
}
|
||||
private T SetInActiveProperty(T entity)
|
||||
{
|
||||
entity.isActive = false;
|
||||
return entity;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Public Functions
|
||||
|
||||
public LoginProfile GetLoginProfile()
|
||||
{
|
||||
// Set login profile in the 1st accessing time only
|
||||
// The user has to login to the system too.
|
||||
if (_loginProfile == null && _contextAccessor.HttpContext.User.Identity.IsAuthenticated)
|
||||
{
|
||||
var dict = new Dictionary<string, string>();
|
||||
|
||||
// A list to keep list of all keys for getting user user claims
|
||||
var userProfileClaimsKey = new List<string>();
|
||||
userProfileClaimsKey.Add(JwtClaimTypes.GivenName);
|
||||
userProfileClaimsKey.Add(JwtClaimTypes.FamilyName);
|
||||
userProfileClaimsKey.Add(JwtClaimTypes.Email);
|
||||
userProfileClaimsKey.Add("customer_guid");
|
||||
userProfileClaimsKey.Add("user_guid");
|
||||
userProfileClaimsKey.Add("permission_level");
|
||||
userProfileClaimsKey.Add("base_system");
|
||||
userProfileClaimsKey.Add(JwtClaimTypes.Name);
|
||||
userProfileClaimsKey.Add(JwtClaimTypes.PreferredUserName);
|
||||
userProfileClaimsKey.Add(JwtClaimTypes.PhoneNumber);
|
||||
|
||||
// Fetch only claims related to user profile
|
||||
_contextAccessor.HttpContext.User.Claims
|
||||
.Where(c=> userProfileClaimsKey.Contains(c.Type)).ToList()
|
||||
.ForEach(item => dict.Add(item.Type, item.Value));
|
||||
|
||||
// Map claims to model
|
||||
_loginProfile = new LoginProfile();
|
||||
|
||||
//if (dict.ContainsKey(JwtClaimTypes.Subject))
|
||||
// _loginProfile.Id = dict[JwtClaimTypes.Subject];
|
||||
|
||||
if (dict.ContainsKey(JwtClaimTypes.GivenName) && !string.IsNullOrEmpty(dict[JwtClaimTypes.GivenName]))
|
||||
_loginProfile.FirstName = dict[JwtClaimTypes.GivenName];
|
||||
|
||||
if (dict.ContainsKey(JwtClaimTypes.FamilyName) && !string.IsNullOrEmpty(dict[JwtClaimTypes.FamilyName]))
|
||||
_loginProfile.LastName = dict[JwtClaimTypes.FamilyName];
|
||||
|
||||
if (dict.ContainsKey(JwtClaimTypes.Email) && !string.IsNullOrEmpty(dict[JwtClaimTypes.Email]))
|
||||
_loginProfile.Email = dict[JwtClaimTypes.Email];
|
||||
|
||||
if (dict.ContainsKey("customer_guid") && !string.IsNullOrEmpty(dict["customer_guid"]))
|
||||
_loginProfile.CustomerGUID = new Guid(dict["customer_guid"]);
|
||||
|
||||
if (dict.ContainsKey("user_guid") && !string.IsNullOrEmpty(dict["user_guid"]))
|
||||
_loginProfile.UserGUID = new Guid(dict["user_guid"]);
|
||||
|
||||
if (dict.ContainsKey("permission_level") && !string.IsNullOrEmpty(dict["permission_level"]))
|
||||
_loginProfile.permission_level = dict["permission_level"];
|
||||
|
||||
if (dict.ContainsKey("base_system") && !string.IsNullOrEmpty(dict["base_system"]))
|
||||
_loginProfile.base_system = dict["base_system"];
|
||||
|
||||
if (string.IsNullOrEmpty(_loginProfile.base_system))
|
||||
{
|
||||
_loginProfile.base_system = "";
|
||||
}
|
||||
|
||||
if (dict.ContainsKey(JwtClaimTypes.Name) && !string.IsNullOrEmpty(dict[JwtClaimTypes.Name]))
|
||||
_loginProfile.UserName = dict[JwtClaimTypes.Name];
|
||||
else if (dict.ContainsKey(JwtClaimTypes.PreferredUserName) && !string.IsNullOrEmpty(dict[JwtClaimTypes.PreferredUserName]))
|
||||
_loginProfile.UserName = dict[JwtClaimTypes.PreferredUserName];
|
||||
|
||||
if (dict.ContainsKey(JwtClaimTypes.PhoneNumber) && !string.IsNullOrEmpty(dict[JwtClaimTypes.PhoneNumber]))
|
||||
_loginProfile.PhoneNumber = dict[JwtClaimTypes.PhoneNumber];
|
||||
}
|
||||
|
||||
return _loginProfile;
|
||||
}
|
||||
|
||||
#region Query Functions
|
||||
public T Get(Key id)
|
||||
{
|
||||
return Entities
|
||||
//.Include(c => c.Creator)
|
||||
//.Include(c => c.Updator)
|
||||
.FirstOrDefault(c=>c.id.Equals(id));
|
||||
}
|
||||
public T Get(Key id, params Expression<Func<T, object>>[] includes)
|
||||
{
|
||||
var query = Entities.AsQueryable();
|
||||
|
||||
//Dynamic include columns
|
||||
foreach (var include in includes)
|
||||
query = query.Include(include);
|
||||
|
||||
return query
|
||||
//.Include(c => c.Creator)
|
||||
//.Include(c => c.Updator)
|
||||
.FirstOrDefault(c => c.id.Equals(id));
|
||||
}
|
||||
|
||||
public IQueryable<T> GetQuery()
|
||||
{
|
||||
return Entities.AsNoTracking();
|
||||
}
|
||||
public List<T> GetList(Expression<Func<T, bool>> predicate)
|
||||
{
|
||||
var query = Entities
|
||||
.AsNoTracking()
|
||||
//.Include(c => c.Creator)
|
||||
//.Include(c => c.Updator)
|
||||
.Where(predicate);
|
||||
|
||||
return query.ToList();
|
||||
}
|
||||
public List<T> GetList(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includes)
|
||||
{
|
||||
var query = Entities.AsQueryable();
|
||||
|
||||
//Dynamic include columns
|
||||
foreach (var include in includes)
|
||||
query = query.Include(include);
|
||||
|
||||
query = query
|
||||
.AsNoTracking()
|
||||
//.Include(c => c.Creator)
|
||||
//.Include(c => c.Updator)
|
||||
.Where(predicate);
|
||||
|
||||
return query.ToList();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Manipulation Functions
|
||||
public T Insert(T entity)
|
||||
{
|
||||
var userLogin = GetLoginProfile();
|
||||
|
||||
entity = SetCreatorProperties(entity);
|
||||
entity = SetUpdatorProperties(entity);
|
||||
entity = SetActiveProperty(entity);
|
||||
|
||||
var result = Entities.Add(entity);
|
||||
|
||||
if(!SaveToDB())
|
||||
throw new NotificationException($"Unable to add new item to database.");
|
||||
|
||||
return Get(result.Entity.id);
|
||||
}
|
||||
public void Insert(IEnumerable<T> list)
|
||||
{
|
||||
var userLogin = GetLoginProfile();
|
||||
|
||||
var insertedList = new List<T>();
|
||||
foreach(var entity in list)
|
||||
{
|
||||
var newItem = entity;
|
||||
newItem = SetCreatorProperties(newItem);
|
||||
newItem = SetUpdatorProperties(newItem);
|
||||
newItem = SetActiveProperty(newItem);
|
||||
|
||||
insertedList.Add(newItem);
|
||||
}
|
||||
|
||||
Entities.AddRange(insertedList);
|
||||
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);
|
||||
|
||||
if(existingItem == null)
|
||||
throw new NotificationException($"No item in database.");
|
||||
|
||||
Mapper.Map(model, existingItem);
|
||||
|
||||
existingItem = SetUpdatorProperties(existingItem);
|
||||
|
||||
if (!SaveToDB())
|
||||
throw new NotificationException($"Unable to save item to database.");
|
||||
|
||||
return existingItem;
|
||||
}
|
||||
public T SetAsInActive(Key id)
|
||||
{
|
||||
var userLogin = GetLoginProfile();
|
||||
|
||||
var existingItem = Get(id);
|
||||
|
||||
if (existingItem == null)
|
||||
throw new NotificationException($"No item in database.");
|
||||
|
||||
existingItem = SetUpdatorProperties(existingItem);
|
||||
existingItem = SetInActiveProperty(existingItem);
|
||||
|
||||
if (!SaveToDB())
|
||||
throw new NotificationException($"Unable to set item as inactive to database.");
|
||||
|
||||
return existingItem;
|
||||
}
|
||||
public T SetAsActive(Key id)
|
||||
{
|
||||
var userLogin = GetLoginProfile();
|
||||
|
||||
var existingItem = Get(id);
|
||||
|
||||
if (existingItem == null)
|
||||
throw new NotificationException($"No item in database.");
|
||||
|
||||
existingItem = SetUpdatorProperties(existingItem);
|
||||
existingItem = SetActiveProperty(existingItem);
|
||||
|
||||
if (!SaveToDB())
|
||||
throw new NotificationException($"Unable to set item as active to database.");
|
||||
|
||||
return existingItem;
|
||||
}
|
||||
public void Delete(Key id)
|
||||
{
|
||||
var userLogin = GetLoginProfile();
|
||||
|
||||
var existingItem = Get(id);
|
||||
|
||||
if (existingItem == null)
|
||||
throw new NotificationException($"No item in database.");
|
||||
|
||||
Entities.Remove(existingItem);
|
||||
|
||||
if (!SaveToDB())
|
||||
throw new NotificationException($"Unable to delete item from database.");
|
||||
|
||||
return;
|
||||
}
|
||||
public void Delete(Expression<Func<T, bool>> predicate)
|
||||
{
|
||||
var userLogin = GetLoginProfile();
|
||||
|
||||
Entities.RemoveRange(Entities.Where(predicate));
|
||||
if (!SaveToDB())
|
||||
throw new NotificationException($"Unable to delete items from database.");
|
||||
}
|
||||
|
||||
public void InsertWithoutCommit(T entity)
|
||||
{
|
||||
var userLogin = GetLoginProfile();
|
||||
|
||||
entity = SetCreatorProperties(entity);
|
||||
entity = SetUpdatorProperties(entity);
|
||||
entity = SetActiveProperty(entity);
|
||||
|
||||
var result = Entities.Add(entity);
|
||||
}
|
||||
|
||||
public void UpdateWithoutCommit(Key id, object model)
|
||||
{
|
||||
var userLogin = GetLoginProfile();
|
||||
|
||||
var existingItem = Get(id);
|
||||
|
||||
if (existingItem == null)
|
||||
throw new NotificationException($"No item in database.");
|
||||
|
||||
Mapper.Map(model, existingItem);
|
||||
|
||||
existingItem = SetUpdatorProperties(existingItem);
|
||||
}
|
||||
|
||||
public void DeleteWithoutCommit(Key id)
|
||||
{
|
||||
var userLogin = GetLoginProfile();
|
||||
|
||||
var existingItem = Get(id);
|
||||
|
||||
if (existingItem == null)
|
||||
throw new NotificationException($"No item in database.");
|
||||
|
||||
Entities.Remove(existingItem);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
19
EF/_BaseViewModel.cs
Normal file
19
EF/_BaseViewModel.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TTSW.EF
|
||||
{
|
||||
public class BaseViewModel<Key> : IBaseViewModel<Key>
|
||||
{
|
||||
public Key id {get; set;}
|
||||
//public string CreatorId {get; set;}
|
||||
//public string CreatorFullname {get; set;}
|
||||
public DateTime Created {get; set;}
|
||||
//public string UpdatorId {get; set;}
|
||||
//public string UpdatorFullname {get; set;}
|
||||
public DateTime Updated {get; set;}
|
||||
public bool isActive { get; set;}
|
||||
}
|
||||
}
|
||||
16
EF/_BaseViewModel2.cs
Normal file
16
EF/_BaseViewModel2.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TTSW.EF
|
||||
{
|
||||
public class BaseViewModel2<Key> : IBaseViewModel2<Key>
|
||||
{
|
||||
public Key id { get; set; }
|
||||
public DateTime Created {get; set;}
|
||||
|
||||
public DateTime Updated {get; set;}
|
||||
public bool isActive { get; set;}
|
||||
}
|
||||
}
|
||||
25
EF/_IBaseEntity.cs
Normal file
25
EF/_IBaseEntity.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TTSW.EF
|
||||
{
|
||||
public interface IBaseEntity<Key>
|
||||
{
|
||||
Key id { get; set; }
|
||||
|
||||
//string CreatorId { get; set; }
|
||||
//User Creator { get; set; }
|
||||
DateTime created { get; set; }
|
||||
|
||||
//string UpdatorId { get; set; }
|
||||
//User Updator { get; set; }
|
||||
DateTime updated { get; set; }
|
||||
|
||||
bool isActive { get; set; }
|
||||
|
||||
//string CreatorFullname { get; }
|
||||
//string UpdatorFullname { get; }
|
||||
}
|
||||
}
|
||||
19
EF/_IBaseEntity2.cs
Normal file
19
EF/_IBaseEntity2.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TTSW.EF
|
||||
{
|
||||
public interface IBaseEntity2<Key>
|
||||
{
|
||||
Key id { get; set; }
|
||||
|
||||
DateTime created { get; set; }
|
||||
|
||||
DateTime updated { get; set; }
|
||||
|
||||
bool isActive { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
41
EF/_IBaseRepository.cs
Normal file
41
EF/_IBaseRepository.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace TTSW.EF
|
||||
{
|
||||
public interface IBaseRepository<T, Key>
|
||||
where T : class, IBaseEntity<Key>
|
||||
{
|
||||
#region Property
|
||||
DbSet<T> Entities { get; }
|
||||
DataContext Context { get; }
|
||||
#endregion
|
||||
|
||||
#region Query Functions
|
||||
T Get(Key id);
|
||||
T Get(Key id, params Expression<Func<T, object>>[] includes);
|
||||
IQueryable<T> GetQuery();
|
||||
List<T> GetList(Expression<Func<T, bool>> predicate);
|
||||
List<T> GetList(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includes);
|
||||
#endregion
|
||||
|
||||
#region Manipulation Functions
|
||||
T Insert(T entity);
|
||||
void Insert(IEnumerable<T> list);
|
||||
T Update(Key id, object model);
|
||||
T SetAsInActive(Key id);
|
||||
T SetAsActive(Key id);
|
||||
void Delete(Key id);
|
||||
void Delete(Expression<Func<T, bool>> predicate);
|
||||
|
||||
void InsertWithoutCommit(T entity);
|
||||
void UpdateWithoutCommit(Key id, object model);
|
||||
void DeleteWithoutCommit(Key id);
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
41
EF/_IBaseRepository2.cs
Normal file
41
EF/_IBaseRepository2.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace TTSW.EF
|
||||
{
|
||||
public interface IBaseRepository2<T, Key>
|
||||
where T : class, IBaseEntity2<Key>
|
||||
{
|
||||
#region Property
|
||||
DbSet<T> Entities { get; }
|
||||
DataContext Context { get; }
|
||||
#endregion
|
||||
|
||||
#region Query Functions
|
||||
T Get(Key id);
|
||||
T Get(Key id, params Expression<Func<T, object>>[] includes);
|
||||
IQueryable<T> GetQuery();
|
||||
List<T> GetList(Expression<Func<T, bool>> predicate);
|
||||
List<T> GetList(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includes);
|
||||
#endregion
|
||||
|
||||
#region Manipulation Functions
|
||||
T Insert(T entity);
|
||||
void Insert(IEnumerable<T> list);
|
||||
T Update(Key id, object model);
|
||||
T SetAsInActive(Key id);
|
||||
T SetAsActive(Key id);
|
||||
void Delete(Key id);
|
||||
void Delete(Expression<Func<T, bool>> predicate);
|
||||
|
||||
void InsertWithoutCommit(T entity);
|
||||
void UpdateWithoutCommit(Key id, object model);
|
||||
void DeleteWithoutCommit(Key id);
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
22
EF/_IBaseService.cs
Normal file
22
EF/_IBaseService.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TTSW.EF
|
||||
{
|
||||
public interface IBaseService<Key, InputModel, ViewModel>
|
||||
{
|
||||
#region Query Functions
|
||||
ViewModel Get(Key id);
|
||||
#endregion
|
||||
|
||||
#region Manipulation Functions
|
||||
ViewModel Insert(InputModel model);
|
||||
ViewModel Update(Key id, InputModel model);
|
||||
ViewModel SetAsActive(Key id);
|
||||
ViewModel SetAsInactive(Key id);
|
||||
void Delete(Key id);
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
23
EF/_IBaseViewModel.cs
Normal file
23
EF/_IBaseViewModel.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TTSW.EF
|
||||
{
|
||||
public interface IBaseViewModel<Key>
|
||||
{
|
||||
Key id { get; set; }
|
||||
|
||||
//string CreatorId { get; set; }
|
||||
//string CreatorFullname { get; set; }
|
||||
DateTime Created { get; set; }
|
||||
|
||||
//string UpdatorId { get; set; }
|
||||
//string UpdatorFullname { get; set; }
|
||||
DateTime Updated { get; set; }
|
||||
|
||||
bool isActive { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
18
EF/_IBaseViewModel2.cs
Normal file
18
EF/_IBaseViewModel2.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TTSW.EF
|
||||
{
|
||||
public interface IBaseViewModel2<Key>
|
||||
{
|
||||
|
||||
DateTime Created { get; set; }
|
||||
|
||||
DateTime Updated { get; set; }
|
||||
|
||||
bool isActive { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user