diff --git a/STODataBaseImplement/CarTechnicalWork.cs b/STODataBaseImplement/CarTechnicalWork.cs new file mode 100644 index 0000000..7dd4b97 --- /dev/null +++ b/STODataBaseImplement/CarTechnicalWork.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DatabaseImplement +{ + public class CarTechnicalWork + { + public int Id { get; set; } + [Required] + public int CarId { get; set; } + [Required] + public int TechnicalWorkId { get; set; } + [Required] + public int Count { get; set; } + public virtual Car Car { get; set; } = new(); + public virtual TechnicalWork TechnicalWork { get; set; } = new(); + } +} diff --git a/STODataBaseImplement/Client.cs b/STODataBaseImplement/Client.cs new file mode 100644 index 0000000..4d13408 --- /dev/null +++ b/STODataBaseImplement/Client.cs @@ -0,0 +1,59 @@ +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 STODataModels; +using STOContracts.BindingModels; +using STOContracts.ViewModels; + +namespace DatabaseImplement +{ + public class Client: IClientModel + { + public int Id { get; private set; } + [Required] + public string ClientFIO { get; private set; } = string.Empty; + [Required] + public string Email { get; private set; } = string.Empty; + [Required] + public string Password { get; private set; } + [ForeignKey("ClientId")] + public virtual List Cars { get; private set; } = new(); + [ForeignKey("ClientId")] + public virtual List Services { get; private 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 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/STODataBaseImplement/ClientStorage.cs b/STODataBaseImplement/ClientStorage.cs new file mode 100644 index 0000000..53ff0e3 --- /dev/null +++ b/STODataBaseImplement/ClientStorage.cs @@ -0,0 +1,83 @@ +using STOContracts.BindingModels; +using STOContracts.SearchModels; +using STOContracts.ViewModels; +using STODatabaseImplement; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DatabaseImplement +{ + public class ClientStorage + { + public List GetFullList() + { + using var context = new STODatabase(); + return context.Clients.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.ClientFIO)) + { + return new(); + } + using var context = new STODatabase(); + return context.Clients.Where(x => x.ClientFIO.Contains(model.ClientFIO)).Select(x => x.GetViewModel).ToList(); + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.Password) && !model.Id.HasValue) + { + return null; + } + using var context = new STODatabase(); + return context.Clients.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Email) && x.Email == model.Email && !string.IsNullOrEmpty(model.Password) && x.Password == model.Password) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public ClientViewModel? Insert(ClientBindingModel model) + { + var newComponent = Client.Create(model); + if (newComponent == null) + { + return null; + } + using var context = new STODatabase(); + context.Clients.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public ClientViewModel? Update(ClientBindingModel model) + { + using var context = new STODatabase(); + var component = context.Clients.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + + public ClientViewModel? Delete(ClientBindingModel model) + { + using var context = new STODatabase(); + var element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Clients.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/STODataBaseImplement/DatabaseImplement.csproj b/STODataBaseImplement/DatabaseImplement.csproj new file mode 100644 index 0000000..95189d8 --- /dev/null +++ b/STODataBaseImplement/DatabaseImplement.csproj @@ -0,0 +1,24 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + diff --git a/STODataBaseImplement/Service.cs b/STODataBaseImplement/Service.cs new file mode 100644 index 0000000..21d9bbe --- /dev/null +++ b/STODataBaseImplement/Service.cs @@ -0,0 +1,58 @@ +using STOContracts.BindingModels; +using STOContracts.ViewModels; +using STODatabaseImplement; +using STODataModels; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DatabaseImplement +{ + public class Service : IServiceModel + { + public int Id { get; private set; } + [Required] + public string Name {get; private set;} = string.Empty; + + [Required] + public int CarId { get; private set; } + public virtual Car Car { get; set; } = new(); + + public int ClientId { get; private set; } + public virtual Client Client { get; set; } = new(); + + public static Service? Create(STODatabase context, ServiceBindingModel model) + { + if (model == null) + { + return null; + } + return new Service() + { + CarId = model.CarId, + Car = context.Cars.First(x => x.Id == model.CarId), + ClientId = model.ClientId, + Client = context.Clients.First(x => x.Id == model.ClientId), + Id = model.Id, + Name = model.Name + }; + } + public void Update(ServiceBindingModel model) + { + if (model == null) + { + return; + } + Name = model.Name; + } + public ServiceViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + CarId = CarId + }; + } +}