diff --git a/UP01Task2App/UP01Task2App/Models/Client.cs b/UP01Task2App/UP01Task2App/Models/Client.cs new file mode 100644 index 0000000..81ad5ed --- /dev/null +++ b/UP01Task2App/UP01Task2App/Models/Client.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace UP01Task2App.Models +{ + public class Client + { + public string Surname { get; set; } + public string Name { get; set; } + public string Patronymic { get; set; } + public DateTime? BirthDate { get; set; } + public string Adress { get; set; } + public string PhoneNumber { get; set; } + public int SubscriptionNumber { get; set; } + + public override string ToString() + { + return Surname + " " + Name + " " + Patronymic; + } + } +} diff --git a/UP01Task2App/UP01Task2App/Models/Record.cs b/UP01Task2App/UP01Task2App/Models/Record.cs new file mode 100644 index 0000000..51625b8 --- /dev/null +++ b/UP01Task2App/UP01Task2App/Models/Record.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace UP01Task2App.Models +{ + public class Record + { + public Client Client { get; set; } + public Trainer Trainer { get; set; } + DateOnly RecordDate { set; get; } + TimeOnly RecordTime { set; get; } + } +} diff --git a/UP01Task2App/UP01Task2App/Models/Trainer.cs b/UP01Task2App/UP01Task2App/Models/Trainer.cs new file mode 100644 index 0000000..a3fa35b --- /dev/null +++ b/UP01Task2App/UP01Task2App/Models/Trainer.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace UP01Task2App.Models +{ + public class Trainer + { + public string Surmname { get; set; } + public string Name { get; set; } + public string Patronymic { get; set; } + public string Speciality { get; set; } + } +} diff --git a/UP01Task2App/UP01Task2App/Services/JsonService.cs b/UP01Task2App/UP01Task2App/Services/JsonService.cs new file mode 100644 index 0000000..f426a7e --- /dev/null +++ b/UP01Task2App/UP01Task2App/Services/JsonService.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Text.Json; +using System.Windows; +using System.Windows.Shapes; +using UP01Task2App.Models; + +namespace UP01Task2App.Services +{ + internal class JsonService + { + private const string trainerPath = "data/trainers.json"; + private const string clientPath = "data/clients.json"; + private const string recordPath = "data/records.json"; + + private void SaveToJson(string path, List data) + { + var options = new JsonSerializerOptions + { + WriteIndented = true + }; + + try + { + data ??= new List(); + + string json = JsonSerializer.Serialize(data, options); + + string? dir = System.IO.Path.GetDirectoryName(path); + if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir)) + Directory.CreateDirectory(dir); + + File.WriteAllText(path, json, Encoding.UTF8); + } + catch + { + throw new ArgumentException("Не удалось сохранить данные"); + } + } + + private List ReadFromJson(string path) + { + if (!File.Exists(path) || new FileInfo(path).Length == 0) + return new List(); + + string json = File.ReadAllText(path); + + if (string.IsNullOrWhiteSpace(json)) + return new List(); + + try + { + return JsonSerializer.Deserialize>(json) ?? new List(); + } + catch + { + return new List(); + } + } + + public List ReadTrainerDataFromJson() => ReadFromJson(trainerPath); + public List ReadClientDataFromJson() => ReadFromJson(clientPath); + public List ReadRecordDataFromJson() => ReadFromJson(recordPath); + + public void SaveTrainerDataToJson(List trainersList) => SaveToJson(trainerPath, trainersList); + public void SaveClientDataToJson(List clientsList) => SaveToJson(clientPath, clientsList); + public void SaveRecordDataToJson(List recordsList) => SaveToJson(recordPath, recordsList); + } +} diff --git a/UP01Task2App/UP01Task2App/Services/ValidationHelper.cs b/UP01Task2App/UP01Task2App/Services/ValidationHelper.cs new file mode 100644 index 0000000..dba71c7 --- /dev/null +++ b/UP01Task2App/UP01Task2App/Services/ValidationHelper.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Windows.Controls; + +namespace UP01Task2App.Services +{ + public static class ValidationHelper + { + public static void ValidateNotNullTextBox(TextBox obj, string fieldName) + { + if (string.IsNullOrWhiteSpace(obj.Text)) + throw new ArgumentException($"Поле \"{fieldName}\" не должно быть пустым"); + } + public static void ValidateNumTextBox(TextBox obj, string fieldName) + { + foreach (var c in obj.Text) + { + if (!char.IsDigit(c)) + throw new ArgumentException($"В поле \"{fieldName}\" можно вводить только числа"); + } + } + public static void ValidateDateNotInFutureTextBox(DatePicker obj, string fieldName) + { + if (obj.SelectedDate > DateTime.Now) + throw new ArgumentException($"В поле \"{fieldName}\" выбранная дата не может быть в будущем"); + } + public static void ValidateNotNullDatePicker(DatePicker obj, string fieldName) + { + if (obj.SelectedDate == null) + throw new ArgumentException($"Выберите дату в поле \"{fieldName}\""); + } + public static void ValidateIntRangeTextBox(TextBox obj, string fieldName) + { + if (!int.TryParse(obj.Text, out int _)) + throw new ArgumentException($"Введенное число в \"{fieldName}\" слишком большое или маленькое"); + } + } +} \ No newline at end of file diff --git a/UP01Task2App/UP01Task2App/ViewModels/BaseViewModel.cs b/UP01Task2App/UP01Task2App/ViewModels/BaseViewModel.cs deleted file mode 100644 index 9901540..0000000 --- a/UP01Task2App/UP01Task2App/ViewModels/BaseViewModel.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace UP01Task2App.ViewModels -{ - using System.ComponentModel; - using System.Runtime.CompilerServices; - - public class BaseViewModel : INotifyPropertyChanged - { - public event PropertyChangedEventHandler PropertyChanged; - - protected void OnPropertyChanged([CallerMemberName] string name = null) - { - PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); - } - } -} diff --git a/UP01Task2App/UP01Task2App/ViewModels/RelayCommand.cs b/UP01Task2App/UP01Task2App/ViewModels/RelayCommand.cs deleted file mode 100644 index a8b28e2..0000000 --- a/UP01Task2App/UP01Task2App/ViewModels/RelayCommand.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.Windows.Input; - -public class RelayCommand : ICommand -{ - private readonly Action _execute; - private readonly Func _canExecute; - - public RelayCommand(Action execute, Func canExecute = null) - { - _execute = execute; - _canExecute = canExecute; - } - - public bool CanExecute(object parameter) => _canExecute?.Invoke() ?? true; - - public void Execute(object parameter) => _execute(); - - public event EventHandler CanExecuteChanged; -} \ No newline at end of file diff --git a/UP01Task2App/UP01Task2App/Windows/ClientWindow.xaml b/UP01Task2App/UP01Task2App/Windows/ClientWindow.xaml index 9bdc2c4..41d504c 100644 --- a/UP01Task2App/UP01Task2App/Windows/ClientWindow.xaml +++ b/UP01Task2App/UP01Task2App/Windows/ClientWindow.xaml @@ -11,32 +11,32 @@ - + - + - + - + - + - + - + - - + + @@ -47,33 +47,27 @@ - +