diff --git a/BeautySalon/BeautySalon.sln b/BeautySalon/BeautySalon.sln index fa561a8..b4c32af 100644 --- a/BeautySalon/BeautySalon.sln +++ b/BeautySalon/BeautySalon.sln @@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BeautySalonContracts", "..\ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BeautySalonBusinessLogic", "..\BeautySalonBusinessLogic\BeautySalonBusinessLogic.csproj", "{EC867077-D21F-4D12-9BD5-16244A3D119C}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BeautySalonDatabaseImplement", "..\BeautySalonDatabaseImplement\BeautySalonDatabaseImplement.csproj", "{BD110484-1896-428D-962D-A0EAE2AAD41E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -33,6 +35,10 @@ Global {EC867077-D21F-4D12-9BD5-16244A3D119C}.Debug|Any CPU.Build.0 = Debug|Any CPU {EC867077-D21F-4D12-9BD5-16244A3D119C}.Release|Any CPU.ActiveCfg = Release|Any CPU {EC867077-D21F-4D12-9BD5-16244A3D119C}.Release|Any CPU.Build.0 = Release|Any CPU + {BD110484-1896-428D-962D-A0EAE2AAD41E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BD110484-1896-428D-962D-A0EAE2AAD41E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BD110484-1896-428D-962D-A0EAE2AAD41E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BD110484-1896-428D-962D-A0EAE2AAD41E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/BeautySalonDatabaseImplement/BeautySalonDatabase.cs b/BeautySalonDatabaseImplement/BeautySalonDatabase.cs new file mode 100644 index 0000000..bd8188f --- /dev/null +++ b/BeautySalonDatabaseImplement/BeautySalonDatabase.cs @@ -0,0 +1,22 @@ +using BeautySalonDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace BeautySalonDatabaseImplement +{ + public class BeautySalonDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-IHH1ICP\SQLEXPRESS;Initial Catalog=BeautySalonDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Masters { set; get; } + public virtual DbSet Services { set; get; } + public virtual DbSet MasterServices { set; get; } + public virtual DbSet Visits { set; get; } + public virtual DbSet Clients { set; get; } + } +} diff --git a/BeautySalonDatabaseImplement/BeautySalonDatabaseImplement.csproj b/BeautySalonDatabaseImplement/BeautySalonDatabaseImplement.csproj new file mode 100644 index 0000000..4c545f6 --- /dev/null +++ b/BeautySalonDatabaseImplement/BeautySalonDatabaseImplement.csproj @@ -0,0 +1,22 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + + + + + + diff --git a/BeautySalonDatabaseImplement/Models/Client.cs b/BeautySalonDatabaseImplement/Models/Client.cs new file mode 100644 index 0000000..d25a80d --- /dev/null +++ b/BeautySalonDatabaseImplement/Models/Client.cs @@ -0,0 +1,53 @@ +using BeautySalonContracts.BindingModels; +using BeautySalonContracts.ViewModels; +using BeautySalonDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace BeautySalonDatabaseImplement.Models +{ + public class Client : IClientModel + { + public int Id { get; private set; } + [Required] + public string ClientFIO { get; private set; } = string.Empty; + [Required] + public string PhoneNumber { get; private set; } = string.Empty; + public static Client? Create(ClientBindingModel model) + { + if (model == null) + { + return null; + } + return new Client() + { + Id = model.Id, + ClientFIO = model.ClientFIO, + PhoneNumber = model.PhoneNumber + }; + } + public static Client Create(ClientViewModel model) + { + return new Client + { + Id = model.Id, + ClientFIO = model.ClientFIO, + PhoneNumber = model.PhoneNumber + }; + } + public void Update(ClientBindingModel model) + { + if (model == null) + { + return; + } + ClientFIO = model.ClientFIO; + PhoneNumber = model.PhoneNumber; + } + public ClientViewModel GetViewModel => new() + { + Id = Id, + ClientFIO = ClientFIO, + PhoneNumber = PhoneNumber + }; + } +} diff --git a/BeautySalonDatabaseImplement/Models/Master.cs b/BeautySalonDatabaseImplement/Models/Master.cs new file mode 100644 index 0000000..e221bf2 --- /dev/null +++ b/BeautySalonDatabaseImplement/Models/Master.cs @@ -0,0 +1,92 @@ +using BeautySalonContracts.BindingModels; +using BeautySalonContracts.ViewModels; +using BeautySalonDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace BeautySalonDatabaseImplement.Models +{ + public class Master : IMasterModel + { + public int Id { get; private set; } + [Required] + public string MasterFIO { get; set; } = string.Empty; + [Required] + public string Specialization { get; set; } = string.Empty; + private Dictionary? _masterServices = null; + [NotMapped] + public Dictionary MasterServices + { + get + { + if (_masterServices == null) + { + _masterServices = Services + .ToDictionary(recPC => recPC.MasterId, recPC => + (recPC.Service as IServiceModel, recPC.Count)); + } + return _masterServices; + } + } + [ForeignKey("MasterId")] + public virtual List Services { get; set; } = new(); + [ForeignKey("MasterId")] + public virtual List Visits { get; set; } = new(); + public static Master Create(BeautySalonDatabase context, MasterBindingModel model) + { + return new Master + { + Id = model.Id, + MasterFIO = model.MasterFIO, + Specialization = model.Specialization, + Services = model.MasterServices.Select(x => new MasterService + { + Service = context.Services.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(MasterBindingModel model) + { + MasterFIO = model.MasterFIO; + Specialization = model.Specialization; + } + public MasterViewModel GetViewModel => new() + { + Id = Id, + MasterFIO = MasterFIO, + Specialization = Specialization, + MasterServices = MasterServices + }; + public void UpdateServices(BeautySalonDatabase context, MasterBindingModel model) + { + var masterServices = context.MasterServices.Where(rec => rec.MasterId == model.Id).ToList(); + if (masterServices != null && masterServices.Count > 0) + { // удалили те, которых нет в модели + context.MasterServices.RemoveRange(masterServices.Where(rec + => !model.MasterServices.ContainsKey(rec.ServiceId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateService in masterServices) + { + updateService.Count = + model.MasterServices[updateService.ServiceId].Item2; + model.MasterServices.Remove(updateService.ServiceId); + } + context.SaveChanges(); + } + var master = context.Masters.First(x => x.Id == Id); + foreach (var pc in model.MasterServices) + { + context.MasterServices.Add(new MasterService + { + Master = master, + Service = context.Services.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _masterServices = null; + } + } +} diff --git a/BeautySalonDatabaseImplement/Models/MasterService.cs b/BeautySalonDatabaseImplement/Models/MasterService.cs new file mode 100644 index 0000000..db8bdaf --- /dev/null +++ b/BeautySalonDatabaseImplement/Models/MasterService.cs @@ -0,0 +1,17 @@ +using System.ComponentModel.DataAnnotations; + +namespace BeautySalonDatabaseImplement.Models +{ + public class MasterService + { + public int Id { get; set; } + [Required] + public int MasterId { get; set; } + [Required] + public int ServiceId { get; set; } + [Required] + public int Count { get; set; } + public virtual Service Service { get; set; } = new(); + public virtual Master Master { get; set; } = new(); + } +} diff --git a/BeautySalonDatabaseImplement/Models/Service.cs b/BeautySalonDatabaseImplement/Models/Service.cs new file mode 100644 index 0000000..282281a --- /dev/null +++ b/BeautySalonDatabaseImplement/Models/Service.cs @@ -0,0 +1,56 @@ +using BeautySalonContracts.BindingModels; +using BeautySalonContracts.ViewModels; +using BeautySalonDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace BeautySalonDatabaseImplement.Models +{ + public class Service : IServiceModel + { + public int Id { get; private set; } + [Required] + public string ServiceName { get; private set; } = string.Empty; + [Required] + public double Cost { get; set; } + [ForeignKey("ServiceId")] + public virtual List MasterServices { get; set; } = new(); + public static Service? Create(ServiceBindingModel model) + { + if (model == null) + { + return null; + } + return new Service() + { + Id = model.Id, + ServiceName = model.ServiceName, + Cost = model.Cost + }; + } + public static Service Create(ServiceViewModel model) + { + return new Service + { + Id = model.Id, + ServiceName = model.ServiceName, + Cost = model.Cost + }; + } + public void Update(ServiceBindingModel model) + { + if (model == null) + { + return; + } + ServiceName = model.ServiceName; + Cost = model.Cost; + } + public ServiceViewModel GetViewModel => new() + { + Id = Id, + ServiceName = ServiceName, + Cost = Cost + }; + } +} diff --git a/BeautySalonDatabaseImplement/Models/Visit.cs b/BeautySalonDatabaseImplement/Models/Visit.cs new file mode 100644 index 0000000..26e323b --- /dev/null +++ b/BeautySalonDatabaseImplement/Models/Visit.cs @@ -0,0 +1,49 @@ +using BeautySalonContracts.BindingModels; +using BeautySalonDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace BeautySalonDatabaseImplement.Models +{ + public class Visit : IVisitModel + { + public int Id { get; private set; } + [Required] + public DateTime DateOfVisit { get; private set; } + [Required] + public int ClientId { get; private set; } + public int MasterId { get; private set; } + public int ServiceId { get; private set; } + public string ClientFIO { get; set; } = string.Empty; + public string MasterFIO { get; set; } = string.Empty; + public string ServiceName { get; set; } = string.Empty; + [Required] + public double Sum { get; private set; } + [Required] + public int Count { get; private set; } + public virtual Client Client { get; set; } + public virtual Master Master { get; set; } + public virtual Service Service { get; set; } + + public static Visit? Create(VisitBindingModel? model) + { + if (model == null) + { + return null; + } + + return new Visit() + { + Id = model.Id, + MasterId = model.MasterId, + ClientId = model.ClientId, + ServiceId = model.ServiceId, + MasterFIO = model.MasterFIO, + ClientFIO = model.ClientFIO, + ServiceName = model.ServiceName, + Count = model.Count, + Sum = model.Sum, + DateOfVisit = model.DateOfVisit + }; + } + } +} diff --git a/BeauySalonContracts/BindingModels/MasterBindingModel.cs b/BeauySalonContracts/BindingModels/MasterBindingModel.cs index 6798f4d..61a855e 100644 --- a/BeauySalonContracts/BindingModels/MasterBindingModel.cs +++ b/BeauySalonContracts/BindingModels/MasterBindingModel.cs @@ -6,6 +6,7 @@ namespace BeautySalonContracts.BindingModels { public int Id { get; set; } public string MasterFIO { get; set; } = string.Empty; - public string Specialization { get; set; } = string.Empty; + public string Specialization { get; set; } = string.Empty; + public Dictionary MasterServices { get; set; } = new(); } } diff --git a/BeauySalonContracts/BindingModels/VisitBindingModel.cs b/BeauySalonContracts/BindingModels/VisitBindingModel.cs index 1020a65..951b21e 100644 --- a/BeauySalonContracts/BindingModels/VisitBindingModel.cs +++ b/BeauySalonContracts/BindingModels/VisitBindingModel.cs @@ -9,6 +9,9 @@ namespace BeautySalonContracts.BindingModels public int ClientId { get; set; } public int MasterId { get; set; } public int ServiceId { get; set; } + public string ClientFIO { get; set; } = string.Empty; + public string MasterFIO { get; set; } = string.Empty; + public string ServiceName { get; set; } = string.Empty; public double Sum { get; set; } public int Count { get; set; } }