@@ -131,8 +131,66 @@ public IEnumerable<SqlPromptEntity> Load()
131131 }
132132
133133 /// <inheritdoc />
134- public Task < SqlPromptEntity ? > GetLatestPromptByName ( string promptName )
134+ public async Task < SqlPromptEntity ? > GetLatestPromptByName ( string promptName )
135135 {
136- throw new NotImplementedException ( ) ;
136+ // Load the SQL query to fetch the latest prompt by name
137+ string ? query = DatabaseConfigReader . LoadQuery ( "GetLatestPromptByName.sql" ) ;
138+ if ( string . IsNullOrEmpty ( query ) )
139+ {
140+ throw new InvalidOperationException (
141+ "Failed to load SQL query file 'GetLatestPromptByName.sql'. The file could not be found or loaded." ) ;
142+ }
143+
144+ // Retrieve the prompt row
145+ var prompt = await _connection . QueryFirstOrDefaultAsync < SqlPromptEntity > (
146+ query ,
147+ new { PromptName = promptName }
148+ ) ;
149+
150+ if ( prompt == null )
151+ {
152+ return null ;
153+ }
154+
155+ // Query to fetch parameters and defaults for the latest version of this prompt
156+ const string parameterQuery = @"
157+ WITH LatestVersion AS (
158+ SELECT MAX(VersionNumber) AS VersionNumber
159+ FROM PromptParameters
160+ WHERE PromptId = @PromptId
161+ )
162+ SELECT pp.ParameterName, pp.ParameterValue, pd.DefaultValue
163+ FROM PromptParameters pp
164+ LEFT JOIN ParameterDefaults pd ON pp.ParameterId = pd.ParameterId AND pp.VersionNumber = pd.VersionNumber
165+ CROSS JOIN LatestVersion
166+ WHERE pp.PromptId = @PromptId
167+ AND pp.VersionNumber = LatestVersion.VersionNumber;" ;
168+
169+ var parameters = await _connection . QueryAsync < PromptParameter > (
170+ parameterQuery ,
171+ new { PromptId = prompt . PromptId }
172+ ) ;
173+
174+ prompt . Parameters = new Dictionary < string , string > ( ) ;
175+ prompt . Default = new Dictionary < string , object > ( ) ;
176+
177+ foreach ( var param in parameters )
178+ {
179+ if ( string . IsNullOrEmpty ( param . ParameterName ) ) return prompt ;
180+ if ( ! prompt . Parameters . ContainsKey ( param . ParameterName ) )
181+ {
182+ prompt . Parameters . Add ( param . ParameterName , param . ParameterValue ) ;
183+ }
184+
185+ if ( param . DefaultValue == null || prompt . Default . ContainsKey ( param . ParameterName ) ) return prompt ;
186+ prompt . Parameters . TryAdd ( param . ParameterName , param . ParameterValue ) ;
187+
188+ if ( param . DefaultValue != null )
189+ {
190+ prompt . Default . TryAdd ( param . ParameterName , param . DefaultValue ) ;
191+ }
192+ }
193+
194+ return prompt ;
137195 }
138196}
0 commit comments