This commit is contained in:
bekodeg 2024-04-29 20:59:09 +04:00
parent 4b1d26dfb5
commit ce72b6d6b0
7 changed files with 285 additions and 0 deletions

View File

@ -6,6 +6,12 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Compile Remove="BusinessLogic\**" />
<EmbeddedResource Remove="BusinessLogic\**" />
<None Remove="BusinessLogic\**" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" /> <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
</ItemGroup> </ItemGroup>

View File

@ -0,0 +1,35 @@
using ComputerHardwareStoreDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace ComputerHardwareStoreDatabaseImplement
{
public class ComputerHardwareStoreDBContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
//optionsBuilder.UseNpgsql(@"Host=localhost;Database=ProductBar_db;Username=postgres;Password=postgres"); // не надо >:
/*
* в program добавить:
* // получаем строку подключения из файла конфигурации
* string connection = builder.Configuration.GetConnectionString("DefaultConnection");
*
* // добавляем контекст ApplicationContext в качестве сервиса в приложение
* builder.Services.AddDbContext<ApplicationContext>(options => options.UseSqlServer(connection));
*
* в appsettings:
* "ConnectionStrings": {
* "DefaultConnection": "Host=localhost;Database=ProductBar_db;Username=compstore;Password=compstore"
* },
*/
}
base.OnConfiguring(optionsBuilder);
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);
}
public virtual DbSet<Component> Components { set; get; }
public virtual DbSet<Product> Products { set; get; }
public virtual DbSet<ProductComponent> ProductComponents { set; get; }
}
}

View File

@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.4" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ComputerHardwareStoreContracts\ComputerHardwareStoreContracts.csproj" />
<ProjectReference Include="..\ComputerHardwareStoreDataModels\ComputerHardwareStoreDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,50 @@
using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ComputerHardwareStoreDatabaseImplement.Models
{
public class Component : IComponentModel
{
public int Id { get; private set; }
[Required]
public string Name { get; private set; } = string.Empty;
[Required]
public double Cost { get; set; }
[ForeignKey("ComponentId")]
public virtual List<ProductComponent> ProductComponents { get; set; } = new();
public static Component? Create(ComponentBindingModel model)
{
if (model == null)
{
return null;
}
return new Component()
{
Id = model.Id,
Name = model.Name,
Cost = model.Cost
};
}
public void Update (ComponentBindingModel model)
{
if (model == null)
{
return;
}
Name = string.IsNullOrEmpty(model.Name) ? Name : model.Name;
Cost = model.Cost;
}
public ComponentViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Cost = Cost
};
public int StoreKeeperId => throw new NotImplementedException();
}
}

View File

@ -0,0 +1,62 @@
using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDataModels.Enums;
using ComputerHardwareStoreDataModels.Models;
using System.ComponentModel.DataAnnotations;
namespace ComputerHardwareStoreDatabaseImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
[Required]
public int Count { get; set; }
[Required]
public double Sum { get; set; }
[Required]
public OrderStatus Status { get; set; } = OrderStatus.Неизвестен;
[Required]
public DateTime DateCreate { get; set; } = DateTime.Now;
public DateTime? DateImplement { get; set; }
[Required]
public int ProductId { get; private set; }
public virtual Product? Product { get; set; }
public static Order? Create(ComputerHardwareStoreDBContext context, OrderBindingModel model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
ProductId = model.CannedId,
Product = context.Products.First(x => x.Id == model.ProductId)
};
}
public void Update(OrderBindingModel model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
ProductId = ProductId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
ProductName = Product.ProductName
};
}

View File

@ -0,0 +1,96 @@
using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDataModels.Models;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ComputerHardwareStoreDatabaseImplement.Models
{
public class Product : IProductModel
{
public int Id { get; set; }
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
private Dictionary<int, (IComponentModel, int)>? _productComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> ProductComponents
{
get
{
if (_productComponents == null)
{
_productComponents = Components
.ToDictionary(c => c.ComponentId, c =>
(c.Component as IComponentModel, c.Count));
}
return _productComponents;
}
}
[ForeignKey("ProductId")]
public virtual List<ProductComponent> Components { get; set; } = new();
[ForeignKey("ProductId")]
public virtual List<Order> Orders { get; set; } = new();
public static Product Create(ComputerHardwareStoreDBContext context, ProductBindingModel model)
{
return new Product()
{
Id = model.Id,
Name = model.Name,
Price = model.Price,
Components = model.ProductComponents.Select(x =>
new ProductComponent
{
Component = context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(ProductBindingModel model)
{
Name = string.IsNullOrEmpty(model.Name) ? Name : model.Name;
Price = model.Price;
}
public ProductViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Price = Price,
ProductComponents = ProductComponents
};
public static void UpdateComponents(ComputerHardwareStoreDBContext context, ProductBindingModel model)
{
var productComponents = context.ProductComponents
.Where(pc => pc.ProductId == model.Id)
.ToList();
if (productComponents.Count != 0 && productComponents.Count > 0)
{
// удалили те, которых нет в модели
context.ProductComponents
.Where(pc => !model.ProductComponents.ContainsKey(pc.ComponentId))
.ExecuteDelete();
// обновили количество у существующих записей
productComponents
.ForEach(updateComponent =>
{
updateComponent.Count = model.ProductComponents[updateComponent.ComponentId].Item2;
model.ProductComponents.Remove(updateComponent.ComponentId);
});
}
// добавили новые
context.ProductComponents
.AddRange(model.ProductComponents.Values
.Select(val => new ProductComponent()
{
ProductId = model.Id,
ComponentId = val.Item1.Id,
Count = val.Item2
}));
context.SaveChanges();
}
}
}

View File

@ -0,0 +1,17 @@
using System.ComponentModel.DataAnnotations;
namespace ComputerHardwareStoreDatabaseImplement.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 Component Component { get; set; } = new();
public virtual Product Product { get; set; } = new();
}
}