Модели базы данных
This commit is contained in:
parent
70d66b114c
commit
6d8678f21a
@ -30,6 +30,8 @@ namespace HardwareShopDatabaseImplement
|
||||
|
||||
public virtual DbSet<Good> Goods { set; get; }
|
||||
|
||||
public virtual DbSet<GoodComponent> GoodComponents { set; get; }
|
||||
|
||||
public virtual DbSet<Order> Orders { set; get; }
|
||||
|
||||
public virtual DbSet<Purchase> Purchases { set; get; }
|
||||
|
@ -0,0 +1,26 @@
|
||||
using HardwareShopDatabaseImplement.Models.Storekeeper;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace HardwareShopDatabaseImplement.Models.ManyToMany
|
||||
{
|
||||
public class GoodComponent
|
||||
{
|
||||
[Key]
|
||||
[Column(Order = 1)]
|
||||
[Required]
|
||||
public int GoodId { get; set; }
|
||||
|
||||
[Key]
|
||||
[Column(Order = 2)]
|
||||
[Required]
|
||||
public int ComponentId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
|
||||
public virtual Component Component { get; set; } = new();
|
||||
|
||||
public virtual Good Good { get; set; } = new();
|
||||
}
|
||||
}
|
@ -1,9 +1,9 @@
|
||||
using HardwareShopDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using HardwareShopContracts.BindingModels;
|
||||
using HardwareShopContracts.ViewModels;
|
||||
using HardwareShopDatabaseImplement.Models.ManyToMany;
|
||||
using HardwareShopDataModels.Models;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace HardwareShopDatabaseImplement.Models.Storekeeper
|
||||
{
|
||||
@ -11,6 +11,52 @@ namespace HardwareShopDatabaseImplement.Models.Storekeeper
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public virtual User User { get; set; }
|
||||
[Required]
|
||||
public string ComponentName { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public double Cost { get; set; }
|
||||
|
||||
[Required]
|
||||
public int UserId { get; set; }
|
||||
|
||||
[ForeignKey("ComponentId")]
|
||||
public virtual List<GoodComponent> SushiComponents { get; set; } = new();
|
||||
|
||||
public virtual User User { get; set; } = null!;
|
||||
|
||||
public static Component? Create(ComponentBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Component()
|
||||
{
|
||||
Id = model.Id,
|
||||
ComponentName = model.ComponentName,
|
||||
Cost = model.Cost,
|
||||
UserId = model.UserId
|
||||
};
|
||||
}
|
||||
|
||||
public void Update(ComponentBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
ComponentName = model.ComponentName;
|
||||
Cost = model.Cost;
|
||||
}
|
||||
|
||||
public ComponentViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
ComponentName = ComponentName,
|
||||
Cost = Cost,
|
||||
UserId = UserId,
|
||||
UserLogin = User.Login
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,9 @@
|
||||
using HardwareShopDataModels.Enums;
|
||||
using HardwareShopContracts.BindingModels;
|
||||
using HardwareShopContracts.ViewModels;
|
||||
using HardwareShopDatabaseImplement.Models.ManyToMany;
|
||||
using HardwareShopDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace HardwareShopDatabaseImplement.Models.Storekeeper
|
||||
{
|
||||
@ -12,6 +11,101 @@ namespace HardwareShopDatabaseImplement.Models.Storekeeper
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public virtual User User { get; set; }
|
||||
[Required]
|
||||
public string GoodName { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public double Price { get; set; }
|
||||
|
||||
[Required]
|
||||
public int UserId { get; set; }
|
||||
|
||||
public virtual User User { get; set; } = null!;
|
||||
|
||||
private Dictionary<int, (IComponentModel, int)>? _goodComponents = null;
|
||||
|
||||
[ForeignKey("GoodId")]
|
||||
public virtual List<GoodComponent> Components { get; set; } = new();
|
||||
|
||||
[ForeignKey("GoodId")]
|
||||
public virtual List<Order> Orders { get; set; } = new();
|
||||
|
||||
[NotMapped]
|
||||
public Dictionary<int, (IComponentModel, int)> GoodComponents
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_goodComponents == null)
|
||||
{
|
||||
_goodComponents = Components
|
||||
.ToDictionary(recGC => recGC.ComponentId, recGC => (recGC.Component as IComponentModel, recGC.Count));
|
||||
}
|
||||
return _goodComponents;
|
||||
}
|
||||
}
|
||||
|
||||
public static Good Create(HardwareShopDatabase context, GoodBindingModel model)
|
||||
{
|
||||
return new Good()
|
||||
{
|
||||
Id = model.Id,
|
||||
GoodName = model.GoodName,
|
||||
Price = model.Price,
|
||||
UserId = model.UserId,
|
||||
Components = model.GoodComponents.Select(x => new GoodComponent
|
||||
{
|
||||
Component = context.Components.First(y => y.Id == x.Key),
|
||||
Count = x.Value.Item2
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
|
||||
public void Update(GoodBindingModel model)
|
||||
{
|
||||
GoodName = model.GoodName;
|
||||
Price = model.Price;
|
||||
}
|
||||
|
||||
public GoodViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
GoodName = GoodName,
|
||||
Price = Price,
|
||||
UserId = UserId,
|
||||
UserLogin = User.Login,
|
||||
GoodComponents = GoodComponents
|
||||
};
|
||||
|
||||
public void UpdateComponents(HardwareShopDatabase context, GoodBindingModel model)
|
||||
{
|
||||
var goodComponents = context.GoodComponents
|
||||
.Where(rec => rec.GoodId == model.Id).ToList();
|
||||
if (goodComponents != null && goodComponents.Count > 0)
|
||||
{ // удалили те, которых нет в модели
|
||||
context.GoodComponents
|
||||
.RemoveRange(goodComponents
|
||||
.Where(rec => !model.GoodComponents.ContainsKey(rec.ComponentId)));
|
||||
context.SaveChanges();
|
||||
// обновили количество у существующих записей
|
||||
foreach (var updateComponent in goodComponents)
|
||||
{
|
||||
updateComponent.Count = model.GoodComponents[updateComponent.ComponentId].Item2;
|
||||
model.GoodComponents.Remove(updateComponent.ComponentId);
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
var good = context.Goods.First(x => x.Id == Id);
|
||||
foreach (var gc in model.GoodComponents)
|
||||
{
|
||||
context.GoodComponents.Add(new GoodComponent
|
||||
{
|
||||
Good = good,
|
||||
Component = context.Components.First(x => x.Id == gc.Key),
|
||||
Count = gc.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_goodComponents = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,80 @@
|
||||
using HardwareShopDataModels.Enums;
|
||||
using HardwareShopContracts.BindingModels;
|
||||
using HardwareShopContracts.ViewModels;
|
||||
using HardwareShopDataModels.Enums;
|
||||
using HardwareShopDataModels.Models;
|
||||
using System.ComponentModel;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace HardwareShopDatabaseImplement.Models.Storekeeper
|
||||
{
|
||||
public class Order : IOrderModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public virtual User User { get; set; }
|
||||
[Required]
|
||||
public int GoodId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int UserId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
|
||||
[Required]
|
||||
public double Sum { get; set; }
|
||||
|
||||
[Required]
|
||||
public OrderStatus Status { get; set; }
|
||||
|
||||
[Required]
|
||||
public DateTime DateCreate { get; set; }
|
||||
|
||||
public DateTime? DateImplement { get; set; }
|
||||
|
||||
public virtual User User { get; set; } = null!;
|
||||
|
||||
public virtual Good Good { get; set; } = null!;
|
||||
|
||||
public static Order? Create(OrderBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Order()
|
||||
{
|
||||
Id = model.Id,
|
||||
GoodId = model.GoodId,
|
||||
UserId = model.UserId,
|
||||
Count = model.Count,
|
||||
Sum = model.Sum,
|
||||
Status = model.Status,
|
||||
DateCreate = model.DateCreate,
|
||||
DateImplement = model.DateImplement,
|
||||
};
|
||||
}
|
||||
|
||||
public void Update(OrderBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Status = model.Status;
|
||||
DateImplement = model.DateImplement;
|
||||
}
|
||||
|
||||
public OrderViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
GoodId = GoodId,
|
||||
UserId = UserId,
|
||||
Count = Count,
|
||||
Sum = Sum,
|
||||
Status = Status,
|
||||
DateCreate = DateCreate,
|
||||
DateImplement = DateImplement,
|
||||
GoodName = Good.GoodName,
|
||||
UserLogin = User.Login
|
||||
};
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user