bib_Talk_Chatprogramm/bibtalk/bib-talk/Repository/MainWindowDatabase.cs

129 lines
4.4 KiB
C#
Raw Normal View History

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace bib_talk.Repository
{
public class MainWindowDatabase
{
private MainWindow mainWindow;
private string loggedinUser;
public MainWindowDatabase(MainWindow mainWindow)
{
this.mainWindow = mainWindow;
}
public MainWindowDatabase(MainWindow mainWindow, string user)
{
this.mainWindow = mainWindow;
loggedinUser = user;
}
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();
var onlineUsers = JsonConvert.DeserializeObject<List<User>>(responseContent);
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();
}
}
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");
}
}
}
}
}