demka/demofinish/AddNewPosition.axaml.cs
2025-04-18 14:41:58 +03:00

63 lines
1.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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();
}
}