using GB5Shared.DaprCache;
using GB5Shared.DTO.Framework.Login;
using GB5Shared.DTO.Framework.User;
using GB5Shared.QueryExecutor;
using PayRollDAL.Query.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static GB5Shared.GB5Constant.Constant;
namespace PayRollDAL.CustomeCode.User
{
public class UserDAL : IUserDAL
{
private readonly IQueryExecutor _queryExecutor;
private readonly Dapr.Client.DaprClient _daprClient;
///
/// Initializes a new instance of the class.
///
/// The query executor.
/// The Dapr client.
public UserDAL(IQueryExecutor queryExecutor, Dapr.Client.DaprClient DaprClient)
{
_queryExecutor = queryExecutor;
_daprClient = DaprClient;
}
///
/// Saves the user.
///
/// The user DTO.
/// The login DTO.
/// The number of affected rows.
public async Task SaveUser(UserDTO UserDTO, LoginDTO LoginDTO)
{
try
{
const string Sql = UserQB.SAVE_USER;
return await _queryExecutor.ExecuteAsync(LoginDTO, Sql, UserDTO);
}
catch (Exception ex)
{
throw new Exception("Error while saving user", ex);
}
}
///
/// Updates the user.
///
/// The user DTO.
/// The login DTO.
/// The number of affected rows.
public async Task UpdateUser(UserDTO UserDTO, LoginDTO LoginDTO)
{
CacheKeyGeneration Key = new();
try
{
string key = Key.KeyGeneration(UserDTO.UserId, EntityConstant.USER, CacheKeyLevel.USER_LEVEL, LoginDTO);
await _daprClient.DeleteStateAsync("statestore", key);
const string Sql = UserQB.UPDATE_USER;
return await _queryExecutor.ExecuteAsync(LoginDTO, Sql, UserDTO);
}
catch (Exception ex)
{
throw new Exception("Error while saving user", ex);
}
}
}
}