Infrastructure

This commit is contained in:
2026-04-22 20:51:02 +04:30
parent c64c3dd9be
commit 64ee78e320
199 changed files with 7428 additions and 1027 deletions
@@ -1,19 +1,18 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Vizhvizh.Shop.Domain.Products.Entities;
using Vizhvizh.Shop.Domain.Products.TypeIds;
using Vizhvizh.Shop.Domain.Products.ValuesObjects;
namespace Vizhvizh.Shop.Infrastructure.EF.Configurations;
internal class CategoryConfiguration : IEntityTypeConfiguration<Category>
internal class CategoryConfiguration : BaseEntityConfiguration<Category,CategoryId ,long>
{
public void Configure(EntityTypeBuilder<Category> builder)
public override void Configure(EntityTypeBuilder<Category> builder)
{
base.Configure(builder);
builder.ToTable("Categories", schema: "Shop");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id)
.HasConversion(x => x.Value, x => new Domain.Products.TypeIds.CategoryId(x))
.ValueGeneratedOnAdd();
builder.Property(x => x.Title)
.HasField("_title")
@@ -39,6 +38,24 @@ internal class CategoryConfiguration : IEntityTypeConfiguration<Category>
.HasConversion(x => x.Value, x => Domain.Products.ValuesObjects.CategoryPath.CreateNew(x))
.HasColumnType("HIERARCHYID");
// ایندکس برای Path با نوع HIERARCHYID
builder.HasIndex(x => x.Path)
.HasDatabaseName("IX_Categories_Path")
.IsClustered(false); // معمولاً برای HierarchyId بهتر است non-clustered باشد
// رابطه خودارجاع برای ParentId
builder.HasOne<Category>()
.WithMany()
.HasForeignKey(x => x.ParentId)
.HasConstraintName("FK_Categories_ParentId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired(false);
// ایندکس برای ParentId جهت بهبود performance در کوئری‌های سلسله مراتبی
builder.HasIndex(x => x.ParentId)
.HasDatabaseName("IX_Categories_ParentId");
builder.Metadata.FindNavigation(nameof(Category.Products))?
.SetPropertyAccessMode(PropertyAccessMode.Field);
builder.Metadata.FindNavigation(nameof(Category.CategoryVariantUnits))?
@@ -1,18 +1,17 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Vizhvizh.Shop.Domain.Products.Entities;
using Vizhvizh.Shop.Domain.Products.TypeIds;
namespace Vizhvizh.Shop.Infrastructure.EF.Configurations;
internal class CategoryVariantUnitConfiguration : IEntityTypeConfiguration<CategoryVariantUnit>
internal class CategoryVariantUnitConfiguration : BaseEntityConfiguration<CategoryVariantUnit, CategoryVariantUnitId,long>
{
public void Configure(EntityTypeBuilder<CategoryVariantUnit> builder)
public override void Configure(EntityTypeBuilder<CategoryVariantUnit> builder)
{
base.Configure(builder);
builder.ToTable("CategoryVariantUnit", schema: "Shop");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id)
.HasConversion(x => x.Value, x => new Domain.Products.TypeIds.CategoryVariantUnitId(x))
.ValueGeneratedOnAdd();
builder.Property(x => x.VariantUnitId)
.HasField("_variantUnitId")
@@ -1,17 +1,17 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Vizhvizh.Shop.Domain.Shops.Entities;
using Vizhvizh.Shop.Domain.Shops.TypeIds;
namespace Vizhvizh.Shop.Infrastructure.EF.Configurations;
internal class ManagementConfiguration : IEntityTypeConfiguration<Management>
internal class ManagementConfiguration : BaseEntityConfiguration<Management, ShopId, long>
{
public void Configure(EntityTypeBuilder<Management> builder)
public override void Configure(EntityTypeBuilder<Management> builder)
{
base.Configure(builder);
builder.ToTable("Managements", schema: "Shop");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id)
.HasConversion(x => x.Value, x => new Domain.Shops.TypeIds.ShopId(x));
builder.Metadata.FindNavigation(nameof(Management.Shop))?
.SetPropertyAccessMode(PropertyAccessMode.Field);
@@ -1,18 +1,18 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Vizhvizh.Shop.Domain.Members.Entities;
using Vizhvizh.Shop.Domain.Members.TypeIds;
using Vizhvizh.Shop.Domain.Members.ValuesObjects;
namespace Vizhvizh.Shop.Infrastructure.EF.Configurations;
internal class MemberConfiguration : IEntityTypeConfiguration<Member>
internal class MemberConfiguration : BaseEntityConfiguration<Member,MemberId , long>
{
public void Configure(EntityTypeBuilder<Member> builder)
public override void Configure(EntityTypeBuilder<Member> builder)
{
base.Configure(builder);
builder.ToTable("Members", schema: "Shop");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id)
.ValueGeneratedOnAdd();
builder.Property(x => x.PhoneNumber)
.HasField("_phoneNumber");
@@ -2,19 +2,19 @@
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System.Drawing;
using Vizhvizh.Shop.Domain.Products.Entities;
using Vizhvizh.Shop.Domain.Products.TypeIds;
using Vizhvizh.Shop.Domain.Products.ValuesObjects;
namespace Vizhvizh.Shop.Infrastructure.EF.Configurations;
internal class ProductConfiguration : IEntityTypeConfiguration<Product>
internal class ProductConfiguration : BaseEntityConfiguration<Product,ProductId,long>
{
public void Configure(EntityTypeBuilder<Product> builder)
public override void Configure(EntityTypeBuilder<Product> builder)
{
base.Configure(builder);
builder.ToTable("Products", schema: "Shop");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id)
.HasConversion(x => x.Value, x => new Domain.Products.TypeIds.ProductId(x))
.ValueGeneratedOnAdd();
builder.OwnsOne(x => x.ProductCondition, sa =>
{
@@ -1,19 +1,19 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Vizhvizh.Shop.Domain.Products.Entities;
using Vizhvizh.Shop.Domain.Products.TypeIds;
using Vizhvizh.Shop.Domain.Products.ValuesObjects;
namespace Vizhvizh.Shop.Infrastructure.EF.Configurations;
internal class ProductVariantConfiguration : IEntityTypeConfiguration<ProductVariant>
internal class ProductVariantConfiguration : BaseEntityConfiguration<ProductVariant,ProductVariantId,long>
{
public void Configure(EntityTypeBuilder<ProductVariant> builder)
public override void Configure(EntityTypeBuilder<ProductVariant> builder)
{
base.Configure(builder);
builder.ToTable("ProductVariants", schema: "Shop");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id)
.HasConversion(x => x.Value, x => new Domain.Products.TypeIds.ProductVariantId(x))
.ValueGeneratedOnAdd();
builder.OwnsOne(x => x.Score, sa =>
{
@@ -2,19 +2,18 @@
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Vizhvizh.Shared.Domain.ValueObjects;
using Vizhvizh.Shop.Domain.Users.Entities;
using Vizhvizh.Shop.Domain.Users.TypeIds;
using Vizhvizh.Shop.Domain.Users.ValueObjects;
namespace Vizhvizh.Shop.Infrastructure.EF.Configurations;
public class ResourceConfiguration : IEntityTypeConfiguration<Resource>
public class ResourceConfiguration : BaseEntityConfiguration<Resource, ResourceId, long>
{
public void Configure(EntityTypeBuilder<Resource> builder)
public override void Configure(EntityTypeBuilder<Resource> builder)
{
base.Configure(builder);
builder.ToTable("Resources", schema: "Shop");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id)
.HasConversion(x => x.Value, x => new Domain.Users.TypeIds.ResourceId(x))
.ValueGeneratedOnAdd();
builder.Property(x => x.Title)
.HasField("_title");
@@ -23,10 +22,10 @@ public class ResourceConfiguration : IEntityTypeConfiguration<Resource>
builder.Property(x => x.ModuleName)
.HasField("_moduleName")
.HasColumnType("int")
.HasConversion(x=> (int)x.Value, x=> ModuleNameVO.CreateNew((ModuleNamesEnum)x));
.HasConversion(x => (int)x.Value, x => ModuleNameVO.CreateNew((ModuleNamesEnum)x));
builder.Property(x => x.ResourceKey)
.HasConversion(x=>x.Value, x=> ResourceKey.CreateNew(x))
.HasConversion(x => x.Value, x => ResourceKey.CreateNew(x))
.HasField("_resourceKey");
@@ -1,18 +1,18 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Vizhvizh.Shop.Domain.Users.Entities;
using Vizhvizh.Shop.Domain.Users.TypeIds;
namespace Vizhvizh.Shop.Infrastructure.EF.Configurations;
public class RoleConfiguration : IEntityTypeConfiguration<Role>
public class RoleConfiguration : BaseEntityConfiguration<Role,RoleId,int>
{
public void Configure(EntityTypeBuilder<Role> builder)
public override void Configure(EntityTypeBuilder<Role> builder)
{
base.Configure(builder);
builder.ToTable("Roles", schema: "Shop");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id)
.HasConversion(x => x.Value, x => new Domain.Users.TypeIds.RoleId(x))
.ValueGeneratedOnAdd();
builder.Property(x => x.Title)
.HasField("_title");
@@ -1,19 +1,18 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Vizhvizh.Shop.Domain.Users.Entities;
using Vizhvizh.Shop.Domain.Users.TypeIds;
using Vizhvizh.Shop.Domain.Users.ValueObjects;
namespace Vizhvizh.Shop.Infrastructure.EF.Configurations;
public class RoleResourceConfiguration : IEntityTypeConfiguration<RoleResource>
public class RoleResourceConfiguration : BaseEntityConfiguration<RoleResource,RoleResourceId,long>
{
public void Configure(EntityTypeBuilder<RoleResource> builder)
public override void Configure(EntityTypeBuilder<RoleResource> builder)
{
base.Configure(builder);
builder.ToTable("RoleResources", schema: "Shop");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id)
.HasConversion(x => x.Value, x => new Domain.Users.TypeIds.RoleResourceId(x))
.ValueGeneratedOnAdd();
builder.Property(x=>x.AccessLevel)
.HasConversion(x=>x.Value, x=> AccessLevel.CreateNew((AccessLevelEnum)x));
@@ -1,19 +1,18 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Vizhvizh.Shop.Domain.Shops.Entities;
using Vizhvizh.Shop.Domain.Shops.TypeIds;
using Vizhvizh.Shop.Domain.Shops.ValuesObjects;
namespace Vizhvizh.Shop.Infrastructure.EF.Configurations;
internal class ShippingMethodConfiguration : IEntityTypeConfiguration<ShippingMethod>
internal class ShippingMethodConfiguration : BaseEntityConfiguration<ShippingMethod,ShippingMethodId ,byte>
{
public void Configure(EntityTypeBuilder<ShippingMethod> builder)
public override void Configure(EntityTypeBuilder<ShippingMethod> builder)
{
base.Configure(builder);
builder.ToTable("ShippingMethods", schema: "Shop");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id)
.HasConversion(x => x.Value, x => new Domain.Shops.TypeIds.ShippingMethodId(x))
.ValueGeneratedOnAdd();
builder.Property(x => x.Title)
.HasField("_title");
@@ -1,19 +1,18 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Vizhvizh.Shop.Domain.Shops.Entities;
using Vizhvizh.Shop.Domain.Shops.TypeIds;
using Vizhvizh.Shop.Domain.Shops.ValuesObjects;
namespace Vizhvizh.Shop.Infrastructure.EF.Configurations;
public class ShopConfiguration : IEntityTypeConfiguration<Domain.Shops.Entities.Shop>
public class ShopConfiguration : BaseEntityConfiguration<Domain.Shops.Entities.Shop, ShopId, long>
{
public void Configure(EntityTypeBuilder<Domain.Shops.Entities.Shop> builder)
public override void Configure(EntityTypeBuilder<Domain.Shops.Entities.Shop> builder)
{
base.Configure(builder);
builder.ToTable("Shops", schema: "Shop");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id)
.HasConversion(x => x.Value, x => new Domain.Shops.TypeIds.ShopId(x))
.ValueGeneratedOnAdd();
builder.Property(x => x.OwnerId)
.HasField("_ownerId");
@@ -1,19 +1,18 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Vizhvizh.Shop.Domain.Sliders.Entities;
using Vizhvizh.Shop.Domain.Sliders.TypeIds;
using Vizhvizh.Shop.Domain.Sliders.ValuesObjects;
namespace Vizhvizh.Shop.Infrastructure.EF.Configurations;
internal class SliderConfiguration : IEntityTypeConfiguration<Slider>
internal class SliderConfiguration : BaseEntityConfiguration<Slider,SliderId,long>
{
public void Configure(EntityTypeBuilder<Slider> builder)
public override void Configure(EntityTypeBuilder<Slider> builder)
{
base.Configure(builder);
builder.ToTable("Sliders", schema: "Shop");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id)
.HasConversion(x => x.Value, x => new Domain.Sliders.TypeIds.SliderId(x))
.ValueGeneratedOnAdd();
builder.Property(x => x.Title)
.HasField("_title");
@@ -2,18 +2,17 @@
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Newtonsoft.Json;
using Vizhvizh.Shop.Domain.Products.Entities;
using Vizhvizh.Shop.Domain.Products.TypeIds;
namespace Vizhvizh.Shop.Infrastructure.EF.Configurations;
internal class SpecificationsConfiguration : IEntityTypeConfiguration<Specifications>
internal class SpecificationsConfiguration : BaseEntityConfiguration<Specifications,SpecificationsId, long>
{
public void Configure(EntityTypeBuilder<Specifications> builder)
public override void Configure(EntityTypeBuilder<Specifications> builder)
{
base.Configure(builder);
builder.ToTable("ProductSpecifications", schema: "Shop");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id)
.HasConversion(x=>x.Value, x=> new Domain.Products.TypeIds.SpecificationsId(x))
.ValueGeneratedOnAdd();
builder.Property(x => x.Title)
.HasField("_title");
@@ -1,17 +1,18 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Vizhvizh.Shop.Domain.Products.Entities;
using Vizhvizh.Shop.Domain.Products.TypeIds;
namespace Vizhvizh.Shop.Infrastructure.EF.Configurations;
internal class UnitConfiguration : IEntityTypeConfiguration<Unit>
internal class UnitConfiguration : BaseEntityConfiguration<Unit, UnitId, long>
{
public void Configure(EntityTypeBuilder<Unit> builder)
public override void Configure(EntityTypeBuilder<Unit> builder)
{
base.Configure(builder);
builder.ToTable("Units", schema: "Shop");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id)
.HasConversion(x => x.Value, x => new Domain.Products.TypeIds.UnitId(x));
builder.Property(x => x.Title)
.HasField("_title");
@@ -1,18 +1,18 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Vizhvizh.Shop.Domain.Users.Entities;
using Vizhvizh.Shop.Domain.Users.TypeIds;
namespace Vizhvizh.Shop.Infrastructure.EF.Configurations;
public class UserConfiguration : IEntityTypeConfiguration<User>
public class UserConfiguration : BaseEntityConfiguration<User,UserId, long>
{
public void Configure(EntityTypeBuilder<User> builder)
public override void Configure(EntityTypeBuilder<User> builder)
{
base.Configure(builder);
builder.ToTable("Users", schema: "Shop");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id)
.HasConversion(x => x.Value, x => new Domain.Users.TypeIds.UserId(x))
.ValueGeneratedOnAdd();
//builder.OwnsOne(x => x.NationalCode, sa =>
//{
@@ -1,17 +1,18 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Vizhvizh.Shop.Domain.Products.Entities;
using Vizhvizh.Shop.Domain.Products.TypeIds;
namespace Vizhvizh.Shop.Infrastructure.EF.Configurations;
internal class VariantConfiguration : IEntityTypeConfiguration<Variant>
internal class VariantConfiguration : BaseEntityConfiguration<Variant,VariantId, long>
{
public void Configure(EntityTypeBuilder<Variant> builder)
public override void Configure(EntityTypeBuilder<Variant> builder)
{
base.Configure(builder);
builder.ToTable("Variants", schema: "Shop");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id)
.HasConversion(x => x.Value, x => new Domain.Products.TypeIds.VariantId(x));
builder.Property(x => x.Title)
.HasField("_title");
@@ -1,17 +1,17 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Vizhvizh.Shop.Domain.Products.Entities;
using Vizhvizh.Shop.Domain.Products.TypeIds;
namespace Vizhvizh.Shop.Infrastructure.EF.Configurations;
internal class VariantUnitConfiguration : IEntityTypeConfiguration<VariantUnit>
internal class VariantUnitConfiguration : BaseEntityConfiguration<VariantUnit,VariantUnitId,long>
{
public void Configure(EntityTypeBuilder<VariantUnit> builder)
public override void Configure(EntityTypeBuilder<VariantUnit> builder)
{
base.Configure(builder);
builder.ToTable("VariantUnits", schema: "Shop");
builder.HasKey(x=>x.Id);
builder.Property(x => x.Id)
.HasConversion(x => x.Value, x => new Domain.Products.TypeIds.VariantUnitId(x));
builder.Property(x => x.VariantUnitType)
.HasColumnType("INT")
@@ -1,5 +1,6 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Abstractions;
using Vizhvizh.BuildingBlocks.Domain;
using Vizhvizh.Shared.Domain.ValueObjects;
using Vizhvizh.Shared.Infrastructure.EF.Configurations.Converters;
using Vizhvizh.Shop.Domain.Members.TypeIds;
@@ -36,15 +37,7 @@ internal class ShopDbContext : DbContext
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(Assemblies.Infrastructure, FindConfigurations);
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
var createOnProperty = entityType.FindProperty("CreateOn");
if (createOnProperty != null && createOnProperty.ClrType == typeof(DateTime))
{
createOnProperty.SetDefaultValueSql("GETDATE()");
}
}
modelBuilder.AddHiLoSequences();
}
public bool FindConfigurations(Type type)
=> type?.FullName?.Contains("Configurations") ?? false;
@@ -0,0 +1,10 @@
namespace Vizhvizh.Shop.Infrastructure.Exceptions;
public class EntityNotFoundException : Exception
{
public EntityNotFoundException()
: base("entity not found")
{
}
}
File diff suppressed because it is too large Load Diff
@@ -1,565 +0,0 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Vizhvizh.Shop.Infrastructure.Migrations
{
/// <inheritdoc />
public partial class initial : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.EnsureSchema(
name: "Shop");
migrationBuilder.CreateTable(
name: "Categories",
schema: "Shop",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Title = table.Column<string>(type: "nvarchar(60)", maxLength: 60, nullable: false),
Description = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: true),
Image = table.Column<string>(type: "nvarchar(150)", maxLength: 150, nullable: false),
ParentId = table.Column<long>(type: "bigint", nullable: true),
Path = table.Column<string>(type: "HIERARCHYID", nullable: false),
CreateOn = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "GETDATE()")
},
constraints: table =>
{
table.PrimaryKey("PK_Categories", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Resources",
schema: "Shop",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Title = table.Column<string>(type: "nvarchar(60)", maxLength: 60, nullable: false),
Description = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: false),
ModuleName = table.Column<int>(type: "int", nullable: false),
ResourceKey = table.Column<string>(type: "nvarchar(max)", nullable: false),
CreateOn = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "GETDATE()")
},
constraints: table =>
{
table.PrimaryKey("PK_Resources", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Shops",
schema: "Shop",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
OwnerId = table.Column<long>(type: "bigint", nullable: false),
Title = table.Column<string>(type: "nvarchar(60)", maxLength: 60, nullable: false),
Description = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: true),
Email = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: true),
Telephone = table.Column<string>(type: "nvarchar(11)", maxLength: 11, nullable: false),
Logo = table.Column<string>(type: "nvarchar(150)", maxLength: 150, nullable: true),
CreateOn = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "GETDATE()"),
Location = table.Column<string>(type: "nvarchar(max)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Shops", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Sliders",
schema: "Shop",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Title = table.Column<string>(type: "nvarchar(60)", maxLength: 60, nullable: false),
Image = table.Column<string>(type: "nvarchar(150)", maxLength: 150, nullable: false),
Url = table.Column<string>(type: "nvarchar(150)", maxLength: 150, nullable: false),
Type = table.Column<int>(type: "INT", nullable: false),
Key = table.Column<string>(type: "nvarchar(150)", maxLength: 150, nullable: false),
CreateOn = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "GETDATE()")
},
constraints: table =>
{
table.PrimaryKey("PK_Sliders", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Units",
schema: "Shop",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false),
Title = table.Column<string>(type: "nvarchar(60)", maxLength: 60, nullable: false),
CreateOn = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "GETDATE()")
},
constraints: table =>
{
table.PrimaryKey("PK_Units", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Users",
schema: "Shop",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
MemberId = table.Column<long>(type: "bigint", nullable: false),
CreateOn = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "GETDATE()")
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Variants",
schema: "Shop",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false),
Title = table.Column<string>(type: "nvarchar(60)", maxLength: 60, nullable: false),
CreateOn = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "GETDATE()")
},
constraints: table =>
{
table.PrimaryKey("PK_Variants", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Managements",
schema: "Shop",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false),
CreateOn = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "GETDATE()")
},
constraints: table =>
{
table.PrimaryKey("PK_Managements", x => x.Id);
table.ForeignKey(
name: "FK_Managements_Shops_Id",
column: x => x.Id,
principalSchema: "Shop",
principalTable: "Shops",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "Members",
schema: "Shop",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
FirstName = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true),
LastName = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true),
PhoneNumber = table.Column<string>(type: "nvarchar(11)", maxLength: 11, nullable: false),
Avatar = table.Column<string>(type: "nvarchar(150)", maxLength: 150, nullable: true),
ShopId = table.Column<long>(type: "bigint", nullable: true),
CreateOn = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "GETDATE()")
},
constraints: table =>
{
table.PrimaryKey("PK_Members", x => x.Id);
table.ForeignKey(
name: "FK_Members_Shops_ShopId",
column: x => x.ShopId,
principalSchema: "Shop",
principalTable: "Shops",
principalColumn: "Id");
});
migrationBuilder.CreateTable(
name: "Products",
schema: "Shop",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ProductCondition = table.Column<int>(type: "int", nullable: false),
Title = table.Column<string>(type: "nvarchar(60)", maxLength: 60, nullable: false),
Description = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: false),
Score_Score = table.Column<byte>(type: "TINYINT", nullable: false, defaultValue: (byte)0),
TotalVoters = table.Column<int>(type: "INT", nullable: false, defaultValue: 0),
Image = table.Column<string>(type: "nvarchar(150)", maxLength: 150, nullable: false),
CategoryId = table.Column<long>(type: "bigint", nullable: true),
ShopId = table.Column<long>(type: "bigint", nullable: true),
CreateOn = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "GETDATE()")
},
constraints: table =>
{
table.PrimaryKey("PK_Products", x => x.Id);
table.ForeignKey(
name: "FK_Products_Categories_CategoryId",
column: x => x.CategoryId,
principalSchema: "Shop",
principalTable: "Categories",
principalColumn: "Id");
table.ForeignKey(
name: "FK_Products_Shops_ShopId",
column: x => x.ShopId,
principalSchema: "Shop",
principalTable: "Shops",
principalColumn: "Id");
});
migrationBuilder.CreateTable(
name: "ShippingMethods",
schema: "Shop",
columns: table => new
{
Id = table.Column<byte>(type: "tinyint", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Title = table.Column<string>(type: "nvarchar(60)", maxLength: 60, nullable: false),
Description = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: false),
Image = table.Column<string>(type: "nvarchar(150)", maxLength: 150, nullable: false),
ShopId = table.Column<long>(type: "bigint", nullable: true),
CreateOn = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "GETDATE()")
},
constraints: table =>
{
table.PrimaryKey("PK_ShippingMethods", x => x.Id);
table.ForeignKey(
name: "FK_ShippingMethods_Shops_ShopId",
column: x => x.ShopId,
principalSchema: "Shop",
principalTable: "Shops",
principalColumn: "Id");
});
migrationBuilder.CreateTable(
name: "Roles",
schema: "Shop",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Title = table.Column<string>(type: "nvarchar(60)", maxLength: 60, nullable: false),
ResourceId = table.Column<long>(type: "bigint", nullable: true),
UserId = table.Column<long>(type: "bigint", nullable: true),
CreateOn = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "GETDATE()")
},
constraints: table =>
{
table.PrimaryKey("PK_Roles", x => x.Id);
table.ForeignKey(
name: "FK_Roles_Resources_ResourceId",
column: x => x.ResourceId,
principalSchema: "Shop",
principalTable: "Resources",
principalColumn: "Id");
table.ForeignKey(
name: "FK_Roles_Users_UserId",
column: x => x.UserId,
principalSchema: "Shop",
principalTable: "Users",
principalColumn: "Id");
});
migrationBuilder.CreateTable(
name: "VariantUnits",
schema: "Shop",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false),
VariantUnitType = table.Column<int>(type: "INT", nullable: false),
UnitId = table.Column<long>(type: "bigint", nullable: true),
VariantId = table.Column<long>(type: "bigint", nullable: true),
CreateOn = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "GETDATE()")
},
constraints: table =>
{
table.PrimaryKey("PK_VariantUnits", x => x.Id);
table.ForeignKey(
name: "FK_VariantUnits_Units_UnitId",
column: x => x.UnitId,
principalSchema: "Shop",
principalTable: "Units",
principalColumn: "Id");
table.ForeignKey(
name: "FK_VariantUnits_Variants_VariantId",
column: x => x.VariantId,
principalSchema: "Shop",
principalTable: "Variants",
principalColumn: "Id");
});
migrationBuilder.CreateTable(
name: "ProductSpecifications",
schema: "Shop",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Title = table.Column<string>(type: "nvarchar(60)", maxLength: 60, nullable: false),
ItemValues = table.Column<string>(type: "nvarchar(max)", nullable: false),
ProductId = table.Column<long>(type: "bigint", nullable: true),
CreateOn = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "GETDATE()")
},
constraints: table =>
{
table.PrimaryKey("PK_ProductSpecifications", x => x.Id);
table.ForeignKey(
name: "FK_ProductSpecifications_Products_ProductId",
column: x => x.ProductId,
principalSchema: "Shop",
principalTable: "Products",
principalColumn: "Id");
});
migrationBuilder.CreateTable(
name: "RoleResources",
schema: "Shop",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
AccessLevel = table.Column<int>(type: "int", nullable: false),
RoleId = table.Column<int>(type: "int", nullable: true),
CreateOn = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "GETDATE()")
},
constraints: table =>
{
table.PrimaryKey("PK_RoleResources", x => x.Id);
table.ForeignKey(
name: "FK_RoleResources_Roles_RoleId",
column: x => x.RoleId,
principalSchema: "Shop",
principalTable: "Roles",
principalColumn: "Id");
});
migrationBuilder.CreateTable(
name: "CategoryVariantUnit",
schema: "Shop",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
CategoryId = table.Column<long>(type: "bigint", nullable: false),
VariantUnitId = table.Column<long>(type: "bigint", nullable: false),
CreateOn = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "GETDATE()")
},
constraints: table =>
{
table.PrimaryKey("PK_CategoryVariantUnit", x => x.Id);
table.ForeignKey(
name: "FK_CategoryVariantUnit_Categories_CategoryId",
column: x => x.CategoryId,
principalSchema: "Shop",
principalTable: "Categories",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_CategoryVariantUnit_VariantUnits_VariantUnitId",
column: x => x.VariantUnitId,
principalSchema: "Shop",
principalTable: "VariantUnits",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "ProductVariants",
schema: "Shop",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Price = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
Currency = table.Column<string>(type: "nvarchar(150)", maxLength: 150, nullable: false),
Score = table.Column<byte>(type: "TINYINT", nullable: false, defaultValue: (byte)0),
TotalVoters = table.Column<int>(type: "INT", nullable: false, defaultValue: 0),
Quantity = table.Column<double>(type: "float", nullable: false),
Value = table.Column<string>(type: "nvarchar(120)", maxLength: 120, nullable: false),
ProductId = table.Column<long>(type: "bigint", nullable: true),
VariantUnitId = table.Column<long>(type: "bigint", nullable: true),
CreateOn = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "GETDATE()"),
Gallery = table.Column<string>(type: "nvarchar(max)", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_ProductVariants", x => x.Id);
table.ForeignKey(
name: "FK_ProductVariants_Products_ProductId",
column: x => x.ProductId,
principalSchema: "Shop",
principalTable: "Products",
principalColumn: "Id");
table.ForeignKey(
name: "FK_ProductVariants_VariantUnits_VariantUnitId",
column: x => x.VariantUnitId,
principalSchema: "Shop",
principalTable: "VariantUnits",
principalColumn: "Id");
});
migrationBuilder.CreateIndex(
name: "IX_CategoryVariantUnit_CategoryId",
schema: "Shop",
table: "CategoryVariantUnit",
column: "CategoryId");
migrationBuilder.CreateIndex(
name: "IX_CategoryVariantUnit_VariantUnitId_CategoryId",
schema: "Shop",
table: "CategoryVariantUnit",
columns: new[] { "VariantUnitId", "CategoryId" },
unique: true);
migrationBuilder.CreateIndex(
name: "IX_Members_ShopId",
schema: "Shop",
table: "Members",
column: "ShopId");
migrationBuilder.CreateIndex(
name: "IX_Products_CategoryId",
schema: "Shop",
table: "Products",
column: "CategoryId");
migrationBuilder.CreateIndex(
name: "IX_Products_ShopId",
schema: "Shop",
table: "Products",
column: "ShopId");
migrationBuilder.CreateIndex(
name: "IX_ProductSpecifications_ProductId",
schema: "Shop",
table: "ProductSpecifications",
column: "ProductId");
migrationBuilder.CreateIndex(
name: "IX_ProductVariants_ProductId",
schema: "Shop",
table: "ProductVariants",
column: "ProductId");
migrationBuilder.CreateIndex(
name: "IX_ProductVariants_VariantUnitId",
schema: "Shop",
table: "ProductVariants",
column: "VariantUnitId");
migrationBuilder.CreateIndex(
name: "IX_RoleResources_RoleId",
schema: "Shop",
table: "RoleResources",
column: "RoleId");
migrationBuilder.CreateIndex(
name: "IX_Roles_ResourceId",
schema: "Shop",
table: "Roles",
column: "ResourceId");
migrationBuilder.CreateIndex(
name: "IX_Roles_UserId",
schema: "Shop",
table: "Roles",
column: "UserId");
migrationBuilder.CreateIndex(
name: "IX_ShippingMethods_ShopId",
schema: "Shop",
table: "ShippingMethods",
column: "ShopId");
migrationBuilder.CreateIndex(
name: "IX_VariantUnits_UnitId",
schema: "Shop",
table: "VariantUnits",
column: "UnitId");
migrationBuilder.CreateIndex(
name: "IX_VariantUnits_VariantId",
schema: "Shop",
table: "VariantUnits",
column: "VariantId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "CategoryVariantUnit",
schema: "Shop");
migrationBuilder.DropTable(
name: "Managements",
schema: "Shop");
migrationBuilder.DropTable(
name: "Members",
schema: "Shop");
migrationBuilder.DropTable(
name: "ProductSpecifications",
schema: "Shop");
migrationBuilder.DropTable(
name: "ProductVariants",
schema: "Shop");
migrationBuilder.DropTable(
name: "RoleResources",
schema: "Shop");
migrationBuilder.DropTable(
name: "ShippingMethods",
schema: "Shop");
migrationBuilder.DropTable(
name: "Sliders",
schema: "Shop");
migrationBuilder.DropTable(
name: "Products",
schema: "Shop");
migrationBuilder.DropTable(
name: "VariantUnits",
schema: "Shop");
migrationBuilder.DropTable(
name: "Roles",
schema: "Shop");
migrationBuilder.DropTable(
name: "Categories",
schema: "Shop");
migrationBuilder.DropTable(
name: "Shops",
schema: "Shop");
migrationBuilder.DropTable(
name: "Units",
schema: "Shop");
migrationBuilder.DropTable(
name: "Variants",
schema: "Shop");
migrationBuilder.DropTable(
name: "Resources",
schema: "Shop");
migrationBuilder.DropTable(
name: "Users",
schema: "Shop");
}
}
}
File diff suppressed because it is too large Load Diff
@@ -1,15 +1,16 @@
using Vizhvizh.Shop.Domain.Shops.Repositories;
using Vizhvizh.BuildingBlocks.Infrastructure.Lifetimes;
using Vizhvizh.Shop.Domain.Shops.Repositories;
using Vizhvizh.Shop.Domain.Shops.TypeIds;
using Vizhvizh.Shop.Infrastructure.EF;
namespace Vizhvizh.Shop.Infrastructure.Repositories
{
internal class ShopRepository(ShopDbContext context) : IShopRepository
internal class ShopRepository(ShopDbContext context) : RepositoryBase<Domain.Shops.Entities.Shop, ShopId>(context), IShopRepository, IScopeDependency
{
public async Task AddAsync(Domain.Shops.Entities.Shop shop, CancellationToken cancellationToken)
=> await context.Shops.AddAsync(shop, cancellationToken);
public async Task<Domain.Shops.Entities.Shop?> FindByIdAsync(ShopId id, CancellationToken cancellationToken)
=> await context.Shops.FindAsync(id,cancellationToken).ConfigureAwait(false);
=> await context.Shops.FindAsync(id, cancellationToken).ConfigureAwait(false);
}
}
@@ -1,18 +1,12 @@
using Vizhvizh.Shop.Domain.Sliders.Entities;
using Vizhvizh.BuildingBlocks.Infrastructure.Lifetimes;
using Vizhvizh.Shop.Domain.Sliders.Entities;
using Vizhvizh.Shop.Domain.Sliders.Repositories;
using Vizhvizh.Shop.Domain.Sliders.TypeIds;
using Vizhvizh.Shop.Infrastructure.EF;
namespace Vizhvizh.Shop.Infrastructure.Repositories;
public class SliderRepository : ISliderRepository
internal class SliderRepository(ShopDbContext context) : RepositoryBase<Slider, SliderId>(context), ISliderRepository, IScopeDependency
{
public Task AddAsync(Slider slider, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public Task GetByIdAsync(SliderId id, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
@@ -0,0 +1,52 @@
using Microsoft.EntityFrameworkCore;
using Vizhvizh.BuildingBlocks.Domain;
using Vizhvizh.Shop.Infrastructure.EF;
using Vizhvizh.Shop.Infrastructure.Exceptions;
namespace Vizhvizh.Shop.Infrastructure;
internal class RepositoryBase<T, TId> : IRepositoryBase<T, TId>
where T : Entity<TId> where TId : ITypeIdValueBase
{
private readonly ShopDbContext _shopDbContext;
public RepositoryBase(ShopDbContext shopDbContext)
{
this._shopDbContext = shopDbContext;
}
public async Task AddAsync(T entity, CancellationToken cancellationToken)
{
await _shopDbContext.AddAsync(entity, cancellationToken);
}
public async Task DeleteAsync(TId id, CancellationToken cancellationToken)
{
var entity = await FindByIdAsync(id, cancellationToken);
if (entity == null)
throw new EntityNotFoundException();
_shopDbContext.Remove(entity);
}
public Task DeleteAsync(T entity, CancellationToken cancellationToken)
{
_shopDbContext.Remove(entity);
return Task.CompletedTask;
}
public async Task<T?> FindByIdAsync(TId id, CancellationToken cancellationToken)
{
var entity = _shopDbContext.Set<T>();
return await entity.FindAsync(id, cancellationToken);
}
public IQueryable<T> GetQueryAsNoTracking()
=> _shopDbContext.Set<T>().AsNoTracking();
public Task UpdateAsync(T entity, CancellationToken cancellationToken)
{
_shopDbContext.Update(entity);
return Task.CompletedTask;
}
}
+15 -6
View File
@@ -15,6 +15,7 @@ using Vizhvizh.Shop.Infrastructure.Repositories;
using Vizhvizh.BuildingBlocks.Infrastructure.SelectBuilders;
using Vizhvizh.BuildingBlocks.Application.SelectBuilders;
using Vizhvizh.Shop.Domain.Sliders.Repositories;
using Vizhvizh.BuildingBlocks.Infrastructure.Lifetimes;
namespace Vizhvizh.Shop.Infrastructure;
@@ -36,6 +37,7 @@ public class ShopStartup
services.AddScoped<IExecutor, Executor>();
services.Scan(scan => scan.
FromAssemblies(Assemblies.Application, Assemblies.Infrastructure)
.AddClasses(classes => classes.AssignableTo(typeof(ICommandHandler<>)), publicOnly: false)
.AsImplementedInterfaces()
.WithScopedLifetime()
@@ -47,7 +49,19 @@ public class ShopStartup
.WithScopedLifetime()
.AddClasses(classes => classes.AssignableTo(typeof(ICommandHandler<>)), publicOnly: false)
.AsImplementedInterfaces()
.WithScopedLifetime());
.WithScopedLifetime()
.AddClasses(classes => classes.AssignableTo(typeof(ISingletonDependency)), publicOnly: false)
.AsImplementedInterfaces()
.WithSingletonLifetime()
.AddClasses(classes => classes.AssignableTo(typeof(IScopeDependency)), publicOnly: false)
.AsImplementedInterfaces()
.WithScopedLifetime()
.AddClasses(classes => classes.AssignableTo(typeof(ITransientDependency)), publicOnly: false)
.AsImplementedInterfaces()
.WithTransientLifetime()
);
services.Decorate(typeof(ICommandHandler<>), typeof(UnitOfWorkCommandHandlerDecorator<>));
services.Decorate(typeof(ICommandHandler<,>), typeof(UnitOfWorkCommandHandlerDecorator<,>));
services.AddScoped(sa =>
@@ -71,10 +85,5 @@ public class ShopStartup
services.AddScoped<ISqlDialect, SqlServerDialect>();
services.AddScoped<ISqlSelectBuilder, SqlSelectBuilder>();
#region Repositories
services.AddScoped<IShopRepository, ShopRepository>();
services.AddScoped<ISliderRepository, SliderRepository>();
#endregion
}
}
@@ -19,7 +19,6 @@
<ItemGroup>
<ProjectReference Include="..\Vichvich.Shared.Infrastructure\Vizhvizh.Shared.Infrastructure.csproj" />
<ProjectReference Include="..\Vichvich.Shop.Application\Vizhvizh.Shop.Application.csproj" />
<ProjectReference Include="..\Vichvich.Shop.Domain\Vizhvizh.Shop.Domain.csproj" />
</ItemGroup>
</Project>