diff --git a/AutoWorkshopDatabaseImplement/AutoWorkshopDatabase.cs b/AutoWorkshopDatabaseImplement/AutoWorkshopDatabase.cs index ced4c8f..31fe0d0 100644 --- a/AutoWorkshopDatabaseImplement/AutoWorkshopDatabase.cs +++ b/AutoWorkshopDatabaseImplement/AutoWorkshopDatabase.cs @@ -25,5 +25,7 @@ namespace AutoWorkshopDatabaseImplement public virtual DbSet RepairComponents { set; get; } public virtual DbSet Orders { set; get; } + + public virtual DbSet Clients { set; get; } } } diff --git a/AutoWorkshopDatabaseImplement/Implements/ClientStorage.cs b/AutoWorkshopDatabaseImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..b75941f --- /dev/null +++ b/AutoWorkshopDatabaseImplement/Implements/ClientStorage.cs @@ -0,0 +1,94 @@ +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.SearchModels; +using AutoWorkshopContracts.StoragesContracts; +using AutoWorkshopContracts.ViewModels; +using AutoWorkshopDatabaseImplement.Models; + +namespace AutoWorkshopDatabaseImplement.Implements +{ + public class ClientStorage : IClientStorage + { + public List GetFullList() + { + using var Context = new AutoWorkshopDatabase(); + + return Context.Clients + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ClientSearchModel Model) + { + if (string.IsNullOrEmpty(Model.ClientFIO) && string.IsNullOrEmpty(Model.Email) && string.IsNullOrEmpty(Model.Password)) + return new(); + + using var Context = new AutoWorkshopDatabase(); + + return Context.Clients + .Where(x => (string.IsNullOrEmpty(Model.ClientFIO) || x.ClientFIO.Contains(Model.ClientFIO) && + (string.IsNullOrEmpty(Model.Email) || x.Email.Contains(Model.Email)) && + (string.IsNullOrEmpty(Model.Password) || x.Password.Contains(Model.Password)))) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ClientViewModel? GetElement(ClientSearchModel Model) + { + if (string.IsNullOrEmpty(Model.ClientFIO) && string.IsNullOrEmpty(Model.Email) && string.IsNullOrEmpty(Model.Password) && + !Model.Id.HasValue) + { + return null; + } + + using var Context = new AutoWorkshopDatabase(); + + return Context.Clients + .FirstOrDefault(x => (string.IsNullOrEmpty(Model.ClientFIO) || x.ClientFIO == Model.ClientFIO) && + (!Model.Id.HasValue || x.Id == Model.Id) && (string.IsNullOrEmpty(Model.Email) || x.Email == Model.Email) && + (string.IsNullOrEmpty(Model.Password) || x.Password == Model.Password))? + .GetViewModel; + } + + public ClientViewModel? Insert(ClientBindingModel Model) + { + var NewClient = Client.Create(Model); + if (NewClient == null) + return null; + + using var Context = new AutoWorkshopDatabase(); + + Context.Clients.Add(NewClient); + Context.SaveChanges(); + + return NewClient.GetViewModel; + } + + public ClientViewModel? Update(ClientBindingModel Model) + { + using var Context = new AutoWorkshopDatabase(); + var Client = Context.Clients.FirstOrDefault(x => x.Id == Model.Id); + + if (Client == null) + return null; + + Client.Update(Model); + Context.SaveChanges(); + + return Client.GetViewModel; + } + + public ClientViewModel? Delete(ClientBindingModel Model) + { + using var Context = new AutoWorkshopDatabase(); + var Element = Context.Clients.FirstOrDefault(rec => rec.Id == Model.Id); + + if (Element == null) + return null; + + Context.Clients.Remove(Element); + Context.SaveChanges(); + + return Element.GetViewModel; + } + } +} diff --git a/AutoWorkshopDatabaseImplement/Implements/OrderStorage.cs b/AutoWorkshopDatabaseImplement/Implements/OrderStorage.cs index 06674b5..1c08b12 100644 --- a/AutoWorkshopDatabaseImplement/Implements/OrderStorage.cs +++ b/AutoWorkshopDatabaseImplement/Implements/OrderStorage.cs @@ -4,7 +4,6 @@ using AutoWorkshopContracts.StoragesContracts; using AutoWorkshopContracts.ViewModels; using AutoWorkshopDatabaseImplement.Models; using Microsoft.EntityFrameworkCore; -using System; namespace AutoWorkshopDatabaseImplement.Implements { @@ -16,6 +15,7 @@ namespace AutoWorkshopDatabaseImplement.Implements return Context.Orders .Include(x => x.Repair) + .Include(x => x.Client) .Select(x => x.GetViewModel) .ToList(); } @@ -31,14 +31,16 @@ namespace AutoWorkshopDatabaseImplement.Implements { return Context.Orders .Include(x => x.Repair) - .Where(x => x.DateCreate >= Model.DateFrom && x.DateCreate <= Model.DateTo) + .Include(x => x.Client) + .Where(x => x.DateCreate >= Model.DateFrom && x.DateCreate <= Model.DateTo && (!Model.ClientId.HasValue || x.ClientId == Model.ClientId)) .Select(x => x.GetViewModel) .ToList(); } return Context.Orders .Include(x => x.Repair) - .Where(x => x.Id == Model.Id) + .Include(x => x.Client) + .Where(x => x.Id == Model.Id && (!Model.ClientId.HasValue || x.ClientId == Model.ClientId)) .Select(x => x.GetViewModel) .ToList(); } @@ -52,6 +54,7 @@ namespace AutoWorkshopDatabaseImplement.Implements return Context.Orders .Include(x => x.Repair) + .Include(x => x.Client) .FirstOrDefault(x => Model.Id.HasValue && x.Id == Model.Id)? .GetViewModel; } @@ -70,7 +73,7 @@ namespace AutoWorkshopDatabaseImplement.Implements Context.Orders.Add(NewOrder); Context.SaveChanges(); - return Context.Orders.Include(x => x.Repair).FirstOrDefault(x => x.Id == NewOrder.Id)?.GetViewModel; + return Context.Orders.Include(x => x.Repair).Include(x => x.Client).FirstOrDefault(x => x.Id == NewOrder.Id)?.GetViewModel; } public OrderViewModel? Update(OrderBindingModel Model) @@ -84,13 +87,13 @@ namespace AutoWorkshopDatabaseImplement.Implements Order.Update(Model); Context.SaveChanges(); - return Context.Orders.Include(x => x.Repair).FirstOrDefault(x => x.Id == Model.Id)?.GetViewModel; + return Context.Orders.Include(x => x.Repair).Include(x => x.Client).FirstOrDefault(x => x.Id == Model.Id)?.GetViewModel; } public OrderViewModel? Delete(OrderBindingModel Model) { using var Context = new AutoWorkshopDatabase(); - var Order = Context.Orders.FirstOrDefault(rec => rec.Id == Model.Id); + var Order = Context.Orders.Include(x => x.Repair).Include(x => x.Client).FirstOrDefault(rec => rec.Id == Model.Id); if (Order == null) return null; diff --git a/AutoWorkshopDatabaseImplement/Models/Client.cs b/AutoWorkshopDatabaseImplement/Models/Client.cs new file mode 100644 index 0000000..56d700a --- /dev/null +++ b/AutoWorkshopDatabaseImplement/Models/Client.cs @@ -0,0 +1,68 @@ +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.ViewModels; +using AutoWorkshopDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace AutoWorkshopDatabaseImplement.Models +{ + public class Client : IClientModel + { + public int Id { get; private set; } + + [Required] + public string ClientFIO { get; private set; } = string.Empty; + + [Required] + public string Email { get; set; } = string.Empty; + + [Required] + public string Password { get; set; } = string.Empty; + + [ForeignKey("ClientId")] + public virtual List Orders { get; set; } = new(); + + public static Client? Create(ClientBindingModel Model) + { + if (Model == null) + return null; + + return new Client() + { + Id = Model.Id, + ClientFIO = Model.ClientFIO, + Email = Model.Email, + Password = Model.Password + }; + } + + public static Client Create(ClientViewModel Model) + { + return new Client() + { + Id = Model.Id, + ClientFIO = Model.ClientFIO, + Email = Model.Email, + Password = Model.Password + }; + } + + public void Update(ClientBindingModel Model) + { + if (Model == null) + return; + + ClientFIO = Model.ClientFIO; + Email = Model.Email; + Password = Model.Password; + } + + public ClientViewModel GetViewModel => new() + { + Id = Id, + ClientFIO = ClientFIO, + Email = Email, + Password = Password, + }; + } +} diff --git a/AutoWorkshopDatabaseImplement/Models/Order.cs b/AutoWorkshopDatabaseImplement/Models/Order.cs index 0a423f4..cb77571 100644 --- a/AutoWorkshopDatabaseImplement/Models/Order.cs +++ b/AutoWorkshopDatabaseImplement/Models/Order.cs @@ -14,7 +14,12 @@ namespace AutoWorkshopDatabaseImplement.Models public int RepairId { get; private set; } public virtual Repair Repair { get; set; } + + [Required] + public int ClientId { get; private set; } + public virtual Client Client { get; set; } + [Required] public int Count { get; private set; } @@ -38,6 +43,7 @@ namespace AutoWorkshopDatabaseImplement.Models { Id = Model.Id, RepairId = Model.RepairId, + ClientId = Model.ClientId, Count = Model.Count, Sum = Model.Sum, Status = Model.Status, @@ -59,7 +65,9 @@ namespace AutoWorkshopDatabaseImplement.Models { Id = Id, RepairId = RepairId, + ClientId = ClientId, RepairName = Repair.RepairName, + ClientFIO = Client.ClientFIO, Count = Count, Sum = Sum, Status = Status,