order + client (db)

This commit is contained in:
VictoriaPresnyakova 2023-04-01 21:09:59 +04:00
parent 6dc87a5d88
commit b3f2794a34
2 changed files with 37 additions and 9 deletions

View File

@ -18,7 +18,8 @@ namespace JewelryStoreDatabaseImplement.Implements
{
using var context = new JewelryStoreDataBase();
var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
var element = context.Orders.Include(x => x.Jewel)
.Include(x => x.Client).FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
@ -40,26 +41,47 @@ namespace JewelryStoreDatabaseImplement.Implements
using var context = new JewelryStoreDataBase();
return context.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
return context.Orders.Include(x => x.Jewel)
.Include(x => x.Client).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.DateFrom.HasValue || ! model.DateTo.HasValue)
if (!model.DateFrom.HasValue && !model.DateTo.HasValue && !model.ClientId.HasValue)
{
return new();
}
using var context = new JewelryStoreDataBase();
return context.Orders.Include(x => x.Jewel).Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo).Select(x => x.GetViewModel).ToList();
if (model.DateFrom.HasValue)
{
return context.Orders
.Include(x => x.Jewel)
.Include(x => x.Client)
.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
.Select(x => x.GetViewModel)
.ToList();
}
else if (model.ClientId.HasValue)
return context.Orders
.Include(x => x.Jewel)
.Include(x => x.Client)
.Where(x => x.ClientId == model.ClientId)
.Select(x => x.GetViewModel)
.ToList();
return context.Orders
.Include(x => x.Jewel)
.Include(x => x.Client)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
public List<OrderViewModel> GetFullList()
{
using var context = new JewelryStoreDataBase();
return context.Orders.Select(x => x.GetViewModel).ToList();
return context.Orders.Include(x => x.Jewel)
.Include(x => x.Client).Select(x => x.GetViewModel).ToList();
}
public OrderViewModel? Insert(OrderBindingModel model)
@ -83,8 +105,8 @@ namespace JewelryStoreDatabaseImplement.Implements
{
using var context = new JewelryStoreDataBase();
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
var order = context.Orders.Include(x => x.Jewel)
.Include(x => x.Client).FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;

View File

@ -18,6 +18,9 @@ namespace JewelryStoreDatabaseImplement.Models
public int JewelId { get; private set; }
[Required]
public int ClientId { get; set; }
public string JewelName { get; private set; } = string.Empty;
[Required]
@ -35,6 +38,7 @@ namespace JewelryStoreDatabaseImplement.Models
public DateTime? DateImplement { get; private set; }
public virtual Jewel Jewel { get; set; }
public Client Client { get; set; }
public static Order? Create(OrderBindingModel? model)
{
@ -47,6 +51,7 @@ namespace JewelryStoreDatabaseImplement.Models
{
Id = model.Id,
JewelId = model.JewelId,
ClientId = model.ClientId,
JewelName = model.JewelName,
Count = model.Count,
Sum = model.Sum,
@ -76,6 +81,7 @@ namespace JewelryStoreDatabaseImplement.Models
{
Id = Id,
JewelId = JewelId,
ClientId = ClientId,
JewelName = JewelName,
Count = Count,
Sum = Sum,