so many stuff

This commit is contained in:
MiskolcziRichard
2024-03-26 12:54:51 +01:00
parent 12f0dd9cd8
commit a3e4fdb5fd
15 changed files with 955 additions and 33 deletions

View File

@@ -5,8 +5,79 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WD7UVN_SzTGUI_2023242.Client.WPF"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
Title="Company database manager" Height="450" Width="900">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="9*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<ListBox Grid.Row="0">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Content="{Binding NAME}" Margin="5px"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Grid.Row="1" Margin="5" Content="Expand all" Click="ExpandAllServices"/>
</Grid>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="9*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<ListBox Grid.Row="0">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Content="{Binding NAME}" Margin="5px"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Grid.Row="1" Margin="5" Content="Expand all" Click="ExpandAllCustomers" />
</Grid>
<Grid Grid.Column="2">
<Grid.RowDefinitions>
<RowDefinition Height="9*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<ListBox Grid.Row="0">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Content="{Binding NAME}" Margin="5px"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Grid.Row="1" Margin="5" Content="Expand all" Click="ExpandAllClients" />
</Grid>
<Grid Grid.Column="3">
<Grid.RowDefinitions>
<RowDefinition Height="9*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<ListBox Grid.Row="0">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Content="{Binding NAME}" Margin="5px"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Grid.Row="1" Margin="5" Content="Expand all" Click="ExpandAllTeams" />
</Grid>
</Grid>
</Window>

View File

@@ -1,17 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows;
namespace WD7UVN_SzTGUI_2023242.Client.WPF
{
@@ -24,5 +11,25 @@ namespace WD7UVN_SzTGUI_2023242.Client.WPF
{
InitializeComponent();
}
private void ExpandAllServices(object sender, RoutedEventArgs e)
{
}
private void ExpandAllCustomers(object sender, RoutedEventArgs e)
{
}
private void ExpandAllClients(object sender, RoutedEventArgs e)
{
}
private void ExpandAllTeams(object sender, RoutedEventArgs e)
{
}
}
}

View File

@@ -0,0 +1,423 @@
using Microsoft.AspNetCore.SignalR.Client;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows;
namespace WD7UVN_SzTGUI_2023242.Client.WPF
{
public class RestService
{
HttpClient client;
public RestService(string baseurl, string pingableEndpoint = "swagger")
{
bool isOk = false;
do
{
isOk = Ping(baseurl + pingableEndpoint);
} while (isOk == false);
Init(baseurl);
}
private bool Ping(string url)
{
try
{
WebClient wc = new WebClient();
wc.DownloadData(url);
return true;
}
catch
{
return false;
}
}
private void Init(string baseurl)
{
client = new HttpClient();
client.BaseAddress = new Uri(baseurl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue
("application/json"));
try
{
client.GetAsync("").GetAwaiter().GetResult();
}
catch (HttpRequestException)
{
throw new ArgumentException("Endpoint is not available!");
}
}
public async Task<List<T>> GetAsync<T>(string endpoint)
{
List<T> items = new List<T>();
HttpResponseMessage response = await client.GetAsync(endpoint);
if (response.IsSuccessStatusCode)
{
items = await response.Content.ReadAsAsync<List<T>>();
}
else
{
var error = await response.Content.ReadAsAsync<RestExceptionInfo>();
throw new ArgumentException(error.Msg);
}
return items;
}
public List<T> Get<T>(string endpoint)
{
List<T> items = new List<T>();
HttpResponseMessage response = client.GetAsync(endpoint).GetAwaiter().GetResult();
if (response.IsSuccessStatusCode)
{
items = response.Content.ReadAsAsync<List<T>>().GetAwaiter().GetResult();
}
else
{
var error = response.Content.ReadAsAsync<RestExceptionInfo>().GetAwaiter().GetResult();
throw new ArgumentException(error.Msg);
}
return items;
}
public async Task<T> GetSingleAsync<T>(string endpoint)
{
T item = default(T);
HttpResponseMessage response = await client.GetAsync(endpoint);
if (response.IsSuccessStatusCode)
{
item = await response.Content.ReadAsAsync<T>();
}
else
{
var error = await response.Content.ReadAsAsync<RestExceptionInfo>();
throw new ArgumentException(error.Msg);
}
return item;
}
public T GetSingle<T>(string endpoint)
{
T item = default(T);
HttpResponseMessage response = client.GetAsync(endpoint).GetAwaiter().GetResult();
if (response.IsSuccessStatusCode)
{
item = response.Content.ReadAsAsync<T>().GetAwaiter().GetResult();
}
else
{
var error = response.Content.ReadAsAsync<RestExceptionInfo>().GetAwaiter().GetResult();
throw new ArgumentException(error.Msg);
}
return item;
}
public async Task<T> GetAsync<T>(int id, string endpoint)
{
T item = default(T);
HttpResponseMessage response = await client.GetAsync(endpoint + "/" + id.ToString());
if (response.IsSuccessStatusCode)
{
item = await response.Content.ReadAsAsync<T>();
}
else
{
var error = await response.Content.ReadAsAsync<RestExceptionInfo>();
throw new ArgumentException(error.Msg);
}
return item;
}
public T Get<T>(int id, string endpoint)
{
T item = default(T);
HttpResponseMessage response = client.GetAsync(endpoint + "/" + id.ToString()).GetAwaiter().GetResult();
if (response.IsSuccessStatusCode)
{
item = response.Content.ReadAsAsync<T>().GetAwaiter().GetResult();
}
else
{
var error = response.Content.ReadAsAsync<RestExceptionInfo>().GetAwaiter().GetResult();
throw new ArgumentException(error.Msg);
}
return item;
}
public async Task PostAsync<T>(T item, string endpoint)
{
HttpResponseMessage response =
await client.PostAsJsonAsync(endpoint, item);
if (!response.IsSuccessStatusCode)
{
var error = await response.Content.ReadAsAsync<RestExceptionInfo>();
throw new ArgumentException(error.Msg);
}
response.EnsureSuccessStatusCode();
}
public void Post<T>(T item, string endpoint)
{
HttpResponseMessage response =
client.PostAsJsonAsync(endpoint, item).GetAwaiter().GetResult();
if (!response.IsSuccessStatusCode)
{
var error = response.Content.ReadAsAsync<RestExceptionInfo>().GetAwaiter().GetResult();
throw new ArgumentException(error.Msg);
}
response.EnsureSuccessStatusCode();
}
public async Task DeleteAsync(int id, string endpoint)
{
HttpResponseMessage response =
await client.DeleteAsync(endpoint + "/" + id.ToString());
if (!response.IsSuccessStatusCode)
{
var error = await response.Content.ReadAsAsync<RestExceptionInfo>();
throw new ArgumentException(error.Msg);
}
response.EnsureSuccessStatusCode();
}
public void Delete(int id, string endpoint)
{
HttpResponseMessage response =
client.DeleteAsync(endpoint + "/" + id.ToString()).GetAwaiter().GetResult();
if (!response.IsSuccessStatusCode)
{
var error = response.Content.ReadAsAsync<RestExceptionInfo>().GetAwaiter().GetResult();
throw new ArgumentException(error.Msg);
}
response.EnsureSuccessStatusCode();
}
public async Task PutAsync<T>(T item, string endpoint)
{
HttpResponseMessage response =
await client.PutAsJsonAsync(endpoint, item);
if (!response.IsSuccessStatusCode)
{
var error = await response.Content.ReadAsAsync<RestExceptionInfo>();
throw new ArgumentException(error.Msg);
}
response.EnsureSuccessStatusCode();
}
public void Put<T>(T item, string endpoint)
{
HttpResponseMessage response =
client.PutAsJsonAsync(endpoint, item).GetAwaiter().GetResult();
if (!response.IsSuccessStatusCode)
{
var error = response.Content.ReadAsAsync<RestExceptionInfo>().GetAwaiter().GetResult();
throw new ArgumentException(error.Msg);
}
response.EnsureSuccessStatusCode();
}
}
public class RestExceptionInfo
{
public RestExceptionInfo()
{
}
public string Msg { get; set; }
}
class NotifyService
{
private HubConnection conn;
public NotifyService(string url)
{
conn = new HubConnectionBuilder()
.WithUrl(url)
.Build();
conn.Closed += async (error) =>
{
await Task.Delay(new Random().Next(0, 5) * 1000);
await conn.StartAsync();
};
}
public void AddHandler<T>(string methodname, Action<T> value)
{
conn.On<T>(methodname, value);
}
public async void Init()
{
await conn.StartAsync();
}
}
public class RestCollection<T> : INotifyCollectionChanged, IEnumerable<T>
{
public event NotifyCollectionChangedEventHandler? CollectionChanged;
NotifyService notify;
RestService rest;
List<T> items;
bool hasSignalR;
Type type = typeof(T);
public RestCollection(string baseurl, string endpoint, string hub = null)
{
hasSignalR = hub != null;
this.rest = new RestService(baseurl, endpoint);
if (hub != null)
{
this.notify = new NotifyService(baseurl + hub);
this.notify.AddHandler<T>(type.Name + "Created", (T item) =>
{
items.Add(item);
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
});
this.notify.AddHandler<T>(type.Name + "Deleted", (T item) =>
{
var element = items.FirstOrDefault(t => t.Equals(item));
if (element != null)
{
items.Remove(item);
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
else
{
Init();
}
});
this.notify.AddHandler<T>(type.Name + "Updated", (T item) =>
{
Init();
});
this.notify.Init();
}
Init();
}
private async Task Init()
{
items = await rest.GetAsync<T>(typeof(T).Name);
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
public IEnumerator<T> GetEnumerator()
{
if (items != null)
{
return items.GetEnumerator();
}
else return new List<T>().GetEnumerator();
}
public List<T> GetItems()
{
if (items != null)
{
return items;
}
else return new List<T>();
}
IEnumerator IEnumerable.GetEnumerator()
{
if (items != null)
{
return items.GetEnumerator();
}
else return new List<T>().GetEnumerator();
}
public void Add(T item)
{
if (hasSignalR)
{
this.rest.PostAsync(item, typeof(T).Name);
}
else
{
this.rest.PostAsync(item, typeof(T).Name).ContinueWith((t) =>
{
Init().ContinueWith(z =>
{
Application.Current.Dispatcher.Invoke(() =>
{
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
});
});
});
}
}
public void Update(T item)
{
if (hasSignalR)
{
this.rest.PutAsync(item, typeof(T).Name);
}
else
{
this.rest.PutAsync(item, typeof(T).Name).ContinueWith((t) =>
{
Init().ContinueWith(z =>
{
Application.Current.Dispatcher.Invoke(() =>
{
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
});
});
});
}
}
public void Delete(int id)
{
if (hasSignalR)
{
this.rest.DeleteAsync(id, typeof(T).Name);
}
else
{
this.rest.DeleteAsync(id, typeof(T).Name).ContinueWith((t) =>
{
Init().ContinueWith(z =>
{
Application.Current.Dispatcher.Invoke(() =>
{
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
});
});
});
}
}
}
}

View File

@@ -0,0 +1,62 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using WD7UVN_HFT_2023241.Models;
namespace WD7UVN_SzTGUI_2023242.Client.WPF
{
internal class ServiceViewModel : ObservableRecipient
{
private string errorService;
public string ErrorService
{
get { return errorService; }
set { SetProperty(ref errorService, value); }
}
public RestCollection<Service> Services { get; set; }
private string newServiceText;
public string NewServiceText
{
get
{
return newServiceText;
}
set
{
newServiceText = value;
}
}
public ICommand SendServiceCommand { get; set; }
public static bool IsInDesignMode
{
get
{
var prop = DesignerProperties.IsInDesignModeProperty;
return (bool)DependencyPropertyDescriptor.FromProperty(prop, typeof(FrameworkElement)).Metadata.DefaultValue;
}
}
public ServiceViewModel()
{
if (!IsInDesignMode)
{
Services = new RestCollection<Service>("http://localhost:62005/", "Service", "hub");
SendServiceCommand = new RelayCommand(() =>
{
Services.Add(new Service(1, NewServiceText, DateTime.Now, new User(1, "Én")));
});
}
}
}
}

View File

@@ -7,4 +7,14 @@
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="7.1.2" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="6.0.2" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\WD7UVN_HFT_2023241.Models\WD7UVN_HFT_2023241.Models.csproj" />
</ItemGroup>
</Project>

View File

@@ -7,6 +7,14 @@
</ApplicationDefinition>
</ItemGroup>
<ItemGroup>
<Compile Update="Windows\GetAllServices.xaml.cs">
<SubType>Code</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<Page Update="Windows\GetAllServices.xaml">
<SubType>Designer</SubType>
</Page>
<Page Update="MainWindow.xaml">
<SubType>Designer</SubType>
</Page>

View File

@@ -0,0 +1,64 @@
<Window x:Class="WD7UVN_SzTGUI_2023242.Client.WPF.GetAllServices"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WD7UVN_SzTGUI_2023242.Client.WPF"
mc:Ignorable="d"
Title="GetAllServices" Height="550" Width="350">
<Grid>
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Gray" BorderThickness="1">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="8*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="ID:" />
<Label Grid.Row="0" Grid.Column="1" Content="{Binding ID}" />
<Label Grid.Row="1" Grid.Column="0" Content="Name:" />
<Label Grid.Row="1" Grid.Column="1" Content="{Binding NAME}" />
<Label Grid.Row="2" Grid.Column="0" Content="Version:" />
<Label Grid.Row="2" Grid.Column="1" Content="{Binding VERSION}" />
<Label Grid.Row="3" Grid.Column="0" Content="Account:" />
<Label Grid.Row="3" Grid.Column="1" Content="{Binding ACCOUNT}" />
<Label Grid.Row="4" Grid.Column="0" Content="Domain:" />
<Label Grid.Row="4" Grid.Column="1" Content="{Binding SERVICE_DOMAIN}" />
<Label Grid.Row="5" Grid.Column="0" Content="IP address:" />
<Label Grid.Row="5" Grid.Column="1" Content="{Binding IP}" />
<Label Grid.Row="6" Grid.Column="0" Content="Port:" />
<Label Grid.Row="6" Grid.Column="1" Content="{Binding PORT}" />
<Label Grid.Row="7" Grid.Column="0" Content="Notes:" />
<Label Grid.Row="7" Grid.Column="1" Content="{Binding NOTES}" />
<Label Grid.Row="8" Grid.Column="0" Content="Maintainer Team ID:" />
<Label Grid.Row="8" Grid.Column="1" Content="{Binding MAINTAINER_ID}" />
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>

View File

@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace WD7UVN_SzTGUI_2023242.Client.WPF
{
/// <summary>
/// Interaction logic for GetAllServices.xaml
/// </summary>
public partial class GetAllServices : Window
{
public GetAllServices()
{
InitializeComponent();
}
}
}