diff --git a/CarService/CarServiceBusinessLogic/BusinessLogics/WorkerLogic.cs b/CarService/CarServiceBusinessLogic/BusinessLogics/WorkerLogic.cs
index 82636e7..33a3e7a 100644
--- a/CarService/CarServiceBusinessLogic/BusinessLogics/WorkerLogic.cs
+++ b/CarService/CarServiceBusinessLogic/BusinessLogics/WorkerLogic.cs
@@ -67,7 +67,7 @@ namespace CarServiceBusinessLogic.BusinessLogics
public bool Delete(WorkerBindingModel model)
{
CheckModel(model, false);
- _logger.LogInformation("Delete. Id:{Id}", model.Id);
+ _logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_workerStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
@@ -79,12 +79,70 @@ namespace CarServiceBusinessLogic.BusinessLogics
{
if (model == null)
{
- throw new ArgumentNullException(nameof(model));
+ throw new ArgumentException(nameof(model));
}
if (!withParams)
{
return;
}
+ //Введён ли логин?
+ if (string.IsNullOrEmpty(model.Login))
+ {
+ _logger.LogWarning("Login is empty");
+ throw new ArgumentException("Не введён логин");
+ }
+ //Логин не должен быть больше 30 символов
+ if (model.Login.Length > 30)
+ {
+ _logger.LogWarning("Login {Login} length > 30", model.Login);
+ throw new ArgumentException("Длина логина не должна превышать 30 символов");
+ }
+ //Проверяем логин на уникальность
+ var existingWorker = _workerStorage.GetElement(new()
+ {
+ Login = model.Login
+ });
+ if (existingWorker != null)
+ {
+ _logger.LogWarning("Worker with login {Login} already exists", model.Login);
+ throw new ArgumentException("Сотрудник с таким логином уже существует");
+ }
+ //Введён ли пароль?
+ if (string.IsNullOrEmpty(model.Password))
+ {
+ _logger.LogWarning("Password is empty");
+ throw new ArgumentException("Не введён пароль");
+ }
+ //Пароль не должен быть менее 7 и более 30 символов
+ if (model.Password.Length < 7 || model.Password.Length > 30)
+ {
+ _logger.LogWarning("Password {Password} length > 30 or < 7", model.Password);
+ throw new ArgumentException("Длина пароля должна быть в промежутке между 7 и 30 символами");
+ }
+ //Введено ли имя?
+ if (string.IsNullOrEmpty(model.Name))
+ {
+ _logger.LogWarning("Name is empty");
+ throw new ArgumentException("Не введёно имя");
+ }
+ //Имя должно быть не более 30 символов
+ if (model.Name.Length > 30)
+ {
+ _logger.LogWarning("Name {Name} length > 30", model.Name);
+ throw new ArgumentException("Длина имени не должна превышать 30 символов");
+ }
+ //Введена ли фамилия?
+ if (string.IsNullOrEmpty(model.Surname))
+ {
+ _logger.LogWarning("Surname is empty");
+ throw new ArgumentException("Не введёна фамилия");
+ }
+ //Фамилия должна быть не более 30 символов
+ if (model.Surname.Length > 30)
+ {
+ _logger.LogWarning("Surname {Surname} length > 30", model.Surname);
+ throw new ArgumentException("Длина фамилии не должна превышать 30 символов");
+ }
_logger.LogInformation("Worker. Id: {Id}", model.Id);
}
}
diff --git a/CarService/CarServiceContracts/SearchModels/WorkerSearchModel.cs b/CarService/CarServiceContracts/SearchModels/WorkerSearchModel.cs
index f130d89..6098efd 100644
--- a/CarService/CarServiceContracts/SearchModels/WorkerSearchModel.cs
+++ b/CarService/CarServiceContracts/SearchModels/WorkerSearchModel.cs
@@ -3,5 +3,13 @@
public class WorkerSearchModel
{
public int? Id { get; set; }
+ ///
+ /// Поиск клиента по логину для проверки логина на уникальность и для входа
+ ///
+ public string? Login { get; set; }
+ ///
+ /// Поиск клиента по паролю для входа
+ ///
+ public string? Password { get; set; }
}
}
diff --git a/CarService/CarServiceDatabase/Implements/WorkerStorage.cs b/CarService/CarServiceDatabase/Implements/WorkerStorage.cs
index 5e734b5..c3f287b 100644
--- a/CarService/CarServiceDatabase/Implements/WorkerStorage.cs
+++ b/CarService/CarServiceDatabase/Implements/WorkerStorage.cs
@@ -21,14 +21,25 @@ namespace CarServiceDatabase.Implements
public WorkerViewModel? GetElement(WorkerSearchModel model)
{
using var context = new CarServiceDbContext();
- return context.Workers.FirstOrDefault(x=> x.Id == model.Id)?.GetViewModel;
+ if (model.Id.HasValue)//Сначала ищем по Id
+ {
+ return context.Workers.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
+ }
+ else if (!string.IsNullOrEmpty(model.Login) && string.IsNullOrEmpty(model.Password))//Затем по логину (для проверки уникальности)
+ {
+ return context.Workers.FirstOrDefault(x => x.Login == model.Login)?.GetViewModel;
+ }
+ else if (!string.IsNullOrEmpty(model.Login) && !string.IsNullOrEmpty(model.Password))//Затем по логину и паролю (для входа)
+ {
+ return context.Workers.FirstOrDefault(x => x.Login == model.Login && x.Password == model.Password)?.GetViewModel;
+ }
+ return null;
}
public WorkerViewModel? Insert(WorkerBindingModel model)
{
- //TODO проверка на униКАЛьность
using var context = new CarServiceDbContext();
var newWorker = Worker.Create(model);
- if(newWorker != null)
+ if (newWorker != null)
{
context.Workers.Add(newWorker);
context.SaveChanges();
@@ -38,11 +49,27 @@ namespace CarServiceDatabase.Implements
}
public WorkerViewModel? Update(WorkerBindingModel model)
{
- throw new NotImplementedException();
+ using var context = new CarServiceDbContext();
+ var worker = context.Workers.FirstOrDefault(x => x.Id == model.Id);
+ if (worker == null)
+ {
+ return null;
+ }
+ worker.Update(model);
+ context.SaveChanges();
+ return worker.GetViewModel;
}
public WorkerViewModel? Delete(WorkerBindingModel model)
{
- throw new NotImplementedException();
+ using var context = new CarServiceDbContext();
+ var worker = context.Workers.FirstOrDefault(x => x.Id == model.Id);
+ if (worker == null)
+ {
+ return null;
+ }
+ context.Workers.Remove(worker);
+ context.SaveChanges();
+ return worker.GetViewModel;
}
}
}
diff --git a/CarService/CarServiceDatabase/Models/Worker.cs b/CarService/CarServiceDatabase/Models/Worker.cs
index 17945c8..da0ff0f 100644
--- a/CarService/CarServiceDatabase/Models/Worker.cs
+++ b/CarService/CarServiceDatabase/Models/Worker.cs
@@ -42,17 +42,6 @@ namespace CarServiceDatabase.Models
Surname = model.Surname
};
}
- public static Worker Create(WorkerViewModel model)
- {
- return new()
- {
- Id = model.Id,
- Login = model.Login,
- Password = model.Password,
- Name = model.Name,
- Surname = model.Surname
- };
- }
public void Update(WorkerBindingModel? model)
{
if (model == null)
diff --git a/CarService/CarServiceView/FormRegistrationWorker.cs b/CarService/CarServiceView/FormRegistrationWorker.cs
index 2ca35f1..02e9aec 100644
--- a/CarService/CarServiceView/FormRegistrationWorker.cs
+++ b/CarService/CarServiceView/FormRegistrationWorker.cs
@@ -15,13 +15,20 @@ namespace CarServiceView
}
private void buttonRegister_Click(object sender, EventArgs e)
{
- _workerLogic.Create(new()
+ try
{
- Login = textBoxLogin.Text,
- Password = textBoxPassword.Text,
- Name = textBoxName.Text,
- Surname= textBoxSurname.Text
- });
+ _workerLogic.Create(new()
+ {
+ Login = textBoxLogin.Text,
+ Password = textBoxPassword.Text,
+ Name = textBoxName.Text,
+ Surname = textBoxSurname.Text
+ });
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
+ }
}
}
}