3 Schichten Model
3 Schichten Model umgesetzt
This commit is contained in:
51
bibtalk/bib-talk/Repository/LoginDatabase.cs
Normal file
51
bibtalk/bib-talk/Repository/LoginDatabase.cs
Normal 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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
96
bibtalk/bib-talk/Repository/MainWindowDatabase.cs
Normal file
96
bibtalk/bib-talk/Repository/MainWindowDatabase.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
47
bibtalk/bib-talk/Repository/RegisterDatabase.cs
Normal file
47
bibtalk/bib-talk/Repository/RegisterDatabase.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user