using AdminDAL.DTO.FinanceBook;
using AdminDAL.Query.FinanceBook;
using GB5Shared.DTO.Framework.Criteria;
using GB5Shared.DTO.Framework.Login;
using GB5Shared.QueryExecutor;
using GB5Shared.Resource.GB5Solution;
using GB5Shared.ResponseStandard;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace AdminDAL.CustomCode.FinanceBook
{
public class FinanceBookDAL : IFinanceBookDAL
{
private readonly IQueryExecutor _QueryExecutor;
public FinanceBookDAL(IQueryExecutor QueryExecutor)
{
_QueryExecutor = QueryExecutor;
}
//
/// Retrieves a single FinanceBook record by its unique identifier.
///
/// The ID of the FinanceBook to retrieve.
/// The current user's login details.
/// A JSON string representing the FinanceBook details.
public async Task GetFinanceBook(int FinanceBookId, LoginDTO LoginDTO)
{
try
{
string Sql = FinanceBookQB.GET_FINANCEBOOK;
var parameters = new { FinanceBookId = FinanceBookId };
FinanceBookDTO financeBookDTO = await _QueryExecutor.QuerySingleAsync(LoginDTO, Sql, parameters);
return JsonConvert.SerializeObject(financeBookDTO);
}
catch (Exception)
{
throw;
}
}
///
/// Retrieves a list of FinanceBook records based on specified filter criteria.
/// Typically used for populating dropdowns or selection lists.
///
/// Filter criteria for selecting FinanceBooks.
/// The current user's login details.
/// A JSON string representing a list of FinanceBooks.
public async Task GetSelectListFinanceBook(CriteriaDTO CriteriaDTO, LoginDTO LoginDTO)
{
try
{
string sql = FinanceBookQB.GET_SELECTLIST_FINANCEBOOK;
var parameters = new { };
IEnumerable result = await _QueryExecutor.QueryAsync(LoginDTO, sql, parameters);
string json = JsonConvert.SerializeObject(result);
return json;
}
catch (Exception ex)
{
throw new Exception(AdminResource.SelectListFailed, ex);
}
}
///
/// Inserts a new FinanceBook record into the database.
/// Automatically sets created and modified metadata fields.
///
/// The FinanceBook data to insert.
/// The current user's login details.
/// The number of rows affected (should be 1 if successful).
public async Task SaveFinanceBook(FinanceBookDTO FinanceBookDTO, LoginDTO LoginDTO)
{
try
{
FinanceBookDTO.FinanceBookCreatedById = LoginDTO.UserId;
FinanceBookDTO.FinanceBookCreatedOn = DateTime.UtcNow;
FinanceBookDTO.FinanceBookModifiedById = LoginDTO.UserId;
FinanceBookDTO.FinanceBookModifiedOn = DateTime.UtcNow;
string Sql = FinanceBookQB.SAVE_FINANCEBOOK;
return await _QueryExecutor.ExecuteAsync(LoginDTO, Sql, FinanceBookDTO);
}
catch (Exception ex)
{
throw new Exception(AdminResource.SaveFailed, ex);
}
}
///
/// Updates an existing FinanceBook record in the database.
/// Automatically updates modified metadata fields.
///
/// The FinanceBook data to update.
/// The current user's login details.
/// The number of rows affected (should be 1 if successful).
public async Task UpdateFinanceBook(FinanceBookDTO FinanceBookDTO, LoginDTO LoginDTO)
{
try
{
FinanceBookDTO.FinanceBookModifiedById = LoginDTO.UserId;
FinanceBookDTO.FinanceBookModifiedOn = DateTime.UtcNow;
string Sql = FinanceBookQB.UPDATE_FINANCEBOOK;
return await _QueryExecutor.ExecuteAsync(LoginDTO, Sql, FinanceBookDTO);
}
catch (Exception ex)
{
throw new Exception(AdminResource.UpdateFailed, ex);
}
}
///
/// Deletes a FinanceBook record by its unique identifier.
///
/// The ID of the FinanceBook to delete.
/// The current user's login details.
/// A message string indicating success or failure of the delete operation.
public async Task DeleteFinanceBook(int FinanceBookId, LoginDTO LoginDTO)
{
try
{
string Sql = FinanceBookQB.DELETE_FINANCEBOOK;
var parameters = new { FinanceBookId = FinanceBookId };
int result = await _QueryExecutor.ExecuteAsync(LoginDTO, Sql, parameters);
if (result > 0)
{
return AdminResource.DeletedSuccessfully;
}
else
{
return AdminResource.NotFoundToDelete;
}
}
catch (Exception ex)
{
throw new Exception(AdminResource.DeleteFailed, ex);
}
}
}
}