new_demo/AddClient.axaml.cs
2025-03-09 17:00:49 +03:00

107 lines
3.5 KiB
C#
Raw Permalink 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.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text.RegularExpressions;
using Avalonia.Controls;
using Avalonia.Interactivity;
using demo_hard.Models;
namespace demo_hard;
public partial class AddClient : Window
{
public List<int> ClientIds = new List<int>();
public AddClient()
{
InitializeComponent();
using var contextForId = new User2Context();
ClientIds = contextForId.Clients.Select(c => c.Id).ToList();
}
public bool CheckInput()
{
if (CodeBox.Text.Length != 8)
{
UserOutput.Text ="В коде клиента должно быть ровно 8 символов";
return false;
}
if(SeriesBox.Text.Length!=4 || NumberPassBox.Text.Length!=6)
{
UserOutput.Text ="Неверно введены паспортные данные";
return false;
}
string pattern = @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|" + @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)" + @"@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$";
var regex = new Regex(pattern, RegexOptions.IgnoreCase);
if(!(regex.IsMatch(EmailBox.Text)))
{
UserOutput.Text ="Введите корректный email";
return false;
}
if (!DateOnly.TryParse(BirthdayBox.Text, out DateOnly birthday))
{
UserOutput.Text = "Введите корректную дату рождения";
return false;
}
return true;
}
private void AddClient_OnClick(object? sender, RoutedEventArgs e)
{
using var context = new User2Context();
if (string.IsNullOrWhiteSpace(CodeBox.Text) || string.IsNullOrWhiteSpace(FIOBox.Text) ||
string.IsNullOrWhiteSpace(EmailBox.Text) || string.IsNullOrWhiteSpace(AddressBox.Text) ||
string.IsNullOrWhiteSpace(BirthdayBox.Text) || string.IsNullOrWhiteSpace(SeriesBox.Text) ||
string.IsNullOrWhiteSpace(NumberPassBox.Text) || string.IsNullOrWhiteSpace(PasswordBox.Text))
{
UserOutput.Text = "Пожалуйста заполните все поля";
return;
}
if (CheckInput())
{
try
{
var passport = SeriesBox.Text + NumberPassBox.Text;
var newClient = new Client
{
Id = ClientIds.Last() + 1,
Fio = FIOBox.Text,
Email = EmailBox.Text,
ClientCode = Convert.ToInt32(CodeBox.Text),
Passport = passport,
Birthday = DateOnly.Parse(BirthdayBox.Text),
Address = AddressBox.Text,
Password = PasswordBox.Text,
RoleId = 4
};
context.Clients.Add(newClient);
context.SaveChanges();
UserOutput.Text = "Клиент успешно добавлен";
FIOBox.Text = "";
CodeBox.Text = "";
EmailBox.Text = "";
PasswordBox.Text = "";
SeriesBox.Text = "";
NumberPassBox.Text = "";
AddressBox.Text = "";
BirthdayBox.Text = "";
}
catch
{
CheckInput();
}
}
}
private void BackToOrder(object? sender, RoutedEventArgs e)
{
new OrderWindow().ShowDialog(this);
}
}