HRM-10/HRM.DA/Service/Fund/FMFundUserPermissionService.cs
Mohaimen Hasan 838b8d4480 first commit
2026-11-03 13:06:00 +06:00

117 lines
3.7 KiB
C#

using System;
using System.Collections.Generic;
using HRM.BO;
using Ease.Core.DataAccess;
using Ease.Core.Utility;
using System.Data;
using Ease.Core.Model;
namespace HRM.DA
{
public class FMFundUserPermissionService : ServiceTemplate, IFundUserPermissionService
{
public FMFundUserPermissionService()
{
}
private void MapObject(FMFundUserPermission item, DataReader dr)
{
// Map ID using ServiceTemplate helper
base.SetObjectID(item, dr.GetInt32("ID").Value);
item.UserID = dr.GetInt32("UserID", 0);
item.FundTypeID = dr.GetInt32("FundTypeID", 0);
// map audit fields if present
item.CreatedBy = dr.GetInt32("CreatedBy", 0);
item.CreatedDate = dr.GetDateTime("CreatedDate").Value;
item.ModifiedBy = dr.GetInt32("ModifiedBy", 0);
item.ModifiedDate = dr.GetDateTime("ModifiedDate");
this.SetObjectState(item, Ease.Core.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader dr)
{
FMFundUserPermission item = new FMFundUserPermission();
MapObject(item, dr);
return item as T;
}
public List<FMFundUserPermission> GetByUserID(int userID)
{
List<FMFundUserPermission> list = new List<FMFundUserPermission>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(FMFundUserPermissionDA.GetByUserID(tc, userID));
list = this.CreateObjects<FMFundUserPermission>(dr);
dr.Close();
tc.End();
}
catch (Exception e)
{
if (tc != null) tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
}
return list;
}
public void SavePermissions(List<FMFundUserPermission> permissions, int performedBy)
{
if (permissions == null) throw new ArgumentNullException(nameof(permissions));
// If empty, delete existing entries for the user and return
if (permissions.Count == 0)
{
return;
}
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
int userId = permissions[0].UserID;
// Remove existing permissions for the user
FMFundUserPermissionDA.DeleteByUserID(tc, userId);
// Insert each permission
foreach (var p in permissions)
{
// set audit fields
p.CreatedBy = performedBy;
p.CreatedDate = DateTime.Now;
FMFundUserPermissionDA.Insert(tc, p);
}
tc.End();
}
catch (Exception e)
{
if (tc != null) tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
}
}
public void DeleteByUserID(int userID)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
FMFundUserPermissionDA.DeleteByUserID(tc, userID);
tc.End();
}
catch (Exception e)
{
if (tc != null) tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
}
}
}
}