Error fixing with input validation
This commit is contained in:
parent
9445cec743
commit
a6654ef708
@ -90,60 +90,22 @@ namespace PrototypWPFHAG
|
||||
|
||||
try
|
||||
{
|
||||
var response = await _httpClient.GetAsync($"/documents/by-id/{documentId}");
|
||||
var response = await _httpClient.GetAsync($"/documents/{documentId}");
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
var result = JsonSerializer.Deserialize<JsonElement>(json);
|
||||
var result = JsonSerializer.Deserialize<DocumentDetail>(json);
|
||||
|
||||
var documents = result.GetProperty("documents");
|
||||
if (documents.GetArrayLength() == 0)
|
||||
{
|
||||
MessageBox.Show("Dokument nicht gefunden");
|
||||
return;
|
||||
}
|
||||
|
||||
var document = documents[0];
|
||||
|
||||
// Korrigierte UI-Aktualisierung
|
||||
await Dispatcher.InvokeAsync(() =>
|
||||
{
|
||||
SearchResultsListBox.ItemsSource = new List<DocumentDetail> // Änderung von SearchResult zu DocumentDetail
|
||||
{
|
||||
new DocumentDetail
|
||||
{
|
||||
Id = document.GetProperty("id").GetInt32(),
|
||||
DocumentName = document.GetProperty("document_name").GetString(), // Korrekter Property-Name
|
||||
Content = document.GetProperty("content").GetString()
|
||||
}
|
||||
};
|
||||
ContentTextBox.Text = document.GetProperty("content").GetString();
|
||||
|
||||
// Wichtig: DisplayMemberPath korrekt setzen
|
||||
SearchResultsListBox.ItemsSource = new List<DocumentDetail> { result };
|
||||
ContentTextBox.Text = result.Content;
|
||||
SearchResultsListBox.DisplayMemberPath = "DocumentName";
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
catch (HttpRequestException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
|
||||
{
|
||||
MessageBox.Show($"Fehler: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SearchByTextAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var encodedQuery = Uri.EscapeDataString(SearchTextBox.Text);
|
||||
var response = await _httpClient.GetAsync($"/documents/search?query={encodedQuery}");
|
||||
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
var result = JsonSerializer.Deserialize<JsonElement>(json);
|
||||
|
||||
var documents = JsonSerializer.Deserialize<List<SearchResult>>(
|
||||
result.GetProperty("documents").GetRawText());
|
||||
|
||||
MessageBox.Show($"Gefundene Dokumente: {documents?.Count}", "Debug");
|
||||
|
||||
SearchResultsListBox.ItemsSource = documents;
|
||||
SearchResultsListBox.DisplayMemberPath = "name";
|
||||
MessageBox.Show("Dokument nicht gefunden");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -151,22 +113,21 @@ namespace PrototypWPFHAG
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private async Task SearchBySimilarityAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var encodedQuery = Uri.EscapeDataString(SearchTextBox.Text);
|
||||
var response = await _httpClient.GetAsync($"/documents/similarity?query={encodedQuery}");
|
||||
var encodedQuery = HttpUtility.UrlEncode(SearchTextBox.Text);
|
||||
var response = await _httpClient.GetAsync($"/documents/search/similarity?query={encodedQuery}");
|
||||
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
var result = JsonSerializer.Deserialize<ApiResponse>(json, new JsonSerializerOptions
|
||||
{
|
||||
PropertyNameCaseInsensitive = true
|
||||
});
|
||||
var result = JsonSerializer.Deserialize<ApiResponse>(json);
|
||||
|
||||
SearchResultsListBox.ItemsSource = result?.Documents;
|
||||
SearchResultsListBox.DisplayMemberPath = "DocumentName";
|
||||
await Dispatcher.InvokeAsync(() =>
|
||||
{
|
||||
SearchResultsListBox.ItemsSource = result?.Documents;
|
||||
SearchResultsListBox.DisplayMemberPath = "DocumentName";
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -245,7 +206,7 @@ namespace PrototypWPFHAG
|
||||
public List<DocumentDetail> Documents { get; set; }
|
||||
}
|
||||
|
||||
public class DocumentDetail
|
||||
public class DocumentDetail : INotifyPropertyChanged
|
||||
{
|
||||
[JsonPropertyName("id")]
|
||||
public int Id { get; set; }
|
||||
@ -259,15 +220,17 @@ namespace PrototypWPFHAG
|
||||
[JsonPropertyName("distance")]
|
||||
public double Distance { get; set; }
|
||||
|
||||
// Für die TextBox-Bindung
|
||||
private bool _isSelected;
|
||||
public bool IsSelected
|
||||
{
|
||||
get => _isSelected;
|
||||
set
|
||||
{
|
||||
_isSelected = value;
|
||||
OnPropertyChanged();
|
||||
if (_isSelected != value)
|
||||
{
|
||||
_isSelected = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -278,6 +241,7 @@ namespace PrototypWPFHAG
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
}
|
||||
|
||||
public class ApiError
|
||||
{
|
||||
public string Detail { get; set; }
|
||||
@ -290,54 +254,31 @@ namespace PrototypWPFHAG
|
||||
MessageBox.Show("Keine PDF ausgewählt!");
|
||||
return;
|
||||
}
|
||||
// luis
|
||||
// UI zurücksetzen
|
||||
|
||||
UploadProgressBar.Visibility = Visibility.Visible;
|
||||
UploadProgressBar.Value = 0;
|
||||
UploadStatusText.Text = "Upload läuft...";
|
||||
|
||||
try
|
||||
{
|
||||
int totalFiles = _selectedPdfPaths.Count;
|
||||
int currentFile = 0;
|
||||
using var formData = new MultipartFormDataContent();
|
||||
|
||||
foreach (var pdfPath in _selectedPdfPaths)
|
||||
{
|
||||
currentFile++;
|
||||
UploadStatusText.Text = $"Upload {currentFile}/{totalFiles}: {System.IO.Path.GetFileName(pdfPath)}";
|
||||
|
||||
using (var httpClient = new HttpClient())
|
||||
using (var fileStream = File.OpenRead(pdfPath))
|
||||
{
|
||||
var content = new StreamContent(fileStream);
|
||||
var formData = new MultipartFormDataContent();
|
||||
formData.Add(content, "file", System.IO.Path.GetFileName(pdfPath));
|
||||
|
||||
var response = await httpClient.PostAsync($"{BaseUrl}/upload-pdf", formData);
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
UploadStatusText.Text = $"Erfolgreich hochgeladen: {System.IO.Path.GetFileName(pdfPath)}";
|
||||
}
|
||||
else
|
||||
{
|
||||
UploadStatusText.Text = $"Fehler beim Upload von {System.IO.Path.GetFileName(pdfPath)}";
|
||||
}
|
||||
}
|
||||
var fileContent = new StreamContent(File.OpenRead(pdfPath));
|
||||
formData.Add(fileContent, "files", Path.GetFileName(pdfPath));
|
||||
}
|
||||
|
||||
// Canvas zurücksetzen
|
||||
Dispatcher.Invoke(() =>
|
||||
{
|
||||
PdfIcon.Visibility = Visibility.Collapsed;
|
||||
PdfFileNameText.Visibility = Visibility.Collapsed;
|
||||
DropHintText.Visibility = Visibility.Visible;
|
||||
_selectedPdfPaths.Clear();
|
||||
});
|
||||
var response = await _httpClient.PostAsync("/upload-pdfs/", formData);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
// Erfolgsmeldung nach 3 Sekunden ausblenden
|
||||
await Task.Delay(3000);
|
||||
UploadStatusText.Text = string.Empty;
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
var result = JsonSerializer.Deserialize<ApiResponse>(json);
|
||||
|
||||
if (result?.Success == true)
|
||||
{
|
||||
UploadStatusText.Text = "Upload erfolgreich!";
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -346,6 +287,10 @@ namespace PrototypWPFHAG
|
||||
finally
|
||||
{
|
||||
UploadProgressBar.Visibility = Visibility.Collapsed;
|
||||
_selectedPdfPaths.Clear();
|
||||
PdfIcon.Visibility = Visibility.Collapsed;
|
||||
PdfFileNameText.Visibility = Visibility.Collapsed;
|
||||
DropHintText.Visibility = Visibility.Visible;
|
||||
}
|
||||
}
|
||||
private async void DeleteButton_Click(object sender, RoutedEventArgs e)
|
||||
@ -372,46 +317,37 @@ namespace PrototypWPFHAG
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await _httpClient.DeleteAsync($"/documents/by-id/{item.Id}/delete");
|
||||
var response = await _httpClient.DeleteAsync($"/documents/{item.Id}");
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
deletedIds.Add(item.Id);
|
||||
Debug.WriteLine($"Gelöscht: {item.Id}");
|
||||
}
|
||||
else
|
||||
{
|
||||
errorIds.Add(item.Id);
|
||||
var errorContent = await response.Content.ReadAsStringAsync();
|
||||
Debug.WriteLine($"Fehler bei {item.Id}: {errorContent}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
catch
|
||||
{
|
||||
errorIds.Add(item.Id);
|
||||
Debug.WriteLine($"Ausnahme: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
// Aktualisiere die Liste unabhängig vom Suchmodus
|
||||
// Aktualisiere die Liste
|
||||
if (SearchByIdRadio.IsChecked == true)
|
||||
{
|
||||
await SearchByIdAsync(); // Neu laden der ID-Suche
|
||||
await SearchByIdAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
await SearchBySimilarityAsync(); // Neu laden der Textsuche
|
||||
await SearchBySimilarityAsync();
|
||||
}
|
||||
|
||||
// Feedback an Benutzer
|
||||
// Feedback
|
||||
var message = new StringBuilder();
|
||||
if (deletedIds.Count > 0)
|
||||
{
|
||||
message.AppendLine($"{deletedIds.Count} Dokument(e) gelöscht.");
|
||||
}
|
||||
if (errorIds.Count > 0)
|
||||
{
|
||||
message.AppendLine($"{errorIds.Count} Dokument(e) konnten nicht gelöscht werden.");
|
||||
}
|
||||
if (deletedIds.Count > 0) message.AppendLine($"{deletedIds.Count} Dokument(e) gelöscht.");
|
||||
if (errorIds.Count > 0) message.AppendLine($"{errorIds.Count} Dokument(e) konnten nicht gelöscht werden.");
|
||||
|
||||
MessageBox.Show(message.ToString());
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user