fix Db models

This commit is contained in:
Илья Федотов 2024-05-25 22:32:22 +04:00
parent d50c9f7f85
commit 7f9720200f
12 changed files with 67 additions and 52 deletions

View File

@ -14,8 +14,6 @@ namespace ElectronicsShopContracts.BindingModels
public int ClientID { get; set; } public int ClientID { get; set; }
public int? EmployeeID { get; set; }
public DateTime DateCreate { get; set; } = DateTime.Now; public DateTime DateCreate { get; set; } = DateTime.Now;
public double Sum { get; set; } public double Sum { get; set; }

View File

@ -12,7 +12,6 @@ namespace ElectronicsShopContracts.SearchModels
{ {
public int? ID { get; set; } public int? ID { get; set; }
public int? ClientID { get; set; } public int? ClientID { get; set; }
public int? EmployeeID { get; set; }
public DateTime? DateFrom { get; set; } public DateTime? DateFrom { get; set; }
public DateTime? DateTo { get; set;} public DateTime? DateTo { get; set;}
} }

View File

@ -15,15 +15,13 @@ namespace ElectronicsShopContracts.ViewModels
public int ClientID { get; set; } public int ClientID { get; set; }
public int? EmployeeID { get; set; }
[DisplayName("Дата заказа")] [DisplayName("Дата заказа")]
public DateTime DateCreate { get; set; } = DateTime.Now; public DateTime DateCreate { get; set; } = DateTime.Now;
[DisplayName("Сумма заказа")] [DisplayName("Сумма заказа")]
public double Sum { get; set; } public double Sum { get; set; }
[DisplayName("Список продуктов")] [DisplayName("Список товаров")]
public Dictionary<int, (IProductModel, int)> ProductList { get; set; } = new(); public Dictionary<int, (IProductModel, int)> ProductList { get; set; } = new();
} }
} }

View File

@ -11,18 +11,23 @@ using System.Threading.Tasks;
namespace ElectronicsShopDataBaseImplement.Models namespace ElectronicsShopDataBaseImplement.Models
{ {
public class Client : IClientModel public class Client : IClientModel {
{
public int ID { get; set; } public int ID { get; set; }
[Required] [Required]
public string ClientFIO { get; set; } = string.Empty; public string ClientFIO { get; set; } = string.Empty;
[Required] [Required]
public string Password { get; set; } = string.Empty; public string Password { get; set; } = string.Empty;
[Required] [Required]
public string Email { get; set; } = string.Empty; public string Email { get; set; } = string.Empty;
//незаконная магия ленивой загрузки
[ForeignKey("ClientID")]
public virtual List<Order> Orders { get; set; } = new();
public static Client? Create(ClientBindingModel model) public static Client? Create(ClientBindingModel? model)
{ {
if(model == null) if(model == null)
{ {
@ -37,8 +42,7 @@ namespace ElectronicsShopDataBaseImplement.Models
}; };
} }
public void Update(ClientBindingModel? model)
public void Update(ClientBindingModel model)
{ {
if (model == null) if (model == null)
{ {

View File

@ -17,8 +17,10 @@ namespace ElectronicsShopDataBaseImplement.Models
[ForeignKey("EmployeeID")] [ForeignKey("EmployeeID")]
public int EmployeeID { get; set; } public int EmployeeID { get; set; }
[Required] [Required]
public string Name { get; set; }= string.Empty; public string Name { get; set; }= string.Empty;
[Required] [Required]
public double Price { get; set; } public double Price { get; set; }
@ -36,7 +38,7 @@ namespace ElectronicsShopDataBaseImplement.Models
Price = model.Price, Price = model.Price,
}; };
} }
public void Update(CostItemBindingModel model) public void Update(CostItemBindingModel? model)
{ {
if (model == null) if (model == null)
{ {

View File

@ -15,10 +15,13 @@ namespace ElectronicsShopDataBaseImplement.Models
public class Employee : IEmployeeModel public class Employee : IEmployeeModel
{ {
public int ID { get; set; } public int ID { get; set; }
[Required] [Required]
public string EmployeeFIO { get; set; } = string.Empty; public string EmployeeFIO { get; set; } = string.Empty;
[Required] [Required]
public string Login { get; set; } = string.Empty; public string Login { get; set; } = string.Empty;
[Required] [Required]
public string Password { get; set; } = string.Empty; public string Password { get; set; } = string.Empty;
@ -36,7 +39,7 @@ namespace ElectronicsShopDataBaseImplement.Models
Password = model.Password, Password = model.Password,
}; };
} }
public void Update(EmployeeBindingModel model) public void Update(EmployeeBindingModel? model)
{ {
if (model == null) if (model == null)
{ {

View File

@ -2,6 +2,7 @@
using ElectronicsShopContracts.ViewModels; using ElectronicsShopContracts.ViewModels;
using ElectronicsShopDataModels.Enums; using ElectronicsShopDataModels.Enums;
using ElectronicsShopDataModels.Models; using ElectronicsShopDataModels.Models;
using Microsoft.EntityFrameworkCore.Migrations;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
@ -16,12 +17,13 @@ namespace ElectronicsShopDataBaseImplement.Models
public class Order : IOrderModel public class Order : IOrderModel
{ {
public int ID { get; set; } public int ID { get; set; }
[Required] [Required]
public double Sum { get; set; } public double Sum { get; set; }
[ForeignKey("ClientID")] [ForeignKey("ClientID")]
public int ClientID { get; set; } public int ClientID { get; set; }
[ForeignKey("EmployeeID")]
public int? EmployeeID { get; set; }
[Required] [Required]
public DateTime DateCreate { get; set; } = DateTime.Now; public DateTime DateCreate { get; set; } = DateTime.Now;
@ -38,8 +40,12 @@ namespace ElectronicsShopDataBaseImplement.Models
} }
} }
[ForeignKey("ProductID")]
public virtual List<OrderProduct> Products { get; set; } = new(); public virtual List<OrderProduct> Products { get; set; } = new();
[ForeignKey("PaymentID")]
public virtual List<Payment> Payments { get; set; } = new();
public static Order? Create(Database context ,OrderBindingModel? model) public static Order? Create(Database context ,OrderBindingModel? model)
{ {
if (model == null) if (model == null)
@ -51,7 +57,6 @@ namespace ElectronicsShopDataBaseImplement.Models
ID = model.ID, ID = model.ID,
ClientID=model.ClientID, ClientID=model.ClientID,
Sum = model.Sum, Sum = model.Sum,
EmployeeID=model.EmployeeID,
DateCreate = model.DateCreate, DateCreate = model.DateCreate,
Products = model.ProductList.Select(x => new OrderProduct Products = model.ProductList.Select(x => new OrderProduct
{ {
@ -60,38 +65,36 @@ namespace ElectronicsShopDataBaseImplement.Models
}).ToList() }).ToList()
}; };
} }
public void Update(Database context,OrderBindingModel model)
{
if (model == null)
{
return;
}
ClientID = model.ClientID;
Sum = model.Sum;
EmployeeID = model.EmployeeID;
DateCreate = model.DateCreate;
Products = model.ProductList.Select(x => new OrderProduct
{
_product = context.Products.First(y => y.ID == x.Key),
Count = x.Value.Item2
}).ToList();
//todo ProductList
}
public OrderViewModel GetViewModel => new() public OrderViewModel GetViewModel => new()
{ {
ID = ID, ID = ID,
ClientID = ClientID, ClientID = ClientID,
Sum = Sum, Sum = Sum,
EmployeeID = EmployeeID,
DateCreate = DateCreate, DateCreate = DateCreate,
/*
ProductList = ProductList.Select(x => new OrderProducts
{
/_product = context.Products.First(y => y.ID == x.Key)
Count = x.Value.Item2
}).ToList()
*/
}; };
public void UpdateProducts(Database context,OrderBindingModel model) {
var orderProducts = context.OrderProducts.Where(rec => rec.ProductID == model.ID).ToList();
if (orderProducts != null && orderProducts.Count > 0) {
context.OrderProducts.RemoveRange(orderProducts.Where(rec => !model.ProductList.ContainsKey(rec.ProductID)));
context.SaveChanges();
foreach (var updateProducts in orderProducts) {
updateProducts.Count = model.ProductList[updateProducts.ProductID].Item2;
model.ProductList.Remove(updateProducts.ProductID);
}
context.SaveChanges();
}
var order = context.Orders.First(x => x.ID == ID);
foreach (var op in model.ProductList) {
context.OrderProducts.Add(new OrderProduct {
_order = order,
_product = context.Products.First(x => x.ID == op.Key),
Count = op.Value.Item2
});
context.SaveChanges();
}
_productList = null;
}
} }
} }

View File

@ -20,7 +20,10 @@ namespace ElectronicsShopDataBaseImplement.Models
[Required] [Required]
public int Count { get; set; } public int Count { get; set; }
[ForeignKey("OrderID")]
public virtual Order _order { get; set; } = new(); public virtual Order _order { get; set; } = new();
[ForeignKey("ProductID")]
public virtual Product _product { get; set; } = new(); public virtual Product _product { get; set; } = new();
} }
} }

View File

@ -16,14 +16,19 @@ namespace ElectronicsShopDataBaseImplement.Models
public class Payment : IPaymentModel public class Payment : IPaymentModel
{ {
public int ID { get; set; } public int ID { get; set; }
[ForeignKey("ProductID")] [ForeignKey("ProductID")]
public int ProductID { get; set; } public int ProductID { get; set; }
[ForeignKey("OrderID")] [ForeignKey("OrderID")]
public int OrderID { get; set; } public int OrderID { get; set; }
[Required] [Required]
public double SumPayment { get; set; } public double SumPayment { get; set; }
[Required] [Required]
public PaymeantOption PayOption { get; set; } = PaymeantOption.Неоплачено; public PaymeantOption PayOption { get; set; } = PaymeantOption.Неоплачено;
public static Payment? Create(PaymentBindingModel? model) public static Payment? Create(PaymentBindingModel? model)
{ {
if (model == null) if (model == null)
@ -39,7 +44,7 @@ namespace ElectronicsShopDataBaseImplement.Models
PayOption = model.PayOption, PayOption = model.PayOption,
}; };
} }
public void Update(PaymentBindingModel model) public void Update(PaymentBindingModel? model)
{ {
if (model == null) if (model == null)
{ {

View File

@ -15,10 +15,13 @@ namespace ElectronicsShopDataBaseImplement.Models
public class Product : IProductModel public class Product : IProductModel
{ {
public int ID { get; set; } public int ID { get; set; }
[Required] [Required]
public string ProductName { get; set; } = string.Empty; public string ProductName { get; set; } = string.Empty;
[Required] [Required]
public double Price { get; set; } public double Price { get; set; }
[ForeignKey("CostItemID")] [ForeignKey("CostItemID")]
public int CostItemID { get; set; } public int CostItemID { get; set; }
@ -28,15 +31,14 @@ namespace ElectronicsShopDataBaseImplement.Models
{ {
return null; return null;
} }
return new Product() return new Product() {
{
ID = model.ID, ID = model.ID,
ProductName = model.ProductName, ProductName = model.ProductName,
Price = model.Price, Price = model.Price,
CostItemID = model.CostItemID, CostItemID = model.CostItemID,
}; };
} }
public void Update(ProductBindingModel model) public void Update(ProductBindingModel? model)
{ {
if (model == null) if (model == null)
{ {

View File

@ -10,7 +10,6 @@ namespace ElectronicsShopDataModels.Models
public interface IOrderModel : IID public interface IOrderModel : IID
{ {
int ClientID { get; } int ClientID { get; }
int? EmployeeID { get; }
DateTime DateCreate { get; } DateTime DateCreate { get; }
double Sum { get; } double Sum { get; }

View File

@ -14,6 +14,5 @@ namespace ElectronicsShopDataModels.Models
int OrderID { get; } int OrderID { get; }
double SumPayment { get; } double SumPayment { get; }
PaymeantOption PayOption { get; } PaymeantOption PayOption { get; }
} }
} }