3 Schichten Model

3 Schichten Model umgesetzt
This commit is contained in:
BlackViron 2024-07-01 16:21:03 +02:00
parent fe3e428f31
commit 068ba16156
16 changed files with 372 additions and 304 deletions

View File

@ -2,7 +2,7 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:bib_talk"
StartupUri="Login.xaml">
StartupUri="UI/Login.xaml">
<Application.Resources>
</Application.Resources>

View File

@ -0,0 +1,2 @@
Alle Business Klassen sind die [name].xaml.cs Dateien.
Diese sind NICHT in diesem Ordner, weil wir sie von der [name].xaml Datei nicht trennen können.

View File

Before

Width:  |  Height:  |  Size: 24 KiB

After

Width:  |  Height:  |  Size: 24 KiB

View File

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 10 KiB

View File

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 26 KiB

View File

@ -1,238 +0,0 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
namespace bib_talk
{
public partial class MainWindow : Window
{
private DispatcherTimer _timer;
public string loggedinUser;
private bool isFirstLoad = true;
public MainWindow()
{
InitializeComponent();
InitializeTimer();
LoadData();
}
public MainWindow(string user)
{
loggedinUser = user;
InitializeComponent();
InitializeTimer();
LoadData();
LoadMessages();
}
private void InitializeTimer()
{
_timer = new DispatcherTimer
{
Interval = TimeSpan.FromSeconds(2)
};
_timer.Tick += (sender, e) => { LoadMessages(); LoadOnlineUsers(); };
_timer.Start();
}
public void LoadData()
{
loggedinuser.Text = "Logged in as: " + loggedinUser;
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
LogoutUser();
this.Close();
}
private void DraggableArea_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ButtonState == MouseButtonState.Pressed)
{
DragMove();
}
}
private 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);
chatlistbox.ItemsSource = messages;
if (chatlistbox.Items.Count > 0)
{
var lastItem = chatlistbox.Items[chatlistbox.Items.Count - 1];
chatlistbox.ScrollIntoView(lastItem);
}
}
}
}
private async void LogoutUser()
{
using (var client = new HttpClient())
{
var logoutData = new { Username = 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();
this.Close();
}
}
private 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);
onlineUsersListBox.ItemsSource = onlineUsers;
}
}
}
private async void messageBOX_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
if (!string.IsNullOrEmpty(messageBOX.Text))
{
var message = new MessageDto
{
Username = loggedinUser,
Message = 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)
{
messageBOX.Clear();
LoadMessages(); // Reload messages after sending
}
}
}
}
}
private async void send_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(messageBOX.Text))
{
var message = new MessageDto
{
Username = loggedinUser,
Message = 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)
{
messageBOX.Clear();
LoadMessages(); // Reload messages after sending
}
}
}
}
public class Message
{
public int Id { get; set; }
public string Username { get; set; }
public string Text { get; set; }
public DateTime Timestamp { get; set; }
}
public class MessageDto
{
public string Username { get; set; }
public string Message { get; set; }
public DateTime Timestamp { get; set; }
}
public class User
{
public int Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public string Birthday { get; set; }
public string IsOnline { get; set; }
}
private void ChatFenster_Click(object sender, RoutedEventArgs e)
{
if (popup.IsOpen = true)
{
popup.Focus();
}
else
{
popup.IsOpen = true;
}
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Button 1 wurde geklickt!");
ClosePopup();
}
private void Button2_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Button 2 wurde geklickt!");
ClosePopup();
}
private void Button3_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Button 3 wurde geklickt!");
ClosePopup();
}
private void Popup_LostFocus(object sender, RoutedEventArgs e)
{
ClosePopup();
}
private void ClosePopup()
{
popup.IsOpen = false;
}
private void onlineUsersListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
}
}

View File

@ -0,0 +1,51 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows;
namespace bib_talk.Repository
{
public class LoginDatabase
{
Login loginWindow;
public LoginDatabase(Login loginWindow)
{
this.loginWindow = loginWindow;
}
public async Task LoginServer()
{
string username = loginWindow.usernbox.Text;
string password = loginWindow.passwordbox.Password;
var loginData = new { Username = username, Password = password, Email = "", Birthday = "", IsOnline = "" };
string json = JsonConvert.SerializeObject(loginData);
using (var client = new HttpClient())
{
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync("http://daddypig.dns.navy:5114/api/users/login", content);
if (response.IsSuccessStatusCode)
{
var responseContent = await response.Content.ReadAsStringAsync();
var responseData = JsonConvert.DeserializeObject<dynamic>(responseContent);
string loggedInUsername = responseData.username;
MainWindow mainWindow = new MainWindow(loggedInUsername);
mainWindow.Show();
loginWindow.Close();
}
else
{
var responseContent = await response.Content.ReadAsStringAsync();
MessageBox.Show($"Login failed: {responseContent}");
}
}
}
}
}

View File

@ -0,0 +1,96 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace bib_talk.Repository
{
public class MainWindowDatabase
{
MainWindow mainWindow;
public MainWindowDatabase(MainWindow mainWindow)
{
this.mainWindow = mainWindow;
}
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();
}
}
}
}

View File

@ -0,0 +1,47 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows;
namespace bib_talk.Repository
{
public class RegisterDatabase
{
RegisterWindow registerWindow;
public RegisterDatabase(RegisterWindow registerWindow)
{
this.registerWindow = registerWindow;
}
public async Task RegisterServer()
{
string username = registerWindow.usernbox.Text;
string password = registerWindow.passwordbox.Password;
string email = registerWindow.emailbox.Text;
string birthday = $"{registerWindow.bday1.Text}.{registerWindow.bday2.Text}.{registerWindow.bday3.Text}";
var newUser = new { Username = username, Password = password, Email = email, Birthday = birthday, IsOnline = "" };
string json = JsonConvert.SerializeObject(newUser);
using (var client = new HttpClient())
{
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync("http://daddypig.dns.navy:5114/api/users/register", content);
if (response.IsSuccessStatusCode)
{
MessageBox.Show("Registrierung erfolgreich.");
}
else
{
MessageBox.Show("Registrierung fehlgeschlagen. " + response);
}
}
}
}
}

View File

@ -5,17 +5,23 @@ using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows;
using bib_talk.Repository;
namespace bib_talk
{
public partial class Login : Window
{
LoginDatabase loginDatabase;
// Field to store logged in user
public static string loggedInUser;
public Login()
{
InitializeComponent();
loginDatabase = new LoginDatabase(this);
}
private void DraggableArea_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
@ -38,11 +44,12 @@ namespace bib_talk
this.Close();
}
private void WeiterButton_Click(object sender, RoutedEventArgs e)
private async void WeiterButton_Click(object sender, RoutedEventArgs e)
{
if (usernbox.Text != "" && passwordbox.Password != "")
{
LoginServer();
await loginDatabase.LoginServer();
}
}
@ -69,35 +76,5 @@ namespace bib_talk
register.Show();
this.Close();
}
public async Task LoginServer()
{
string username = usernbox.Text;
string password = passwordbox.Password;
var loginData = new { Username = username, Password = password, Email = "", Birthday = "", IsOnline = ""};
string json = JsonConvert.SerializeObject(loginData);
using (var client = new HttpClient())
{
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync("http://daddypig.dns.navy:5114/api/users/login", content);
if (response.IsSuccessStatusCode)
{
var responseContent = await response.Content.ReadAsStringAsync();
var responseData = JsonConvert.DeserializeObject<dynamic>(responseContent);
string loggedInUsername = responseData.username;
MainWindow mainWindow = new MainWindow(loggedInUsername);
mainWindow.Show();
this.Close();
}
else
{
var responseContent = await response.Content.ReadAsStringAsync();
MessageBox.Show($"Login failed: {responseContent}");
}
}
}
}
}

View File

@ -0,0 +1,157 @@
using bib_talk.Repository;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
namespace bib_talk
{
public partial class MainWindow : Window
{
MainWindowDatabase database;
private DispatcherTimer _timer;
public string loggedinUser;
private bool isFirstLoad = true;
public MainWindow()
{
InitializeComponent();
InitializeTimer();
LoadData();
database = new MainWindowDatabase(this);
}
public MainWindow(string user)
{
loggedinUser = user;
InitializeComponent();
InitializeTimer();
LoadData();
database = new MainWindowDatabase(this);
database.LoadMessages();
}
private void InitializeTimer()
{
_timer = new DispatcherTimer
{
Interval = TimeSpan.FromSeconds(2)
};
_timer.Tick += (sender, e) => { database.LoadMessages(); database.LoadOnlineUsers(); };
_timer.Start();
}
public void LoadData()
{
loggedinuser.Text = "Logged in as: " + loggedinUser;
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
database.LogoutUser();
this.Close();
}
private void DraggableArea_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ButtonState == MouseButtonState.Pressed)
{
DragMove();
}
}
private async void messageBOX_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
database.SendMessage();
}
}
private async void send_Click(object sender, RoutedEventArgs e)
{
database.SendMessage();
}
private void ChatFenster_Click(object sender, RoutedEventArgs e)
{
if (popup.IsOpen = true)
{
popup.Focus();
}
else
{
popup.IsOpen = true;
}
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Button 1 wurde geklickt!");
ClosePopup();
}
private void Button2_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Button 2 wurde geklickt!");
ClosePopup();
}
private void Button3_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Button 3 wurde geklickt!");
ClosePopup();
}
private void Popup_LostFocus(object sender, RoutedEventArgs e)
{
ClosePopup();
}
private void ClosePopup()
{
popup.IsOpen = false;
}
private void onlineUsersListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
}
public class Message
{
public int Id { get; set; }
public string Username { get; set; }
public string Text { get; set; }
public DateTime Timestamp { get; set; }
}
public class MessageDto
{
public string Username { get; set; }
public string Message { get; set; }
public DateTime Timestamp { get; set; }
}
public class User
{
public int Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public string Birthday { get; set; }
public string IsOnline { get; set; }
}
}

View File

@ -1,4 +1,5 @@
using Newtonsoft.Json;
using bib_talk.Repository;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
@ -52,13 +53,15 @@ namespace bib_talk
this.Close();
}
private void WeiterButton_Click(object sender, RoutedEventArgs e)
private async void WeiterButton_Click(object sender, RoutedEventArgs e)
{
if (checkboxAGB.IsChecked == true)
{
if (ValidateRegistration())
{
RegisterServer();
RegisterDatabase registerDatabase = new RegisterDatabase(this);
await registerDatabase.RegisterServer();
Login login = new Login();
login.Show();
this.Close();
@ -125,29 +128,6 @@ namespace bib_talk
return true;
}
public async Task RegisterServer()
{
string username = usernbox.Text;
string password = passwordbox.Password;
string email = emailbox.Text;
string birthday = $"{bday1.Text}.{bday2.Text}.{bday3.Text}";
var newUser = new { Username = username, Password = password, Email = email, Birthday = birthday, IsOnline = "" };
string json = JsonConvert.SerializeObject(newUser);
using (var client = new HttpClient())
{
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync("http://daddypig.dns.navy:5114/api/users/register", content);
if (response.IsSuccessStatusCode)
{
MessageBox.Show("Registrierung erfolgreich.");
}
else
{
MessageBox.Show("Registrierung fehlgeschlagen. " + response);
}
}
}
}
}

View File

@ -19,13 +19,9 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Icons\" />
</ItemGroup>
<ItemGroup>
<Resource Include="logo.png" />
<Resource Include="Logo_bib-Talk.png" />
<Resource Include="PICA.jpg" />
<Resource Include="Icons\logo.png" />
<Resource Include="Icons\Logo_bib-Talk.png" />
<Resource Include="Icons\PICA.jpg" />
</ItemGroup>
</Project>