Надежда

This commit is contained in:
Владимир Данилов 2024-04-18 16:51:50 +04:00
parent be264cb02b
commit d696e4d6b7
8 changed files with 92 additions and 127 deletions

View File

@ -92,11 +92,11 @@ namespace RenovationWorkBusinessLogic.BusinessLogics
} }
if (string.IsNullOrEmpty(model.RepairName)) if (string.IsNullOrEmpty(model.RepairName))
{ {
throw new ArgumentNullException("Нет названия компьютера", nameof(model.RepairName)); throw new ArgumentNullException("Нет названия ремонта", nameof(model.RepairName));
} }
if (model.Price <= 0) 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); _logger.LogInformation("Repair. RepairName:{RepairName}.Price:{ Price}. Id: { Id} ", model.RepairName, model.Price, model.Id);
var element = _repairStorage.GetElement(new RepairSearchModel var element = _repairStorage.GetElement(new RepairSearchModel
@ -105,7 +105,7 @@ namespace RenovationWorkBusinessLogic.BusinessLogics
}); });
if (element != null && element.Id != model.Id) if (element != null && element.Id != model.Id)
{ {
throw new InvalidOperationException("Компьютер с таким названием уже есть"); throw new InvalidOperationException("Ремонт с таким названием уже есть");
} }
} }
} }

View File

@ -12,9 +12,7 @@ namespace RenovationWorkDatabaseImplement.Implements
public List<ComponentViewModel> GetFullList() public List<ComponentViewModel> GetFullList()
{ {
using var context = new RenovationWorkDatabase(); using var context = new RenovationWorkDatabase();
return context.Components return context.Components.Select(x => x.GetViewModel).ToList();
.Select(x => x.GetViewModel)
.ToList();
} }
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel model) public List<ComponentViewModel> GetFilteredList(ComponentSearchModel model)
{ {
@ -23,10 +21,7 @@ namespace RenovationWorkDatabaseImplement.Implements
return new(); return new();
} }
using var context = new RenovationWorkDatabase(); using var context = new RenovationWorkDatabase();
return context.Components return context.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList();
.Where(x => x.ComponentName.Contains(model.ComponentName))
.Select(x => x.GetViewModel)
.ToList();
} }
public ComponentViewModel? GetElement(ComponentSearchModel model) public ComponentViewModel? GetElement(ComponentSearchModel model)
{ {
@ -35,11 +30,7 @@ namespace RenovationWorkDatabaseImplement.Implements
return null; return null;
} }
using var context = new RenovationWorkDatabase(); using var context = new RenovationWorkDatabase();
return context.Components return context.Components.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName ==
model.ComponentName) ||
(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
} }
public ComponentViewModel? Insert(ComponentBindingModel model) public ComponentViewModel? Insert(ComponentBindingModel model)
{ {
@ -69,13 +60,13 @@ namespace RenovationWorkDatabaseImplement.Implements
{ {
using var context = new RenovationWorkDatabase(); using var context = new RenovationWorkDatabase();
var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null) if (element == null)
{ {
return null;
}
context.Components.Remove(element); context.Components.Remove(element);
context.SaveChanges(); context.SaveChanges();
return element.GetViewModel; return element.GetViewModel;
} }
return null;
}
} }
} }

View File

@ -11,84 +11,76 @@ namespace RenovationWorkDatabaseImplement.Implements
{ {
public List<OrderViewModel> GetFullList() public List<OrderViewModel> GetFullList()
{ {
using var Context = new RenovationWorkDatabase(); using var context = new RenovationWorkDatabase();
return context.Orders.Include(x => x.Repair).Select(x => x.GetViewModel).ToList();
return Context.Orders }
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new RenovationWorkDatabase();
return context.Orders
.Include(x => x.Repair) .Include(x => x.Repair)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel) .Select(x => x.GetViewModel)
.ToList(); .ToList();
} }
public OrderViewModel? GetElement(OrderSearchModel model)
public List<OrderViewModel> GetFilteredList(OrderSearchModel Model)
{ {
if (!Model.Id.HasValue) if (!model.Id.HasValue)
return new(); {
return null;
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();
public OrderViewModel? GetElement(OrderSearchModel Model) return context.Orders.Include(x => x.Repair)
.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
public OrderViewModel? Insert(OrderBindingModel model)
{ {
if (!Model.Id.HasValue) using var context = new RenovationWorkDatabase();
return new(); if (model == null)
return null;
using var Context = new RenovationWorkDatabase(); var newOrder = Order.Create(context, model);
if (newOrder == null)
return Context.Orders {
return null;
}
context.Orders.Add(newOrder);
context.SaveChanges();
return newOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
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 context.Orders
.Include(x => x.Repair) .Include(x => x.Repair)
.FirstOrDefault(x => x.Id == Model.Id) .FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel; ?.GetViewModel;
} }
public OrderViewModel? Delete(OrderBindingModel model)
public OrderViewModel? Insert(OrderBindingModel Model)
{ {
using var Context = new RenovationWorkDatabase(); using var context = new RenovationWorkDatabase();
var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
if (Model == null) if (element != null)
return null;
var NewOrder = Order.Create(Context, Model);
if (NewOrder == null)
return null;
Context.Orders.Add(NewOrder);
Context.SaveChanges();
return NewOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel Model)
{ {
using var Context = new RenovationWorkDatabase(); var deletedElement = context.Orders
.Include(x => x.Repair)
var Order = Context.Orders.FirstOrDefault(x => x.Id == Model.Id); .FirstOrDefault(x => x.Id == model.Id)
if (Order == null) ?.GetViewModel;
context.Orders.Remove(element);
context.SaveChanges();
return deletedElement;
}
return null; return null;
Order.Update(Model);
Context.SaveChanges();
return Order.GetViewModel;
}
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;
} }
} }
} }

View File

@ -11,12 +11,7 @@ namespace RenovationWorkDatabaseImplement.Implements
public List<RepairViewModel> GetFullList() public List<RepairViewModel> GetFullList()
{ {
using var context = new RenovationWorkDatabase(); using var context = new RenovationWorkDatabase();
return context.Repairs return context.Repairs.Include(x => x.Components).ThenInclude(x => x.Component).ToList().Select(x => x.GetViewModel).ToList();
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
} }
public List<RepairViewModel> GetFilteredList(RepairSearchModel model) public List<RepairViewModel> GetFilteredList(RepairSearchModel model)
{ {
@ -25,13 +20,8 @@ namespace RenovationWorkDatabaseImplement.Implements
return new(); return new();
} }
using var context = new RenovationWorkDatabase(); using var context = new RenovationWorkDatabase();
return context.Repairs return context.Repairs.Include(x => x.Components).ThenInclude(x => x.Component).Where(x => x.RepairName.Contains(model.RepairName)).ToList().Select(x => x.GetViewModel).ToList();
.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) public RepairViewModel? GetElement(RepairSearchModel model)
{ {
@ -41,24 +31,20 @@ namespace RenovationWorkDatabaseImplement.Implements
return null; return null;
} }
using var context = new RenovationWorkDatabase(); using var context = new RenovationWorkDatabase();
return context.Repairs return context.Repairs.Include(x => x.Components).ThenInclude(x => x.Component)
.Include(x => x.Components) .FirstOrDefault(x => (!string.IsNullOrEmpty(model.RepairName) && x.RepairName == model.RepairName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
.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) public RepairViewModel? Insert(RepairBindingModel model)
{ {
using var context = new RenovationWorkDatabase(); using var context = new RenovationWorkDatabase();
var newRepair = Repair.Create(context, model); var newEngine = Repair.Create(context, model);
if (newRepair == null) if (newEngine == null)
{ {
return null; return null;
} }
context.Repairs.Add(newRepair); context.Repairs.Add(newEngine);
context.SaveChanges(); context.SaveChanges();
return newRepair.GetViewModel; return newEngine.GetViewModel;
} }
public RepairViewModel? Update(RepairBindingModel model) public RepairViewModel? Update(RepairBindingModel model)
{ {
@ -66,17 +52,17 @@ namespace RenovationWorkDatabaseImplement.Implements
using var transaction = context.Database.BeginTransaction(); using var transaction = context.Database.BeginTransaction();
try try
{ {
var repair = context.Repairs.FirstOrDefault(rec => var engine = context.Repairs.FirstOrDefault(rec =>
rec.Id == model.Id); rec.Id == model.Id);
if (repair == null) if (engine == null)
{ {
return null; return null;
} }
repair.Update(model); engine.Update(model);
context.SaveChanges(); context.SaveChanges();
repair.UpdateComponents(context, model); engine.UpdateComponents(context, model);
transaction.Commit(); transaction.Commit();
return repair.GetViewModel; return engine.GetViewModel;
} }
catch catch
{ {

View File

@ -12,7 +12,7 @@ using RenovationWorkDatabaseImplement;
namespace RenovationWorkDatabaseImplement.Migrations namespace RenovationWorkDatabaseImplement.Migrations
{ {
[DbContext(typeof(RenovationWorkDatabase))] [DbContext(typeof(RenovationWorkDatabase))]
[Migration("20240405063901_InitialCreate")] [Migration("20240418122550_InitialCreate")]
partial class InitialCreate partial class InitialCreate
{ {
/// <inheritdoc /> /// <inheritdoc />

View File

@ -27,7 +27,7 @@ namespace RenovationWorkDatabaseImplement.Models
{ {
return null; return null;
} }
return new Component() return new Component
{ {
Id = model.Id, Id = model.Id,
ComponentName = model.ComponentName, ComponentName = model.ComponentName,
@ -58,6 +58,5 @@ namespace RenovationWorkDatabaseImplement.Models
ComponentName = ComponentName, ComponentName = ComponentName,
Cost = Cost Cost = Cost
}; };
} }
} }

View File

@ -35,17 +35,18 @@ namespace RenovationWorkDatabaseImplement.Models
public DateTime? DateImplement { get; private set; } 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() return new Order()
{ {
Id = Model.Id, Id = model.Id,
RepairId = Model.RepairId, RepairId = model.RepairId,
Count = Model.Count, Repair = context.Repairs.First(x => x.Id == model.RepairId),
Sum = Model.Sum, Count = model.Count,
Status = Model.Status, Sum = model.Sum,
DateCreate = Model.DateCreate, Status = model.Status,
DateImplement = Model.DateImplement, DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
}; };
} }
@ -54,11 +55,7 @@ namespace RenovationWorkDatabaseImplement.Models
if (Model == null) if (Model == null)
return; return;
Id = Model.Id;
RepairId = Model.RepairId;
Sum = Model.Sum;
Status = Model.Status; Status = Model.Status;
DateCreate = Model.DateCreate;
DateImplement = Model.DateImplement; DateImplement = Model.DateImplement;
} }