demka/demofinish/AddNewPosition.axaml.cs

63 lines
1.8 KiB
C#
Raw Normal View History

2025-04-18 11:41:58 +00:00
using System;
using System.Linq;
using Avalonia.Controls;
using Avalonia.Interactivity;
using demofinish.Model;
using Microsoft.EntityFrameworkCore;
namespace demofinish;
public partial class AddNewPosition : Window
{
public AddNewPosition()
{
InitializeComponent();
LoadData();
}
private void LoadData()
{
try
{
using var context = new User1Context();
AgentIdBox.ItemsSource = context.Productsales.Select(x => x.Agentid).ToList();
ProductIdBox.ItemsSource = context.Productsales.Select(x => x.Productid).ToList();
}
catch (Exception ex)
{
Console.WriteLine($"Ошибка загрузки данных: {ex.Message}");
}
}
private void AddPosition(object? sender, RoutedEventArgs e)
{
try
{
using var context = new User1Context();
var newPosition = new Productsale
{
Agentid = (int)(AgentIdBox.SelectedItem ?? throw new Exception("Не выбран агент")),
Productid = (int)(ProductIdBox.SelectedItem ?? throw new Exception("Не выбран продукт")),
Saledate = DateOnly.Parse(SalDateBox.Text ?? throw new Exception("Не указана дата")),
Productcount = int.Parse(CountBox.Text ?? throw new Exception("Не указано количество"))
};
context.Productsales.Add(newPosition);
context.SaveChanges();
Close();
}
catch (Exception ex)
{
Console.WriteLine($"Ошибка сохранения: {ex.Message}");
}
}
private void Button_OnClick(object? sender, RoutedEventArgs e)
{
Close();
}
}