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

View File

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