using bib_talk.Business; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Net.Http; using System.Text; using System.Threading.Tasks; using System.Windows; namespace bib_talk.Repository { public class MainWindowDatabase { private MainWindow mainWindow; // MainWindow initialisieren private string loggedinUser; // Feld um eingeloggten User zu speichern // Konstruktor für MainWindowDatabase mit MainWindow und User Referenz public MainWindowDatabase(MainWindow mainWindow, string user) { this.mainWindow = mainWindow; loggedinUser = user; } // Methode zum Senden einer Nachricht via Datenbank public async void SendMessage() { if (!string.IsNullOrEmpty(mainWindow.messageBox.Text)) { var message = new MessageDto { Username = mainWindow.loggedinUser, Message = mainWindow.messageBox.Text, Timestamp = DateTime.Now, }; using (var client = new HttpClient()) { var json = JsonConvert.SerializeObject(message); var content = new StringContent(json, Encoding.UTF8, "application/json"); var response = await client.PostAsync("http://daddypig.dns.navy:5114/api/messages/send", content); if (response.IsSuccessStatusCode) { mainWindow.messageBox.Clear(); LoadMessages(); // Reload messages after sending } } } } // Methode zum Laden der Nachrichten via Datenbank public async void LoadMessages() { using (var client = new HttpClient()) { var response = await client.GetAsync("http://daddypig.dns.navy:5114/api/messages/get"); if (response.IsSuccessStatusCode) { var responseContent = await response.Content.ReadAsStringAsync(); var messages = JsonConvert.DeserializeObject>(responseContent); mainWindow.chatlistbox.ItemsSource = messages; if (mainWindow.chatlistbox.Items.Count > 0) { var lastItem = mainWindow.chatlistbox.Items[mainWindow.chatlistbox.Items.Count - 1]; mainWindow.chatlistbox.ScrollIntoView(lastItem); } } } } // Methode zum Laden der Online Benutzer via Datenbank public async void LoadOnlineUsers() { using (var client = new HttpClient()) { var response = await client.GetAsync("http://daddypig.dns.navy:5114/api/users/online-users"); if (response.IsSuccessStatusCode) { var responseContent = await response.Content.ReadAsStringAsync(); var onlineUsers = JsonConvert.DeserializeObject>(responseContent); mainWindow.onlineUsersListBox.ItemsSource = onlineUsers; } } } public async void ClearMessages() { using (var client = new HttpClient()) { try { // Sending a DELETE request to the clear messages endpoint var response = await client.DeleteAsync("http://daddypig.dns.navy:5114/api/messages/clear"); if (response.IsSuccessStatusCode) { // Log success message or take any other action as needed MessageBox.Show("Alle Nachrichten wurden gelöscht"); } else { // Log failure message with the status code MessageBox.Show($"Fehler beim Löschen der Nachrichten. Status code: {response.StatusCode}"); } } catch (Exception ex) { // Handle any exceptions that occur during the request Console.WriteLine($"Ein Fehler ist aufgetreten: {ex.Message}"); } } } // Methode zum Ausloggen des Users via Datenbank public async void LogoutUser() { using (var client = new HttpClient()) { var logoutData = new { Username = mainWindow.loggedinUser, Password = "", Email = "", Birthday = "", IsOnline = "" }; var json = JsonConvert.SerializeObject(logoutData); var content = new StringContent(json, Encoding.UTF8, "application/json"); var response = await client.PostAsync("http://daddypig.dns.navy:5114/api/users/logout", content); var responseContent = await response.Content.ReadAsStringAsync(); mainWindow.Close(); } } // Methode zum Hochladen eines Profilbildes via Datenbank public async Task UploadProfileImage(string filePath) { using (var client = new HttpClient()) { var form = new MultipartFormDataContent(); var stream = File.OpenRead(filePath); form.Add(new StreamContent(stream), "file", Path.GetFileName(filePath)); var response = await client.PostAsync($"http://daddypig.dns.navy:5114/api/users/upload-profile-image?username={loggedinUser}", form); if (response.IsSuccessStatusCode) { MessageBox.Show("Profilbild erfolgreich hochgeladen"); } else { MessageBox.Show("Fehler beim Hochladen des Profilbilds"); } } } } }