using Dapper; using DealerSelection.Api.Infrastructure.Mulesoft; using DealerSelection.Api.Models; using DealerSelection.Common.Data.Dapper; using DealerSelection.Common.Exceptions; using Microsoft.Extensions.Logging; using System.Data; using System.Data.SqlClient; namespace DealerSelection.Api.Infrastructure.CustomerDetail; public class Repository : RepositoryBaseDapperAsync, IRepository { private readonly ILogger _logger; public Repository(string cxnName, ILogger logger) : base(cxnName) { _logger = logger; } public async Task UpdateDealerDetail(CustomerDealerInfoRequestDto dto) { try { using (SqlConnection cxn = await OpenCxnAsync()) { int receivedBookingId = await cxn.ExecuteScalarAsync(Procedure.UpdateDealerDetails, new { dto.BuId, dto.RecordId, dto.MobileNumber, dto.ModelCode, dto.ModelName, dto.ModelVariant, dto.DealerName, dto.DealerCode, dto.PinCode, dto.Latitude, dto.Longitude, dto.DealerSelectionJobStatus }, commandType: CommandType.StoredProcedure); return receivedBookingId; } } catch (Exception ex) { _logger.LogError($"BU:- {dto.BuId}. Error at Repository:CustomerDetail in UpdateDealerDetail for Request: {dto.RequestDetail()}.", ex.StackTrace); throw new UnexpectedDataException($"Error at Repository:CustomerDetail in UpdateDealerDetail for Request: {dto.RequestDetail()}.", ex); } } public async Task GetCustomerData(int buId, int recordId) { try { using (SqlConnection cxn = await OpenCxnAsync()) { IEnumerable cusotmerInfo = await cxn.QueryAsync(Procedure.GetCustomerDataForJob, commandType: CommandType.StoredProcedure); return cusotmerInfo.ToList().FirstOrDefault(); } } catch (Exception ex) { _logger.LogError("Error at Repository:CustomerDetail in UpdateDealerDetail for Request: {customerInfo.RequestDetail()}.", ex.StackTrace); throw new UnexpectedDataException($"Error at Repository:CustomerDetail in UpdateDealerDetail for Request: ", ex); } } public async Task IsDuplicateMobileNumber(int mobileNumber) { try { using (SqlConnection cxn = await OpenCxnAsync()) { bool isDuplicate = await cxn.QueryFirstAsync(Procedure.IsDuplicateMobileNumber, new { mobileNumber }, commandType: CommandType.StoredProcedure); return isDuplicate; } } catch (Exception ex) { _logger.LogError($"Error at Repository:CustomerDetail in IsDuplicateMobileNumber for MobileNumber: {mobileNumber}. ErrorMessage", ex.StackTrace); throw new DuplicateException($"Error at Repository:CustomerDetail in IsDuplicateMobileNumber for MobileNumber: {mobileNumber}."); } } public async Task UpdateDealerSelectionJobStatus(int buId, int recordId, int mobileNumber, DealerSelectionJobStatus dealerSelectionJobStatus) { try { using (SqlConnection cxn = await OpenCxnAsync()) { await cxn.QueryFirstAsync(Procedure.UpdateDealerSelectionJobStatus, new { buId, recordId, mobileNumber, dealerSelectionJobStatus }, commandType: CommandType.StoredProcedure); } } catch (Exception ex) { _logger.LogError($"Error at Repository:CustomerDetail in UpdateDealerSelectionJobStatus for BuId: {buId}, RecordId: {recordId}, MobileNumber: {mobileNumber}, DealerSelectionJobStatus: {dealerSelectionJobStatus}. ErrorMessage", ex.StackTrace); throw new DuplicateException($"Error at Repository:CustomerDetail in UpdateDealerSelectionJobStatus for BuId: {buId}, RecordId: {recordId}, MobileNumber: {mobileNumber}, DealerSelectionJobStatus: {dealerSelectionJobStatus}"); } } }