diff --git a/RenovationWork/RenovationWorkBusinessLogic/BusinessLogics/RepairLogic.cs b/RenovationWork/RenovationWorkBusinessLogic/BusinessLogics/RepairLogic.cs index 3301975..bb0fc84 100644 --- a/RenovationWork/RenovationWorkBusinessLogic/BusinessLogics/RepairLogic.cs +++ b/RenovationWork/RenovationWorkBusinessLogic/BusinessLogics/RepairLogic.cs @@ -92,11 +92,11 @@ namespace RenovationWorkBusinessLogic.BusinessLogics } if (string.IsNullOrEmpty(model.RepairName)) { - throw new ArgumentNullException("Нет названия компьютера", nameof(model.RepairName)); + throw new ArgumentNullException("Нет названия ремонта", nameof(model.RepairName)); } if (model.Price <= 0) { - throw new ArgumentNullException("Цена компьютера должна быть больше 0", nameof(model.Price)); + throw new ArgumentNullException("Цена ремонта должна быть больше 0", nameof(model.Price)); } _logger.LogInformation("Repair. RepairName:{RepairName}.Price:{ Price}. Id: { Id} ", model.RepairName, model.Price, model.Id); var element = _repairStorage.GetElement(new RepairSearchModel @@ -105,7 +105,7 @@ namespace RenovationWorkBusinessLogic.BusinessLogics }); if (element != null && element.Id != model.Id) { - throw new InvalidOperationException("Компьютер с таким названием уже есть"); + throw new InvalidOperationException("Ремонт с таким названием уже есть"); } } } diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Implements/ComponentStorage.cs b/RenovationWork/RenovationWorkDatabaseImplement/Implements/ComponentStorage.cs index 66535a6..066bb08 100644 --- a/RenovationWork/RenovationWorkDatabaseImplement/Implements/ComponentStorage.cs +++ b/RenovationWork/RenovationWorkDatabaseImplement/Implements/ComponentStorage.cs @@ -12,9 +12,7 @@ namespace RenovationWorkDatabaseImplement.Implements public List GetFullList() { using var context = new RenovationWorkDatabase(); - return context.Components - .Select(x => x.GetViewModel) - .ToList(); + return context.Components.Select(x => x.GetViewModel).ToList(); } public List GetFilteredList(ComponentSearchModel model) { @@ -23,10 +21,7 @@ namespace RenovationWorkDatabaseImplement.Implements return new(); } using var context = new RenovationWorkDatabase(); - return context.Components - .Where(x => x.ComponentName.Contains(model.ComponentName)) - .Select(x => x.GetViewModel) - .ToList(); + return context.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList(); } public ComponentViewModel? GetElement(ComponentSearchModel model) { @@ -35,11 +30,7 @@ namespace RenovationWorkDatabaseImplement.Implements return null; } using var context = new RenovationWorkDatabase(); - return context.Components - .FirstOrDefault(x => - (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == - model.ComponentName) || - (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + return context.Components.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } public ComponentViewModel? Insert(ComponentBindingModel model) { @@ -69,13 +60,13 @@ namespace RenovationWorkDatabaseImplement.Implements { using var context = new RenovationWorkDatabase(); var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) + if (element == null) { - context.Components.Remove(element); - context.SaveChanges(); - return element.GetViewModel; + return null; } - return null; + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; } } } diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Implements/OrderStorage.cs b/RenovationWork/RenovationWorkDatabaseImplement/Implements/OrderStorage.cs index 561b83d..fefaecb 100644 --- a/RenovationWork/RenovationWorkDatabaseImplement/Implements/OrderStorage.cs +++ b/RenovationWork/RenovationWorkDatabaseImplement/Implements/OrderStorage.cs @@ -11,84 +11,76 @@ namespace RenovationWorkDatabaseImplement.Implements { public List GetFullList() { - using var Context = new RenovationWorkDatabase(); - - return Context.Orders - .Include(x => x.Repair) - .Select(x => x.GetViewModel) - .ToList(); + using var context = new RenovationWorkDatabase(); + return context.Orders.Include(x => x.Repair).Select(x => x.GetViewModel).ToList(); } - - public List GetFilteredList(OrderSearchModel Model) + public List GetFilteredList(OrderSearchModel model) { - if (!Model.Id.HasValue) + if (!model.Id.HasValue) + { return new(); - - using var Context = new RenovationWorkDatabase(); - - return Context.Orders - .Include(x => x.Repair) - .Where(x => x.Id == Model.Id) - .Select(x => x.GetViewModel) - .ToList(); + } + using var context = new RenovationWorkDatabase(); + return context.Orders + .Include(x => x.Repair) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); } - - public OrderViewModel? GetElement(OrderSearchModel Model) + public OrderViewModel? GetElement(OrderSearchModel model) { - if (!Model.Id.HasValue) - return new(); - - using var Context = new RenovationWorkDatabase(); - - return Context.Orders - .Include(x => x.Repair) - .FirstOrDefault(x => x.Id == Model.Id) - ?.GetViewModel; + if (!model.Id.HasValue) + { + return null; + } + using var context = new RenovationWorkDatabase(); + return context.Orders.Include(x => x.Repair) + .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; } - - public OrderViewModel? Insert(OrderBindingModel Model) + public OrderViewModel? Insert(OrderBindingModel model) { - using var Context = new RenovationWorkDatabase(); - - if (Model == null) + using var context = new RenovationWorkDatabase(); + if (model == null) return null; - - var NewOrder = Order.Create(Context, Model); - if (NewOrder == null) + var newOrder = Order.Create(context, model); + if (newOrder == null) + { return null; - - Context.Orders.Add(NewOrder); - Context.SaveChanges(); - - return NewOrder.GetViewModel; + } + context.Orders.Add(newOrder); + context.SaveChanges(); + return newOrder.GetViewModel; } - - public OrderViewModel? Update(OrderBindingModel Model) + public OrderViewModel? Update(OrderBindingModel model) { - using var Context = new RenovationWorkDatabase(); - - var Order = Context.Orders.FirstOrDefault(x => x.Id == Model.Id); - if (Order == null) + using var context = new RenovationWorkDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { return null; - - Order.Update(Model); - Context.SaveChanges(); - - return Order.GetViewModel; + } + order.Update(model); + context.SaveChanges(); + return context.Orders + .Include(x => x.Repair) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; } - - public OrderViewModel? Delete(OrderBindingModel Model) + public OrderViewModel? Delete(OrderBindingModel model) { - using var Context = new RenovationWorkDatabase(); - var Order = Context.Orders.FirstOrDefault(rec => rec.Id == Model.Id); - - if (Order == null) - return null; - - Context.Orders.Remove(Order); - Context.SaveChanges(); - - return Order.GetViewModel; + using var context = new RenovationWorkDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + var deletedElement = context.Orders + .Include(x => x.Repair) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + context.Orders.Remove(element); + context.SaveChanges(); + return deletedElement; + } + return null; } } } diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Implements/RepairStorage.cs b/RenovationWork/RenovationWorkDatabaseImplement/Implements/RepairStorage.cs index 837c6b7..2c27e22 100644 --- a/RenovationWork/RenovationWorkDatabaseImplement/Implements/RepairStorage.cs +++ b/RenovationWork/RenovationWorkDatabaseImplement/Implements/RepairStorage.cs @@ -11,12 +11,7 @@ namespace RenovationWorkDatabaseImplement.Implements public List GetFullList() { using var context = new RenovationWorkDatabase(); - return context.Repairs - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .ToList() - .Select(x => x.GetViewModel) - .ToList(); + return context.Repairs.Include(x => x.Components).ThenInclude(x => x.Component).ToList().Select(x => x.GetViewModel).ToList(); } public List GetFilteredList(RepairSearchModel model) { @@ -25,13 +20,8 @@ namespace RenovationWorkDatabaseImplement.Implements return new(); } using var context = new RenovationWorkDatabase(); - return context.Repairs - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .Where(x => x.RepairName.Contains(model.RepairName)) - .ToList() - .Select(x => x.GetViewModel) - .ToList(); + return context.Repairs.Include(x => x.Components).ThenInclude(x => x.Component).Where(x => x.RepairName.Contains(model.RepairName)).ToList().Select(x => x.GetViewModel).ToList(); + } public RepairViewModel? GetElement(RepairSearchModel model) { @@ -41,24 +31,20 @@ namespace RenovationWorkDatabaseImplement.Implements return null; } using var context = new RenovationWorkDatabase(); - return context.Repairs - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .FirstOrDefault(x => (!string.IsNullOrEmpty(model.RepairName) && - x.RepairName == model.RepairName) || - (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + return context.Repairs.Include(x => x.Components).ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.RepairName) && x.RepairName == model.RepairName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } public RepairViewModel? Insert(RepairBindingModel model) { using var context = new RenovationWorkDatabase(); - var newRepair = Repair.Create(context, model); - if (newRepair == null) + var newEngine = Repair.Create(context, model); + if (newEngine == null) { return null; } - context.Repairs.Add(newRepair); + context.Repairs.Add(newEngine); context.SaveChanges(); - return newRepair.GetViewModel; + return newEngine.GetViewModel; } public RepairViewModel? Update(RepairBindingModel model) { @@ -66,17 +52,17 @@ namespace RenovationWorkDatabaseImplement.Implements using var transaction = context.Database.BeginTransaction(); try { - var repair = context.Repairs.FirstOrDefault(rec => + var engine = context.Repairs.FirstOrDefault(rec => rec.Id == model.Id); - if (repair == null) + if (engine == null) { return null; } - repair.Update(model); + engine.Update(model); context.SaveChanges(); - repair.UpdateComponents(context, model); + engine.UpdateComponents(context, model); transaction.Commit(); - return repair.GetViewModel; + return engine.GetViewModel; } catch { diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240405063901_InitialCreate.Designer.cs b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240418122550_InitialCreate.Designer.cs similarity index 99% rename from RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240405063901_InitialCreate.Designer.cs rename to RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240418122550_InitialCreate.Designer.cs index 7830f94..60d0019 100644 --- a/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240405063901_InitialCreate.Designer.cs +++ b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240418122550_InitialCreate.Designer.cs @@ -12,7 +12,7 @@ using RenovationWorkDatabaseImplement; namespace RenovationWorkDatabaseImplement.Migrations { [DbContext(typeof(RenovationWorkDatabase))] - [Migration("20240405063901_InitialCreate")] + [Migration("20240418122550_InitialCreate")] partial class InitialCreate { /// diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240405063901_InitialCreate.cs b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240418122550_InitialCreate.cs similarity index 100% rename from RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240405063901_InitialCreate.cs rename to RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240418122550_InitialCreate.cs diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Models/Component.cs b/RenovationWork/RenovationWorkDatabaseImplement/Models/Component.cs index 169c024..767a238 100644 --- a/RenovationWork/RenovationWorkDatabaseImplement/Models/Component.cs +++ b/RenovationWork/RenovationWorkDatabaseImplement/Models/Component.cs @@ -27,7 +27,7 @@ namespace RenovationWorkDatabaseImplement.Models { return null; } - return new Component() + return new Component { Id = model.Id, ComponentName = model.ComponentName, @@ -58,6 +58,5 @@ namespace RenovationWorkDatabaseImplement.Models ComponentName = ComponentName, Cost = Cost }; - } } diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Models/Order.cs b/RenovationWork/RenovationWorkDatabaseImplement/Models/Order.cs index a946e8e..bc93c79 100644 --- a/RenovationWork/RenovationWorkDatabaseImplement/Models/Order.cs +++ b/RenovationWork/RenovationWorkDatabaseImplement/Models/Order.cs @@ -35,17 +35,18 @@ namespace RenovationWorkDatabaseImplement.Models public DateTime? DateImplement { get; private set; } - public static Order Create(RenovationWorkDatabase context, OrderBindingModel Model) + public static Order Create(RenovationWorkDatabase context, OrderBindingModel model) { return new Order() { - Id = Model.Id, - RepairId = Model.RepairId, - Count = Model.Count, - Sum = Model.Sum, - Status = Model.Status, - DateCreate = Model.DateCreate, - DateImplement = Model.DateImplement, + Id = model.Id, + RepairId = model.RepairId, + Repair = context.Repairs.First(x => x.Id == model.RepairId), + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, }; } @@ -54,11 +55,7 @@ namespace RenovationWorkDatabaseImplement.Models if (Model == null) return; - Id = Model.Id; - RepairId = Model.RepairId; - Sum = Model.Sum; Status = Model.Status; - DateCreate = Model.DateCreate; DateImplement = Model.DateImplement; }