поменял слеганца

This commit is contained in:
antoc0der 2024-03-25 21:49:54 +04:00
parent 380740c36a
commit 2eb03ea5c1
2 changed files with 27 additions and 28 deletions

View File

@ -27,9 +27,9 @@ namespace FlowerShopDatabaseImplement.Models
public DateTime? DateImplement { get; private set; }
[Required]
public int FlowerId { get; private set; }
public virtual Flower Flower { get; private set; }
public virtual Flower? Flower { get; private set; }
public static Order? Create(FlowerShopDataBase context,OrderBindingModel model)
public static Order? Create(OrderBindingModel model)
{
if (model == null)
{
@ -44,7 +44,7 @@ namespace FlowerShopDatabaseImplement.Models
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
FlowerId = model.FlowerId,
Flower = context.Flowers.FirstOrDefault(x => x.Id == model.FlowerId)
//Flower = context.Flowers.FirstOrDefault(x => x.Id == model.FlowerId)
};
}
@ -66,7 +66,7 @@ namespace FlowerShopDatabaseImplement.Models
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
FlowerName = Flower.FlowerName,
FlowerName = Flower?.FlowerName ?? String.Empty ,
Id = Id,
};

View File

@ -19,7 +19,7 @@ namespace FlowerShopDatabaseImplement.Implements
{
using var context = new FlowerShopDataBase();
return context.Orders.Include(x => x.Flower)
.Select(x => AccessFlowerStorage(x.GetViewModel))
.Select(x => x.GetViewModel)
.ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
@ -31,7 +31,7 @@ namespace FlowerShopDatabaseImplement.Implements
using var context = new FlowerShopDataBase();
return context.Orders.Include(x => x.Flower)
.Where(x => x.Id == model.Id)
.Select(x => AccessFlowerStorage(x.GetViewModel))
.Select(x => x.GetViewModel)
.ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
@ -41,19 +41,19 @@ namespace FlowerShopDatabaseImplement.Implements
return null;
}
using var context = new FlowerShopDataBase();
return AccessFlowerStorage(context.Orders.Include(x => x.Flower).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel);
return context.Orders.Include(x => x.Flower).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
public OrderViewModel? Insert(OrderBindingModel model)
{
using var context = new FlowerShopDataBase();
var newOrder = Order.Create(context, model);
var newOrder = Order.Create( model);
if (newOrder == null)
{
return null;
}
context.Orders.Add(newOrder);
context.SaveChanges();
return AccessFlowerStorage(newOrder.GetViewModel);
return newOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
@ -65,36 +65,35 @@ namespace FlowerShopDatabaseImplement.Implements
}
order.Update(model);
context.SaveChanges();
return AccessFlowerStorage(order.GetViewModel);
return order.GetViewModel;
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new FlowerShopDataBase();
var element = context.Orders.Include(x => x.Flower).FirstOrDefault(rec => rec.Id ==
model.Id);
var element = context.Orders.Include(x => x.Flower).FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
return AccessFlowerStorage(element.GetViewModel);
return element.GetViewModel;
}
return null;
}
public static OrderViewModel AccessFlowerStorage(OrderViewModel model)
{
if (model == null)
return null;
using var context = new FlowerShopDataBase();
foreach (var flower in context.Flowers)
{
if (flower.Id == model.FlowerId)
{
model.FlowerName = flower.FlowerName;
break;
}
}
return model;
}
//public static OrderViewModel AccessFlowerStorage(OrderViewModel model)
//{
// if (model == null)
// return null;
// using var context = new FlowerShopDataBase();
// foreach (var flower in context.Flowers)
// {
// if (flower.Id == model.FlowerId)
// {
// model.FlowerName = flower.FlowerName;
// break;
// }
// }
// return model;
//}
}
}