107 lines
3.5 KiB
C#
107 lines
3.5 KiB
C#
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);
|
||
}
|
||
} |