Absolute garbage, trying to forward JWT, bad idea
This commit is contained in:
@@ -21,7 +21,6 @@
|
||||
<Folder Include="Users\Create\" />
|
||||
<Folder Include="Users\Delete\" />
|
||||
<Folder Include="Users\Query\" />
|
||||
<Folder Include="Users\Roles\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
39
src/Imprink.Application/Users/Roles/AddUserToRoleHandler.cs
Normal file
39
src/Imprink.Application/Users/Roles/AddUserToRoleHandler.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using Imprink.Domain.Entities.Users;
|
||||
using Imprink.Domain.Repositories;
|
||||
using MediatR;
|
||||
|
||||
namespace Imprink.Application.Users.Roles;
|
||||
|
||||
public record AddUserToRoleCommand(string UserId, Guid RoleId) : IRequest<bool>;
|
||||
|
||||
public class AddUserToRoleCommandHandler(
|
||||
IUserRoleRepository userRoleRepository,
|
||||
IRoleRepository roleRepository,
|
||||
IUserRepository userRepository)
|
||||
: IRequestHandler<AddUserToRoleCommand, bool>
|
||||
{
|
||||
public async Task<bool> Handle(AddUserToRoleCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var userExists = await userRepository.UserExistsAsync(request.UserId, cancellationToken);
|
||||
if (!userExists)
|
||||
return false;
|
||||
|
||||
var roleExists = await roleRepository.RoleExistsAsync(request.RoleId, cancellationToken);
|
||||
if (!roleExists)
|
||||
return false;
|
||||
|
||||
var isAlreadyInRole = await userRoleRepository.IsUserInRoleAsync(request.UserId, request.RoleId, cancellationToken);
|
||||
if (isAlreadyInRole)
|
||||
return true;
|
||||
|
||||
var userRole = new UserRole
|
||||
{
|
||||
UserId = request.UserId,
|
||||
RoleId = request.RoleId
|
||||
};
|
||||
|
||||
await userRoleRepository.AddUserRoleAsync(userRole, cancellationToken);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using Imprink.Domain.Repositories;
|
||||
using MediatR;
|
||||
|
||||
namespace Imprink.Application.Users.Roles;
|
||||
|
||||
public record RemoveUserFromRoleCommand(string UserId, Guid RoleId) : IRequest<bool>;
|
||||
|
||||
public class RemoveUserFromRoleCommandHandler(
|
||||
IUserRoleRepository userRoleRepository,
|
||||
IRoleRepository roleRepository,
|
||||
IUserRepository userRepository)
|
||||
: IRequestHandler<RemoveUserFromRoleCommand, bool>
|
||||
{
|
||||
public async Task<bool> Handle(RemoveUserFromRoleCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var userExists = await userRepository.UserExistsAsync(request.UserId, cancellationToken);
|
||||
if (!userExists)
|
||||
return false;
|
||||
|
||||
var roleExists = await roleRepository.RoleExistsAsync(request.RoleId, cancellationToken);
|
||||
if (!roleExists)
|
||||
return false;
|
||||
|
||||
var userRole = await userRoleRepository.GetUserRoleAsync(request.UserId, request.RoleId, cancellationToken);
|
||||
if (userRole == null)
|
||||
return true;
|
||||
|
||||
await userRoleRepository.RemoveUserRoleAsync(userRole, cancellationToken);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
12
src/Imprink.Domain/Common/Models/Auth0User.cs
Normal file
12
src/Imprink.Domain/Common/Models/Auth0User.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Imprink.Domain.Common.Models;
|
||||
|
||||
public class Auth0User
|
||||
{
|
||||
public string Sub { get; set; } = null!;
|
||||
public string Name { get; set; } = null!;
|
||||
public string Nickname { get; set; } = null!;
|
||||
public string Email { get; set; } = null!;
|
||||
public bool EmailVerified { get; set; }
|
||||
}
|
||||
@@ -2,9 +2,9 @@ using Imprink.Domain.Entities.Orders;
|
||||
|
||||
namespace Imprink.Domain.Entities.Users;
|
||||
|
||||
public class User : EntityBase
|
||||
public class User
|
||||
{
|
||||
public new string Id { get; set; } = null!;
|
||||
public string Id { get; set; } = null!;
|
||||
public required string Email { get; set; }
|
||||
public required string FirstName { get; set; }
|
||||
public required string LastName { get; set; }
|
||||
|
||||
@@ -6,4 +6,5 @@ public class UserRole
|
||||
public Guid RoleId { get; set; }
|
||||
|
||||
public virtual Role Role { get; set; } = null!;
|
||||
public virtual User User { get; set; } = null!;
|
||||
}
|
||||
@@ -4,12 +4,10 @@ using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace Imprink.Infrastructure.Configuration.Users;
|
||||
|
||||
public class UserConfiguration : EntityBaseConfiguration<User>
|
||||
public class UserConfiguration : IEntityTypeConfiguration<User>
|
||||
{
|
||||
public override void Configure(EntityTypeBuilder<User> builder)
|
||||
public void Configure(EntityTypeBuilder<User> builder)
|
||||
{
|
||||
base.Configure(builder);
|
||||
|
||||
builder.Property(u => u.Id)
|
||||
.HasMaxLength(450)
|
||||
.ValueGeneratedNever();
|
||||
@@ -51,12 +49,6 @@ public class UserConfiguration : EntityBaseConfiguration<User>
|
||||
.HasForeignKey(a => a.UserId)
|
||||
.HasPrincipalKey(u => u.Id)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
builder.HasMany(u => u.UserRoles)
|
||||
.WithOne()
|
||||
.HasForeignKey(ur => ur.UserId)
|
||||
.HasPrincipalKey(u => u.Id)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
builder.Ignore(u => u.FullName);
|
||||
builder.Ignore(u => u.DefaultAddress);
|
||||
|
||||
@@ -27,5 +27,10 @@ public class UserRoleConfiguration : IEntityTypeConfiguration<UserRole>
|
||||
.WithMany(r => r.UserRoles)
|
||||
.HasForeignKey(ur => ur.RoleId)
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
|
||||
builder.HasOne(ur => ur.User)
|
||||
.WithMany(u => u.UserRoles)
|
||||
.HasForeignKey(ur => ur.UserId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,9 @@ public class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options
|
||||
public DbSet<Address> Addresses { get; set; }
|
||||
public DbSet<OrderStatus> OrderStatuses { get; set; }
|
||||
public DbSet<ShippingStatus> ShippingStatuses { get; set; }
|
||||
public DbSet<User> Users { get; set; }
|
||||
public DbSet<UserRole> UserRoles { get; set; }
|
||||
public DbSet<Role> Roles { get; set; }
|
||||
public DbSet<Category> Categories { get; set; }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
|
||||
@@ -1,853 +0,0 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Imprink.Infrastructure.Database;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Imprink.Infrastructure.Migrations
|
||||
{
|
||||
[DbContext(typeof(ApplicationDbContext))]
|
||||
[Migration("20250603173128_InitialSetup")]
|
||||
partial class InitialSetup
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "9.0.5")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||
|
||||
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Orders.Order", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uniqueidentifier")
|
||||
.HasDefaultValueSql("NEWID()");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<string>("CreatedBy")
|
||||
.IsRequired()
|
||||
.HasMaxLength(450)
|
||||
.HasColumnType("nvarchar(450)");
|
||||
|
||||
b.Property<DateTime>("ModifiedAt")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("datetime2")
|
||||
.HasDefaultValueSql("GETUTCDATE()");
|
||||
|
||||
b.Property<string>("ModifiedBy")
|
||||
.IsRequired()
|
||||
.HasMaxLength(450)
|
||||
.HasColumnType("nvarchar(450)");
|
||||
|
||||
b.Property<string>("Notes")
|
||||
.IsRequired()
|
||||
.HasMaxLength(1000)
|
||||
.HasColumnType("nvarchar(1000)");
|
||||
|
||||
b.Property<DateTime>("OrderDate")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<string>("OrderNumber")
|
||||
.IsRequired()
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("nvarchar(50)");
|
||||
|
||||
b.Property<int>("OrderStatusId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("ShippingStatusId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<decimal>("TotalPrice")
|
||||
.HasColumnType("decimal(18,2)");
|
||||
|
||||
b.Property<string>("UserId")
|
||||
.IsRequired()
|
||||
.HasMaxLength(450)
|
||||
.HasColumnType("nvarchar(450)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("CreatedAt")
|
||||
.HasDatabaseName("IX_Order_CreatedAt");
|
||||
|
||||
b.HasIndex("CreatedBy")
|
||||
.HasDatabaseName("IX_Order_CreatedBy");
|
||||
|
||||
b.HasIndex("ModifiedAt")
|
||||
.HasDatabaseName("IX_Order_ModifiedAt");
|
||||
|
||||
b.HasIndex("OrderDate")
|
||||
.HasDatabaseName("IX_Order_OrderDate");
|
||||
|
||||
b.HasIndex("OrderNumber")
|
||||
.IsUnique()
|
||||
.HasDatabaseName("IX_Order_OrderNumber");
|
||||
|
||||
b.HasIndex("OrderStatusId")
|
||||
.HasDatabaseName("IX_Order_OrderStatusId");
|
||||
|
||||
b.HasIndex("ShippingStatusId")
|
||||
.HasDatabaseName("IX_Order_ShippingStatusId");
|
||||
|
||||
b.HasIndex("UserId")
|
||||
.HasDatabaseName("IX_Order_UserId");
|
||||
|
||||
b.HasIndex("UserId", "OrderDate")
|
||||
.HasDatabaseName("IX_Order_User_Date");
|
||||
|
||||
b.ToTable("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Orders.OrderAddress", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uniqueidentifier")
|
||||
.HasDefaultValueSql("NEWID()");
|
||||
|
||||
b.Property<string>("City")
|
||||
.IsRequired()
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("nvarchar(100)");
|
||||
|
||||
b.Property<string>("Country")
|
||||
.IsRequired()
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("nvarchar(100)");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<string>("CreatedBy")
|
||||
.IsRequired()
|
||||
.HasMaxLength(450)
|
||||
.HasColumnType("nvarchar(450)");
|
||||
|
||||
b.Property<DateTime>("ModifiedAt")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("datetime2")
|
||||
.HasDefaultValueSql("GETUTCDATE()");
|
||||
|
||||
b.Property<string>("ModifiedBy")
|
||||
.IsRequired()
|
||||
.HasMaxLength(450)
|
||||
.HasColumnType("nvarchar(450)");
|
||||
|
||||
b.Property<Guid>("OrderId")
|
||||
.HasColumnType("uniqueidentifier");
|
||||
|
||||
b.Property<string>("PostalCode")
|
||||
.IsRequired()
|
||||
.HasMaxLength(20)
|
||||
.HasColumnType("nvarchar(20)");
|
||||
|
||||
b.Property<string>("State")
|
||||
.IsRequired()
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("nvarchar(100)");
|
||||
|
||||
b.Property<string>("Street")
|
||||
.IsRequired()
|
||||
.HasMaxLength(200)
|
||||
.HasColumnType("nvarchar(200)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("CreatedAt")
|
||||
.HasDatabaseName("IX_OrderAddress_CreatedAt");
|
||||
|
||||
b.HasIndex("CreatedBy")
|
||||
.HasDatabaseName("IX_OrderAddress_CreatedBy");
|
||||
|
||||
b.HasIndex("ModifiedAt")
|
||||
.HasDatabaseName("IX_OrderAddress_ModifiedAt");
|
||||
|
||||
b.HasIndex("OrderId")
|
||||
.IsUnique()
|
||||
.HasDatabaseName("IX_OrderAddress_OrderId");
|
||||
|
||||
b.ToTable("OrderAddresses");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Orders.OrderItem", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uniqueidentifier")
|
||||
.HasDefaultValueSql("NEWID()");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<string>("CreatedBy")
|
||||
.IsRequired()
|
||||
.HasMaxLength(450)
|
||||
.HasColumnType("nvarchar(450)");
|
||||
|
||||
b.Property<string>("CustomizationDescription")
|
||||
.IsRequired()
|
||||
.HasMaxLength(2000)
|
||||
.HasColumnType("nvarchar(2000)");
|
||||
|
||||
b.Property<string>("CustomizationImageUrl")
|
||||
.IsRequired()
|
||||
.HasMaxLength(500)
|
||||
.HasColumnType("nvarchar(500)");
|
||||
|
||||
b.Property<DateTime>("ModifiedAt")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("datetime2")
|
||||
.HasDefaultValueSql("GETUTCDATE()");
|
||||
|
||||
b.Property<string>("ModifiedBy")
|
||||
.IsRequired()
|
||||
.HasMaxLength(450)
|
||||
.HasColumnType("nvarchar(450)");
|
||||
|
||||
b.Property<Guid>("OrderId")
|
||||
.HasColumnType("uniqueidentifier");
|
||||
|
||||
b.Property<Guid>("ProductId")
|
||||
.HasColumnType("uniqueidentifier");
|
||||
|
||||
b.Property<Guid?>("ProductVariantId")
|
||||
.HasColumnType("uniqueidentifier");
|
||||
|
||||
b.Property<int>("Quantity")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int")
|
||||
.HasDefaultValue(1);
|
||||
|
||||
b.Property<decimal>("TotalPrice")
|
||||
.HasColumnType("decimal(18,2)");
|
||||
|
||||
b.Property<decimal>("UnitPrice")
|
||||
.HasColumnType("decimal(18,2)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("CreatedAt")
|
||||
.HasDatabaseName("IX_OrderItem_CreatedAt");
|
||||
|
||||
b.HasIndex("CreatedBy")
|
||||
.HasDatabaseName("IX_OrderItem_CreatedBy");
|
||||
|
||||
b.HasIndex("ModifiedAt")
|
||||
.HasDatabaseName("IX_OrderItem_ModifiedAt");
|
||||
|
||||
b.HasIndex("OrderId")
|
||||
.HasDatabaseName("IX_OrderItem_OrderId");
|
||||
|
||||
b.HasIndex("ProductId")
|
||||
.HasDatabaseName("IX_OrderItem_ProductId");
|
||||
|
||||
b.HasIndex("ProductVariantId")
|
||||
.HasDatabaseName("IX_OrderItem_ProductVariantId");
|
||||
|
||||
b.HasIndex("OrderId", "ProductId")
|
||||
.HasDatabaseName("IX_OrderItem_Order_Product");
|
||||
|
||||
b.ToTable("OrderItems");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Orders.OrderStatus", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("nvarchar(50)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("Name")
|
||||
.IsUnique()
|
||||
.HasDatabaseName("IX_OrderStatus_Name");
|
||||
|
||||
b.ToTable("OrderStatuses");
|
||||
|
||||
b.HasData(
|
||||
new
|
||||
{
|
||||
Id = 0,
|
||||
Name = "Pending"
|
||||
},
|
||||
new
|
||||
{
|
||||
Id = 1,
|
||||
Name = "Processing"
|
||||
},
|
||||
new
|
||||
{
|
||||
Id = 2,
|
||||
Name = "Completed"
|
||||
},
|
||||
new
|
||||
{
|
||||
Id = 3,
|
||||
Name = "Cancelled"
|
||||
});
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Orders.ShippingStatus", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("nvarchar(50)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("Name")
|
||||
.IsUnique()
|
||||
.HasDatabaseName("IX_ShippingStatus_Name");
|
||||
|
||||
b.ToTable("ShippingStatuses");
|
||||
|
||||
b.HasData(
|
||||
new
|
||||
{
|
||||
Id = 0,
|
||||
Name = "Prepping"
|
||||
},
|
||||
new
|
||||
{
|
||||
Id = 1,
|
||||
Name = "Packaging"
|
||||
},
|
||||
new
|
||||
{
|
||||
Id = 2,
|
||||
Name = "Shipped"
|
||||
},
|
||||
new
|
||||
{
|
||||
Id = 3,
|
||||
Name = "Delivered"
|
||||
});
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Product.Category", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uniqueidentifier")
|
||||
.HasDefaultValueSql("NEWID()");
|
||||
|
||||
b.Property<DateTime?>("CreatedAt")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<string>("CreatedBy")
|
||||
.HasMaxLength(450)
|
||||
.HasColumnType("nvarchar(450)");
|
||||
|
||||
b.Property<string>("Description")
|
||||
.IsRequired()
|
||||
.HasMaxLength(2000)
|
||||
.HasColumnType("nvarchar(2000)");
|
||||
|
||||
b.Property<string>("ImageUrl")
|
||||
.HasMaxLength(500)
|
||||
.HasColumnType("nvarchar(500)");
|
||||
|
||||
b.Property<bool>("IsActive")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("bit")
|
||||
.HasDefaultValue(true);
|
||||
|
||||
b.Property<DateTime?>("ModifiedAt")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("datetime2")
|
||||
.HasDefaultValueSql("GETUTCDATE()");
|
||||
|
||||
b.Property<string>("ModifiedBy")
|
||||
.HasMaxLength(450)
|
||||
.HasColumnType("nvarchar(450)");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasMaxLength(200)
|
||||
.HasColumnType("nvarchar(200)");
|
||||
|
||||
b.Property<Guid?>("ParentCategoryId")
|
||||
.HasColumnType("uniqueidentifier");
|
||||
|
||||
b.Property<int>("SortOrder")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int")
|
||||
.HasDefaultValue(0);
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("CreatedAt")
|
||||
.HasDatabaseName("IX_Category_CreatedAt");
|
||||
|
||||
b.HasIndex("CreatedBy")
|
||||
.HasDatabaseName("IX_Category_CreatedBy");
|
||||
|
||||
b.HasIndex("IsActive")
|
||||
.HasDatabaseName("IX_Category_IsActive");
|
||||
|
||||
b.HasIndex("ModifiedAt")
|
||||
.HasDatabaseName("IX_Category_ModifiedAt");
|
||||
|
||||
b.HasIndex("Name")
|
||||
.HasDatabaseName("IX_Category_Name");
|
||||
|
||||
b.HasIndex("ParentCategoryId")
|
||||
.HasDatabaseName("IX_Category_ParentCategoryId");
|
||||
|
||||
b.HasIndex("IsActive", "SortOrder")
|
||||
.HasDatabaseName("IX_Category_Active_SortOrder");
|
||||
|
||||
b.HasIndex("ParentCategoryId", "SortOrder")
|
||||
.HasDatabaseName("IX_Category_Parent_SortOrder");
|
||||
|
||||
b.ToTable("Categories");
|
||||
|
||||
b.HasData(
|
||||
new
|
||||
{
|
||||
Id = new Guid("11111111-1111-1111-1111-111111111111"),
|
||||
CreatedAt = new DateTime(2025, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc),
|
||||
CreatedBy = "system@printbase.com",
|
||||
Description = "Textile and fabric-based products",
|
||||
IsActive = true,
|
||||
ModifiedAt = new DateTime(2025, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc),
|
||||
ModifiedBy = "system@printbase.com",
|
||||
Name = "Textile",
|
||||
SortOrder = 1
|
||||
},
|
||||
new
|
||||
{
|
||||
Id = new Guid("22222222-2222-2222-2222-222222222222"),
|
||||
CreatedAt = new DateTime(2025, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc),
|
||||
CreatedBy = "system@printbase.com",
|
||||
Description = "Products for hard surface printing",
|
||||
IsActive = true,
|
||||
ModifiedAt = new DateTime(2025, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc),
|
||||
ModifiedBy = "system@printbase.com",
|
||||
Name = "Hard Surfaces",
|
||||
SortOrder = 2
|
||||
},
|
||||
new
|
||||
{
|
||||
Id = new Guid("33333333-3333-3333-3333-333333333333"),
|
||||
CreatedAt = new DateTime(2025, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc),
|
||||
CreatedBy = "system@printbase.com",
|
||||
Description = "Paper-based printing products",
|
||||
IsActive = true,
|
||||
ModifiedAt = new DateTime(2025, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc),
|
||||
ModifiedBy = "system@printbase.com",
|
||||
Name = "Paper",
|
||||
SortOrder = 3
|
||||
});
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Product.Product", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uniqueidentifier")
|
||||
.HasDefaultValueSql("NEWID()");
|
||||
|
||||
b.Property<decimal>("BasePrice")
|
||||
.HasColumnType("decimal(18,2)");
|
||||
|
||||
b.Property<Guid?>("CategoryId")
|
||||
.HasColumnType("uniqueidentifier");
|
||||
|
||||
b.Property<DateTime?>("CreatedAt")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<string>("CreatedBy")
|
||||
.HasMaxLength(450)
|
||||
.HasColumnType("nvarchar(450)");
|
||||
|
||||
b.Property<string>("Description")
|
||||
.HasMaxLength(2000)
|
||||
.HasColumnType("nvarchar(2000)");
|
||||
|
||||
b.Property<string>("ImageUrl")
|
||||
.HasMaxLength(500)
|
||||
.HasColumnType("nvarchar(500)");
|
||||
|
||||
b.Property<bool>("IsActive")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("bit")
|
||||
.HasDefaultValue(true);
|
||||
|
||||
b.Property<bool>("IsCustomizable")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("bit")
|
||||
.HasDefaultValue(false);
|
||||
|
||||
b.Property<DateTime?>("ModifiedAt")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("datetime2")
|
||||
.HasDefaultValueSql("GETUTCDATE()");
|
||||
|
||||
b.Property<string>("ModifiedBy")
|
||||
.HasMaxLength(450)
|
||||
.HasColumnType("nvarchar(450)");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasMaxLength(200)
|
||||
.HasColumnType("nvarchar(200)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("CategoryId")
|
||||
.HasDatabaseName("IX_Product_CategoryId");
|
||||
|
||||
b.HasIndex("CreatedAt")
|
||||
.HasDatabaseName("IX_Product_CreatedAt");
|
||||
|
||||
b.HasIndex("CreatedBy")
|
||||
.HasDatabaseName("IX_Product_CreatedBy");
|
||||
|
||||
b.HasIndex("IsActive")
|
||||
.HasDatabaseName("IX_Product_IsActive");
|
||||
|
||||
b.HasIndex("IsCustomizable")
|
||||
.HasDatabaseName("IX_Product_IsCustomizable");
|
||||
|
||||
b.HasIndex("ModifiedAt")
|
||||
.HasDatabaseName("IX_Product_ModifiedAt");
|
||||
|
||||
b.HasIndex("Name")
|
||||
.HasDatabaseName("IX_Product_Name");
|
||||
|
||||
b.HasIndex("CategoryId", "IsActive")
|
||||
.HasDatabaseName("IX_Product_Category_Active");
|
||||
|
||||
b.HasIndex("IsActive", "IsCustomizable")
|
||||
.HasDatabaseName("IX_Product_Active_Customizable");
|
||||
|
||||
b.ToTable("Products");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Product.ProductVariant", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uniqueidentifier")
|
||||
.HasDefaultValueSql("NEWID()");
|
||||
|
||||
b.Property<string>("Color")
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("nvarchar(50)");
|
||||
|
||||
b.Property<DateTime?>("CreatedAt")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<string>("CreatedBy")
|
||||
.HasMaxLength(450)
|
||||
.HasColumnType("nvarchar(450)");
|
||||
|
||||
b.Property<string>("ImageUrl")
|
||||
.HasMaxLength(500)
|
||||
.HasColumnType("nvarchar(500)");
|
||||
|
||||
b.Property<bool>("IsActive")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("bit")
|
||||
.HasDefaultValue(true);
|
||||
|
||||
b.Property<DateTime?>("ModifiedAt")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("datetime2")
|
||||
.HasDefaultValueSql("GETUTCDATE()");
|
||||
|
||||
b.Property<string>("ModifiedBy")
|
||||
.HasMaxLength(450)
|
||||
.HasColumnType("nvarchar(450)");
|
||||
|
||||
b.Property<decimal>("Price")
|
||||
.HasColumnType("decimal(18,2)");
|
||||
|
||||
b.Property<Guid>("ProductId")
|
||||
.HasColumnType("uniqueidentifier");
|
||||
|
||||
b.Property<string>("Size")
|
||||
.IsRequired()
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("nvarchar(50)");
|
||||
|
||||
b.Property<string>("Sku")
|
||||
.IsRequired()
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("nvarchar(100)");
|
||||
|
||||
b.Property<int>("StockQuantity")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int")
|
||||
.HasDefaultValue(0);
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("CreatedAt")
|
||||
.HasDatabaseName("IX_ProductVariant_CreatedAt");
|
||||
|
||||
b.HasIndex("CreatedBy")
|
||||
.HasDatabaseName("IX_ProductVariant_CreatedBy");
|
||||
|
||||
b.HasIndex("IsActive")
|
||||
.HasDatabaseName("IX_ProductVariant_IsActive");
|
||||
|
||||
b.HasIndex("ModifiedAt")
|
||||
.HasDatabaseName("IX_ProductVariant_ModifiedAt");
|
||||
|
||||
b.HasIndex("ProductId")
|
||||
.HasDatabaseName("IX_ProductVariant_ProductId");
|
||||
|
||||
b.HasIndex("Sku")
|
||||
.IsUnique()
|
||||
.HasDatabaseName("IX_ProductVariant_SKU");
|
||||
|
||||
b.HasIndex("ProductId", "Size", "Color")
|
||||
.IsUnique()
|
||||
.HasDatabaseName("IX_ProductVariant_Product_Size_Color")
|
||||
.HasFilter("[Color] IS NOT NULL");
|
||||
|
||||
b.ToTable("ProductVariants");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Users.Address", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uniqueidentifier")
|
||||
.HasDefaultValueSql("NEWID()");
|
||||
|
||||
b.Property<string>("AddressType")
|
||||
.IsRequired()
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("nvarchar(50)");
|
||||
|
||||
b.Property<string>("City")
|
||||
.IsRequired()
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("nvarchar(100)");
|
||||
|
||||
b.Property<string>("Country")
|
||||
.IsRequired()
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("nvarchar(100)");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<string>("CreatedBy")
|
||||
.IsRequired()
|
||||
.HasMaxLength(450)
|
||||
.HasColumnType("nvarchar(450)");
|
||||
|
||||
b.Property<bool>("IsActive")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("bit")
|
||||
.HasDefaultValue(true);
|
||||
|
||||
b.Property<bool>("IsDefault")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("bit")
|
||||
.HasDefaultValue(false);
|
||||
|
||||
b.Property<DateTime>("ModifiedAt")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("datetime2")
|
||||
.HasDefaultValueSql("GETUTCDATE()");
|
||||
|
||||
b.Property<string>("ModifiedBy")
|
||||
.IsRequired()
|
||||
.HasMaxLength(450)
|
||||
.HasColumnType("nvarchar(450)");
|
||||
|
||||
b.Property<string>("PostalCode")
|
||||
.IsRequired()
|
||||
.HasMaxLength(20)
|
||||
.HasColumnType("nvarchar(20)");
|
||||
|
||||
b.Property<string>("State")
|
||||
.IsRequired()
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("nvarchar(100)");
|
||||
|
||||
b.Property<string>("Street")
|
||||
.IsRequired()
|
||||
.HasMaxLength(200)
|
||||
.HasColumnType("nvarchar(200)");
|
||||
|
||||
b.Property<string>("UserId")
|
||||
.IsRequired()
|
||||
.HasMaxLength(450)
|
||||
.HasColumnType("nvarchar(450)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("CreatedAt")
|
||||
.HasDatabaseName("IX_Address_CreatedAt");
|
||||
|
||||
b.HasIndex("CreatedBy")
|
||||
.HasDatabaseName("IX_Address_CreatedBy");
|
||||
|
||||
b.HasIndex("ModifiedAt")
|
||||
.HasDatabaseName("IX_Address_ModifiedAt");
|
||||
|
||||
b.HasIndex("UserId")
|
||||
.HasDatabaseName("IX_Address_UserId");
|
||||
|
||||
b.HasIndex("UserId", "AddressType")
|
||||
.HasDatabaseName("IX_Address_User_Type");
|
||||
|
||||
b.HasIndex("UserId", "IsDefault")
|
||||
.HasDatabaseName("IX_Address_User_Default");
|
||||
|
||||
b.ToTable("Addresses");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Orders.Order", b =>
|
||||
{
|
||||
b.HasOne("Imprink.Domain.Entities.Orders.OrderStatus", "OrderStatus")
|
||||
.WithMany("Orders")
|
||||
.HasForeignKey("OrderStatusId")
|
||||
.OnDelete(DeleteBehavior.Restrict)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("Imprink.Domain.Entities.Orders.ShippingStatus", "ShippingStatus")
|
||||
.WithMany("Orders")
|
||||
.HasForeignKey("ShippingStatusId")
|
||||
.OnDelete(DeleteBehavior.Restrict)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("OrderStatus");
|
||||
|
||||
b.Navigation("ShippingStatus");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Orders.OrderAddress", b =>
|
||||
{
|
||||
b.HasOne("Imprink.Domain.Entities.Orders.Order", "Order")
|
||||
.WithOne("OrderAddress")
|
||||
.HasForeignKey("Imprink.Domain.Entities.Orders.OrderAddress", "OrderId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Order");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Orders.OrderItem", b =>
|
||||
{
|
||||
b.HasOne("Imprink.Domain.Entities.Orders.Order", "Order")
|
||||
.WithMany("OrderItems")
|
||||
.HasForeignKey("OrderId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("Imprink.Domain.Entities.Product.Product", "Product")
|
||||
.WithMany("OrderItems")
|
||||
.HasForeignKey("ProductId")
|
||||
.OnDelete(DeleteBehavior.Restrict)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("Imprink.Domain.Entities.Product.ProductVariant", "ProductVariant")
|
||||
.WithMany("OrderItems")
|
||||
.HasForeignKey("ProductVariantId")
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
|
||||
b.Navigation("Order");
|
||||
|
||||
b.Navigation("Product");
|
||||
|
||||
b.Navigation("ProductVariant");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Product.Category", b =>
|
||||
{
|
||||
b.HasOne("Imprink.Domain.Entities.Product.Category", "ParentCategory")
|
||||
.WithMany("SubCategories")
|
||||
.HasForeignKey("ParentCategoryId")
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
|
||||
b.Navigation("ParentCategory");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Product.Product", b =>
|
||||
{
|
||||
b.HasOne("Imprink.Domain.Entities.Product.Category", "Category")
|
||||
.WithMany("Products")
|
||||
.HasForeignKey("CategoryId")
|
||||
.OnDelete(DeleteBehavior.SetNull);
|
||||
|
||||
b.Navigation("Category");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Product.ProductVariant", b =>
|
||||
{
|
||||
b.HasOne("Imprink.Domain.Entities.Product.Product", "Product")
|
||||
.WithMany("ProductVariants")
|
||||
.HasForeignKey("ProductId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Product");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Orders.Order", b =>
|
||||
{
|
||||
b.Navigation("OrderAddress")
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("OrderItems");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Orders.OrderStatus", b =>
|
||||
{
|
||||
b.Navigation("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Orders.ShippingStatus", b =>
|
||||
{
|
||||
b.Navigation("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Product.Category", b =>
|
||||
{
|
||||
b.Navigation("Products");
|
||||
|
||||
b.Navigation("SubCategories");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Product.Product", b =>
|
||||
{
|
||||
b.Navigation("OrderItems");
|
||||
|
||||
b.Navigation("ProductVariants");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Product.ProductVariant", b =>
|
||||
{
|
||||
b.Navigation("OrderItems");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,152 +0,0 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional
|
||||
|
||||
namespace Imprink.Infrastructure.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AddUserRoles : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Role",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
|
||||
RoleName = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Role", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "User",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<string>(type: "nvarchar(450)", maxLength: 450, nullable: false, defaultValueSql: "NEWID()"),
|
||||
Email = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: false),
|
||||
FirstName = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
|
||||
LastName = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
|
||||
PhoneNumber = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: true),
|
||||
DateOfBirth = table.Column<DateTime>(type: "date", nullable: true),
|
||||
IsActive = table.Column<bool>(type: "bit", nullable: false, defaultValue: true),
|
||||
LastLoginAt = table.Column<DateTime>(type: "datetime2", nullable: true),
|
||||
CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||
ModifiedAt = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "GETUTCDATE()"),
|
||||
CreatedBy = table.Column<string>(type: "nvarchar(450)", maxLength: 450, nullable: false),
|
||||
ModifiedBy = table.Column<string>(type: "nvarchar(450)", maxLength: 450, nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_User", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "UserRole",
|
||||
columns: table => new
|
||||
{
|
||||
UserId = table.Column<string>(type: "nvarchar(450)", maxLength: 450, nullable: false),
|
||||
RoleId = table.Column<Guid>(type: "uniqueidentifier", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_UserRole", x => new { x.UserId, x.RoleId });
|
||||
table.ForeignKey(
|
||||
name: "FK_UserRole_Role_RoleId",
|
||||
column: x => x.RoleId,
|
||||
principalTable: "Role",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Restrict);
|
||||
table.ForeignKey(
|
||||
name: "FK_UserRole_User_UserId",
|
||||
column: x => x.UserId,
|
||||
principalTable: "User",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.InsertData(
|
||||
table: "Role",
|
||||
columns: new[] { "Id", "RoleName" },
|
||||
values: new object[,]
|
||||
{
|
||||
{ new Guid("11111111-1111-1111-1111-111111111111"), "User" },
|
||||
{ new Guid("22222222-2222-2222-2222-222222222222"), "Merchant" },
|
||||
{ new Guid("33333333-3333-3333-3333-333333333333"), "Admin" }
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Role_RoleName",
|
||||
table: "Role",
|
||||
column: "RoleName",
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_User_CreatedAt",
|
||||
table: "User",
|
||||
column: "CreatedAt");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_User_CreatedBy",
|
||||
table: "User",
|
||||
column: "CreatedBy");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_User_Email",
|
||||
table: "User",
|
||||
column: "Email",
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_User_IsActive",
|
||||
table: "User",
|
||||
column: "IsActive");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_User_ModifiedAt",
|
||||
table: "User",
|
||||
column: "ModifiedAt");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_UserRole_RoleId",
|
||||
table: "UserRole",
|
||||
column: "RoleId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_UserRole_UserId",
|
||||
table: "UserRole",
|
||||
column: "UserId");
|
||||
|
||||
migrationBuilder.AddForeignKey(
|
||||
name: "FK_Addresses_User_UserId",
|
||||
table: "Addresses",
|
||||
column: "UserId",
|
||||
principalTable: "User",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropForeignKey(
|
||||
name: "FK_Addresses_User_UserId",
|
||||
table: "Addresses");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "UserRole");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Role");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "User");
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,30 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Imprink.Infrastructure.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class RelateUsersAndOrders : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.AddForeignKey(
|
||||
name: "FK_Orders_User_UserId",
|
||||
table: "Orders",
|
||||
column: "UserId",
|
||||
principalTable: "User",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Restrict);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropForeignKey(
|
||||
name: "FK_Orders_User_UserId",
|
||||
table: "Orders");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,8 +12,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
namespace Imprink.Infrastructure.Migrations
|
||||
{
|
||||
[DbContext(typeof(ApplicationDbContext))]
|
||||
[Migration("20250606161235_AddUserRoles")]
|
||||
partial class AddUserRoles
|
||||
[Migration("20250606173957_InitialSetup")]
|
||||
partial class InitialSetup
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
@@ -740,7 +740,7 @@ namespace Imprink.Infrastructure.Migrations
|
||||
.IsUnique()
|
||||
.HasDatabaseName("IX_Role_RoleName");
|
||||
|
||||
b.ToTable("Role");
|
||||
b.ToTable("Roles");
|
||||
|
||||
b.HasData(
|
||||
new
|
||||
@@ -763,15 +763,6 @@ namespace Imprink.Infrastructure.Migrations
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Users.User", b =>
|
||||
{
|
||||
b.Property<string>("Id")
|
||||
.HasMaxLength(450)
|
||||
.HasColumnType("nvarchar(450)")
|
||||
.HasDefaultValueSql("NEWID()");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<string>("CreatedBy")
|
||||
.IsRequired()
|
||||
.HasMaxLength(450)
|
||||
.HasColumnType("nvarchar(450)");
|
||||
|
||||
@@ -801,28 +792,12 @@ namespace Imprink.Infrastructure.Migrations
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("nvarchar(100)");
|
||||
|
||||
b.Property<DateTime>("ModifiedAt")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("datetime2")
|
||||
.HasDefaultValueSql("GETUTCDATE()");
|
||||
|
||||
b.Property<string>("ModifiedBy")
|
||||
.IsRequired()
|
||||
.HasMaxLength(450)
|
||||
.HasColumnType("nvarchar(450)");
|
||||
|
||||
b.Property<string>("PhoneNumber")
|
||||
.HasMaxLength(20)
|
||||
.HasColumnType("nvarchar(20)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("CreatedAt")
|
||||
.HasDatabaseName("IX_User_CreatedAt");
|
||||
|
||||
b.HasIndex("CreatedBy")
|
||||
.HasDatabaseName("IX_User_CreatedBy");
|
||||
|
||||
b.HasIndex("Email")
|
||||
.IsUnique()
|
||||
.HasDatabaseName("IX_User_Email");
|
||||
@@ -830,10 +805,7 @@ namespace Imprink.Infrastructure.Migrations
|
||||
b.HasIndex("IsActive")
|
||||
.HasDatabaseName("IX_User_IsActive");
|
||||
|
||||
b.HasIndex("ModifiedAt")
|
||||
.HasDatabaseName("IX_User_ModifiedAt");
|
||||
|
||||
b.ToTable("User");
|
||||
b.ToTable("Users");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Users.UserRole", b =>
|
||||
@@ -853,7 +825,7 @@ namespace Imprink.Infrastructure.Migrations
|
||||
b.HasIndex("UserId")
|
||||
.HasDatabaseName("IX_UserRole_UserId");
|
||||
|
||||
b.ToTable("UserRole");
|
||||
b.ToTable("UserRoles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Orders.Order", b =>
|
||||
@@ -870,9 +842,17 @@ namespace Imprink.Infrastructure.Migrations
|
||||
.OnDelete(DeleteBehavior.Restrict)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("Imprink.Domain.Entities.Users.User", "User")
|
||||
.WithMany("Orders")
|
||||
.HasForeignKey("UserId")
|
||||
.OnDelete(DeleteBehavior.Restrict)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("OrderStatus");
|
||||
|
||||
b.Navigation("ShippingStatus");
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Orders.OrderAddress", b =>
|
||||
@@ -960,13 +940,15 @@ namespace Imprink.Infrastructure.Migrations
|
||||
.OnDelete(DeleteBehavior.Restrict)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("Imprink.Domain.Entities.Users.User", null)
|
||||
b.HasOne("Imprink.Domain.Entities.Users.User", "User")
|
||||
.WithMany("UserRoles")
|
||||
.HasForeignKey("UserId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Role");
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Orders.Order", b =>
|
||||
@@ -1015,6 +997,8 @@ namespace Imprink.Infrastructure.Migrations
|
||||
{
|
||||
b.Navigation("Addresses");
|
||||
|
||||
b.Navigation("Orders");
|
||||
|
||||
b.Navigation("UserRoles");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
@@ -13,30 +13,6 @@ namespace Imprink.Infrastructure.Migrations
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Addresses",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false, defaultValueSql: "NEWID()"),
|
||||
UserId = table.Column<string>(type: "nvarchar(450)", maxLength: 450, nullable: false),
|
||||
AddressType = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
|
||||
Street = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: false),
|
||||
City = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
|
||||
State = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
|
||||
PostalCode = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: false),
|
||||
Country = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
|
||||
IsDefault = table.Column<bool>(type: "bit", nullable: false, defaultValue: false),
|
||||
IsActive = table.Column<bool>(type: "bit", nullable: false, defaultValue: true),
|
||||
CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||
ModifiedAt = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "GETUTCDATE()"),
|
||||
CreatedBy = table.Column<string>(type: "nvarchar(450)", maxLength: 450, nullable: false),
|
||||
ModifiedBy = table.Column<string>(type: "nvarchar(450)", maxLength: 450, nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Addresses", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Categories",
|
||||
columns: table => new
|
||||
@@ -76,6 +52,18 @@ namespace Imprink.Infrastructure.Migrations
|
||||
table.PrimaryKey("PK_OrderStatuses", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Roles",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
|
||||
RoleName = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Roles", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "ShippingStatuses",
|
||||
columns: table => new
|
||||
@@ -88,6 +76,24 @@ namespace Imprink.Infrastructure.Migrations
|
||||
table.PrimaryKey("PK_ShippingStatuses", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Users",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<string>(type: "nvarchar(450)", maxLength: 450, nullable: false),
|
||||
Email = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: false),
|
||||
FirstName = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
|
||||
LastName = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
|
||||
PhoneNumber = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: true),
|
||||
DateOfBirth = table.Column<DateTime>(type: "date", nullable: true),
|
||||
IsActive = table.Column<bool>(type: "bit", nullable: false, defaultValue: true),
|
||||
LastLoginAt = table.Column<DateTime>(type: "datetime2", nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Users", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Products",
|
||||
columns: table => new
|
||||
@@ -116,6 +122,36 @@ namespace Imprink.Infrastructure.Migrations
|
||||
onDelete: ReferentialAction.SetNull);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Addresses",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false, defaultValueSql: "NEWID()"),
|
||||
UserId = table.Column<string>(type: "nvarchar(450)", maxLength: 450, nullable: false),
|
||||
AddressType = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
|
||||
Street = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: false),
|
||||
City = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
|
||||
State = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
|
||||
PostalCode = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: false),
|
||||
Country = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
|
||||
IsDefault = table.Column<bool>(type: "bit", nullable: false, defaultValue: false),
|
||||
IsActive = table.Column<bool>(type: "bit", nullable: false, defaultValue: true),
|
||||
CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||
ModifiedAt = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "GETUTCDATE()"),
|
||||
CreatedBy = table.Column<string>(type: "nvarchar(450)", maxLength: 450, nullable: false),
|
||||
ModifiedBy = table.Column<string>(type: "nvarchar(450)", maxLength: 450, nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Addresses", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_Addresses_Users_UserId",
|
||||
column: x => x.UserId,
|
||||
principalTable: "Users",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Orders",
|
||||
columns: table => new
|
||||
@@ -148,6 +184,36 @@ namespace Imprink.Infrastructure.Migrations
|
||||
principalTable: "ShippingStatuses",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Restrict);
|
||||
table.ForeignKey(
|
||||
name: "FK_Orders_Users_UserId",
|
||||
column: x => x.UserId,
|
||||
principalTable: "Users",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Restrict);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "UserRoles",
|
||||
columns: table => new
|
||||
{
|
||||
UserId = table.Column<string>(type: "nvarchar(450)", maxLength: 450, nullable: false),
|
||||
RoleId = table.Column<Guid>(type: "uniqueidentifier", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_UserRoles", x => new { x.UserId, x.RoleId });
|
||||
table.ForeignKey(
|
||||
name: "FK_UserRoles_Roles_RoleId",
|
||||
column: x => x.RoleId,
|
||||
principalTable: "Roles",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Restrict);
|
||||
table.ForeignKey(
|
||||
name: "FK_UserRoles_Users_UserId",
|
||||
column: x => x.UserId,
|
||||
principalTable: "Users",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
@@ -268,6 +334,16 @@ namespace Imprink.Infrastructure.Migrations
|
||||
{ 3, "Cancelled" }
|
||||
});
|
||||
|
||||
migrationBuilder.InsertData(
|
||||
table: "Roles",
|
||||
columns: new[] { "Id", "RoleName" },
|
||||
values: new object[,]
|
||||
{
|
||||
{ new Guid("11111111-1111-1111-1111-111111111111"), "User" },
|
||||
{ new Guid("22222222-2222-2222-2222-222222222222"), "Merchant" },
|
||||
{ new Guid("33333333-3333-3333-3333-333333333333"), "Admin" }
|
||||
});
|
||||
|
||||
migrationBuilder.InsertData(
|
||||
table: "ShippingStatuses",
|
||||
columns: new[] { "Id", "Name" },
|
||||
@@ -540,11 +616,38 @@ namespace Imprink.Infrastructure.Migrations
|
||||
column: "Sku",
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Role_RoleName",
|
||||
table: "Roles",
|
||||
column: "RoleName",
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_ShippingStatus_Name",
|
||||
table: "ShippingStatuses",
|
||||
column: "Name",
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_UserRole_RoleId",
|
||||
table: "UserRoles",
|
||||
column: "RoleId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_UserRole_UserId",
|
||||
table: "UserRoles",
|
||||
column: "UserId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_User_Email",
|
||||
table: "Users",
|
||||
column: "Email",
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_User_IsActive",
|
||||
table: "Users",
|
||||
column: "IsActive");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -559,18 +662,27 @@ namespace Imprink.Infrastructure.Migrations
|
||||
migrationBuilder.DropTable(
|
||||
name: "OrderItems");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "UserRoles");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Orders");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "ProductVariants");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Roles");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "OrderStatuses");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "ShippingStatuses");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Users");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Products");
|
||||
|
||||
@@ -737,7 +737,7 @@ namespace Imprink.Infrastructure.Migrations
|
||||
.IsUnique()
|
||||
.HasDatabaseName("IX_Role_RoleName");
|
||||
|
||||
b.ToTable("Role");
|
||||
b.ToTable("Roles");
|
||||
|
||||
b.HasData(
|
||||
new
|
||||
@@ -760,15 +760,6 @@ namespace Imprink.Infrastructure.Migrations
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Users.User", b =>
|
||||
{
|
||||
b.Property<string>("Id")
|
||||
.HasMaxLength(450)
|
||||
.HasColumnType("nvarchar(450)")
|
||||
.HasDefaultValueSql("NEWID()");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<string>("CreatedBy")
|
||||
.IsRequired()
|
||||
.HasMaxLength(450)
|
||||
.HasColumnType("nvarchar(450)");
|
||||
|
||||
@@ -798,28 +789,12 @@ namespace Imprink.Infrastructure.Migrations
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("nvarchar(100)");
|
||||
|
||||
b.Property<DateTime>("ModifiedAt")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("datetime2")
|
||||
.HasDefaultValueSql("GETUTCDATE()");
|
||||
|
||||
b.Property<string>("ModifiedBy")
|
||||
.IsRequired()
|
||||
.HasMaxLength(450)
|
||||
.HasColumnType("nvarchar(450)");
|
||||
|
||||
b.Property<string>("PhoneNumber")
|
||||
.HasMaxLength(20)
|
||||
.HasColumnType("nvarchar(20)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("CreatedAt")
|
||||
.HasDatabaseName("IX_User_CreatedAt");
|
||||
|
||||
b.HasIndex("CreatedBy")
|
||||
.HasDatabaseName("IX_User_CreatedBy");
|
||||
|
||||
b.HasIndex("Email")
|
||||
.IsUnique()
|
||||
.HasDatabaseName("IX_User_Email");
|
||||
@@ -827,10 +802,7 @@ namespace Imprink.Infrastructure.Migrations
|
||||
b.HasIndex("IsActive")
|
||||
.HasDatabaseName("IX_User_IsActive");
|
||||
|
||||
b.HasIndex("ModifiedAt")
|
||||
.HasDatabaseName("IX_User_ModifiedAt");
|
||||
|
||||
b.ToTable("User");
|
||||
b.ToTable("Users");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Users.UserRole", b =>
|
||||
@@ -850,7 +822,7 @@ namespace Imprink.Infrastructure.Migrations
|
||||
b.HasIndex("UserId")
|
||||
.HasDatabaseName("IX_UserRole_UserId");
|
||||
|
||||
b.ToTable("UserRole");
|
||||
b.ToTable("UserRoles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Orders.Order", b =>
|
||||
@@ -965,13 +937,15 @@ namespace Imprink.Infrastructure.Migrations
|
||||
.OnDelete(DeleteBehavior.Restrict)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("Imprink.Domain.Entities.Users.User", null)
|
||||
b.HasOne("Imprink.Domain.Entities.Users.User", "User")
|
||||
.WithMany("UserRoles")
|
||||
.HasForeignKey("UserId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Role");
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Imprink.Domain.Entities.Orders.Order", b =>
|
||||
|
||||
36
src/Imprink.Infrastructure/Repositories/RoleRepository.cs
Normal file
36
src/Imprink.Infrastructure/Repositories/RoleRepository.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using Imprink.Domain.Entities.Users;
|
||||
using Imprink.Domain.Repositories;
|
||||
using Imprink.Infrastructure.Database;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Imprink.Infrastructure.Repositories;
|
||||
|
||||
public class RoleRepository(ApplicationDbContext context) : IRoleRepository
|
||||
{
|
||||
public async Task<IEnumerable<Role>> GetAllRolesAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await context.Roles
|
||||
.AsNoTracking()
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<Role?> GetRoleByIdAsync(Guid roleId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await context.Roles
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(r => r.Id == roleId, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<Role?> GetRoleByNameAsync(string roleName, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await context.Roles
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(r => r.RoleName == roleName, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<bool> RoleExistsAsync(Guid roleId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await context.Roles
|
||||
.AnyAsync(r => r.Id == roleId, cancellationToken);
|
||||
}
|
||||
}
|
||||
181
src/Imprink.Infrastructure/Repositories/UserRepository.cs
Normal file
181
src/Imprink.Infrastructure/Repositories/UserRepository.cs
Normal file
@@ -0,0 +1,181 @@
|
||||
using Imprink.Domain.Entities.Users;
|
||||
using Imprink.Domain.Repositories;
|
||||
using Imprink.Infrastructure.Database;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Imprink.Infrastructure.Repositories;
|
||||
|
||||
public class UserRepository(ApplicationDbContext context) : IUserRepository
|
||||
{
|
||||
public async Task<User?> GetUserByIdAsync(string userId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await context.Users
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(u => u.Id == userId, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<User?> GetUserByEmailAsync(string email, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await context.Users
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(u => u.Email == email, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<User>> GetAllUsersAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await context.Users
|
||||
.AsNoTracking()
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<User>> GetActiveUsersAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await context.Users
|
||||
.AsNoTracking()
|
||||
.Where(u => u.IsActive)
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<User> CreateUserAsync(User user, CancellationToken cancellationToken = default)
|
||||
{
|
||||
context.Users.Add(user);
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
return user;
|
||||
}
|
||||
|
||||
public async Task<User> UpdateUserAsync(User user, CancellationToken cancellationToken = default)
|
||||
{
|
||||
context.Users.Update(user);
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
return user;
|
||||
}
|
||||
|
||||
public async Task DeleteUserAsync(string userId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var user = await context.Users.FindAsync(new object[] { userId }, cancellationToken);
|
||||
if (user != null)
|
||||
{
|
||||
context.Users.Remove(user);
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> UserExistsAsync(string userId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await context.Users
|
||||
.AnyAsync(u => u.Id == userId, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<bool> EmailExistsAsync(string email, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await context.Users
|
||||
.AnyAsync(u => u.Email == email, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<bool> ActivateUserAsync(string userId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var user = await context.Users.FindAsync(new object[] { userId }, cancellationToken);
|
||||
if (user == null) return false;
|
||||
|
||||
user.IsActive = true;
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> DeactivateUserAsync(string userId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var user = await context.Users.FindAsync(new object[] { userId }, cancellationToken);
|
||||
if (user == null) return false;
|
||||
|
||||
user.IsActive = false;
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task UpdateLastLoginAsync(string userId, DateTime loginTime, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var user = await context.Users.FindAsync(new object[] { userId }, cancellationToken);
|
||||
if (user != null)
|
||||
{
|
||||
user.LastLoginAt = loginTime;
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<User>> SearchUsersAsync(string searchTerm, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await context.Users
|
||||
.AsNoTracking()
|
||||
.Where(u => u.Email.Contains(searchTerm) ||
|
||||
u.FirstName.Contains(searchTerm) ||
|
||||
u.LastName.Contains(searchTerm))
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<User>> GetUsersByRoleAsync(Guid roleId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await context.Users
|
||||
.AsNoTracking()
|
||||
.Where(u => u.UserRoles.Any(ur => ur.RoleId == roleId))
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<(IEnumerable<User> Users, int TotalCount)> GetUsersPagedAsync(
|
||||
int pageNumber,
|
||||
int pageSize,
|
||||
string? searchTerm = null,
|
||||
bool? isActive = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var query = context.Users.AsNoTracking();
|
||||
|
||||
if (!string.IsNullOrEmpty(searchTerm))
|
||||
{
|
||||
query = query.Where(u => u.Email.Contains(searchTerm) ||
|
||||
u.FirstName.Contains(searchTerm) ||
|
||||
u.LastName.Contains(searchTerm));
|
||||
}
|
||||
|
||||
if (isActive.HasValue)
|
||||
{
|
||||
query = query.Where(u => u.IsActive == isActive.Value);
|
||||
}
|
||||
|
||||
var totalCount = await query.CountAsync(cancellationToken);
|
||||
|
||||
var users = await query
|
||||
.Skip((pageNumber - 1) * pageSize)
|
||||
.Take(pageSize)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return (users, totalCount);
|
||||
}
|
||||
|
||||
public async Task<User?> GetUserWithAddressesAsync(string userId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await context.Users
|
||||
.AsNoTracking()
|
||||
.Include(u => u.Addresses)
|
||||
.FirstOrDefaultAsync(u => u.Id == userId, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<User?> GetUserWithRolesAsync(string userId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await context.Users
|
||||
.AsNoTracking()
|
||||
.Include(u => u.UserRoles)
|
||||
.ThenInclude(ur => ur.Role)
|
||||
.FirstOrDefaultAsync(u => u.Id == userId, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<User?> GetUserWithAllRelatedDataAsync(string userId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await context.Users
|
||||
.AsNoTracking()
|
||||
.Include(u => u.Addresses)
|
||||
.Include(u => u.UserRoles)
|
||||
.ThenInclude(ur => ur.Role)
|
||||
.Include(u => u.Orders)
|
||||
.FirstOrDefaultAsync(u => u.Id == userId, cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using Imprink.Domain.Entities.Users;
|
||||
using Imprink.Domain.Repositories;
|
||||
using Imprink.Infrastructure.Database;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Imprink.Infrastructure.Repositories;
|
||||
|
||||
public class UserRoleRepository(ApplicationDbContext context) : IUserRoleRepository
|
||||
{
|
||||
public async Task<IEnumerable<Role>> GetUserRolesAsync(string userId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await context.UserRoles
|
||||
.AsNoTracking()
|
||||
.Where(ur => ur.UserId == userId)
|
||||
.Select(ur => ur.Role)
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<User>> GetUsersInRoleAsync(Guid roleId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await context.UserRoles
|
||||
.AsNoTracking()
|
||||
.Where(ur => ur.RoleId == roleId)
|
||||
.Select(ur => ur.User)
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<bool> IsUserInRoleAsync(string userId, Guid roleId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await context.UserRoles
|
||||
.AnyAsync(ur => ur.UserId == userId && ur.RoleId == roleId, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<UserRole?> GetUserRoleAsync(string userId, Guid roleId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await context.UserRoles
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(ur => ur.UserId == userId && ur.RoleId == roleId, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task AddUserRoleAsync(UserRole userRole, CancellationToken cancellationToken = default)
|
||||
{
|
||||
context.UserRoles.Add(userRole);
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task RemoveUserRoleAsync(UserRole userRole, CancellationToken cancellationToken = default)
|
||||
{
|
||||
context.UserRoles.Remove(userRole);
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<UserRole>> GetUserRolesByUserIdAsync(string userId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await context.UserRoles
|
||||
.AsNoTracking()
|
||||
.Where(ur => ur.UserId == userId)
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
30
src/Imprink.WebApi/Controllers/Users/UserController.cs
Normal file
30
src/Imprink.WebApi/Controllers/Users/UserController.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.Security.Claims;
|
||||
using Imprink.Domain.Common.Models;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Imprink.WebApi.Controllers.Users;
|
||||
|
||||
[ApiController]
|
||||
[Route("/api/[controller]")]
|
||||
public class UserController : ControllerBase
|
||||
{
|
||||
[Authorize]
|
||||
[HttpPost]
|
||||
public IActionResult SyncUser()
|
||||
{
|
||||
var claims = User.Claims;
|
||||
|
||||
var enumerable = claims as Claim[] ?? claims.ToArray();
|
||||
var user = new Auth0User
|
||||
{
|
||||
Sub = enumerable.FirstOrDefault(c => c.Type == "sub")?.Value ?? "",
|
||||
Name = enumerable.FirstOrDefault(c => c.Type == "name")?.Value ?? "",
|
||||
Nickname = enumerable.FirstOrDefault(c => c.Type == "nickname")?.Value ?? "",
|
||||
Email = enumerable.FirstOrDefault(c => c.Type == "email")?.Value ?? "",
|
||||
EmailVerified = enumerable.FirstOrDefault(c => c.Type == "email_verified")?.Value == "true"
|
||||
};
|
||||
|
||||
return Ok(user);
|
||||
}
|
||||
}
|
||||
@@ -31,7 +31,6 @@
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Controllers\Orders\" />
|
||||
<Folder Include="Controllers\Users\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -18,6 +18,9 @@ public static class Startup
|
||||
services.AddScoped<IProductRepository, ProductRepository>();
|
||||
services.AddScoped<IProductVariantRepository, ProductVariantRepository>();
|
||||
services.AddScoped<ICategoryRepository, CategoryRepository>();
|
||||
services.AddScoped<IRoleRepository, RoleRepository>();
|
||||
services.AddScoped<IUserRepository, UserRepository>();
|
||||
services.AddScoped<IUserRoleRepository, UserRoleRepository>();
|
||||
services.AddScoped<IUnitOfWork, UnitOfWork>();
|
||||
|
||||
services.AddDbContext<ApplicationDbContext>(options =>
|
||||
|
||||
Reference in New Issue
Block a user