lab3 correcting some mistakes

This commit is contained in:
Zakharov_Rostislav 2024-03-24 20:26:32 +04:00
parent 697ef996b9
commit 7c0f517716
2 changed files with 10 additions and 27 deletions

View File

@ -17,7 +17,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
{
using var context = new BlacksmithWorkshopDataBase();
return context.Orders
.Select(x => AccessManufactureStorage(x.GetViewModel))
.Select(x => x.GetViewModel)
.ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
@ -29,7 +29,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
using var context = new BlacksmithWorkshopDataBase();
return context.Orders
.Where(x => x.Id == model.Id)
.Select(x => AccessManufactureStorage(x.GetViewModel))
.Select(x => x.GetViewModel)
.ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
@ -39,7 +39,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
return null;
}
using var context = new BlacksmithWorkshopDataBase();
return AccessManufactureStorage(context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel);
return context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
public OrderViewModel? Insert(OrderBindingModel model)
{
@ -51,7 +51,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
using var context = new BlacksmithWorkshopDataBase();
context.Orders.Add(newOrder);
context.SaveChanges();
return AccessManufactureStorage(newOrder.GetViewModel);
return newOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
@ -64,7 +64,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
}
order.Update(model);
context.SaveChanges();
return AccessManufactureStorage(order.GetViewModel);
return order.GetViewModel;
}
public OrderViewModel? Delete(OrderBindingModel model)
{
@ -75,25 +75,9 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
{
context.Orders.Remove(element);
context.SaveChanges();
return AccessManufactureStorage(element.GetViewModel);
return element.GetViewModel;
}
return null;
}
public static OrderViewModel AccessManufactureStorage(OrderViewModel model)
{
if (model == null)
return null;
using var context = new BlacksmithWorkshopDataBase();
foreach (var manufacture in context.Manufactures)
{
if (manufacture.Id == model.ManufactureId)
{
model.ManufactureName = manufacture.ManufactureName;
break;
}
}
return model;
}
}
}

View File

@ -25,13 +25,10 @@ namespace BlacksmithWorkshopDatabaseImplement.Models
public DateTime? DateImplement { get; private set; }
[Required]
public int ManufactureId { get; private set; }
public virtual Manufacture Manufacture { get; set; } = new();
public static Order? Create(OrderBindingModel model)
public static Order? Create(BlacksmithWorkshopDataBase context, OrderBindingModel model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
@ -41,6 +38,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Models
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
ManufactureId = model.ManufactureId,
Manufacture = context.Manufactures.First(y => y.Id == model.ManufactureId)
};
}
public void Update(OrderBindingModel? model)
@ -55,6 +53,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Models
public OrderViewModel GetViewModel => new()
{
ManufactureId = ManufactureId,
ManufactureName = Manufacture.ManufactureName,
Count = Count,
Sum = Sum,
Status = Status,