diff --git a/LawFirm/LawFirmBusinessLogic/BusinessLogics/CaseLogic.cs b/LawFirm/LawFirmBusinessLogic/BusinessLogics/CaseLogic.cs index aff1b6d..7a169ff 100644 --- a/LawFirm/LawFirmBusinessLogic/BusinessLogics/CaseLogic.cs +++ b/LawFirm/LawFirmBusinessLogic/BusinessLogics/CaseLogic.cs @@ -52,7 +52,6 @@ namespace LawFirmBusinessLogic.BusinessLogics _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); return element; } - public bool Create(CaseBindingModel model) { CheckModel(model); diff --git a/LawFirm/LawFirmBusinessLogic/BusinessLogics/CaseServiceLogic.cs b/LawFirm/LawFirmBusinessLogic/BusinessLogics/CaseServiceLogic.cs new file mode 100644 index 0000000..b3dd257 --- /dev/null +++ b/LawFirm/LawFirmBusinessLogic/BusinessLogics/CaseServiceLogic.cs @@ -0,0 +1,96 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.BusinessLogicsContracts; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StorageContracts; +using LawFirmContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmBusinessLogic.BusinessLogics +{ + public class CaseServiceLogic : ICaseServiceLogic + { + private readonly ILogger _logger; + private readonly ICaseServiceStorage _caseServiceStorage; + public CaseServiceLogic(ILogger logger, ICaseServiceStorage caseServiceStorage) + { + _logger = logger; + _caseServiceStorage = caseServiceStorage; + } + public List? ReadList(CaseServiceSearchModel? model) + { + _logger.LogInformation("ReadList. Id: {Id}", model?.Id); + var list = model == null ? _caseServiceStorage.GetFullList() : _caseServiceStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count: {Count}", list.Count); + return list; + } + public CaseServiceViewModel? ReadElement(CaseServiceSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Id: {Id}", model.Id); + var element = _caseServiceStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement found. Id: {Id}", element.Id); + return element; + } + public bool Create(CaseServiceBindingModel model) + { + CheckModel(model); + if (_caseServiceStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(CaseServiceBindingModel model) + { + CheckModel(model); + if (_caseServiceStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(CaseServiceBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_caseServiceStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(CaseServiceBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + _logger.LogInformation("CaseService. Id: {Id}", model.Id); + } + } +} diff --git a/LawFirm/LawFirmBusinessLogic/BusinessLogics/CustomerLogic.cs b/LawFirm/LawFirmBusinessLogic/BusinessLogics/CustomerLogic.cs index de21f31..09c6c09 100644 --- a/LawFirm/LawFirmBusinessLogic/BusinessLogics/CustomerLogic.cs +++ b/LawFirm/LawFirmBusinessLogic/BusinessLogics/CustomerLogic.cs @@ -21,30 +21,19 @@ namespace LawFirmBusinessLogic.BusinessLogics _logger = logger; _customerStorage = customerStorage; } - public bool Create(CustomerBindingModel model) + public List? ReadList(CustomerSearchModel? model) { - CheckModel(model); - if (_customerStorage.Insert(model) == null) + _logger.LogInformation("ReadList. Id: {Id}", model?.Id); + var list = model == null ? _customerStorage.GetFullList() : _customerStorage.GetFilteredList(model); + if (list == null) { - _logger.LogWarning("Insert operation failed"); - return false; + _logger.LogWarning("ReadList return null list"); + return null; } - return true; + _logger.LogInformation("ReadList. Count: {Count}", list.Count); + return list; } - - public bool Delete(CustomerBindingModel model) - { - CheckModel(model, false); - _logger.LogInformation("Delete. Id: {Id}", model.Id); - if (_customerStorage.Delete(model) == null) - { - _logger.LogWarning("Delete operation failed"); - return false; - } - return true; - } - - public CustomerViewModel ReadElement(CustomerSearchModel model) + public CustomerViewModel? ReadElement(CustomerSearchModel model) { if (model == null) { @@ -60,7 +49,16 @@ namespace LawFirmBusinessLogic.BusinessLogics _logger.LogInformation("ReadElement found. Id: {Id}", element.Id); return element; } - + public bool Create(CustomerBindingModel model) + { + CheckModel(model); + if (_customerStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } public bool Update(CustomerBindingModel model) { CheckModel(model); @@ -71,20 +69,17 @@ namespace LawFirmBusinessLogic.BusinessLogics } return true; } - - public List? ReadList(CustomerSearchModel? model) + public bool Delete(CustomerBindingModel model) { - _logger.LogInformation("ReadList. Id: {Id}", model?.Id); - var list = model == null ? _customerStorage.GetFullList() : _customerStorage.GetFilteredList(model); - if (list == null) + CheckModel(model, false); + _logger.LogInformation("Delete. Id: {Id}", model.Id); + if (_customerStorage.Delete(model) == null) { - _logger.LogWarning("ReadList return null list"); - return null; + _logger.LogWarning("Delete operation failed"); + return false; } - _logger.LogInformation("ReadList. Count: {Count}", list.Count); - return list; + return true; } - private void CheckModel(CustomerBindingModel model, bool withParams = true) { if (model == null) @@ -98,29 +93,36 @@ namespace LawFirmBusinessLogic.BusinessLogics if (string.IsNullOrEmpty(model.Login)) { _logger.LogWarning("Login is empty"); - throw new ArgumentNullException("Не заполнен логин"); + throw new ArgumentException("Не введён логин"); } - if (string.IsNullOrEmpty(model.Name)) + var existingCustomer = _customerStorage.GetElement(new() { - _logger.LogWarning("Name is empty"); - throw new ArgumentNullException("Не заполнено имя"); - } - if (string.IsNullOrEmpty(model.Surname)) + Login = model.Login + }); + if (existingCustomer != null) { - _logger.LogWarning("Surname is empty"); - throw new ArgumentNullException("Не заполнена фамилия"); + _logger.LogWarning("Customer with login {Login} already exists", model.Login); + throw new ArgumentException("Клиент с таким логином уже существует"); } if (string.IsNullOrEmpty(model.Password)) { _logger.LogWarning("Password is empty"); - throw new ArgumentNullException("Не заполнен пароль"); + throw new ArgumentException("Не введён пароль"); } - var ExistingCustomer = _customerStorage.GetElement - (new CustomerSearchModel() { Login = model.Login }); - if (ExistingCustomer != null) + if (model.Password.Length > 30) { - _logger.LogWarning("There is already a user with this login"); - throw new ArgumentException("Пользователь с таким логином уже есть"); + _logger.LogWarning("Password {Password} length > 30", model.Password); + throw new ArgumentException("Длина пароля должна быть не больше 30 символов"); + } + if (string.IsNullOrEmpty(model.Name)) + { + _logger.LogWarning("Name is empty"); + throw new ArgumentException("Не введёно имя"); + } + if (string.IsNullOrEmpty(model.Surname)) + { + _logger.LogWarning("Surname is empty"); + throw new ArgumentException("Не введёна фамилия"); } _logger.LogInformation("Customer. Id: {Id}", model.Id); } diff --git a/LawFirm/LawFirmBusinessLogic/BusinessLogics/WorkerLogic.cs b/LawFirm/LawFirmBusinessLogic/BusinessLogics/WorkerLogic.cs new file mode 100644 index 0000000..366754e --- /dev/null +++ b/LawFirm/LawFirmBusinessLogic/BusinessLogics/WorkerLogic.cs @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using LawFirmContracts.BindingModels; +using LawFirmContracts.BusinessLogicsContracts; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StorageContracts; +using LawFirmContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace LawFirmBusinessLogic.BusinessLogics +{ + public class WorkerLogic : IWorkerLogic + { + private readonly ILogger _logger; + private readonly IWorkerStorage _workerStorage; + public WorkerLogic(ILogger logger, IWorkerStorage workerStorage) + { + _logger = logger; + _workerStorage = workerStorage; + } + public List? ReadList(WorkerSearchModel? model) + { + _logger.LogInformation("ReadList. Id: {Id}", model?.Id); + var list = model == null ? _workerStorage.GetFullList() : _workerStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count: {Count}", list.Count); + return list; + } + public WorkerViewModel? ReadElement(WorkerSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Id: {Id}", model.Id); + var element = _workerStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement found. Id: {Id}", element.Id); + return element; + } + public bool Create(WorkerBindingModel model) + { + CheckModel(model); + if (_workerStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(WorkerBindingModel model) + { + CheckModel(model); + if (_workerStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(WorkerBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_workerStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(WorkerBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + _logger.LogInformation("Worker. Id: {Id}", model.Id); + } + } +} diff --git a/LawFirm/LawFirmContracts/BindingModels/CaseServiceBindingModel.cs b/LawFirm/LawFirmContracts/BindingModels/CaseServiceBindingModel.cs new file mode 100644 index 0000000..a0898b5 --- /dev/null +++ b/LawFirm/LawFirmContracts/BindingModels/CaseServiceBindingModel.cs @@ -0,0 +1,17 @@ +using LawFirmContracts.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmContracts.BindingModels +{ + public class CaseServiceBindingModel : ICaseServiceModel + { + public int Id { get; set; } + public int CaseId { get; set; } + public int ServiceId { get; set; } + } +} + diff --git a/LawFirm/LawFirmContracts/BindingModels/WorkerBindingModel.cs b/LawFirm/LawFirmContracts/BindingModels/WorkerBindingModel.cs new file mode 100644 index 0000000..d0ed510 --- /dev/null +++ b/LawFirm/LawFirmContracts/BindingModels/WorkerBindingModel.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using LawFirmContracts.Models; + +namespace LawFirmContracts.BindingModels +{ + public class WorkerBindingModel : IWorkerModel + { + public int Id { get; set; } + public string Login { get; set; } = string.Empty; + public string Password { get; set; } = string.Empty; + public string Name { get; set; } = string.Empty; + public string Surname { get; set; } = string.Empty; + } +} diff --git a/LawFirm/LawFirmContracts/BusinessLogicsContracts/ICaseServiceLogic.cs b/LawFirm/LawFirmContracts/BusinessLogicsContracts/ICaseServiceLogic.cs new file mode 100644 index 0000000..da7078d --- /dev/null +++ b/LawFirm/LawFirmContracts/BusinessLogicsContracts/ICaseServiceLogic.cs @@ -0,0 +1,20 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmContracts.BusinessLogicsContracts +{ + public interface ICaseServiceLogic + { + List? ReadList(CaseServiceSearchModel? model); + CaseServiceViewModel? ReadElement(CaseServiceSearchModel model); + bool Create(CaseServiceBindingModel model); + bool Update(CaseServiceBindingModel model); + bool Delete(CaseServiceBindingModel model); + } +} diff --git a/LawFirm/LawFirmContracts/BusinessLogicsContracts/IWorkerLogic.cs b/LawFirm/LawFirmContracts/BusinessLogicsContracts/IWorkerLogic.cs new file mode 100644 index 0000000..6ef6cda --- /dev/null +++ b/LawFirm/LawFirmContracts/BusinessLogicsContracts/IWorkerLogic.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.ViewModels; + +namespace LawFirmContracts.BusinessLogicsContracts +{ + public interface IWorkerLogic + { + List? ReadList(WorkerSearchModel? model); + WorkerViewModel? ReadElement(WorkerSearchModel model); + bool Create(WorkerBindingModel model); + bool Update(WorkerBindingModel model); + bool Delete(WorkerBindingModel model); + } +} diff --git a/LawFirm/LawFirmContracts/Models/ICaseServiceModel.cs b/LawFirm/LawFirmContracts/Models/ICaseServiceModel.cs new file mode 100644 index 0000000..bb441d6 --- /dev/null +++ b/LawFirm/LawFirmContracts/Models/ICaseServiceModel.cs @@ -0,0 +1,21 @@ +using LawFirmContracts.Id; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmContracts.Models +{ + public interface ICaseServiceModel : IId + { + /// + /// дела id + /// + int CaseId { get; } + /// + /// услуги id + /// + int ServiceId { get; } + } +} diff --git a/LawFirm/LawFirmContracts/Models/IWorkerModel.cs b/LawFirm/LawFirmContracts/Models/IWorkerModel.cs new file mode 100644 index 0000000..ee4ef98 --- /dev/null +++ b/LawFirm/LawFirmContracts/Models/IWorkerModel.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using LawFirmContracts.Id; + +namespace LawFirmContracts.Models +{ + public interface IWorkerModel : IId + { + /// + /// Логин + /// + string Login { get; } + /// + /// Пароль + /// + string Password { get; } + /// + /// Имя + /// + string Name { get; } + /// + /// Фамилия + /// + string Surname { get; } + } +} diff --git a/LawFirm/LawFirmContracts/SearchModels/CaseServiceSearchModel.cs b/LawFirm/LawFirmContracts/SearchModels/CaseServiceSearchModel.cs new file mode 100644 index 0000000..00ca861 --- /dev/null +++ b/LawFirm/LawFirmContracts/SearchModels/CaseServiceSearchModel.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmContracts.SearchModels +{ + public class CaseServiceSearchModel + { + public int? Id { get; set; } + } +} diff --git a/LawFirm/LawFirmContracts/SearchModels/CustomerSearchModel.cs b/LawFirm/LawFirmContracts/SearchModels/CustomerSearchModel.cs index c6f34ed..cf52d70 100644 --- a/LawFirm/LawFirmContracts/SearchModels/CustomerSearchModel.cs +++ b/LawFirm/LawFirmContracts/SearchModels/CustomerSearchModel.cs @@ -10,5 +10,6 @@ namespace LawFirmContracts.SearchModels { public int? Id { get; set; } public string? Login { get; set; } + public string? Password { get; set; } } } diff --git a/LawFirm/LawFirmContracts/SearchModels/WorkerSearchModel.cs b/LawFirm/LawFirmContracts/SearchModels/WorkerSearchModel.cs new file mode 100644 index 0000000..4ddd24c --- /dev/null +++ b/LawFirm/LawFirmContracts/SearchModels/WorkerSearchModel.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmContracts.SearchModels +{ + public class WorkerSearchModel + { + public int? Id { get; set; } + } +} diff --git a/LawFirm/LawFirmContracts/StorageContracts/ICaseServiceStorage.cs b/LawFirm/LawFirmContracts/StorageContracts/ICaseServiceStorage.cs new file mode 100644 index 0000000..c54caca --- /dev/null +++ b/LawFirm/LawFirmContracts/StorageContracts/ICaseServiceStorage.cs @@ -0,0 +1,21 @@ +using LawFirmContracts.SearchModels; +using LawFirmContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using LawFirmContracts.BindingModels; + +namespace LawFirmContracts.StorageContracts +{ + public interface ICaseServiceStorage + { + List GetFullList(); + List GetFilteredList(CaseServiceSearchModel model); + CaseServiceViewModel? GetElement(CaseServiceSearchModel model); + CaseServiceViewModel? Insert(CaseServiceBindingModel model); + CaseServiceViewModel? Update(CaseServiceBindingModel model); + CaseServiceViewModel? Delete(CaseServiceBindingModel model); + } +} diff --git a/LawFirm/LawFirmContracts/StorageContracts/IWorkerStorage.cs b/LawFirm/LawFirmContracts/StorageContracts/IWorkerStorage.cs new file mode 100644 index 0000000..602771e --- /dev/null +++ b/LawFirm/LawFirmContracts/StorageContracts/IWorkerStorage.cs @@ -0,0 +1,21 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmContracts.StorageContracts +{ + public interface IWorkerStorage + { + List GetFullList(); + List GetFilteredList(WorkerSearchModel model); + WorkerViewModel? GetElement(WorkerSearchModel model); + WorkerViewModel? Insert(WorkerBindingModel model); + WorkerViewModel? Update(WorkerBindingModel model); + WorkerViewModel? Delete(WorkerBindingModel model); + } +} diff --git a/LawFirm/LawFirmContracts/ViewModels/CaseServiceViewModel.cs b/LawFirm/LawFirmContracts/ViewModels/CaseServiceViewModel.cs new file mode 100644 index 0000000..c1e03af --- /dev/null +++ b/LawFirm/LawFirmContracts/ViewModels/CaseServiceViewModel.cs @@ -0,0 +1,16 @@ +using LawFirmContracts.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmContracts.ViewModels +{ + public class CaseServiceViewModel : ICaseServiceModel + { + public int Id { get; set; } + public int CaseId { get; set; } + public int ServiceId { get; set; } + } +} diff --git a/LawFirm/LawFirmContracts/ViewModels/WorkerViewModel.cs b/LawFirm/LawFirmContracts/ViewModels/WorkerViewModel.cs new file mode 100644 index 0000000..b02a23c --- /dev/null +++ b/LawFirm/LawFirmContracts/ViewModels/WorkerViewModel.cs @@ -0,0 +1,24 @@ +using LawFirmContracts.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmContracts.ViewModels +{ + public class WorkerViewModel : IWorkerModel + { + public int Id { get; set; } + [DisplayName("Логин")] + public string Login { get; set; } = string.Empty; + [DisplayName("Пароль")] + public string Password { get; set; } = string.Empty; + [DisplayName("Имя")] + public string Name { get; set; } = string.Empty; + [DisplayName("Фамилия")] + public string Surname { get; set; } = string.Empty; + } +} + diff --git a/LawFirm/LawFirmDatabase/Implements/CaseServiceStorage.cs b/LawFirm/LawFirmDatabase/Implements/CaseServiceStorage.cs new file mode 100644 index 0000000..1341365 --- /dev/null +++ b/LawFirm/LawFirmDatabase/Implements/CaseServiceStorage.cs @@ -0,0 +1,86 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StorageContracts; +using LawFirmContracts.ViewModels; +using LawFirmDatabase.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmDatabase.Implements +{ + public class CaseServiceStorage : ICaseServiceStorage + { + public List GetFullList() + { + using var context = new LawFirmDBContext(); + return context.CaseServices + .Include(x => x.Case) + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(CaseServiceSearchModel model) + { + using var context = new LawFirmDBContext(); + return context.CaseServices + .Where(x => x.Id == model.Id) + .Include(x => x.Case) + .Select(x => x.GetViewModel) + .ToList(); + } + public CaseServiceViewModel? GetElement(CaseServiceSearchModel model) + { + if (model == null) + { + return null; + } + using var context = new LawFirmDBContext(); + if (model.Id.HasValue) + { + return context.CaseServices + .Include(x => x.Case) + .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + return null; + } + public CaseServiceViewModel? Insert(CaseServiceBindingModel model) + { + using var context = new LawFirmDBContext(); + var newCaseService = CaseService.Create(context, model); + if (newCaseService != null) + { + context.CaseServices.Add(newCaseService); + context.SaveChanges(); + return newCaseService.GetViewModel; + } + return null; + } + public CaseServiceViewModel? Update(CaseServiceBindingModel model) + { + using var context = new LawFirmDBContext(); + var CaseService = context.CaseServices.FirstOrDefault(x => x.Id == model.Id); + if (CaseService == null) + { + return null; + } + CaseService.Update(context, model); + context.SaveChanges(); + return CaseService.GetViewModel; + } + public CaseServiceViewModel? Delete(CaseServiceBindingModel model) + { + using var context = new LawFirmDBContext(); + var CaseService = context.CaseServices.FirstOrDefault(x => x.Id == model.Id); + if (CaseService == null) + { + return null; + } + context.CaseServices.Remove(CaseService); + context.SaveChanges(); + return CaseService.GetViewModel; + } + } +} diff --git a/LawFirm/LawFirmDatabase/Implements/CustomerStorage.cs b/LawFirm/LawFirmDatabase/Implements/CustomerStorage.cs index 48b44ae..40d15cd 100644 --- a/LawFirm/LawFirmDatabase/Implements/CustomerStorage.cs +++ b/LawFirm/LawFirmDatabase/Implements/CustomerStorage.cs @@ -16,23 +16,38 @@ namespace LawFirmDatabase.Implements public List GetFullList() { using var context = new LawFirmDBContext(); - return context.Customers.Select(x => x.GetViewModel).ToList(); + return context.Customers + .Select(x => x.GetViewModel) + .ToList(); } public List GetFilteredList(CustomerSearchModel model) { using var context = new LawFirmDBContext(); - return context.Customers.Select(x => x.GetViewModel).Where(x => x.Id == model.Id).ToList(); + return context.Customers + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); } public CustomerViewModel? GetElement(CustomerSearchModel model) { + if (model == null) + { + return null; + } using var context = new LawFirmDBContext(); if (model.Id.HasValue) { return context.Customers.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; } - else if (!string.IsNullOrEmpty(model.Login)) + else if (!string.IsNullOrEmpty(model.Login) && string.IsNullOrEmpty(model.Password)) { - return context.Customers.FirstOrDefault(x => x.Login == model.Login)?.GetViewModel; + return context.Customers + .FirstOrDefault(x => x.Login == model.Login)?.GetViewModel; + } + else if (!string.IsNullOrEmpty(model.Login) && !string.IsNullOrEmpty(model.Password)) + { + return context.Customers + .FirstOrDefault(x => x.Login == model.Login && x.Password == model.Password)?.GetViewModel; } return null; } @@ -51,7 +66,8 @@ namespace LawFirmDatabase.Implements public CustomerViewModel? Update(CustomerBindingModel model) { using var context = new LawFirmDBContext(); - var customer = context.Customers.FirstOrDefault(x => x.Id == model.Id); + var customer = context.Customers + .FirstOrDefault(x => x.Id == model.Id); if (customer == null) { return null; @@ -63,7 +79,8 @@ namespace LawFirmDatabase.Implements public CustomerViewModel? Delete(CustomerBindingModel model) { using var context = new LawFirmDBContext(); - var customer = context.Customers.FirstOrDefault(x => x.Id == model.Id); + var customer = context.Customers + .FirstOrDefault(x => x.Id == model.Id); if (customer == null) { return null; diff --git a/LawFirm/LawFirmDatabase/Implements/ItemStorage.cs b/LawFirm/LawFirmDatabase/Implements/ItemStorage.cs index 7af399d..44e97d6 100644 --- a/LawFirm/LawFirmDatabase/Implements/ItemStorage.cs +++ b/LawFirm/LawFirmDatabase/Implements/ItemStorage.cs @@ -18,12 +18,12 @@ namespace LawFirmDatabase.Implements public List GetFullList() { using var context = new LawFirmDBContext(); - return context.Items.Include(x => x.Services).Select(x => x.GetViewModel).ToList(); + return context.Items.Include(x => x.Payment).Select(x => x.GetViewModel).ToList(); } public List GetFilteredList(ItemSearchModel model) { using var context = new LawFirmDBContext(); - return context.Items.Where(x => x.Id == model.Id).Include(x => x.Services).Select(x => x.GetViewModel).ToList(); + return context.Items.Where(x => x.Id == model.Id).Include(x => x.Payment).Select(x => x.GetViewModel).ToList(); } public ItemViewModel? GetElement(ItemSearchModel model) { @@ -34,11 +34,11 @@ namespace LawFirmDatabase.Implements using var context = new LawFirmDBContext(); if (model.Id.HasValue) { - return context.Items.Include(x => x.Services).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + return context.Items.Include(x => x.Payment).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; } if (!string.IsNullOrEmpty(model.Name)) { - return context.Items.Include(x => x.Services).FirstOrDefault(x => x.Name == model.Name)?.GetViewModel; + return context.Items.Include(x => x.Payment).FirstOrDefault(x => x.Name == model.Name)?.GetViewModel; } return null; } diff --git a/LawFirm/LawFirmDatabase/Implements/WorkerStorage.cs b/LawFirm/LawFirmDatabase/Implements/WorkerStorage.cs new file mode 100644 index 0000000..abbb601 --- /dev/null +++ b/LawFirm/LawFirmDatabase/Implements/WorkerStorage.cs @@ -0,0 +1,84 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StorageContracts; +using LawFirmContracts.ViewModels; +using LawFirmDatabase.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmDatabase.Implements +{ + public class WorkerStorage : IWorkerStorage + { + public List GetFullList() + { + using var context = new LawFirmDBContext(); + return context.Workers + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(WorkerSearchModel model) + { + using var context = new LawFirmDBContext(); + return context.Workers + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + public WorkerViewModel? GetElement(WorkerSearchModel model) + { + if (model == null) + { + return null; + } + using var context = new LawFirmDBContext(); + if (model.Id.HasValue) + { + return context.Workers.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + //другие варианты поиска не реализуются (заглушка для роли Worker) + return null; + } + public WorkerViewModel? Insert(WorkerBindingModel model) + { + using var context = new LawFirmDBContext(); + var newWorker = Worker.Create(model); + if (newWorker != null) + { + context.Workers.Add(newWorker); + context.SaveChanges(); + return newWorker.GetViewModel; + } + return null; + } + public WorkerViewModel? Update(WorkerBindingModel model) + { + using var context = new LawFirmDBContext(); + 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) + { + using var context = new LawFirmDBContext(); + 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/LawFirm/LawFirmDatabase/LawFirmDBContext.cs b/LawFirm/LawFirmDatabase/LawFirmDBContext.cs index 927268e..c427529 100644 --- a/LawFirm/LawFirmDatabase/LawFirmDBContext.cs +++ b/LawFirm/LawFirmDatabase/LawFirmDBContext.cs @@ -26,9 +26,11 @@ namespace LawFirmDatabase base.OnConfiguring(optionsBuilder); } public virtual DbSet Customers { get; set; } + public virtual DbSet Workers { get; set; } public virtual DbSet Items { get; set; } public virtual DbSet Cases { get; set; } public virtual DbSet Payments { get; set; } public virtual DbSet Services { get; set; } + public virtual DbSet CaseServices { get; set; } } } \ No newline at end of file diff --git a/LawFirm/LawFirmDatabase/Migrations/20230408032638_InitMigration.Designer.cs b/LawFirm/LawFirmDatabase/Migrations/20230408133321_InitialCreate.Designer.cs similarity index 66% rename from LawFirm/LawFirmDatabase/Migrations/20230408032638_InitMigration.Designer.cs rename to LawFirm/LawFirmDatabase/Migrations/20230408133321_InitialCreate.Designer.cs index 3509f68..12a0a78 100644 --- a/LawFirm/LawFirmDatabase/Migrations/20230408032638_InitMigration.Designer.cs +++ b/LawFirm/LawFirmDatabase/Migrations/20230408133321_InitialCreate.Designer.cs @@ -1,4 +1,5 @@ // +using System; using LawFirmDatabase; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; @@ -11,8 +12,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace LawFirmDatabase.Migrations { [DbContext(typeof(LawFirmDBContext))] - [Migration("20230408032638_InitMigration")] - partial class InitMigration + [Migration("20230408133321_InitialCreate")] + partial class InitialCreate { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -32,20 +33,52 @@ namespace LawFirmDatabase.Migrations SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + b.Property("CaseId") + .HasColumnType("int"); + b.Property("CustomerId") .HasColumnType("int"); + b.Property("DateCreated") + .HasColumnType("date"); + b.Property("Name") .IsRequired() .HasColumnType("nvarchar(max)"); + b.Property("WorkerId") + .HasColumnType("int"); + b.HasKey("Id"); - b.HasIndex("CustomerId"); + b.HasIndex("CaseId"); + + b.HasIndex("WorkerId"); b.ToTable("Cases"); }); + modelBuilder.Entity("LawFirmDatabase.Models.CaseService", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CaseId") + .HasColumnType("int"); + + b.Property("ServiceId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("CaseId"); + + b.ToTable("CaseServices"); + }); + modelBuilder.Entity("LawFirmDatabase.Models.Customer", b => { b.Property("Id") @@ -83,6 +116,9 @@ namespace LawFirmDatabase.Migrations SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + b.Property("ItemId") + .HasColumnType("int"); + b.Property("Name") .IsRequired() .HasColumnType("nvarchar(max)"); @@ -95,7 +131,7 @@ namespace LawFirmDatabase.Migrations b.HasKey("Id"); - b.HasIndex("PaymentId"); + b.HasIndex("ItemId"); b.ToTable("Items"); }); @@ -111,8 +147,11 @@ namespace LawFirmDatabase.Migrations b.Property("CaseId") .HasColumnType("int"); + b.Property("DatePayment") + .HasColumnType("date"); + b.Property("Sum") - .HasColumnType("decimal (10,2)"); + .HasColumnType("decimal (15,2)"); b.HasKey("Id"); @@ -140,72 +179,106 @@ namespace LawFirmDatabase.Migrations .HasColumnType("nvarchar(max)"); b.Property("Price") - .HasColumnType("decimal (10,2)"); + .HasColumnType("decimal (15,2)"); b.HasKey("Id"); b.HasIndex("CaseId"); - b.HasIndex("ItemId"); - b.ToTable("Services"); }); + modelBuilder.Entity("LawFirmDatabase.Models.Worker", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Login") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Surname") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Workers"); + }); + modelBuilder.Entity("LawFirmDatabase.Models.Case", b => { b.HasOne("LawFirmDatabase.Models.Customer", "Customer") .WithMany("Cases") - .HasForeignKey("CustomerId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Customer"); - }); - - modelBuilder.Entity("LawFirmDatabase.Models.Item", b => - { - b.HasOne("LawFirmDatabase.Models.Payment", "Payments") - .WithMany("Items") - .HasForeignKey("PaymentId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Payments"); - }); - - modelBuilder.Entity("LawFirmDatabase.Models.Payment", b => - { - b.HasOne("LawFirmDatabase.Models.Case", "Cases") - .WithMany("Payments") .HasForeignKey("CaseId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - b.Navigation("Cases"); + b.HasOne("LawFirmDatabase.Models.Worker", null) + .WithMany("Cases") + .HasForeignKey("WorkerId"); + + b.Navigation("Customer"); }); - modelBuilder.Entity("LawFirmDatabase.Models.Service", b => + modelBuilder.Entity("LawFirmDatabase.Models.CaseService", b => { - b.HasOne("LawFirmDatabase.Models.Case", "Cases") + b.HasOne("LawFirmDatabase.Models.Case", "Case") + .WithMany("CaseServices") + .HasForeignKey("CaseId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Case"); + }); + + modelBuilder.Entity("LawFirmDatabase.Models.Item", b => + { + b.HasOne("LawFirmDatabase.Models.Payment", "Payment") + .WithMany() + .HasForeignKey("ItemId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Payment"); + }); + + modelBuilder.Entity("LawFirmDatabase.Models.Payment", b => + { + b.HasOne("LawFirmDatabase.Models.Case", "Case") .WithMany() .HasForeignKey("CaseId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - b.HasOne("LawFirmDatabase.Models.Item", "Items") - .WithMany("Services") - .HasForeignKey("ItemId") + b.Navigation("Case"); + }); + + modelBuilder.Entity("LawFirmDatabase.Models.Service", b => + { + b.HasOne("LawFirmDatabase.Models.Case", "Case") + .WithMany() + .HasForeignKey("CaseId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - b.Navigation("Cases"); - - b.Navigation("Items"); + b.Navigation("Case"); }); modelBuilder.Entity("LawFirmDatabase.Models.Case", b => { - b.Navigation("Payments"); + b.Navigation("CaseServices"); }); modelBuilder.Entity("LawFirmDatabase.Models.Customer", b => @@ -213,14 +286,9 @@ namespace LawFirmDatabase.Migrations b.Navigation("Cases"); }); - modelBuilder.Entity("LawFirmDatabase.Models.Item", b => + modelBuilder.Entity("LawFirmDatabase.Models.Worker", b => { - b.Navigation("Services"); - }); - - modelBuilder.Entity("LawFirmDatabase.Models.Payment", b => - { - b.Navigation("Items"); + b.Navigation("Cases"); }); #pragma warning restore 612, 618 } diff --git a/LawFirm/LawFirmDatabase/Migrations/20230408032638_InitMigration.cs b/LawFirm/LawFirmDatabase/Migrations/20230408133321_InitialCreate.cs similarity index 65% rename from LawFirm/LawFirmDatabase/Migrations/20230408032638_InitMigration.cs rename to LawFirm/LawFirmDatabase/Migrations/20230408133321_InitialCreate.cs index 0722275..a7b7acb 100644 --- a/LawFirm/LawFirmDatabase/Migrations/20230408032638_InitMigration.cs +++ b/LawFirm/LawFirmDatabase/Migrations/20230408133321_InitialCreate.cs @@ -1,11 +1,12 @@ -using Microsoft.EntityFrameworkCore.Migrations; +using System; +using Microsoft.EntityFrameworkCore.Migrations; #nullable disable namespace LawFirmDatabase.Migrations { /// - public partial class InitMigration : Migration + public partial class InitialCreate : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) @@ -26,24 +27,68 @@ namespace LawFirmDatabase.Migrations table.PrimaryKey("PK_Customers", x => x.Id); }); + migrationBuilder.CreateTable( + name: "Workers", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Login = table.Column(type: "nvarchar(max)", nullable: false), + Password = table.Column(type: "nvarchar(max)", nullable: false), + Name = table.Column(type: "nvarchar(max)", nullable: false), + Surname = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Workers", x => x.Id); + }); + migrationBuilder.CreateTable( name: "Cases", columns: table => new { Id = table.Column(type: "int", nullable: false) .Annotation("SqlServer:Identity", "1, 1"), + DateCreated = table.Column(type: "date", nullable: false), Name = table.Column(type: "nvarchar(max)", nullable: false), - CustomerId = table.Column(type: "int", nullable: false) + CustomerId = table.Column(type: "int", nullable: false), + CaseId = table.Column(type: "int", nullable: false), + WorkerId = table.Column(type: "int", nullable: true) }, constraints: table => { table.PrimaryKey("PK_Cases", x => x.Id); table.ForeignKey( - name: "FK_Cases_Customers_CustomerId", - column: x => x.CustomerId, + name: "FK_Cases_Customers_CaseId", + column: x => x.CaseId, principalTable: "Customers", principalColumn: "Id", onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_Cases_Workers_WorkerId", + column: x => x.WorkerId, + principalTable: "Workers", + principalColumn: "Id"); + }); + + migrationBuilder.CreateTable( + name: "CaseServices", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + CaseId = table.Column(type: "int", nullable: false), + ServiceId = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_CaseServices", x => x.Id); + table.ForeignKey( + name: "FK_CaseServices_Cases_CaseId", + column: x => x.CaseId, + principalTable: "Cases", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateTable( @@ -52,7 +97,8 @@ namespace LawFirmDatabase.Migrations { Id = table.Column(type: "int", nullable: false) .Annotation("SqlServer:Identity", "1, 1"), - Sum = table.Column(type: "decimal (10,2)", nullable: false), + DatePayment = table.Column(type: "date", nullable: false), + Sum = table.Column(type: "decimal (15,2)", nullable: false), CaseId = table.Column(type: "int", nullable: false) }, constraints: table => @@ -66,27 +112,6 @@ namespace LawFirmDatabase.Migrations onDelete: ReferentialAction.Cascade); }); - migrationBuilder.CreateTable( - name: "Items", - columns: table => new - { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - Name = table.Column(type: "nvarchar(max)", nullable: false), - Price = table.Column(type: "decimal (10,2)", nullable: false), - PaymentId = table.Column(type: "int", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Items", x => x.Id); - table.ForeignKey( - name: "FK_Items_Payments_PaymentId", - column: x => x.PaymentId, - principalTable: "Payments", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - migrationBuilder.CreateTable( name: "Services", columns: table => new @@ -94,7 +119,7 @@ namespace LawFirmDatabase.Migrations Id = table.Column(type: "int", nullable: false) .Annotation("SqlServer:Identity", "1, 1"), Name = table.Column(type: "nvarchar(max)", nullable: false), - Price = table.Column(type: "decimal (10,2)", nullable: false), + Price = table.Column(type: "decimal (15,2)", nullable: false), ItemId = table.Column(type: "int", nullable: false), CaseId = table.Column(type: "int", nullable: false) }, @@ -107,23 +132,49 @@ namespace LawFirmDatabase.Migrations principalTable: "Cases", principalColumn: "Id", onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Items", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "decimal (10,2)", nullable: false), + PaymentId = table.Column(type: "int", nullable: false), + ItemId = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Items", x => x.Id); table.ForeignKey( - name: "FK_Services_Items_ItemId", + name: "FK_Items_Payments_ItemId", column: x => x.ItemId, - principalTable: "Items", + principalTable: "Payments", principalColumn: "Id", onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateIndex( - name: "IX_Cases_CustomerId", + name: "IX_Cases_CaseId", table: "Cases", - column: "CustomerId"); + column: "CaseId"); migrationBuilder.CreateIndex( - name: "IX_Items_PaymentId", + name: "IX_Cases_WorkerId", + table: "Cases", + column: "WorkerId"); + + migrationBuilder.CreateIndex( + name: "IX_CaseServices_CaseId", + table: "CaseServices", + column: "CaseId"); + + migrationBuilder.CreateIndex( + name: "IX_Items_ItemId", table: "Items", - column: "PaymentId"); + column: "ItemId"); migrationBuilder.CreateIndex( name: "IX_Payments_CaseId", @@ -134,22 +185,20 @@ namespace LawFirmDatabase.Migrations name: "IX_Services_CaseId", table: "Services", column: "CaseId"); - - migrationBuilder.CreateIndex( - name: "IX_Services_ItemId", - table: "Services", - column: "ItemId"); } /// protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropTable( - name: "Services"); + name: "CaseServices"); migrationBuilder.DropTable( name: "Items"); + migrationBuilder.DropTable( + name: "Services"); + migrationBuilder.DropTable( name: "Payments"); @@ -158,6 +207,9 @@ namespace LawFirmDatabase.Migrations migrationBuilder.DropTable( name: "Customers"); + + migrationBuilder.DropTable( + name: "Workers"); } } } diff --git a/LawFirm/LawFirmDatabase/Migrations/LawFirmDBContextModelSnapshot.cs b/LawFirm/LawFirmDatabase/Migrations/LawFirmDBContextModelSnapshot.cs index b55a5f5..305c9af 100644 --- a/LawFirm/LawFirmDatabase/Migrations/LawFirmDBContextModelSnapshot.cs +++ b/LawFirm/LawFirmDatabase/Migrations/LawFirmDBContextModelSnapshot.cs @@ -1,4 +1,5 @@ // +using System; using LawFirmDatabase; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; @@ -29,20 +30,52 @@ namespace LawFirmDatabase.Migrations SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + b.Property("CaseId") + .HasColumnType("int"); + b.Property("CustomerId") .HasColumnType("int"); + b.Property("DateCreated") + .HasColumnType("date"); + b.Property("Name") .IsRequired() .HasColumnType("nvarchar(max)"); + b.Property("WorkerId") + .HasColumnType("int"); + b.HasKey("Id"); - b.HasIndex("CustomerId"); + b.HasIndex("CaseId"); + + b.HasIndex("WorkerId"); b.ToTable("Cases"); }); + modelBuilder.Entity("LawFirmDatabase.Models.CaseService", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CaseId") + .HasColumnType("int"); + + b.Property("ServiceId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("CaseId"); + + b.ToTable("CaseServices"); + }); + modelBuilder.Entity("LawFirmDatabase.Models.Customer", b => { b.Property("Id") @@ -80,6 +113,9 @@ namespace LawFirmDatabase.Migrations SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + b.Property("ItemId") + .HasColumnType("int"); + b.Property("Name") .IsRequired() .HasColumnType("nvarchar(max)"); @@ -92,7 +128,7 @@ namespace LawFirmDatabase.Migrations b.HasKey("Id"); - b.HasIndex("PaymentId"); + b.HasIndex("ItemId"); b.ToTable("Items"); }); @@ -108,8 +144,11 @@ namespace LawFirmDatabase.Migrations b.Property("CaseId") .HasColumnType("int"); + b.Property("DatePayment") + .HasColumnType("date"); + b.Property("Sum") - .HasColumnType("decimal (10,2)"); + .HasColumnType("decimal (15,2)"); b.HasKey("Id"); @@ -137,72 +176,106 @@ namespace LawFirmDatabase.Migrations .HasColumnType("nvarchar(max)"); b.Property("Price") - .HasColumnType("decimal (10,2)"); + .HasColumnType("decimal (15,2)"); b.HasKey("Id"); b.HasIndex("CaseId"); - b.HasIndex("ItemId"); - b.ToTable("Services"); }); + modelBuilder.Entity("LawFirmDatabase.Models.Worker", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Login") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Surname") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Workers"); + }); + modelBuilder.Entity("LawFirmDatabase.Models.Case", b => { b.HasOne("LawFirmDatabase.Models.Customer", "Customer") .WithMany("Cases") - .HasForeignKey("CustomerId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Customer"); - }); - - modelBuilder.Entity("LawFirmDatabase.Models.Item", b => - { - b.HasOne("LawFirmDatabase.Models.Payment", "Payments") - .WithMany("Items") - .HasForeignKey("PaymentId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Payments"); - }); - - modelBuilder.Entity("LawFirmDatabase.Models.Payment", b => - { - b.HasOne("LawFirmDatabase.Models.Case", "Cases") - .WithMany("Payments") .HasForeignKey("CaseId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - b.Navigation("Cases"); + b.HasOne("LawFirmDatabase.Models.Worker", null) + .WithMany("Cases") + .HasForeignKey("WorkerId"); + + b.Navigation("Customer"); }); - modelBuilder.Entity("LawFirmDatabase.Models.Service", b => + modelBuilder.Entity("LawFirmDatabase.Models.CaseService", b => { - b.HasOne("LawFirmDatabase.Models.Case", "Cases") + b.HasOne("LawFirmDatabase.Models.Case", "Case") + .WithMany("CaseServices") + .HasForeignKey("CaseId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Case"); + }); + + modelBuilder.Entity("LawFirmDatabase.Models.Item", b => + { + b.HasOne("LawFirmDatabase.Models.Payment", "Payment") + .WithMany() + .HasForeignKey("ItemId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Payment"); + }); + + modelBuilder.Entity("LawFirmDatabase.Models.Payment", b => + { + b.HasOne("LawFirmDatabase.Models.Case", "Case") .WithMany() .HasForeignKey("CaseId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - b.HasOne("LawFirmDatabase.Models.Item", "Items") - .WithMany("Services") - .HasForeignKey("ItemId") + b.Navigation("Case"); + }); + + modelBuilder.Entity("LawFirmDatabase.Models.Service", b => + { + b.HasOne("LawFirmDatabase.Models.Case", "Case") + .WithMany() + .HasForeignKey("CaseId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - b.Navigation("Cases"); - - b.Navigation("Items"); + b.Navigation("Case"); }); modelBuilder.Entity("LawFirmDatabase.Models.Case", b => { - b.Navigation("Payments"); + b.Navigation("CaseServices"); }); modelBuilder.Entity("LawFirmDatabase.Models.Customer", b => @@ -210,14 +283,9 @@ namespace LawFirmDatabase.Migrations b.Navigation("Cases"); }); - modelBuilder.Entity("LawFirmDatabase.Models.Item", b => + modelBuilder.Entity("LawFirmDatabase.Models.Worker", b => { - b.Navigation("Services"); - }); - - modelBuilder.Entity("LawFirmDatabase.Models.Payment", b => - { - b.Navigation("Items"); + b.Navigation("Cases"); }); #pragma warning restore 612, 618 } diff --git a/LawFirm/LawFirmDatabase/Models/Case.cs b/LawFirm/LawFirmDatabase/Models/Case.cs index 638535c..27b4025 100644 --- a/LawFirm/LawFirmDatabase/Models/Case.cs +++ b/LawFirm/LawFirmDatabase/Models/Case.cs @@ -17,17 +17,17 @@ namespace LawFirmDatabase.Models { public class Case : ICaseModel { + [Required, Column(TypeName = "date")] + public DateTime DateCreated { get; private set; } = DateTime.Now; public int Id { get; private set; } [Required] public string Name { get; private set; } = string.Empty; - [NotMapped] - public DateTime DateCreated { get; private set; } [Required] public int CustomerId { get; private set; } [ForeignKey("CaseId")] - public virtual List Payments { get; set; } = new(); + public virtual List CaseServices { get; set; } = new(); public virtual Customer Customer { get; set; } = new(); - public static Case? Create(LawFirmDBContext context, ICaseModel? model) + public static Case? Create(LawFirmDBContext context, CaseBindingModel? model) { if (model == null) { @@ -35,27 +35,27 @@ namespace LawFirmDatabase.Models } return new() { - Id = model.Id, + Name= model.Name, DateCreated = model.DateCreated, Customer = context.Customers.First(x => x.Id == model.CustomerId) }; } - - public void Update(LawFirmDBContext context, ICaseModel? model) + public void Update(LawFirmDBContext context, CaseBindingModel? model) { if (model == null) { return; } - Id = model.Id; + Name = model.Name; DateCreated = model.DateCreated; Customer = context.Customers.First(x => x.Id == model.CustomerId); - } - public CaseViewModel? GetViewModel => new() + + } + public CaseViewModel GetViewModel => new() { Id = Id, - Name = Name, + Name= Name, DateCreated = DateCreated, CustomerId = CustomerId }; diff --git a/LawFirm/LawFirmDatabase/Models/CaseService.cs b/LawFirm/LawFirmDatabase/Models/CaseService.cs new file mode 100644 index 0000000..7873a63 --- /dev/null +++ b/LawFirm/LawFirmDatabase/Models/CaseService.cs @@ -0,0 +1,50 @@ +using LawFirmContracts.Models; +using LawFirmContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmDatabase.Models +{ + public class CaseService : ICaseServiceModel + { + public int Id { get; private set; } + [Required] + public int CaseId { get; private set; } + [Required] + public int ServiceId { get; private set; } + public virtual Case Case { get; set; } = new(); + public static CaseService? Create(LawFirmDBContext context, ICaseServiceModel? model) + { + if (model == null) + { + return null; + } + return new() + { + + Case = context.Cases.First(x => x.Id == model.CaseId), + }; + } + + public void Update(LawFirmDBContext context, ICaseServiceModel? model) + { + if (model == null) + { + return; + } + + Case = context.Cases.First(x => x.Id == model.CaseId); + } + + public CaseServiceViewModel? GetViewModel => new() + { + Id = Id, + CaseId = CaseId, + ServiceId = ServiceId + }; + } +} diff --git a/LawFirm/LawFirmDatabase/Models/Customer.cs b/LawFirm/LawFirmDatabase/Models/Customer.cs index de3aeef..f81d572 100644 --- a/LawFirm/LawFirmDatabase/Models/Customer.cs +++ b/LawFirm/LawFirmDatabase/Models/Customer.cs @@ -24,9 +24,10 @@ namespace LawFirmDatabase.Models public string Name { get; private set; } = string.Empty; [Required] public string Surname { get; private set; } = string.Empty; - [ForeignKey("CustomerId")] + + [ForeignKey("CaseId")] public virtual List Cases { get; set; } = new(); - public static Customer? Create(ICustomerModel? model) + public static Customer? Create(CustomerBindingModel? model) { if (model == null) { @@ -38,29 +39,28 @@ namespace LawFirmDatabase.Models Login = model.Login, Password = model.Password, Name = model.Name, - Surname = model.Surname, + Surname = model.Surname }; } - - public void Update(ICustomerModel? model) + public void Update(CustomerBindingModel? model) { if (model == null) { return; } + Id = model.Id; Login = model.Login; Password = model.Password; Name = model.Name; Surname = model.Surname; } - public CustomerViewModel GetViewModel => new() { Id = Id, Login = Login, Password = Password, Name = Name, - Surname = Surname + Surname = Surname, }; } } diff --git a/LawFirm/LawFirmDatabase/Models/Item.cs b/LawFirm/LawFirmDatabase/Models/Item.cs index fd7349f..72f6157 100644 --- a/LawFirm/LawFirmDatabase/Models/Item.cs +++ b/LawFirm/LawFirmDatabase/Models/Item.cs @@ -19,8 +19,7 @@ namespace LawFirmDatabase.Models [Required] public int PaymentId { get; private set; } [ForeignKey("ItemId")] - public virtual List Services { get; set; } = new(); - public virtual Payment Payments { get; set; } = new(); + public virtual Payment Payment { get; set; } = new(); public static Item? Create(LawFirmDBContext context, ItemBindingModel? model) { if (model == null) @@ -31,7 +30,7 @@ namespace LawFirmDatabase.Models { Name = model.Name, Price = model.Price, - Payments = context.Payments.First(x => x.Id == model.PaymentId) + Payment = context.Payments.First(x => x.Id == model.PaymentId) }; } public void Update(LawFirmDBContext context, ItemBindingModel? model) @@ -42,14 +41,14 @@ namespace LawFirmDatabase.Models } Name = model.Name; Price = model.Price; - Payments = context.Payments.First(x => x.Id == model.PaymentId); + Payment = context.Payments.First(x => x.Id == model.PaymentId); } public ItemViewModel GetViewModel => new() { Id = Id, Name = Name, Price = Price, - PaymentId = PaymentId, + PaymentId = PaymentId }; } } \ No newline at end of file diff --git a/LawFirm/LawFirmDatabase/Models/Payment.cs b/LawFirm/LawFirmDatabase/Models/Payment.cs index 8c5c6aa..c9ecf98 100644 --- a/LawFirm/LawFirmDatabase/Models/Payment.cs +++ b/LawFirm/LawFirmDatabase/Models/Payment.cs @@ -16,15 +16,13 @@ namespace LawFirmDatabase.Models public class Payment : IPaymentModel { public int Id { get; private set; } - [NotMapped] + [Required, Column(TypeName = "date")] public DateTime DatePayment { get; private set; } = DateTime.Now; - [Required, Column(TypeName = "decimal (10,2)")] + [Required, Column(TypeName = "decimal (15,2)")] public decimal Sum { get; private set; } [Required] public int CaseId { get; private set; } - [ForeignKey("PaymentId")] - public virtual List Items { get; set; } = new(); - public virtual Case Cases { get; set; } = new(); + public virtual Case Case { get; set; } = new(); public static Payment? Create(LawFirmDBContext context, IPaymentModel? model) { @@ -35,9 +33,9 @@ namespace LawFirmDatabase.Models return new() { Id = model.Id, - Sum = model.Sum, DatePayment = model.DatePayment, - Cases = context.Cases.First(x => x.Id == model.CaseId) + Sum = model.Sum, + Case = context.Cases.First(x => x.Id == model.CaseId) }; } @@ -48,16 +46,16 @@ namespace LawFirmDatabase.Models return; } Id = model.Id; - Sum = model.Sum; DatePayment = model.DatePayment; - Cases = context.Cases.First(x => x.Id == model.CaseId); + Sum = model.Sum; + Case = context.Cases.First(x => x.Id == model.CaseId); } public PaymentViewModel? GetViewModel => new() { Id = Id, - Sum = Sum, DatePayment = DatePayment, + Sum = Sum, CaseId = CaseId, }; } diff --git a/LawFirm/LawFirmDatabase/Models/Service.cs b/LawFirm/LawFirmDatabase/Models/Service.cs index af2ddf1..c1d0b03 100644 --- a/LawFirm/LawFirmDatabase/Models/Service.cs +++ b/LawFirm/LawFirmDatabase/Models/Service.cs @@ -17,13 +17,13 @@ namespace LawFirmDatabase.Models public int Id { get; private set; } [Required] public string Name { get; private set; } = string.Empty; - [Required, Column(TypeName = "decimal (10,2)")] + [Required, Column(TypeName = "decimal (15,2)")] public decimal Price { get; private set; } [Required] public int ItemId { get; private set; } + [Required] public int CaseId { get; private set; } - public virtual Item Items { get; set; } = new(); - public virtual Case Cases { get; set; } = new(); + public virtual Case Case { get; set; } = new(); public static Service? Create(LawFirmDBContext context, IServiceModel? model) { @@ -36,8 +36,7 @@ namespace LawFirmDatabase.Models Id = model.Id, Name = model.Name, Price = model.Price, - Items = context.Items.First(x => x.Id == model.ItemId), - Cases = context.Cases.First(x => x.Id == model.CaseId) + Case = context.Cases.First(x => x.Id == model.CaseId) }; } @@ -50,16 +49,14 @@ namespace LawFirmDatabase.Models Id = model.Id; Name = model.Name; Price = model.Price; - Items = context.Items.First(x => x.Id == model.ItemId); - Cases = context.Cases.First(x => x.Id == model.CaseId); + Case = context.Cases.First(x => x.Id == model.CaseId); } public ServiceViewModel? GetViewModel => new() { Id = Id, Name = Name, - Price = Price, - ItemId = ItemId, + Price = Price }; } } diff --git a/LawFirm/LawFirmDatabase/Models/Worker.cs b/LawFirm/LawFirmDatabase/Models/Worker.cs new file mode 100644 index 0000000..4f33e6e --- /dev/null +++ b/LawFirm/LawFirmDatabase/Models/Worker.cs @@ -0,0 +1,65 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.Models; +using LawFirmContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Azure; + +namespace LawFirmDatabase.Models +{ + public class Worker : IWorkerModel + { + public int Id { get; private set; } + [Required] + public string Login { get; private set; } = string.Empty; + [Required] + public string Password { get; private set; } = string.Empty; + [Required] + public string Name { get; private set; } = string.Empty; + [Required] + public string Surname { get; private set; } = string.Empty; + [Required] + [ForeignKey("WorkerId")] + public virtual List Cases { get; set; } = new(); + public static Worker? Create(WorkerBindingModel? model) + { + if (model == null) + { + return null; + } + 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) + { + return; + } + Id = model.Id; + Login = model.Login; + Password = model.Password; + Name = model.Name; + Surname = model.Surname; + } + public WorkerViewModel GetViewModel => new() + { + Id = Id, + Login = Login, + Password = Password, + Name = Name, + Surname = Surname + }; + } +}