data base fix

This commit is contained in:
Илья Федотов 2024-04-29 18:34:49 +04:00
parent 11ea6cc0c0
commit d934652b6c
3 changed files with 61 additions and 23 deletions

View File

@ -3,6 +3,8 @@ using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.StorageContracts;
using ElectronicsShopContracts.ViewModels;
using ElectronicsShopDataBaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Migrations;
using System;
using System.Collections.Generic;
using System.Linq;
@ -38,13 +40,17 @@ namespace ElectronicsShopDataBaseImplement.Implements
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.DateFrom.HasValue)
if (!model.ID.HasValue && (model.DateFrom == null || model.DateTo == null))
{
return new();
}
using var context = new Database();
//Придумать решение
return context.Orders.Where(x => x.Status.Contains(model.ID).Select(x => x.GetViewModel)).ToList();
return context.Orders
.Include(x => x.Products)
.ThenInclude(x => x._product)
.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
.Select(x => x.GetViewModel)
.ToList();
}
public List<OrderViewModel> GetFullList()
@ -55,12 +61,12 @@ namespace ElectronicsShopDataBaseImplement.Implements
public OrderViewModel? Insert(OrderBindingModel model)
{
var newComponent = Order.Create(model);
using var context = new Database();
var newComponent = Order.Create(context,model);
if (newComponent == null)
{
return null;
}
using var context = new Database();
context.Orders.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;

View File

@ -5,6 +5,7 @@ using ElectronicsShopDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics;
using System.Linq;
using System.Text;
@ -24,11 +25,24 @@ namespace ElectronicsShopDataBaseImplement.Models
[Required]
public DateTime DateCreate { get; set; }= DateTime.Now;
[Required]
public DateTime? DateImplemet { get; set; } = null;
public DateTime? DateImplement { get; set; }
[Required]
public Dictionary<int, (IProductModel, int)> ProductList { get; set; } = new();
public static Order? Create(OrderBindingModel? model)
[NotMapped]
public Dictionary<int, (IProductModel, int)> _productList
{
get {
if (ProductList == null) {
ProductList = Products.ToDictionary(recPC => recPC.ProductID, recPC => (recPC._product as IProductModel, recPC.Count));
}
return _productList;
}
}
public virtual List<OrderProducts> Products { get; set; } = new();
public static Order? Create(Database context ,OrderBindingModel? model)
{
if (model == null)
{
@ -41,19 +55,11 @@ namespace ElectronicsShopDataBaseImplement.Models
Status = model.Status,
PaymeantOption = model.PaymeantOption,
DateCreate = model.DateCreate,
//todo ProductList
};
}
public static Order? Create(OrderViewModel model)
{
return new Order
{
ID = model.ID,
Sum = model.Sum,
Status = model.Status,
PaymeantOption = model.PaymeantOption,
DateCreate = model.DateCreate,
//todo ProductList
Products = model.ProductList.Select(x => new OrderProducts
{
_product = context.Products.First(y => y.ID == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(OrderBindingModel model)
@ -67,9 +73,9 @@ namespace ElectronicsShopDataBaseImplement.Models
PaymeantOption = model.PaymeantOption;
Status = model.Status;
DateCreate = model.DateCreate;
if (model.DateImplemet != null)
if (model.DateImplement != null)
{
DateImplemet = model.DateImplemet;
DateImplement = model.DateImplement;
}
//todo ProductList
}
@ -81,7 +87,7 @@ namespace ElectronicsShopDataBaseImplement.Models
PaymeantOption = PaymeantOption,
Status = Status,
DateCreate = DateCreate,
DateImplemet=DateImplemet,
DateImplement=DateImplement,
ProductList = ProductList
};
}

View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ElectronicsShopDataBaseImplement.Models
{
public class OrderProducts
{
public int ID { get; set; }
[Required]
public int OrdersID { get; set; }
[Required]
public int ProductID { get; set; }
[Required]
public int Count { get; set; }
public virtual Order _order { get; set; } = new();
public virtual Product _product { get; set; } = new();
}
}