Потихоньку исправляем структуру под MVVM.
This commit is contained in:
parent
22632b08d5
commit
6797533d76
4
App.xaml
4
App.xaml
@ -1,8 +1,6 @@
|
||||
<Application x:Class="EmployeeManager.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:EmployeeManager"
|
||||
StartupUri="View/MainWindow.xaml">
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
||||
<Application.Resources>
|
||||
<!-- Стиль для закругленных кнопок -->
|
||||
<Style x:Key="RoundedButtonStyle" TargetType="Button">
|
||||
|
32
App.xaml.cs
32
App.xaml.cs
@ -1,4 +1,8 @@
|
||||
using System.Configuration;
|
||||
using EmployeeManager.Model.Data;
|
||||
using EmployeeManager.View;
|
||||
using EmployeeManager.ViewModel;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Configuration;
|
||||
using System.Data;
|
||||
using System.Windows;
|
||||
|
||||
@ -9,6 +13,32 @@ namespace EmployeeManager
|
||||
/// </summary>
|
||||
public partial class App : Application
|
||||
{
|
||||
public IServiceProvider ServiceProvider { get; private set; }
|
||||
|
||||
protected override void OnStartup(StartupEventArgs e)
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
|
||||
// Регистрация DbContext
|
||||
services.AddDbContext<ApplicationConext>();
|
||||
|
||||
// Регистрация сервисов
|
||||
services.AddScoped<IPhysicalPersonService, PhysicalPersonService>();
|
||||
|
||||
// Регистрация ViewModel
|
||||
services.AddTransient<DataManageViewModel>();
|
||||
|
||||
// Регистрация окон
|
||||
services.AddTransient<MainWindow>();
|
||||
|
||||
// Создаем ServiceProvider
|
||||
ServiceProvider = services.BuildServiceProvider();
|
||||
|
||||
// Открываем главное окно
|
||||
var mainWindow = ActivatorUtilities.CreateInstance<MainWindow>(ServiceProvider);
|
||||
mainWindow.Show();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,113 +1,80 @@
|
||||
using EmployeeManager.Model.Data;
|
||||
using EmployeeManager.Model;
|
||||
using EmployeeManager.Model.Data;
|
||||
using System.Data.Common;
|
||||
using System.Linq;
|
||||
|
||||
namespace EmployeeManager.Model
|
||||
public interface IPhysicalPersonService
|
||||
{
|
||||
public static class DataWorker
|
||||
List<PhysicalPerson> GetAllPhysicalPersons();
|
||||
string CreatePhysicalPerson(PhysicalPersonDto personDto);
|
||||
string EditPhysicalPerson(int id, PhysicalPersonDto updatedData);
|
||||
string DeletePhysicalPerson(int id);
|
||||
}
|
||||
|
||||
public class PhysicalPersonService : IPhysicalPersonService
|
||||
{
|
||||
private readonly ApplicationConext _context;
|
||||
|
||||
public PhysicalPersonService(ApplicationConext context)
|
||||
{
|
||||
public static string CreatePhysicalPerson(string namePhysicalPersons, string surnamePhysicalPersons, string patronomicPhysicalPersons, DateTime birthday, string gender, string address, string telephone)
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public List<PhysicalPerson> GetAllPhysicalPersons() => _context.PhysicalPersons.ToList();
|
||||
|
||||
public string CreatePhysicalPerson(PhysicalPersonDto personDto)
|
||||
{
|
||||
if (_context.PhysicalPersons.Any(p =>
|
||||
p.NamePhysicalPersons == personDto.Name &&
|
||||
p.SurnamePhysicalPersons == personDto.Surname &&
|
||||
p.Birthday == personDto.Birthday))
|
||||
{
|
||||
string result = "Возникла ошибка. Обратитесь в ЛАОД.";
|
||||
|
||||
using(ApplicationConext db = new ApplicationConext())
|
||||
{
|
||||
bool checkIsExist = db.PhysicalPersons.Any(
|
||||
e => e.NamePhysicalPersons == namePhysicalPersons
|
||||
&& e.SurnamePhysicalPersons == surnamePhysicalPersons
|
||||
&& e.PatronomicPhysicalPersons == patronomicPhysicalPersons
|
||||
&& e.Birthday == birthday
|
||||
);
|
||||
|
||||
if (!checkIsExist)
|
||||
{
|
||||
PhysicalPerson newPhysicalPerson = new PhysicalPerson
|
||||
{
|
||||
NamePhysicalPersons = namePhysicalPersons,
|
||||
SurnamePhysicalPersons = surnamePhysicalPersons,
|
||||
PatronomicPhysicalPersons = patronomicPhysicalPersons,
|
||||
Birthday = birthday,
|
||||
Gender = gender,
|
||||
Address = address,
|
||||
Telephone = telephone
|
||||
};
|
||||
db.PhysicalPersons.Add(newPhysicalPerson);
|
||||
db.SaveChanges();
|
||||
result = "Физическое лицо добавлено.";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
return "Физическое лицо уже существует.";
|
||||
}
|
||||
|
||||
public static string CreateEmployee(string nameJob, DateTime startJob, DateTime? endJob, string? partTimeJob, float bid, PhysicalPerson? physicalPersons)
|
||||
var newPerson = new PhysicalPerson
|
||||
{
|
||||
string result = "Возникла ошибка. Обратитесь в ЛАОД.";
|
||||
NamePhysicalPersons = personDto.Name,
|
||||
SurnamePhysicalPersons = personDto.Surname,
|
||||
PatronomicPhysicalPersons = personDto.Patronomic,
|
||||
Birthday = personDto.Birthday,
|
||||
Gender = personDto.Gender,
|
||||
Address = personDto.Address,
|
||||
Telephone = personDto.Telephone
|
||||
};
|
||||
|
||||
using (ApplicationConext db = new ApplicationConext())
|
||||
{
|
||||
_context.PhysicalPersons.Add(newPerson);
|
||||
_context.SaveChanges();
|
||||
return "Физическое лицо добавлено.";
|
||||
}
|
||||
|
||||
Employee newEmployee = new Employee
|
||||
{
|
||||
NameJob = nameJob,
|
||||
StartJob = startJob,
|
||||
EndJob = endJob,
|
||||
PartTimeJob = partTimeJob,
|
||||
Bid = bid,
|
||||
PhysicalPersonsId = physicalPersons.Id
|
||||
|
||||
};
|
||||
db.Employees.Add(newEmployee);
|
||||
db.SaveChanges();
|
||||
result = "Работник добавлен.";
|
||||
public string EditPhysicalPerson(int id, PhysicalPersonDto updatedData)
|
||||
{
|
||||
var person = _context.PhysicalPersons.FirstOrDefault(p => p.Id == id);
|
||||
if (person == null)
|
||||
return "Физическое лицо не найдено.";
|
||||
|
||||
return result;
|
||||
person.NamePhysicalPersons = updatedData.Name;
|
||||
person.SurnamePhysicalPersons = updatedData.Surname;
|
||||
person.PatronomicPhysicalPersons = updatedData.Patronomic;
|
||||
person.Birthday = updatedData.Birthday;
|
||||
person.Gender = updatedData.Gender;
|
||||
person.Address = updatedData.Address;
|
||||
person.Telephone = updatedData.Telephone;
|
||||
|
||||
}
|
||||
}
|
||||
_context.SaveChanges();
|
||||
return "Данные физического лица обновлены.";
|
||||
}
|
||||
|
||||
public static string DeletePhysicalPerson(PhysicalPerson physicalPerson)
|
||||
{
|
||||
string result = "Возникла ошибка. Обратитесь в ЛАОД.";
|
||||
public string DeletePhysicalPerson(int id)
|
||||
{
|
||||
var person = _context.PhysicalPersons.FirstOrDefault(p => p.Id == id);
|
||||
if (person == null)
|
||||
return "Физическое лицо не найдено.";
|
||||
|
||||
using(ApplicationConext db = new ApplicationConext())
|
||||
{
|
||||
db.PhysicalPersons.Remove(physicalPerson);
|
||||
db.SaveChanges();
|
||||
result = "Физическое лицо удалено.";
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
public static string EditPhysicalPerson(PhysicalPerson oldPhysicalPerson, string newNamePhysicalPersons, string newSurnamePhysicalPersons, string newPatronomicPhysicalPersons, DateTime newBirthday, string newGender, string newAddress, string newTelephone)
|
||||
{
|
||||
string result = "Возникла ошибка. Обратитесь в ЛАОД.";
|
||||
|
||||
using (ApplicationConext db = new ApplicationConext())
|
||||
{
|
||||
PhysicalPerson physicalPerson = db.PhysicalPersons.FirstOrDefault(e => e.Id == oldPhysicalPerson.Id);
|
||||
|
||||
physicalPerson.SurnamePhysicalPersons = newSurnamePhysicalPersons;
|
||||
physicalPerson.NamePhysicalPersons = newNamePhysicalPersons;
|
||||
physicalPerson.PatronomicPhysicalPersons = newPatronomicPhysicalPersons;
|
||||
physicalPerson.Birthday = newBirthday;
|
||||
physicalPerson.Gender = newGender;
|
||||
physicalPerson.Address = newAddress;
|
||||
physicalPerson.Telephone = newTelephone;
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
public static List<PhysicalPerson> GetAllPhysicalPerson()
|
||||
{
|
||||
using (ApplicationConext db = new ApplicationConext())
|
||||
{
|
||||
var result = db.PhysicalPersons.ToList();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
_context.PhysicalPersons.Remove(person);
|
||||
_context.SaveChanges();
|
||||
return "Физическое лицо удалено.";
|
||||
}
|
||||
}
|
||||
|
||||
|
19
Model/PhysicalPersonDto.cs
Normal file
19
Model/PhysicalPersonDto.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace EmployeeManager.Model
|
||||
{
|
||||
public class PhysicalPersonDto
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Surname { get; set; }
|
||||
public string Patronomic { get; set; }
|
||||
public DateTime Birthday { get; set; }
|
||||
public string Gender { get; set; }
|
||||
public string Address { get; set; }
|
||||
public string Telephone { get; set; }
|
||||
}
|
||||
}
|
@ -17,10 +17,13 @@ namespace EmployeeManager.View
|
||||
/// </summary>
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
public MainWindow()
|
||||
private readonly DataManageViewModel _viewModel;
|
||||
|
||||
public MainWindow(DataManageViewModel viewModel)
|
||||
{
|
||||
InitializeComponent();
|
||||
DataContext = new DataManageViewModel();
|
||||
_viewModel = viewModel;
|
||||
DataContext = _viewModel;
|
||||
}
|
||||
}
|
||||
}
|
@ -15,15 +15,12 @@ using System.Windows.Shapes;
|
||||
|
||||
namespace EmployeeManager.View.PhysicalPerson
|
||||
{
|
||||
/// <summary>
|
||||
/// Логика взаимодействия для AddPhysicalPersonWindow.xaml
|
||||
/// </summary>
|
||||
public partial class AddPhysicalPersonWindow : Window
|
||||
{
|
||||
public AddPhysicalPersonWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
DataContext = new DataManageViewModel();
|
||||
//DataContext = new DataManageViewModel();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ namespace EmployeeManager.View.PhysicalPerson
|
||||
public PhysicalPersoManagementWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
DataContext = new DataManageViewModel();
|
||||
//DataContext = new DataManageViewModel();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,109 +12,53 @@ namespace EmployeeManager.ViewModel
|
||||
{
|
||||
public class DataManageViewModel : INotifyPropertyChanged
|
||||
{
|
||||
private List<PhysicalPerson> allPhysicalPeoples = DataWorker.GetAllPhysicalPerson();
|
||||
private readonly IPhysicalPersonService _physicalPersonService;
|
||||
|
||||
public DataManageViewModel(IPhysicalPersonService physicalPersonService)
|
||||
{
|
||||
_physicalPersonService = physicalPersonService;
|
||||
AllPhysicalPeoples = _physicalPersonService.GetAllPhysicalPersons();
|
||||
}
|
||||
|
||||
private List<PhysicalPerson> allPhysicalPeoples;
|
||||
public List<PhysicalPerson> AllPhysicalPeoples
|
||||
{
|
||||
get { return allPhysicalPeoples; }
|
||||
get => allPhysicalPeoples;
|
||||
set
|
||||
{
|
||||
allPhysicalPeoples = value;
|
||||
NotifyPropertyChanged("AllPhysicalPeoples");
|
||||
NotifyPropertyChanged(nameof(AllPhysicalPeoples));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public string NamePhysicalPersons { get; set; } = string.Empty;
|
||||
public string SurnamePhysicalPersons { get; set; } = string.Empty;
|
||||
public string PatronomicPhysicalPersons { get; set; } = string.Empty;
|
||||
public string NamePhysicalPersons { get; set; }
|
||||
public string SurnamePhysicalPersons { get; set; }
|
||||
public string PatronomicPhysicalPersons { get; set; }
|
||||
public DateTime Birthday { get; set; }
|
||||
public string Gender { get; set; } = string.Empty;
|
||||
public string Address { get; set; } = string.Empty;
|
||||
public string Telephone { get; set; } = string.Empty;
|
||||
public string Gender { get; set; }
|
||||
public string Address { get; set; }
|
||||
public string Telephone { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private RelayCommand addNewPhysicalPerson;
|
||||
public RelayCommand AddNewPhysicalPerson
|
||||
public RelayCommand AddNewPhysicalPersonCommand => new RelayCommand(obj =>
|
||||
{
|
||||
get
|
||||
var newPerson = new PhysicalPersonDto
|
||||
{
|
||||
return addNewPhysicalPerson ?? new RelayCommand(obj =>
|
||||
{
|
||||
string resultStr = "";
|
||||
|
||||
resultStr = DataWorker.CreatePhysicalPerson(NamePhysicalPersons, SurnamePhysicalPersons, PatronomicPhysicalPersons, Birthday, Gender, Address, Telephone);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private RelayCommand openPhysicalPersoManagementWindow;
|
||||
public RelayCommand OpenPhysicalPersoManagementWindow
|
||||
{
|
||||
get
|
||||
{
|
||||
return openPhysicalPersoManagementWindow ?? new RelayCommand(obj =>
|
||||
{
|
||||
OpenPhysicalPersoManagementWindowMethod();
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private RelayCommand openAddPhysicalPersonWindow;
|
||||
public RelayCommand OpenAddPhysicalPersonWindow
|
||||
{
|
||||
get
|
||||
{
|
||||
return openAddPhysicalPersonWindow ?? new RelayCommand(obj =>
|
||||
{
|
||||
OpenAddPhysicalPersonWindowMethod();
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private void OpenPhysicalPersoManagementWindowMethod()
|
||||
{
|
||||
PhysicalPersoManagementWindow newPhysicalPersoManagementWindow = new PhysicalPersoManagementWindow();
|
||||
SetCenterPositionAndOpen(newPhysicalPersoManagementWindow);
|
||||
}
|
||||
|
||||
private void OpenAddPhysicalPersonWindowMethod()
|
||||
{
|
||||
AddPhysicalPersonWindow newAddPhysicalPersonWindow = new AddPhysicalPersonWindow();
|
||||
SetCenterPositionAndOpen(newAddPhysicalPersonWindow);
|
||||
}
|
||||
|
||||
private void SetCenterPositionAndOpen(Window window)
|
||||
{
|
||||
window.Owner = Application.Current.MainWindow;
|
||||
window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
|
||||
window.ShowDialog();
|
||||
}
|
||||
|
||||
|
||||
Name = NamePhysicalPersons,
|
||||
Surname = SurnamePhysicalPersons,
|
||||
Patronomic = PatronomicPhysicalPersons,
|
||||
Birthday = Birthday,
|
||||
Gender = Gender,
|
||||
Address = Address,
|
||||
Telephone = Telephone
|
||||
};
|
||||
|
||||
string result = _physicalPersonService.CreatePhysicalPerson(newPerson);
|
||||
// Обновить список
|
||||
AllPhysicalPeoples = _physicalPersonService.GetAllPhysicalPersons();
|
||||
});
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
private void NotifyPropertyChanged(String propertyName)
|
||||
{
|
||||
if (PropertyChanged != null)
|
||||
{
|
||||
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
}
|
||||
private void NotifyPropertyChanged(string propertyName) =>
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user