feat: add geography entities with countries, states and cities

This commit is contained in:
masoodafar-web
2025-12-18 00:43:55 +03:30
parent 26e1243cfe
commit ead0cf4235
40 changed files with 5386 additions and 7 deletions
@@ -3,7 +3,7 @@ using CMSMicroservice.Application.Common.Interfaces;
using Microsoft.EntityFrameworkCore.Diagnostics;
using CMSMicroservice.Domain.Entities;
using CMSMicroservice.Domain.Entities.Payment;
using CMSMicroservice.Domain.Entities.Geography;
using CMSMicroservice.Domain.Entities.Order;
using CMSMicroservice.Domain.Entities.DiscountShop;
using CMSMicroservice.Infrastructure.Persistence.Interceptors;
@@ -111,4 +111,9 @@ public class ApplicationDbContext : DbContext, IApplicationDbContext
public DbSet<DiscountShoppingCart> DiscountShoppingCarts => Set<DiscountShoppingCart>();
public DbSet<DiscountOrder> DiscountOrders => Set<DiscountOrder>();
public DbSet<DiscountOrderDetail> DiscountOrderDetails => Set<DiscountOrderDetail>();
// ============= Geography DbSets =============
public DbSet<Country> Countries => Set<Country>();
public DbSet<State> States => Set<State>();
public DbSet<City> Cities => Set<City>();
}
@@ -0,0 +1,55 @@
using CMSMicroservice.Domain.Entities.Geography;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace CMSMicroservice.Infrastructure.Persistence.Configurations.Geography;
public class CityConfiguration : IEntityTypeConfiguration<City>
{
public void Configure(EntityTypeBuilder<City> builder)
{
builder.ToTable("Cities", "GMS");
builder.HasKey(c => c.Id);
builder.Property(c => c.Id)
.ValueGeneratedOnAdd();
builder.Property(c => c.ExternalId)
.IsRequired();
builder.Property(c => c.Name)
.IsRequired()
.HasMaxLength(500);
builder.Property(c => c.Latitude)
.IsRequired()
.HasMaxLength(50);
builder.Property(c => c.Longitude)
.IsRequired()
.HasMaxLength(50);
builder.Property(c => c.Native)
.IsRequired()
.HasMaxLength(500)
.HasDefaultValue("");
builder.Property(c => c.IsDeleted)
.IsRequired()
.HasDefaultValue(false);
builder.Property(c => c.StateId)
.IsRequired();
// Index
builder.HasIndex(c => c.StateId)
.HasDatabaseName("IX_Cities_StateId");
// Relationships
builder.HasOne(c => c.State)
.WithMany(s => s.Cities)
.HasForeignKey(c => c.StateId)
.OnDelete(DeleteBehavior.Cascade);
}
}
@@ -0,0 +1,99 @@
using CMSMicroservice.Domain.Entities.Geography;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace CMSMicroservice.Infrastructure.Persistence.Configurations.Geography;
public class CountryConfiguration : IEntityTypeConfiguration<Country>
{
public void Configure(EntityTypeBuilder<Country> builder)
{
builder.ToTable("Countries", "GMS");
builder.HasKey(c => c.Id);
builder.Property(c => c.Id)
.ValueGeneratedOnAdd();
builder.Property(c => c.ExternalId)
.IsRequired();
builder.Property(c => c.Name)
.IsRequired()
.HasMaxLength(500);
builder.Property(c => c.Iso3)
.IsRequired()
.HasMaxLength(3);
builder.Property(c => c.Iso2)
.IsRequired()
.HasMaxLength(2);
builder.Property(c => c.NumericCode)
.IsRequired()
.HasMaxLength(10);
builder.Property(c => c.PhoneCode)
.IsRequired()
.HasMaxLength(10);
builder.Property(c => c.Capital)
.IsRequired()
.HasMaxLength(500);
builder.Property(c => c.Currency)
.IsRequired()
.HasMaxLength(10);
builder.Property(c => c.CurrencyName)
.IsRequired()
.HasMaxLength(100);
builder.Property(c => c.CurrencySymbol)
.IsRequired()
.HasMaxLength(10);
builder.Property(c => c.Tld)
.IsRequired()
.HasMaxLength(10);
builder.Property(c => c.Native)
.IsRequired()
.HasMaxLength(500);
builder.Property(c => c.Region)
.IsRequired()
.HasMaxLength(100);
builder.Property(c => c.Subregion)
.IsRequired()
.HasMaxLength(100);
builder.Property(c => c.Latitude)
.IsRequired()
.HasMaxLength(50);
builder.Property(c => c.Longitude)
.IsRequired()
.HasMaxLength(50);
builder.Property(c => c.Emoji)
.IsRequired()
.HasMaxLength(10);
builder.Property(c => c.EmojiU)
.IsRequired()
.HasMaxLength(50);
builder.Property(c => c.IsDeleted)
.IsRequired()
.HasDefaultValue(false);
// Relationships
builder.HasMany(c => c.States)
.WithOne(s => s.Country)
.HasForeignKey(s => s.CountryId)
.OnDelete(DeleteBehavior.Cascade);
}
}
@@ -0,0 +1,68 @@
using CMSMicroservice.Domain.Entities.Geography;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace CMSMicroservice.Infrastructure.Persistence.Configurations.Geography;
public class StateConfiguration : IEntityTypeConfiguration<State>
{
public void Configure(EntityTypeBuilder<State> builder)
{
builder.ToTable("States", "GMS");
builder.HasKey(s => s.Id);
builder.Property(s => s.Id)
.ValueGeneratedOnAdd();
builder.Property(s => s.ExternalId)
.IsRequired();
builder.Property(s => s.Name)
.IsRequired()
.HasMaxLength(500);
builder.Property(s => s.StateCode)
.IsRequired()
.HasMaxLength(10);
builder.Property(s => s.Latitude)
.IsRequired()
.HasMaxLength(50);
builder.Property(s => s.Longitude)
.IsRequired()
.HasMaxLength(50);
builder.Property(s => s.Type)
.IsRequired()
.HasMaxLength(100);
builder.Property(s => s.Native)
.IsRequired()
.HasMaxLength(500)
.HasDefaultValue("");
builder.Property(s => s.IsDeleted)
.IsRequired()
.HasDefaultValue(false);
builder.Property(s => s.CountryId)
.IsRequired();
// Index
builder.HasIndex(s => s.CountryId)
.HasDatabaseName("IX_States_CountryId");
// Relationships
builder.HasOne(s => s.Country)
.WithMany(c => c.States)
.HasForeignKey(s => s.CountryId)
.OnDelete(DeleteBehavior.Cascade);
builder.HasMany(s => s.Cities)
.WithOne(c => c.State)
.HasForeignKey(c => c.StateId)
.OnDelete(DeleteBehavior.Cascade);
}
}
@@ -0,0 +1,146 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace CMSMicroservice.Infrastructure.Persistence.Migrations
{
/// <inheritdoc />
public partial class AddGeographyEntities : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.EnsureSchema(
name: "GMS");
migrationBuilder.CreateTable(
name: "Countries",
schema: "GMS",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ExternalId = table.Column<long>(type: "bigint", nullable: false),
Name = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: false),
Iso3 = table.Column<string>(type: "nvarchar(3)", maxLength: 3, nullable: false),
Iso2 = table.Column<string>(type: "nvarchar(2)", maxLength: 2, nullable: false),
NumericCode = table.Column<string>(type: "nvarchar(10)", maxLength: 10, nullable: false),
PhoneCode = table.Column<string>(type: "nvarchar(10)", maxLength: 10, nullable: false),
Capital = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: false),
Currency = table.Column<string>(type: "nvarchar(10)", maxLength: 10, nullable: false),
CurrencyName = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
CurrencySymbol = table.Column<string>(type: "nvarchar(10)", maxLength: 10, nullable: false),
Tld = table.Column<string>(type: "nvarchar(10)", maxLength: 10, nullable: false),
Native = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: false),
Region = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
Subregion = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
Latitude = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
Longitude = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
Emoji = table.Column<string>(type: "nvarchar(10)", maxLength: 10, nullable: false),
EmojiU = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
Created = table.Column<DateTime>(type: "datetime2", nullable: false),
CreatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
LastModified = table.Column<DateTime>(type: "datetime2", nullable: true),
LastModifiedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
IsDeleted = table.Column<bool>(type: "bit", nullable: false, defaultValue: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Countries", x => x.Id);
});
migrationBuilder.CreateTable(
name: "States",
schema: "GMS",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ExternalId = table.Column<long>(type: "bigint", nullable: false),
Name = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: false),
StateCode = table.Column<string>(type: "nvarchar(10)", maxLength: 10, nullable: false),
Latitude = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
Longitude = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
Type = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
Native = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: false, defaultValue: ""),
CountryId = table.Column<long>(type: "bigint", nullable: false),
Created = table.Column<DateTime>(type: "datetime2", nullable: false),
CreatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
LastModified = table.Column<DateTime>(type: "datetime2", nullable: true),
LastModifiedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
IsDeleted = table.Column<bool>(type: "bit", nullable: false, defaultValue: false)
},
constraints: table =>
{
table.PrimaryKey("PK_States", x => x.Id);
table.ForeignKey(
name: "FK_States_Countries_CountryId",
column: x => x.CountryId,
principalSchema: "GMS",
principalTable: "Countries",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "Cities",
schema: "GMS",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ExternalId = table.Column<long>(type: "bigint", nullable: false),
Name = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: false),
Latitude = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
Longitude = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
Native = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: false, defaultValue: ""),
StateId = table.Column<long>(type: "bigint", nullable: false),
Created = table.Column<DateTime>(type: "datetime2", nullable: false),
CreatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
LastModified = table.Column<DateTime>(type: "datetime2", nullable: true),
LastModifiedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
IsDeleted = table.Column<bool>(type: "bit", nullable: false, defaultValue: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Cities", x => x.Id);
table.ForeignKey(
name: "FK_Cities_States_StateId",
column: x => x.StateId,
principalSchema: "GMS",
principalTable: "States",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_Cities_StateId",
schema: "GMS",
table: "Cities",
column: "StateId");
migrationBuilder.CreateIndex(
name: "IX_States_CountryId",
schema: "GMS",
table: "States",
column: "CountryId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Cities",
schema: "GMS");
migrationBuilder.DropTable(
name: "States",
schema: "GMS");
migrationBuilder.DropTable(
name: "Countries",
schema: "GMS");
}
}
}
@@ -1014,6 +1014,256 @@ namespace CMSMicroservice.Infrastructure.Persistence.Migrations
b.ToTable("FactorDetails", "CMS");
});
modelBuilder.Entity("CMSMicroservice.Domain.Entities.Geography.City", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<long>("Id"));
b.Property<DateTime>("Created")
.HasColumnType("datetime2");
b.Property<string>("CreatedBy")
.HasColumnType("nvarchar(max)");
b.Property<long>("ExternalId")
.HasColumnType("bigint");
b.Property<bool>("IsDeleted")
.ValueGeneratedOnAdd()
.HasColumnType("bit")
.HasDefaultValue(false);
b.Property<DateTime?>("LastModified")
.HasColumnType("datetime2");
b.Property<string>("LastModifiedBy")
.HasColumnType("nvarchar(max)");
b.Property<string>("Latitude")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("nvarchar(50)");
b.Property<string>("Longitude")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("nvarchar(50)");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(500)
.HasColumnType("nvarchar(500)");
b.Property<string>("Native")
.IsRequired()
.ValueGeneratedOnAdd()
.HasMaxLength(500)
.HasColumnType("nvarchar(500)")
.HasDefaultValue("");
b.Property<long>("StateId")
.HasColumnType("bigint");
b.HasKey("Id");
b.HasIndex("StateId")
.HasDatabaseName("IX_Cities_StateId");
b.ToTable("Cities", "GMS");
});
modelBuilder.Entity("CMSMicroservice.Domain.Entities.Geography.Country", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<long>("Id"));
b.Property<string>("Capital")
.IsRequired()
.HasMaxLength(500)
.HasColumnType("nvarchar(500)");
b.Property<DateTime>("Created")
.HasColumnType("datetime2");
b.Property<string>("CreatedBy")
.HasColumnType("nvarchar(max)");
b.Property<string>("Currency")
.IsRequired()
.HasMaxLength(10)
.HasColumnType("nvarchar(10)");
b.Property<string>("CurrencyName")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("nvarchar(100)");
b.Property<string>("CurrencySymbol")
.IsRequired()
.HasMaxLength(10)
.HasColumnType("nvarchar(10)");
b.Property<string>("Emoji")
.IsRequired()
.HasMaxLength(10)
.HasColumnType("nvarchar(10)");
b.Property<string>("EmojiU")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("nvarchar(50)");
b.Property<long>("ExternalId")
.HasColumnType("bigint");
b.Property<bool>("IsDeleted")
.ValueGeneratedOnAdd()
.HasColumnType("bit")
.HasDefaultValue(false);
b.Property<string>("Iso2")
.IsRequired()
.HasMaxLength(2)
.HasColumnType("nvarchar(2)");
b.Property<string>("Iso3")
.IsRequired()
.HasMaxLength(3)
.HasColumnType("nvarchar(3)");
b.Property<DateTime?>("LastModified")
.HasColumnType("datetime2");
b.Property<string>("LastModifiedBy")
.HasColumnType("nvarchar(max)");
b.Property<string>("Latitude")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("nvarchar(50)");
b.Property<string>("Longitude")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("nvarchar(50)");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(500)
.HasColumnType("nvarchar(500)");
b.Property<string>("Native")
.IsRequired()
.HasMaxLength(500)
.HasColumnType("nvarchar(500)");
b.Property<string>("NumericCode")
.IsRequired()
.HasMaxLength(10)
.HasColumnType("nvarchar(10)");
b.Property<string>("PhoneCode")
.IsRequired()
.HasMaxLength(10)
.HasColumnType("nvarchar(10)");
b.Property<string>("Region")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("nvarchar(100)");
b.Property<string>("Subregion")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("nvarchar(100)");
b.Property<string>("Tld")
.IsRequired()
.HasMaxLength(10)
.HasColumnType("nvarchar(10)");
b.HasKey("Id");
b.ToTable("Countries", "GMS");
});
modelBuilder.Entity("CMSMicroservice.Domain.Entities.Geography.State", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<long>("Id"));
b.Property<long>("CountryId")
.HasColumnType("bigint");
b.Property<DateTime>("Created")
.HasColumnType("datetime2");
b.Property<string>("CreatedBy")
.HasColumnType("nvarchar(max)");
b.Property<long>("ExternalId")
.HasColumnType("bigint");
b.Property<bool>("IsDeleted")
.ValueGeneratedOnAdd()
.HasColumnType("bit")
.HasDefaultValue(false);
b.Property<DateTime?>("LastModified")
.HasColumnType("datetime2");
b.Property<string>("LastModifiedBy")
.HasColumnType("nvarchar(max)");
b.Property<string>("Latitude")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("nvarchar(50)");
b.Property<string>("Longitude")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("nvarchar(50)");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(500)
.HasColumnType("nvarchar(500)");
b.Property<string>("Native")
.IsRequired()
.ValueGeneratedOnAdd()
.HasMaxLength(500)
.HasColumnType("nvarchar(500)")
.HasDefaultValue("");
b.Property<string>("StateCode")
.IsRequired()
.HasMaxLength(10)
.HasColumnType("nvarchar(10)");
b.Property<string>("Type")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("nvarchar(100)");
b.HasKey("Id");
b.HasIndex("CountryId")
.HasDatabaseName("IX_States_CountryId");
b.ToTable("States", "GMS");
});
modelBuilder.Entity("CMSMicroservice.Domain.Entities.History.ClubMembershipHistory", b =>
{
b.Property<long>("Id")
@@ -2789,6 +3039,28 @@ namespace CMSMicroservice.Infrastructure.Persistence.Migrations
b.Navigation("Product");
});
modelBuilder.Entity("CMSMicroservice.Domain.Entities.Geography.City", b =>
{
b.HasOne("CMSMicroservice.Domain.Entities.Geography.State", "State")
.WithMany("Cities")
.HasForeignKey("StateId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("State");
});
modelBuilder.Entity("CMSMicroservice.Domain.Entities.Geography.State", b =>
{
b.HasOne("CMSMicroservice.Domain.Entities.Geography.Country", "Country")
.WithMany("States")
.HasForeignKey("CountryId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Country");
});
modelBuilder.Entity("CMSMicroservice.Domain.Entities.History.ClubMembershipHistory", b =>
{
b.HasOne("CMSMicroservice.Domain.Entities.Club.ClubMembership", "ClubMembership")
@@ -3149,6 +3421,16 @@ namespace CMSMicroservice.Infrastructure.Persistence.Migrations
b.Navigation("ShoppingCarts");
});
modelBuilder.Entity("CMSMicroservice.Domain.Entities.Geography.Country", b =>
{
b.Navigation("States");
});
modelBuilder.Entity("CMSMicroservice.Domain.Entities.Geography.State", b =>
{
b.Navigation("Cities");
});
modelBuilder.Entity("CMSMicroservice.Domain.Entities.Package", b =>
{
b.Navigation("UserOrders");