Compare commits

...

5 Commits

13 changed files with 269 additions and 23 deletions

View File

@ -13,7 +13,7 @@ namespace ComputerShopContracts.BindingModels
public int UserId { get; set; }
public int AssemblyId { get; set; }
public int? AssemblyId { get; set; }
public DateTime DateMake { get; set; } = DateTime.Now;

View File

@ -7,12 +7,12 @@ namespace ComputerShopContracts.ViewModels
{
public int Id { get; set; }
public int UserId { get; }
public int UserId { get; set; }
[DisplayName("Название комплектующей")]
public string ComponentName { get; } = string.Empty;
public string ComponentName { get; set; } = string.Empty;
[DisplayName("Стоимость")]
public double Cost { get; }
public double Cost { get; set; }
}
}

View File

@ -22,7 +22,7 @@ namespace ComputerShopContracts.ViewModels
public string UserLogin { get; set; } = string.Empty;
//id сборки
public int AssemblyId { get; set; }
public int? AssemblyId { get; set; }
//!!!МБ НАДО БУДЕТ ПРИВЯЗЫВАТЬ НАЗВАНИЕ СБОРКИ И Т.Д. ДЛЯ ОТЧЁТОВ

View File

@ -17,7 +17,7 @@ namespace ComputerShopDataModels.Models
/// <summary>
/// ID сборки
/// </summary>
int AssemblyId { get; }
int? AssemblyId { get; }
Dictionary<int, IOrderModel> RequestOrders { get; }

View File

@ -17,12 +17,23 @@ namespace ComputerShopDatabaseImplement
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);
}
public virtual DbSet<User> Users { get; set; }
public virtual DbSet<Order> Orders { get; set; }
public virtual DbSet<Request> Requests { get; set; }
public virtual DbSet<Component> Components { get; set; }
public virtual DbSet<Assembly> Assemblies { get; set; }
public virtual DbSet<AssemblyComponent> AssemblyComponents { get; set; }
public virtual DbSet<Product> Products { get; set; }
public virtual DbSet<ProductComponent> ProductComponents { get; set; }
public virtual DbSet<Order> Orders { get; set; }
public virtual DbSet<Request> Requests { get; set; }
public virtual DbSet<Shipment> Shipments { get; set; }
public virtual DbSet<ShipmentOrder> ShipmentOrders { get; set; }
public virtual DbSet<RequestOrder> RequestOrders { get; set; }
public virtual DbSet<ShipmentOrder> ShipmentOrders { get; set; }
public virtual DbSet<RequestOrder> RequestOrders { get; set; }
}
}

View File

@ -0,0 +1,65 @@
using ComputerShopContracts.BindingModels;
using ComputerShopDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ComputerShopDatabaseImplement.Models
{
public class Assembly : IAssemblyModel
{
public int Id { get; private set; }
[Required]
public int UserId { get; private set; }
[Required]
public string AssemblyName { get; private set; } = string.Empty;
[Required]
public double Cost { get; private set; }
[Required]
public string Category { get; private set; } = string.Empty;
[ForeignKey("ComponentId")]
public virtual List<AssemblyComponent> Components { get; set; } = new();
private Dictionary<int, (IComponentModel, int)>? _assemblyComponents;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> AssemblyComponents
{
get
{
if (_assemblyComponents == null)
{
_assemblyComponents = Components.ToDictionary(
AsmComp => AsmComp.ComponentId,
AsmComp => (AsmComp.Component as IComponentModel, AsmComp.Count)
);
}
return _assemblyComponents;
}
}
public static Assembly Create(ComputerShopDatabase Context, AssemblyBindingModel Model)
{
return new()
{
Id = Model.Id,
UserId = Model.UserId,
AssemblyName = Model.AssemblyName,
Cost = Model.Cost,
Category = Model.Category,
Components = Model.AssemblyComponents.Select(x => new AssemblyComponent
{
Component = Context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList(),
};
}
// TODO: Update(), ViewModel, UpdateComponents()
}
}

View File

@ -0,0 +1,22 @@
using System.ComponentModel.DataAnnotations;
namespace ComputerShopDatabaseImplement.Models
{
public class AssemblyComponent
{
public int Id { get; set; }
[Required]
public int AssemblyId { get; set; }
[Required]
public int ComponentId { get; set; }
[Required]
public int Count { get; set; }
public virtual Assembly Assembly { get; set; } = new();
public virtual Component Component { get; set; } = new();
}
}

View File

@ -0,0 +1,50 @@
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.ViewModels;
using ComputerShopDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ComputerShopDatabaseImplement.Models
{
public class Component : IComponentModel
{
public int Id { get; private set; }
[Required]
public int UserId { get; private set; }
[Required]
public string ComponentName { get; private set; } = string.Empty;
[Required]
public double Cost { get; private set; }
[ForeignKey("ComponentId")]
public virtual List<AssemblyComponent> AssemblyComponents { get; set; } = new();
public static Component Create(ComponentBindingModel Model)
{
return new()
{
Id = Model.Id,
UserId = Model.UserId,
ComponentName = Model.ComponentName,
Cost = Model.Cost,
};
}
public void Update(ComponentBindingModel Model)
{
ComponentName = Model.ComponentName;
Cost = Model.Cost;
}
public ComponentViewModel ViewModel => new()
{
Id = Id,
UserId = UserId,
ComponentName = ComponentName,
Cost = Cost,
};
}
}

View File

@ -5,6 +5,7 @@ using ComputerShopDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -16,7 +17,6 @@ namespace ComputerShopDatabaseImplement.Models
{
public int Id { get; set; }
//!!!ТУТ МБ СДЕЛАТЬ КАК FOREIGN KEY
[Required]
public int UserId { get; private set; }
@ -32,13 +32,20 @@ namespace ComputerShopDatabaseImplement.Models
//!!!мб не нужен
public virtual User User { get; set; }
//!!!ПРОВЕРМТЬ, ЧТО ПАРВИЛЬНЫЕ ВНЕШНИЕ КЛЮЧИ
[ForeignKey("OrderId")]
public virtual List<RequestOrder> RequestOrders { get; set; } = new();
[ForeignKey("OrderId")]
public virtual List<ShipmentOrder> ShipmentOrders { get; set; } = new();
public static Order? Create(OrderBindingModel model)
{
if (model == null)
{
return null;
}
using var context = new ComputerShopDatabase();
return new Order()
{
Id = model.Id,
@ -56,7 +63,7 @@ namespace ComputerShopDatabaseImplement.Models
return;
}
using var context = new ComputerShopDatabase();
//!!!МБ НАДО БУДЕТ ОБНОВЛЯТЬ ПОЛЬЗОВАТЕЛЯ, НО ОН ПО СУТИ НЕ ДОЛЖЕН ОБНОВЯЛТЬСЯ ПОСЛЕ СОЗДАНИЯ
//!!!МБ НАДО БУДЕТ ОБНОВЛЯТЬ ПОЛЬЗОВАТЕЛЯ, НО ОН ПО СУТИ НЕ ДОЛЖЕН ОБНОВЛЯТЬСЯ ПОСЛЕ СОЗДАНИЯ
Status = model.Status;
context.SaveChanges();
}

View File

@ -0,0 +1,66 @@
using ComputerShopContracts.BindingModels;
using ComputerShopDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ComputerShopDatabaseImplement.Models
{
public class Product : IProductModel
{
public int Id { get; set; }
[Required]
public int UserId { get; set; }
[Required]
public string ProductName { get; set; } = string.Empty;
[Required]
public double Cost { get; set; }
[Required]
public int Warranty { get; set; }
public int? ShipmentId { get; set; }
[ForeignKey("ComponentId")]
public virtual List<ProductComponent> Components { get; set; } = new();
private Dictionary<int, (IComponentModel, int)>? _productComponents;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> ProductComponents
{
get
{
if (_productComponents == null)
{
_productComponents = Components.ToDictionary(
ProdComp => ProdComp.ComponentId,
ProdComp => (ProdComp.Component as IComponentModel, ProdComp.Count)
);
}
return _productComponents;
}
}
public static Product Create(ComputerShopDatabase Context, ProductBindingModel Model)
{
return new()
{
Id = Model.Id,
UserId = Model.UserId,
ProductName = Model.ProductName,
Cost = Model.Cost,
Warranty = Model.Warranty,
Components = Model.ProductComponents.Select(x => new ProductComponent
{
Component = Context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList(),
ShipmentId = Model.ShipmentId,
};
}
}
}

View File

@ -0,0 +1,22 @@
using System.ComponentModel.DataAnnotations;
namespace ComputerShopDatabaseImplement.Models
{
public class ProductComponent
{
public int Id { get; set; }
[Required]
public int ProductId { get; set; }
[Required]
public int ComponentId { get; set; }
[Required]
public int Count { get; set; }
public virtual Product Product { get; set; } = new();
public virtual Component Component { get; set; } = new();
}
}

View File

@ -12,6 +12,7 @@ using System.Threading.Tasks;
namespace ComputerShopDatabaseImplement.Models
{
//!!!ДОБАВИТЬ МЕТОД ПО ПРИВЯЗКЕ СБОРКИ
//!!!МБ У Id сделать private set у ВСЕХ МОИХ МОДЕЛЕЙ
public class Request : IRequestModel
{
public int Id { get; set; }
@ -20,9 +21,11 @@ namespace ComputerShopDatabaseImplement.Models
public virtual User User { get; set; }
public int AssemblyId { get; set; }
public int? AssemblyId { get; set; }
[Required]
public DateTime DateMake { get; set; }
[Required]
public string ClientFIO { get; set; } = string.Empty;
@ -36,14 +39,17 @@ namespace ComputerShopDatabaseImplement.Models
if (_requestOrders == null)
{
//!!!ТУТ ПРОПИСАТЬ ЛОГИКУ (НЕ ЗНАЮ ПОКА, КАК)
if (_requestOrders == null)
{
_requestOrders = Orders.ToDictionary(x => x.OrderId, x => (x.Order as IOrderModel));
}
}
return _requestOrders;
}
}
[ForeignKey("OrderId")]
[ForeignKey("RequestId")]
public virtual List<RequestOrder> Orders { get; set; } = new();
public static Request Create(ComputerShopDatabase context, RequestBindingModel model)

View File

@ -16,7 +16,6 @@ namespace ComputerShopDatabaseImplement.Models
{
public int Id { get; set; }
//!!!ТУТ МБ СДЕЛАТЬ КАК FOREIGN KEY
[Required]
public int UserId { get; private set; }
@ -42,17 +41,16 @@ namespace ComputerShopDatabaseImplement.Models
if (_shipmentOrders == null)
{
//!!!ТУТ ПРОПИСАТЬ ЛОГИКУ (НЕ ЗНАЮ ПОКА, КАК)
_shipmentOrders = Orders.ToDictionary(x => x.OrderId, x => (x.Order as IOrderModel));
}
return _shipmentOrders;
}
}
//!!!ПРОВЕРИТЬ, ЧТО ТУТ ПРАВИЛЬНО СДЕЛАНО (ЧТО НАЗВАНИЕ ПРАВИЛЬНОЕ У ВНЕШ. КЛЮЧА, ЧТО ПРАВИЛЬНЫЙ ТИП в <>)
[ForeignKey("OrderId")]
[ForeignKey("ShipmentId")]
public virtual List<ShipmentOrder> Orders { get; set; } = new();
//!!!МБ ТУТ НАДО БУДЕТ ПРИСВАИВАТЬ ORDERS (но вряд ли, потому что по заданию заказы привязываются отдельно)
//!!!ПРОВЕРИТЬ, ЧТО ПРАВИЛЬНО ПРИСВАИВАЮ ЗНАЧЕНИЕ СПИСКУ ЗАКАЗОВ
public static Shipment Create(ComputerShopDatabase context, ShipmentBindingModel model)
{
@ -96,7 +94,6 @@ namespace ComputerShopDatabaseImplement.Models
//удаление тех заказов, которых нет в модели
if (shipmentOrders != null && shipmentOrders.Count > 0)
{
//!!!ТУТ МБ НЕ x.OrderId, а x.ShipmentId, но не факт (вряд ли)
context.ShipmentOrders.RemoveRange(shipmentOrders.Where(x => !model.ShipmentOrders.ContainsKey(x.OrderId)));
context.SaveChanges();