Refactor dictionary application to use API for word meanings and update UI components

This commit is contained in:
vista-man
2025-01-27 22:14:46 +01:00
parent ca3a62d774
commit eec73a1d81
12 changed files with 42 additions and 30 deletions

View File

@@ -1,57 +1,69 @@
using System; using System;
using Microsoft.Data.SqlClient; // Update this line using System.Net.Http;
using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using System.Drawing; using System.Drawing;
namespace Woordenboek namespace DictionaryApp
{ {
public class Program : Form public class Program : Form
{ {
private TextBox wordTextBox; private TextBox wordTextBox;
private Button searchButton; private Button searchButton;
private Label meaningLabel; private ListBox meaningListBox;
private static readonly HttpClient client = new HttpClient();
public Program() public Program()
{ {
wordTextBox = new TextBox { Left = 50, Top = 20, Width = 200 }; wordTextBox = new TextBox { Left = 50, Top = 20, Width = 200 };
searchButton = new Button { Text = "Zoek", Left = 260, Top = 20, Width = 100 }; searchButton = new Button { Text = "Search", Left = 260, Top = 20, Width = 100 };
meaningLabel = new Label { Left = 50, Top = 60, Width = 310 }; meaningListBox = new ListBox { Left = 50, Top = 60, Width = 310, Height = 200 };
searchButton.Click += new EventHandler(SearchButton_Click); searchButton.Click += new EventHandler(SearchButton_Click);
wordTextBox.KeyDown += new KeyEventHandler(WordTextBox_KeyDown);
Controls.Add(wordTextBox); Controls.Add(wordTextBox);
Controls.Add(searchButton); Controls.Add(searchButton);
Controls.Add(meaningLabel); Controls.Add(meaningListBox);
Text = "Woordenboek"; Text = "Dictionary";
Size = new System.Drawing.Size(400, 150); Size = new System.Drawing.Size(400, 300);
} }
private void SearchButton_Click(object? sender, EventArgs e) private void WordTextBox_KeyDown(object? sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
SearchButton_Click(this, new EventArgs());
e.SuppressKeyPress = true; // Prevent the beep sound on Enter key press
}
}
private async void SearchButton_Click(object? sender, EventArgs e)
{ {
string word = wordTextBox.Text; string word = wordTextBox.Text;
string? meaning = GetMeaningFromDatabase(word); string? meaning = await GetMeaningFromApi(word);
meaningLabel.Text = meaning ?? "Betekenis niet gevonden."; meaningListBox.Items.Clear();
meaningListBox.Items.Add(meaning ?? "Meaning not found.");
} }
private string? GetMeaningFromDatabase(string word) private async Task<string?> GetMeaningFromApi(string word)
{ {
string connectionString = "Server=your_server_name;Database=your_database_name;User Id=your_username;Password=your_password;"; try
string query = "SELECT Meaning FROM Dictionary WHERE Word = @word";
using (SqlConnection connection = new SqlConnection(connectionString))
{ {
SqlCommand command = new SqlCommand(query, connection); string apiUrl = $"https://api.dictionaryapi.dev/api/v2/entries/en/{word}";
command.Parameters.AddWithValue("@word", word); HttpResponseMessage response = await client.GetAsync(apiUrl);
connection.Open(); response.EnsureSuccessStatusCode();
SqlDataReader reader = command.ExecuteReader(); string responseBody = await response.Content.ReadAsStringAsync();
if (reader.Read()) // Parse the responseBody to extract the meaning
{ // This is a simplified example, you may need to adjust the parsing based on the actual API response format
return reader["Meaning"].ToString(); var json = System.Text.Json.JsonDocument.Parse(responseBody);
} var meaning = json.RootElement[0].GetProperty("meanings")[0].GetProperty("definitions")[0].GetProperty("definition").GetString();
else return meaning;
{ }
return null; catch (HttpRequestException)
} {
return null;
} }
} }

View File

@@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("woordenboek")] [assembly: System.Reflection.AssemblyCompanyAttribute("woordenboek")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+aa5e80f9e3910a71e1746d19afcc838a78abfd1d")] [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+ca3a62d7744d2cbad686550a04e7f0edbf1507ba")]
[assembly: System.Reflection.AssemblyProductAttribute("woordenboek")] [assembly: System.Reflection.AssemblyProductAttribute("woordenboek")]
[assembly: System.Reflection.AssemblyTitleAttribute("woordenboek")] [assembly: System.Reflection.AssemblyTitleAttribute("woordenboek")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@@ -1 +1 @@
a4d97aee743d38e06f5c7332cf0b02a732fd1c583a5827cdff8f3b4115f2f4d1 7fdaf72c06cd35ed008236cce24530d9814f5189d5f4d6d7744da073e9b12c9f

View File

@@ -1 +1 @@
{"documents":{"C:\\Users\\steen\\Desktop\\Alvin\\ict-algemeen-opdrachten\\*":"https://raw.githubusercontent.com/Alvin-Zilverstand/ict-algemeen-opdrachten/aa5e80f9e3910a71e1746d19afcc838a78abfd1d/*"}} {"documents":{"C:\\Users\\steen\\Desktop\\Alvin\\ict-algemeen-opdrachten\\*":"https://raw.githubusercontent.com/Alvin-Zilverstand/ict-algemeen-opdrachten/ca3a62d7744d2cbad686550a04e7f0edbf1507ba/*"}}