Надежда

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))
{
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("Ремонт с таким названием уже есть");
}
}
}

View File

@ -12,9 +12,7 @@ namespace RenovationWorkDatabaseImplement.Implements
public List<ComponentViewModel> GetFullList()
{
using var context = new RenovationWorkDatabase();
return context.Components
.Select(x => x.GetViewModel)
.ToList();
return context.Components.Select(x => x.GetViewModel).ToList();
}
public List<ComponentViewModel> 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)
{
return null;
}
context.Components.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

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

View File

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

View File

@ -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
};
}
}

View File

@ -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;
}