Files
jdescopingtool/OLD/DataModel/Process/LotFinderDB.SearchManagement.cs
T
Joseph Doherty 7e92e35991 Point search pipeline at Search2 table and *2 procs for v5 POC
Re-target all search reads, writes, and stored proc calls to dbo.Search2 /
SubmitSearch2 / StartSearch2 / CompleteSearch2 / ResetPartialSearches2 so
the POC can run side-by-side with prod against the same QA database
without contending on the Search table or its procs. Standalone deployment
SQL for the new table and procs lives in OLD/sql/ and must be run against
the target DB before the POC apps are started.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 09:24:45 -04:00

220 lines
7.1 KiB
C#
Executable File

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using Dapper;
using DataModel.Helpers;
using DataModel.Models;
using Newtonsoft.Json;
namespace DataModel.Process
{
/// <summary>
/// Search management functionality of LotFinderDB interface
/// </summary>
public partial class LotFinderDB
{
/// <summary>
/// Query to get user's searches
/// </summary>
private const string SQL_GET_USER_SEARCHES = @"
SELECT s.ID,
s.Name,
s.Status,
s.SubmitDT,
s.StartDT,
s.EndDT
FROM dbo.Search2 s
WHERE s.UserName = @userName
ORDER BY s.SubmitDT";
/// <summary>
/// Gets user's searches
/// </summary>
/// <param name="userName">User name to match</param>
/// <returns>List of user's searches</returns>
public static List<Search> GetUserSearches(string userName)
{
List<Search> results = new List<Search>();
try
{
using (SqlConnection connection = GetConnection())
{
results.AddRange(connection.Query<Search>(SQL_GET_USER_SEARCHES, new {userName}));
}
}
catch (Exception error)
{
//Log but do not forward error
logger.Error("GetUserSearches: failed to get searches for user '{0}': {1}.", userName, error.Message);
}
return results;
}
/// <summary>
/// Query to get queued searches
/// </summary>
private const string SQL_GET_QUEUED_SEARCHES = @"
SELECT s.ID,
s.UserName,
s.Name,
s.Status,
s.SubmitDT,
s.StartDT,
s.EndDT
FROM dbo.Search2 s
WHERE s.Status < 3
ORDER BY s.SubmitDT";
/// <summary>
/// Gets queued searches
/// </summary>
/// <returns>List of searches that have been submitted but not completed</returns>
public static List<Search> GetQueuedSearches()
{
List<Search> results = new List<Search>();
try
{
using (SqlConnection connection = GetConnection())
{
results.AddRange(connection.Query<Search>(SQL_GET_QUEUED_SEARCHES));
}
}
catch (Exception error)
{
//Log but do not forward error
logger.Error("GetQueuedSearches: failed to get queued searches: {0}.", error.Message);
}
return results;
}
/// <summary>
/// Query to get search with matching ID
/// </summary>
private const string SQL_GET_SEARCH = @"
SELECT s.UserName,
s.Name,
s.Status,
s.SubmitDT,
s.StartDT,
s.EndDT,
s.Criteria as CriteriaJSON
FROM dbo.Search2 s
WHERE s.ID = @id";
/// <summary>
/// Gets the search with matching ID
/// </summary>
/// <param name="id">Search ID to match</param>
/// <returns>Search with matching ID</returns>
public static Search GetSearch(int id)
{
Search result = null;
try
{
using (SqlConnection connection = GetConnection())
{
result = connection.QueryFirstOrDefault<Search>(SQL_GET_SEARCH, new {id});
if (result != null && !string.IsNullOrEmpty(result.CriteriaJSON))
{
result.ID = id;
result.Criteria = JsonConvert.DeserializeObject<SearchCriteria>(result.CriteriaJSON);
}
}
}
catch (Exception error)
{
//Log but do not forward error
logger.Error("GetSearch: failed to get search with ID '{0}': {1}.", id, error.Message);
}
return result;
}
/// <summary>
/// Query to get the saved results for the search with matching ID
/// </summary>
private const string SQL_GET_SEARCH_RESULTS = @"
SELECT s.Results
FROM dbo.Search2 AS s
WHERE s.ID = @id";
/// <summary>
/// Gets the saved results for the search with matching ID
/// </summary>
/// <param name="id">Search ID to match</param>
/// <returns>Raw data for saved search results</returns>
public static byte[] GetSearchResults(int id)
{
byte[] result = null;
try
{
using (SqlConnection connection = GetConnection())
{
result = connection.Query<byte[]>(SQL_GET_SEARCH_RESULTS, new {id}).FirstOrDefault();
}
}
catch (Exception error)
{
//Log but do not forward error
logger.Error("GetSearchResults: failed to get results for search with ID '{0}': {1}.", id, error.Message);
}
return result;
}
/// <summary>
/// Saves the search to the database
/// </summary>
/// <param name="search">Search to submit</param>
/// <returns>PK ID of search record generated</returns>
public static int SubmitSearch(Search search)
{
int searchID = -1;
try
{
search.Status = SearchStatus.Submitted;
search.SubmitDT = DateTime.Now;
using (SqlConnection connection = GetConnection())
{
using (SqlCommand command = new SqlCommand("SubmitSearch2", connection))
{
command.CommandType = CommandType.StoredProcedure;
//Bind parameters
command.Bind("p_UserName", search.UserName);
command.Bind("p_Name", search.Name);
command.Bind("p_Criteria", search.Criteria.ToJSON());
SqlParameter searchIDParameter = new SqlParameter("o_SearchID", SqlDbType.Int)
{
Direction = ParameterDirection.Output
};
command.Parameters.Add(searchIDParameter);
//Execute procedure and extract values
command.ExecuteNonQuery();
searchID = Convert.ToInt32(searchIDParameter.Value);
}
}
}
catch (Exception error)
{
//Log but do not forward error
logger.Error("SubmitSearch: failed to get submit search: {0}.", error.Message);
}
return searchID;
}
}
}