2024-08-14 14:31:45 +02:00
|
|
|
|
using bib_talk.Business;
|
|
|
|
|
using Newtonsoft.Json;
|
2024-07-01 16:21:03 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2024-07-03 15:58:51 +02:00
|
|
|
|
using System.IO;
|
2024-07-01 16:21:03 +02:00
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
|
|
|
|
namespace bib_talk.Repository
|
|
|
|
|
{
|
|
|
|
|
public class MainWindowDatabase
|
|
|
|
|
{
|
2024-07-03 15:58:51 +02:00
|
|
|
|
private MainWindow mainWindow;
|
|
|
|
|
private string loggedinUser;
|
2024-07-01 16:21:03 +02:00
|
|
|
|
|
2024-07-03 15:58:51 +02:00
|
|
|
|
public MainWindowDatabase(MainWindow mainWindow)
|
|
|
|
|
{
|
|
|
|
|
this.mainWindow = mainWindow;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MainWindowDatabase(MainWindow mainWindow, string user)
|
2024-07-01 16:21:03 +02:00
|
|
|
|
{
|
|
|
|
|
this.mainWindow = mainWindow;
|
2024-07-03 15:58:51 +02:00
|
|
|
|
loggedinUser = user;
|
2024-07-01 16:21:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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<List<Message>>(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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
2024-07-02 22:40:43 +02:00
|
|
|
|
|
2024-07-01 16:21:03 +02:00
|
|
|
|
var onlineUsers = JsonConvert.DeserializeObject<List<User>>(responseContent);
|
2024-07-02 22:40:43 +02:00
|
|
|
|
|
|
|
|
|
|
2024-07-01 16:21:03 +02:00
|
|
|
|
mainWindow.onlineUsersListBox.ItemsSource = onlineUsers;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-03 15:58:51 +02:00
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-01 16:21:03 +02:00
|
|
|
|
}
|