Demo
@@ -1,31 +1,52 @@
|
||||
using AvaloniaPostrgresApp.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace AvaloniaPostrgresApp;
|
||||
|
||||
public class AppDbContext : DbContext
|
||||
{
|
||||
public DbSet<UserInfo> Users { get; set; } = null!;
|
||||
public DbSet<Tovar> Tovars { get; set; } = null!;
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
|
||||
string connectionString = "Host=127.0.0.1;Port=5432;Database=postgres;Username=postgres;Password=mysecretpassword;Timeout=15;Command Timeout=15;Trust Server Certificate=true;SSL Mode=Prefer";
|
||||
string connectionString =
|
||||
"Host=127.0.0.1;Port=5432;Database=postgres;Username=postgres;Password=mysecretpassword;Timeout=15;Command Timeout=15;Trust Server Certificate=true;SSL Mode=Prefer";
|
||||
optionsBuilder.UseNpgsql(connectionString);
|
||||
}
|
||||
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
modelBuilder.Entity<UserInfo>().ToTable("UserInfo");
|
||||
|
||||
modelBuilder.Entity<UserInfo>().ToTable("UserInfo");
|
||||
modelBuilder.Entity<UserInfo>().Property(u => u.Id).HasColumnName("id");
|
||||
modelBuilder.Entity<UserInfo>().Property(u => u.Fio).HasColumnName("fio");
|
||||
modelBuilder.Entity<UserInfo>().Property(u => u.Login).HasColumnName("login");
|
||||
modelBuilder.Entity<UserInfo>().Property(u => u.Password).HasColumnName("password");
|
||||
modelBuilder.Entity<UserInfo>().Property(u => u.RoleId).HasColumnName("role_id");
|
||||
|
||||
modelBuilder.Entity<UserInfo>().Ignore(u => u.Orders);
|
||||
modelBuilder.Entity<UserInfo>().Ignore(u => u.Role);
|
||||
|
||||
modelBuilder.Entity<Tovar>().ToTable("tovar");
|
||||
modelBuilder.Entity<Tovar>().Property(t => t.Id).HasColumnName("id");
|
||||
modelBuilder.Entity<Tovar>().Property(t => t.Art).HasColumnName("art");
|
||||
modelBuilder.Entity<Tovar>().Property(t => t.IdCategory).HasColumnName("id_category");
|
||||
modelBuilder.Entity<Tovar>().Property(t => t.IdSection).HasColumnName("id_section");
|
||||
modelBuilder.Entity<Tovar>().Property(t => t.IdManufacturer).HasColumnName("id_manufacturer");
|
||||
modelBuilder.Entity<Tovar>().Property(t => t.IdSupplier).HasColumnName("id_supplier");
|
||||
modelBuilder.Entity<Tovar>().Property(t => t.Cost).HasColumnName("cost");
|
||||
modelBuilder.Entity<Tovar>().Property(t => t.Discount).HasColumnName("discount");
|
||||
modelBuilder.Entity<Tovar>().Property(t => t.Amount).HasColumnName("amount");
|
||||
modelBuilder.Entity<Tovar>().Property(t => t.Description).HasColumnName("description");
|
||||
modelBuilder.Entity<Tovar>().Property(t => t.Photo).HasColumnName("photo");
|
||||
|
||||
modelBuilder.Entity<Tovar>().Ignore(t => t.IdCategoryNavigation);
|
||||
modelBuilder.Entity<Tovar>().Ignore(t => t.IdManufacturerNavigation);
|
||||
modelBuilder.Entity<Tovar>().Ignore(t => t.IdSectionNavigation);
|
||||
modelBuilder.Entity<Tovar>().Ignore(t => t.IdSupplierNavigation);
|
||||
modelBuilder.Entity<Tovar>().Ignore(t => t.OrderDetails);
|
||||
modelBuilder.Entity<Tovar>().Ignore(t => t.ProductBitmap);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
After Width: | Height: | Size: 83 KiB |
|
After Width: | Height: | Size: 83 KiB |
|
After Width: | Height: | Size: 78 KiB |
|
After Width: | Height: | Size: 91 KiB |
|
After Width: | Height: | Size: 29 KiB |
|
After Width: | Height: | Size: 47 KiB |
|
After Width: | Height: | Size: 58 KiB |
|
After Width: | Height: | Size: 72 KiB |
|
After Width: | Height: | Size: 56 KiB |
|
After Width: | Height: | Size: 68 KiB |
@@ -2,19 +2,18 @@ using Avalonia.Controls;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Media;
|
||||
using AvaloniaPostrgresApp.Models;
|
||||
using System.Linq;
|
||||
namespace AvaloniaPostrgresApp;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Npgsql;
|
||||
using System;
|
||||
|
||||
namespace AvaloniaPostrgresApp;
|
||||
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
|
||||
private async void SignInClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
@@ -23,48 +22,57 @@ public partial class MainWindow : Window
|
||||
|
||||
if (string.IsNullOrWhiteSpace(login) || string.IsNullOrWhiteSpace(password))
|
||||
{
|
||||
StatusText.Text = "Заполните все поля!";
|
||||
StatusText.Foreground = Brushes.Red;
|
||||
ShowStatus("Заполните все поля!", Brushes.Red);
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using var db = new AppDbContext();
|
||||
|
||||
using (var db = new PostgresContext())
|
||||
{
|
||||
UserInfo? user = await db.UserInfos
|
||||
.FirstOrDefaultAsync(u => u.Login == login && u.Password == password);
|
||||
var user = await db.Users
|
||||
.FirstOrDefaultAsync(u => u.Login == login && u.Password == password);
|
||||
|
||||
if (user != null)
|
||||
{
|
||||
StatusText.Text = $"Успешно! Добро пожаловать, {user.Fio}.";
|
||||
StatusText.Foreground = Brushes.Green;
|
||||
}
|
||||
else
|
||||
{
|
||||
StatusText.Text = "Неверный логин или пароль.";
|
||||
StatusText.Foreground = Brushes.Red;
|
||||
}
|
||||
if (user == null)
|
||||
{
|
||||
ShowStatus("Неверный логин или пароль.", Brushes.Red);
|
||||
return;
|
||||
}
|
||||
|
||||
OpenCatalogWindow();
|
||||
}
|
||||
catch (NpgsqlException)
|
||||
{
|
||||
StatusText.Text = "Ошибка подключения. Проверьте строку в PostgresContext.cs или Docker.";
|
||||
StatusText.Foreground = Brushes.Red;
|
||||
ShowStatus("Ошибка подключения. Проверьте строку в AppDbContext.cs или Docker.", Brushes.Red);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
StatusText.Text = ex.InnerException?.Message ?? ex.Message;
|
||||
StatusText.Foreground = Brushes.Red;
|
||||
ShowStatus(ex.InnerException?.Message ?? ex.Message, Brushes.Red);
|
||||
}
|
||||
}
|
||||
|
||||
private void GuestSignInClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
StatusText.Text = "Вы вошли в систему как Гость.";
|
||||
LoginInput.Text = string.Empty;
|
||||
PasswordInput.Text = string.Empty;
|
||||
OpenCatalogWindow();
|
||||
}
|
||||
}
|
||||
|
||||
private void OpenCatalogWindow()
|
||||
{
|
||||
var window1 = new Window1();
|
||||
|
||||
if (Avalonia.Application.Current?.ApplicationLifetime is Avalonia.Controls.ApplicationLifetimes.IClassicDesktopStyleApplicationLifetime desktop)
|
||||
{
|
||||
desktop.MainWindow = window1;
|
||||
}
|
||||
|
||||
window1.Show();
|
||||
this.Close();
|
||||
}
|
||||
|
||||
|
||||
private void ShowStatus(string message, IBrush color)
|
||||
{
|
||||
StatusText.Text = message;
|
||||
StatusText.Foreground = color;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using Avalonia.Media.Imaging;
|
||||
using System.IO;
|
||||
|
||||
namespace AvaloniaPostrgresApp.Models;
|
||||
|
||||
@@ -27,6 +30,36 @@ public partial class Tovar
|
||||
|
||||
public string? Photo { get; set; }
|
||||
|
||||
[NotMapped]
|
||||
public Bitmap? ProductBitmap
|
||||
{
|
||||
get
|
||||
{
|
||||
if (string.IsNullOrEmpty(Photo)) return null;
|
||||
try
|
||||
{
|
||||
string localPath = Path.Combine(AppContext.BaseDirectory, "Assets", Photo);
|
||||
if (File.Exists(localPath))
|
||||
{
|
||||
return new Bitmap(localPath);
|
||||
}
|
||||
|
||||
string projectPath = Path.Combine(AppContext.BaseDirectory, "..", "..", "..", "Assets", Photo);
|
||||
if (File.Exists(projectPath))
|
||||
{
|
||||
return new Bitmap(projectPath);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public virtual Category IdCategoryNavigation { get; set; } = null!;
|
||||
|
||||
public virtual Manufacturer IdManufacturerNavigation { get; set; } = null!;
|
||||
@@ -36,4 +69,4 @@ public partial class Tovar
|
||||
public virtual Supplier IdSupplierNavigation { get; set; } = null!;
|
||||
|
||||
public virtual ICollection<OrderDetail> OrderDetails { get; set; } = new List<OrderDetail>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<Window xmlns="https://github.com/avaloniaui"
|
||||
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:models="clr-namespace:AvaloniaPostrgresApp.Models"
|
||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||
x:Class="AvaloniaPostrgresApp.Window1"
|
||||
Title="Tables">
|
||||
|
||||
<Grid RowDefinitions="Auto, *" Margin="20">
|
||||
|
||||
<Grid Grid.Row="0" ColumnDefinitions="*, Auto, Auto" ColumnSpacing="10" Margin="0,0,0,20">
|
||||
<TextBox Grid.Column="0" Watermark="Поиск" HorizontalAlignment="Stretch"/>
|
||||
<ComboBox Grid.Column="1" PlaceholderText="Сортировка" MinWidth="120" HorizontalAlignment="Stretch"/>
|
||||
<ComboBox Grid.Column="2" PlaceholderText="Фильтрация" MinWidth="120" HorizontalAlignment="Stretch"/>
|
||||
</Grid>
|
||||
|
||||
<ListBox Grid.Row="1" x:Name="ProductList">
|
||||
<ListBox.ItemTemplate>
|
||||
<DataTemplate DataType="models:Tovar">
|
||||
<Grid ColumnDefinitions="Auto, *" Margin="4">
|
||||
|
||||
<Image Grid.Column="0"
|
||||
Source="{Binding ProductBitmap}"
|
||||
Width="80"
|
||||
Height="80"
|
||||
Stretch="UniformToFill"
|
||||
VerticalAlignment="Top"/>
|
||||
|
||||
<StackPanel Grid.Column="1" Spacing="4" Margin="12,0,0,0">
|
||||
|
||||
<TextBlock Text="{Binding Art}" FontWeight="SemiBold" FontSize="16" />
|
||||
|
||||
<TextBlock Text="{Binding Description}" FontSize="14" Foreground="#333333" TextWrapping="Wrap"/>
|
||||
|
||||
<StackPanel Orientation="Horizontal" Spacing="15" Margin="0,4,0,0">
|
||||
<TextBlock Text="{Binding Cost, StringFormat='{}{0:C}'}" Foreground="Green" FontWeight="Bold" />
|
||||
<TextBlock Text="{Binding Amount, StringFormat='В наличии: {0} шт.'}" Foreground="Gray" FontSize="12" VerticalAlignment="Center"/>
|
||||
</StackPanel>
|
||||
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</DataTemplate>
|
||||
</ListBox.ItemTemplate>
|
||||
</ListBox>
|
||||
|
||||
</Grid>
|
||||
</Window>
|
||||
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Media;
|
||||
using AvaloniaPostrgresApp.Models;
|
||||
|
||||
namespace AvaloniaPostrgresApp;
|
||||
|
||||
public partial class Window1 : Window
|
||||
{
|
||||
public Window1()
|
||||
{
|
||||
InitializeComponent();
|
||||
LoadProducts();
|
||||
}
|
||||
|
||||
private void LoadProducts()
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = new AppDbContext();
|
||||
var products = context.Tovars.ToList();
|
||||
ProductList.ItemsSource = products;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.Title = $"Ошибка загрузки БД: {ex.Message}";
|
||||
}
|
||||
}
|
||||
}
|
||||