using System.Data;
using Dapper;
using JdeScoping.Core.Models.Enums;
using JdeScoping.Core.Models.Search;
using JdeScoping.DataAccess.Queries;
using Microsoft.Data.SqlClient;
namespace JdeScoping.DataAccess.Repositories;
///
/// Search management operations for LotFinder repository.
///
public partial class LotFinderRepository
{
///
public async Task> GetUserSearchesAsync(string userName, CancellationToken ct = default)
{
ArgumentException.ThrowIfNullOrWhiteSpace(userName);
return await ExecuteQueryAsync(
nameof(GetUserSearchesAsync),
"SQL_GET_USER_SEARCHES",
async connection => (await connection.QueryAsync(
LotFinderQueries.SqlGetUserSearches,
new { userName },
commandTimeout: _options.Value.DefaultTimeoutSeconds)).ToList(),
ct);
}
///
public async Task> GetQueuedSearchesAsync(CancellationToken ct = default)
{
return await ExecuteQueryAsync(
nameof(GetQueuedSearchesAsync),
"SQL_GET_QUEUED_SEARCHES",
async connection => (await connection.QueryAsync(
LotFinderQueries.SqlGetQueuedSearches,
commandTimeout: _options.Value.DefaultTimeoutSeconds)).ToList(),
ct);
}
///
public async Task GetSearchAsync(int id, CancellationToken ct = default)
{
return await ExecuteQueryAsync(
nameof(GetSearchAsync),
"SQL_GET_SEARCH",
async connection =>
{
var result = await connection.QueryFirstOrDefaultAsync(
LotFinderQueries.SqlGetSearch,
new { id },
commandTimeout: _options.Value.DefaultTimeoutSeconds);
if (result != null)
{
result.Id = id;
}
return result;
},
ct);
}
///
public async Task GetSearchResultsAsync(int id, CancellationToken ct = default)
{
return await ExecuteQueryAsync(
nameof(GetSearchResultsAsync),
"SQL_GET_SEARCH_RESULTS",
connection => connection.QueryFirstOrDefaultAsync(
LotFinderQueries.SqlGetSearchResults,
new { id },
commandTimeout: _options.Value.DefaultTimeoutSeconds),
ct);
}
///
public async Task SubmitSearchAsync(Search search, CancellationToken ct = default)
{
ArgumentNullException.ThrowIfNull(search);
search.Status = SearchStatus.Queued;
search.SubmitDt = DateTime.UtcNow;
return await ExecuteQueryAsync(
nameof(SubmitSearchAsync),
SqlObjects.SubmitSearch,
async connection =>
{
await using var command = new SqlCommand(SqlObjects.SubmitSearch, connection)
{
CommandType = CommandType.StoredProcedure,
CommandTimeout = _options.Value.DefaultTimeoutSeconds
};
command.Parameters.AddWithValue("p_UserName", search.UserName);
command.Parameters.AddWithValue("p_Name", search.Name);
command.Parameters.AddWithValue("p_Criteria", search.CriteriaJson);
var searchIdParam = new SqlParameter("o_SearchID", SqlDbType.Int)
{
Direction = ParameterDirection.Output
};
command.Parameters.Add(searchIdParam);
await command.ExecuteNonQueryAsync(ct);
return Convert.ToInt32(searchIdParam.Value);
},
ct);
}
}