From 40cb4fc70dbe8509a172b8a8ffe4a67de4e8137b Mon Sep 17 00:00:00 2001 From: Allllen4a Date: Fri, 31 May 2024 21:07:37 +0400 Subject: [PATCH] ...... --- .../BindingModels/ProcedureBindingModel.cs | 2 + .../SearchModels/ProcedureSearchModel.cs | 1 + .../StoragesContracts/IProcedureStorage.cs | 1 - .../ViewModels/ProcedureCosmetic.cs | 26 +++++ .../ViewModels/ProcedureViewModel.cs | 2 + .../Models/IProcedureModel.cs | 1 + .../BeautySalonDatabase.cs | 3 +- .../Implements/ProcedureStorage.cs | 99 +++++++++---------- .../Models/Procedure.cs | 9 +- .../Models/ProcedureCosmetic.cs | 27 +++++ 10 files changed, 116 insertions(+), 55 deletions(-) create mode 100644 BeautySalonView/BeautySalonContracts/ViewModels/ProcedureCosmetic.cs create mode 100644 BeautySalonView/BeautySalonDatabaseImplement/Models/ProcedureCosmetic.cs diff --git a/BeautySalonView/BeautySalonContracts/BindingModels/ProcedureBindingModel.cs b/BeautySalonView/BeautySalonContracts/BindingModels/ProcedureBindingModel.cs index 8c9326c..648dfad 100644 --- a/BeautySalonView/BeautySalonContracts/BindingModels/ProcedureBindingModel.cs +++ b/BeautySalonView/BeautySalonContracts/BindingModels/ProcedureBindingModel.cs @@ -11,6 +11,8 @@ namespace BeautySalonContracts.BindingModels public double ProcedurePrice { get; set; } public double ProcedureDuration { get; set; } public int WorkerId { get; set; } + + public int EvaluationId { get; set; } //public List ProcedureCosmetics { get; set; } = new(); public Dictionary ProcedureCosmetics diff --git a/BeautySalonView/BeautySalonContracts/SearchModels/ProcedureSearchModel.cs b/BeautySalonView/BeautySalonContracts/SearchModels/ProcedureSearchModel.cs index c60850e..96beb2c 100644 --- a/BeautySalonView/BeautySalonContracts/SearchModels/ProcedureSearchModel.cs +++ b/BeautySalonView/BeautySalonContracts/SearchModels/ProcedureSearchModel.cs @@ -4,6 +4,7 @@ { public int? Id { get; set; } public string? ProcedureName { get; set; } + public int? EvaluationId { get; set; } public int? WorkerId { get; set; } } } diff --git a/BeautySalonView/BeautySalonContracts/StoragesContracts/IProcedureStorage.cs b/BeautySalonView/BeautySalonContracts/StoragesContracts/IProcedureStorage.cs index 90d686a..7478c8e 100644 --- a/BeautySalonView/BeautySalonContracts/StoragesContracts/IProcedureStorage.cs +++ b/BeautySalonView/BeautySalonContracts/StoragesContracts/IProcedureStorage.cs @@ -17,7 +17,6 @@ namespace BeautySalonContracts.StoragesContracts ProcedureViewModel? Insert(ProcedureBindingModel model); ProcedureViewModel? Update(ProcedureBindingModel model); ProcedureViewModel? Delete(ProcedureBindingModel model); - List GetProcedureCosmetics(ProcedureSearchModel model); } } diff --git a/BeautySalonView/BeautySalonContracts/ViewModels/ProcedureCosmetic.cs b/BeautySalonView/BeautySalonContracts/ViewModels/ProcedureCosmetic.cs new file mode 100644 index 0000000..1b0dc01 --- /dev/null +++ b/BeautySalonView/BeautySalonContracts/ViewModels/ProcedureCosmetic.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BeautySalonContracts.ViewModels +{ + + public class ProcedureCosmeticViewModel + { + public CosmeticViewModel Cosmetic { get; set; } = null!; + public ProcedureViewModel Procedure { get; set; } = null!; + public int Count { get; set; } + + public ProcedureCosmeticViewModel() { } + + public ProcedureCosmeticViewModel(CosmeticViewModel cosmetic, ProcedureViewModel procedure, int count) + { + Cosmetic = cosmetic; + Procedure = procedure; + Count = count; + } + } + } + diff --git a/BeautySalonView/BeautySalonContracts/ViewModels/ProcedureViewModel.cs b/BeautySalonView/BeautySalonContracts/ViewModels/ProcedureViewModel.cs index 4e2cd3f..5a20368 100644 --- a/BeautySalonView/BeautySalonContracts/ViewModels/ProcedureViewModel.cs +++ b/BeautySalonView/BeautySalonContracts/ViewModels/ProcedureViewModel.cs @@ -23,6 +23,8 @@ namespace BeautySalonContracts.ViewModels public int WorkerId { get; set; } public string WorkerName { get; set; } = string.Empty; + public int EvaluationId { get; set; } + public Dictionary ProcedureCosmetics { get; diff --git a/BeautySalonView/BeautySalonDataModels/Models/IProcedureModel.cs b/BeautySalonView/BeautySalonDataModels/Models/IProcedureModel.cs index 3a2d2eb..843c0b9 100644 --- a/BeautySalonView/BeautySalonDataModels/Models/IProcedureModel.cs +++ b/BeautySalonView/BeautySalonDataModels/Models/IProcedureModel.cs @@ -8,6 +8,7 @@ namespace BeautySalonDataModels.Models double ProcedurePrice { get; } double ProcedureDuration { get; } int WorkerId { get; } + int EvaluationId { get; } Dictionary ProcedureCosmetics { get; } //список косметики "участвующей" в заказе } } diff --git a/BeautySalonView/BeautySalonDatabaseImplement/BeautySalonDatabase.cs b/BeautySalonView/BeautySalonDatabaseImplement/BeautySalonDatabase.cs index c0cbff5..73db706 100644 --- a/BeautySalonView/BeautySalonDatabaseImplement/BeautySalonDatabase.cs +++ b/BeautySalonView/BeautySalonDatabaseImplement/BeautySalonDatabase.cs @@ -17,7 +17,7 @@ namespace BeautySalonDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=PRETTYNAME;Initial Catalog=BeautySalonDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=ALYONA\;Initial Catalog=BeautySalonDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } @@ -37,6 +37,7 @@ namespace BeautySalonDatabaseImplement public virtual DbSet OrderServices { set; get; } public virtual DbSet OrderProcedures { set; get; } public virtual DbSet CosmeticProcedures { set; get; } + public virtual DbSet ProcedureCosmetics { set; get; } public virtual DbSet MessageInfos { set; get; } } diff --git a/BeautySalonView/BeautySalonDatabaseImplement/Implements/ProcedureStorage.cs b/BeautySalonView/BeautySalonDatabaseImplement/Implements/ProcedureStorage.cs index c0f42c9..155d76f 100644 --- a/BeautySalonView/BeautySalonDatabaseImplement/Implements/ProcedureStorage.cs +++ b/BeautySalonView/BeautySalonDatabaseImplement/Implements/ProcedureStorage.cs @@ -14,38 +14,17 @@ namespace BeautySalonDatabaseImplement.Implements { public class ProcedureStorage : IProcedureStorage { - public ProcedureViewModel? Delete(ProcedureBindingModel model) + public List GetFullList() { - using var context = new BeautySalonDatabase(); - var element = context.Procedures - .Include(x => x.Cosmetics) - .Include(x => x.Worker) - .FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) - { - context.Procedures.Remove(element); - context.SaveChanges(); - return element.GetViewModel; - } - return null; - } - - public ProcedureViewModel? GetElement(ProcedureSearchModel model) - { - if (string.IsNullOrEmpty(model.ProcedureName) && !model.Id.HasValue) - { - return null; - } using var context = new BeautySalonDatabase(); return context.Procedures - .Include(x => x.Worker) - .Include(x => x.Cosmetics) - .ThenInclude(x => x.Cosmetic) - .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ProcedureName) && x.ProcedureName == model.ProcedureName) || - (model.Id.HasValue && x.Id == model.Id)) - ?.GetViewModel; + .Include(x => x.Worker) + .Include(x => x.Cosmetics) + .ThenInclude(x => x.Cosmetic) + .Select(x => x.GetViewModel) + .ToList(); } - + public List GetFilteredList(ProcedureSearchModel model) { using var context = new BeautySalonDatabase(); @@ -65,18 +44,24 @@ namespace BeautySalonDatabaseImplement.Implements .Select(x => x.GetViewModel) .ToList(); } + return new(); } - public List GetFullList() + + public ProcedureViewModel? GetElement(ProcedureSearchModel model) { + if (string.IsNullOrEmpty(model.ProcedureName) && !model.Id.HasValue) + { + return null; + } using var context = new BeautySalonDatabase(); return context.Procedures - .Include(x => x.Worker) - .Include(x => x.Cosmetics) - .ThenInclude(x => x.Cosmetic) - .Select(x => x.GetViewModel) - .ToList(); + .Include(x => x.Cosmetics) + .ThenInclude(x => x.Cosmetic) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ProcedureName) && x.ProcedureName == model.ProcedureName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; } public ProcedureViewModel? Insert(ProcedureBindingModel model) @@ -87,33 +72,42 @@ namespace BeautySalonDatabaseImplement.Implements { return null; } + context.Procedures.Add(newProcedure); context.SaveChanges(); return newProcedure.GetViewModel; } - public ProcedureViewModel? Update(ProcedureBindingModel model) + + public ProcedureViewModel? Update(BeautySalonDatabase context, ProcedureBindingModel model) + { + + var element = context.Procedures + .Include(x => x.Evaluation) + .FirstOrDefault(x => x.Id.Equals(model.Id)); + if (element == null) + { + return null; + } + + element.Update(context, model); + context.SaveChanges(); + return element.GetViewModel; + } + + public ProcedureViewModel? Delete(ProcedureBindingModel model) { using var context = new BeautySalonDatabase(); - using var transaction = context.Database.BeginTransaction(); - try + var element = context.Procedures + .Include(x => x.EvaluationId) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) { - var procedure = context.Procedures.FirstOrDefault(rec => rec.Id == model.Id); - if (procedure == null) - { - return null; - } - procedure.Update(model); + context.Procedures.Remove(element); context.SaveChanges(); - procedure.UpdateCosmetics(context, model); - transaction.Commit(); - return procedure.GetViewModel; - } - catch - { - transaction.Rollback(); - throw; + return element.GetViewModel; } + return null; } public List GetProcedureCosmetics(ProcedureSearchModel model) @@ -123,11 +117,12 @@ namespace BeautySalonDatabaseImplement.Implements return new(); } using var context = new BeautySalonDatabase(); - var cosmetics = context.CosmeticProcedures + var cosmetics = context.ProcedureCosmetics .Where(x => x.ProcedureId == model.Id) .Select(x => x.Cosmetic.GetViewModel) .ToList(); return cosmetics; } + } } diff --git a/BeautySalonView/BeautySalonDatabaseImplement/Models/Procedure.cs b/BeautySalonView/BeautySalonDatabaseImplement/Models/Procedure.cs index d0bf63e..00d9ed5 100644 --- a/BeautySalonView/BeautySalonDatabaseImplement/Models/Procedure.cs +++ b/BeautySalonView/BeautySalonDatabaseImplement/Models/Procedure.cs @@ -28,6 +28,10 @@ namespace BeautySalonDatabaseImplement.Models public int WorkerId { get; set; } public virtual Worker Worker { get; set; } + public int EvaluationId { get; set; } + + public virtual Evaluation Evaluation { get; set; } = new(); + #region //// связь процедуры и оценок один - ко - многим //[ForeignKey("ProcedureId")] @@ -95,6 +99,7 @@ namespace BeautySalonDatabaseImplement.Models ProcedurePrice = model.ProcedurePrice, ProcedureDuration = model.ProcedureDuration, WorkerId = model.WorkerId, + EvaluationId = model.EvaluationId, Cosmetics = model.ProcedureCosmetics.Select(x => new CosmeticProcedure { Cosmetic = context.Cosmetics.First(y => y.Id == x.Key) @@ -109,6 +114,7 @@ namespace BeautySalonDatabaseImplement.Models ProcedureName = model.ProcedureName; ProcedurePrice = model.ProcedurePrice; ProcedureDuration = model.ProcedureDuration; + EvaluationId = model.EvaluationId; } public ProcedureViewModel GetViewModel => new() @@ -119,7 +125,8 @@ namespace BeautySalonDatabaseImplement.Models ProcedureDuration = ProcedureDuration, ProcedureCosmetics = ProcedureCosmetics, WorkerId = WorkerId, - WorkerName = Worker.FullName + WorkerName = Worker.FullName, + EvaluationId = EvaluationId, }; public void UpdateCosmetics(BeautySalonDatabase context, ProcedureBindingModel model) diff --git a/BeautySalonView/BeautySalonDatabaseImplement/Models/ProcedureCosmetic.cs b/BeautySalonView/BeautySalonDatabaseImplement/Models/ProcedureCosmetic.cs new file mode 100644 index 0000000..7a4b461 --- /dev/null +++ b/BeautySalonView/BeautySalonDatabaseImplement/Models/ProcedureCosmetic.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BeautySalonDatabaseImplement.Models +{ + public class ProcedureCosmetic + { + public int Id { get; set; } + + [Required] + public int CosmeticId { get; set; } + + [Required] + public int ProcedureId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Procedure Procedure { get; set; } = new(); + public virtual Cosmetic Cosmetic { get; set; } = new(); + + } +}