Модели базы данных

This commit is contained in:
dasha 2023-04-01 17:42:33 +04:00
parent 12cc4e83b0
commit b385ed5e89
6 changed files with 260 additions and 23 deletions

View File

@ -12,7 +12,7 @@ namespace HardwareShopDatabaseImplement
{ {
if (optionsBuilder.IsConfigured == false) if (optionsBuilder.IsConfigured == false)
{ {
optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-SINQU55\SQLEXPRESS;Initial Catalog=HardwareShopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-D8KMQQU\SQLEXPRESS;Initial Catalog=HardwareShopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
} }
base.OnConfiguring(optionsBuilder); base.OnConfiguring(optionsBuilder);
} }
@ -34,6 +34,8 @@ namespace HardwareShopDatabaseImplement
public virtual DbSet<Good> Goods { set; get; } public virtual DbSet<Good> Goods { set; get; }
public virtual DbSet<GoodComponent> GoodComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; } public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Purchase> Purchases { set; get; } public virtual DbSet<Purchase> Purchases { set; get; }

View File

@ -6,6 +6,12 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Compile Remove="Implements\**" />
<EmbeddedResource Remove="Implements\**" />
<None Remove="Implements\**" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.4" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.4" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.4" />
@ -20,8 +26,4 @@
<ProjectReference Include="..\HardwareShopDataModels\HardwareShopDataModels.csproj" /> <ProjectReference Include="..\HardwareShopDataModels\HardwareShopDataModels.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Implements\" />
</ItemGroup>
</Project> </Project>

View File

@ -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();
}
}

View File

@ -1,9 +1,9 @@
using HardwareShopDataModels.Models; using HardwareShopContracts.BindingModels;
using System; using HardwareShopContracts.ViewModels;
using System.Collections.Generic; using HardwareShopDatabaseImplement.Models.ManyToMany;
using System.Linq; using HardwareShopDataModels.Models;
using System.Text; using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks; using System.ComponentModel.DataAnnotations.Schema;
namespace HardwareShopDatabaseImplement.Models.Storekeeper namespace HardwareShopDatabaseImplement.Models.Storekeeper
{ {
@ -11,6 +11,52 @@ namespace HardwareShopDatabaseImplement.Models.Storekeeper
{ {
public int Id { get; set; } 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
};
} }
} }

View File

@ -1,10 +1,9 @@
using HardwareShopDataModels.Enums; using HardwareShopContracts.BindingModels;
using HardwareShopContracts.ViewModels;
using HardwareShopDatabaseImplement.Models.ManyToMany;
using HardwareShopDataModels.Models; using HardwareShopDataModels.Models;
using System; using System.ComponentModel.DataAnnotations;
using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HardwareShopDatabaseImplement.Models.Storekeeper namespace HardwareShopDatabaseImplement.Models.Storekeeper
{ {
@ -12,6 +11,101 @@ namespace HardwareShopDatabaseImplement.Models.Storekeeper
{ {
public int Id { get; set; } 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;
}
} }
} }

View File

@ -1,13 +1,80 @@
using HardwareShopDataModels.Enums; using HardwareShopContracts.BindingModels;
using HardwareShopContracts.ViewModels;
using HardwareShopDataModels.Enums;
using HardwareShopDataModels.Models; using HardwareShopDataModels.Models;
using HardwareShopDataModels.Models; using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
namespace HardwareShopDatabaseImplement.Models.Storekeeper namespace HardwareShopDatabaseImplement.Models.Storekeeper
{ {
public class Order : IOrderModel public class Order : IOrderModel
{ {
public int Id { get; set; } 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
};
} }
} }