updated DeleteOrder

This commit is contained in:
NikitaOnianov 2025-03-09 21:40:38 +03:00
parent 5ffe40bf92
commit 00ae76a0ab
2 changed files with 125 additions and 39 deletions

View File

@ -4,19 +4,68 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="demo_blagodat.DeleteOrder"
xmlns:vm="using:demo_blagodat.Models"
Title="Удалить заказ">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<StackPanel Spacing="15" HorizontalAlignment="Center" Orientation="Vertical">
<StackPanel Orientation="Vertical">
<TextBlock Text="Введите ID заказа"/>
<TextBox Width="300" x:Name="TextID"/>
<Border Background="Red"
CornerRadius="10"
Padding="20"
Margin="20"
Width="550"
Height="200"
>
<ScrollViewer>
<ListBox Name="ListOrder" SelectionChanged="ListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate x:DataType="vm:Order">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding OrderClient}"/>
<TextBlock Text=" : "/>
<TextBlock Text="{Binding OrderDate}"/>
<TextBlock Text=" : "/>
<TextBlock Text="{Binding OrderTime}"/>
<TextBlock Text=" : "/>
<TextBlock Text="{Binding OrderRentalTime}"/>
<TextBlock Text=" : "/>
<TextBlock Text="{Binding OrderStatus}"/>
<TextBlock Text=" : "/>
<TextBlock Text="{Binding OrderClosingDate}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
</Border>
<Border Background="Red"
CornerRadius="10"
Padding="20"
Margin="20"
Width="550"
Height="200">
<ScrollViewer>
<ListBox Name="ListService">
<ListBox.ItemTemplate>
<DataTemplate x:DataType="vm:Service">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ServiceName}"/>
<TextBlock Text=" : "/>
<TextBlock Text="{Binding ServiceCode}"/>
<TextBlock Text=" : "/>
<TextBlock Text="{Binding ServiceCostPerHour}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
</Border>
</StackPanel>
</StackPanel>
<StackPanel HorizontalAlignment="Center" Orientation="Vertical" VerticalAlignment="Center">
<Button Content="Удвлить заказ" Click="deleteclient"/>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<Button Content="Удалить заказ" Click="deleteOrder"/>
<Button Content="Вернуться назад" Click="ExitClik"/>
</StackPanel>
<StackPanel HorizontalAlignment="Center" Orientation="Vertical">

View File

@ -5,62 +5,96 @@ using Avalonia.Markup.Xaml;
using demo_blagodat.Models;
using System.Linq;
using System;
using System.Collections.ObjectModel;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using static System.Net.Mime.MediaTypeNames;
namespace demo_blagodat;
public partial class DeleteOrder : Window
{
public int? idOrder;
public object? idOrder;
static User11Context db = new User11Context();
ObservableCollection<Order> OrderitemSource = new ObservableCollection<Order>(db.Orders);
ObservableCollection<Service> ServiceitemSource;
public Employee User;
public DeleteOrder(Employee user)
{
InitializeComponent();
User = user;
ListOrder.ItemsSource = OrderitemSource;
}
private void deleteclient(object sender, RoutedEventArgs e)
private void ListBox_SelectionChanged(object? sender, Avalonia.Controls.SelectionChangedEventArgs e)
{
if(sender != null)
{
try
{
idOrder = sender;
Order? order = db.Orders.Where(it => it == ListOrder.SelectedItem).FirstOrDefault();// ñîçäà¸ì ýêçåìïëÿð êëàññà Order
OrdersWithService? ordersWithService = db.OrdersWithServices.Where(it => it.Order == order.OrderId).FirstOrDefault();
var services = db.OrdersWithServices
.Where(ows => ows.Order == ordersWithService.Order)
.Select(ows => new Service
{
ServiceName = ows.ServiceNavigation.ServiceName,
ServiceCode = ows.ServiceNavigation.ServiceCode,
ServiceCostPerHour = ows.ServiceNavigation.ServiceCostPerHour
})
.ToList();
ServiceitemSource = new ObservableCollection<Service>(services);
ListService.ItemsSource = ServiceitemSource;
}
catch (Exception ex)
{
// Âûâîäèì ñîîáùåíèå îá îøèáêå â êîíñîëü
Console.WriteLine(ex.Message);
// Ìåíÿåì ñîîáùåíèå
MesDel.Text = "";
MesDelError.Text = "ïðîèçîøëà îøèáêà";
}
}
}
private void deleteOrder(object sender, RoutedEventArgs e)
{
try // Êîíñòðóêöèÿ äëÿ îáðàáîòêè èñêëþ÷åíèé
{
using (User11Context db = new User11Context()) // Ïîäêëþ÷åíèå ê ÁÄ
if (idOrder != null) // Ïðîâåðÿåì íàëè÷èå âàæíûõ äàííûõ
{
// Ñîáèðàåì âñå äàííûå ñ ýêðàíà
idOrder = int.Parse(TextID.Text);
Order? order = db.Orders.Where(it => it == ListOrder.SelectedItem).FirstOrDefault();// ñîçäà¸ì ýêçåìïëÿð êëàññà Order
int count = db.OrdersWithServices.Where(it => it.Order == order.OrderId).Count();
if (idOrder != null) // Ïðîâåðÿåì íàëè÷èå âàæíûõ äàííûõ
if (order != null)
{
Order? order = db.Orders.Where(it => it.OrderId == idOrder).FirstOrDefault(); // ñîçäà¸ì ýêçåìïëÿð êëàññà Order
int count = db.OrdersWithServices.Where(it => it.Order == order.OrderId).Count();
if (order != null)
for (int i = 0; i < count; i++)
{
for (int i = 0; i < count; i++)
{
OrdersWithService ordersWithService = db.OrdersWithServices.Where(it => it.Order == order.OrderId).FirstOrDefault();
db.OrdersWithServices.Remove(ordersWithService);
db.SaveChanges();
}
deleteclient2();
MesDel.Text = "Çàêàç óäàë¸í";
MesDelError.Text = "";
}
else
{
MesDel.Text = "Òàêîãî çàêàçà íåò";
OrdersWithService ordersWithService = db.OrdersWithServices.Where(it => it.Order == order.OrderId).FirstOrDefault();
db.OrdersWithServices.Remove(ordersWithService);
db.SaveChanges();
}
deleteOrder2(order);
// Ñòåðàåì âñå äàííûå
TextID.Text = "";
MesDel.Text = "Çàêàç óäàë¸í";
MesDelError.Text = "";
}
else
{
// Ìåíÿåì ñîîáùåíèå
MesDel.Text = "";
MesDelError.Text = "Íóæíî äîáàâèòü ID.";
MesDel.Text = "Òàêîãî çàêàçà íåò";
}
}
else
{
// Ìåíÿåì ñîîáùåíèå
MesDel.Text = "";
MesDelError.Text = "Íóæíî âûáðàòü çàêàç.";
}
}
catch (Exception ex)
{
@ -74,15 +108,18 @@ public partial class DeleteOrder : Window
}
private void deleteclient2()
private void deleteOrder2(Order Order)
{
try // Êîíñòðóêöèÿ äëÿ îáðàáîòêè èñêëþ÷åíèé
{
using (User11Context db = new User11Context()) // Ïîäêëþ÷åíèå ê ÁÄ
{
Order order = db.Orders.Where(it => it.OrderId == idOrder).FirstOrDefault();
Order order = db.Orders.Where(it => it.OrderId == Order.OrderId).FirstOrDefault();
db.Orders.Remove(order);
db.SaveChanges();
OrderitemSource.Remove(ListOrder.SelectedItem as Order);
ListService.ItemsSource = ServiceitemSource;
}
}
catch (Exception ex)