feat : add new entites , befor add migration

release
Amir Hossein Khademi 2024-04-15 21:01:40 +03:30
parent aae924e77b
commit 27a6b10edf
21 changed files with 313 additions and 31 deletions

View File

@ -31,6 +31,15 @@
"FirstName": "همه کاره",
"LastName": "سیستم"
},
"Manager": {
"Username": "09128387004",
"Email": "mahanmasiha6@gmail.com",
"Password": "eF79o4P4BopCUbUK",
"Phone": "09128387004",
"RoleName": "Manager",
"FirstName": "ماهان",
"LastName": ""
},
"StorageSetting": {
"AccessKey": "979313b7-30fb-40ff-94d8-d0390d3fa876",
"SecretKey": "d37a1cc6acfea3a6f92c538ef0f6601f1edcdc9143942b6470e5d1032aa6bfe2",

View File

@ -184,7 +184,6 @@ public static class ServiceExtensions
}).AddEntityFrameworkStores<ApplicationContext>()
.AddDefaultTokenProviders()
.AddErrorDescriber<PersianIdentityErrorDescriber>();
;
}
public static void AddCustomApiVersioning(this IServiceCollection serviceCollection)

View File

@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!--<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
@ -12,9 +12,9 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.4.1" />
</ItemGroup>
</ItemGroup>-->
<!--<PropertyGroup>
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>10</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
@ -25,7 +25,7 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.3.1" />
</ItemGroup>-->
</ItemGroup>
<ItemGroup>
<Using Include="MD.PersianDateTime.Standard" />

View File

@ -10,7 +10,7 @@ public class AccountService : IAccountService
private readonly ICurrentUserService _currentUserService;
private readonly IRepositoryWrapper _repositoryWrapper;
private readonly ISmsService _smsService;
private readonly IUserService _userService;
private readonly IManagerUserService _managerUserService;
public AccountService(
UserManager<ApplicationUser> userManager,
@ -19,7 +19,7 @@ public class AccountService : IAccountService
ICurrentUserService currentUserService,
IRepositoryWrapper repositoryWrapper,
ISmsService smsService,
IUserService userService)
IManagerUserService managerUserService)
{
_userManager = userManager;
_userSignInManager = userSignInManager;
@ -27,7 +27,7 @@ public class AccountService : IAccountService
_currentUserService = currentUserService;
_repositoryWrapper = repositoryWrapper;
_smsService = smsService;
_userService = userService;
_managerUserService = managerUserService;
}
@ -69,7 +69,7 @@ public class AccountService : IAccountService
throw new AppException("شماره تلفن ارسالی اشتباه است");
var user = await _userManager.FindByNameAsync(newPhoneNumber);
if (user == null)
user = await _userService.CreateUserAsync(phoneNumber);
user = await _managerUserService.CreateManagerAsync(phoneNumber);
var token = await _userManager.GenerateTwoFactorTokenAsync(user, "Phone");
await _smsService.SendVerifyCodeAsync(newPhoneNumber, token);

View File

@ -7,16 +7,19 @@ public class UserService : IUserService
private readonly UserManager<ApplicationUser> _userManager;
private readonly RoleManager<ApplicationRole> _roleManager;
private readonly IExternalFilesService _externalFilesService;
private readonly IRepositoryWrapper _repositoryWrapper;
public UserService(ICurrentUserService currentUserService,
UserManager<ApplicationUser> userManager,
RoleManager<ApplicationRole> roleManager,
IExternalFilesService externalFilesService)
IExternalFilesService externalFilesService,
IRepositoryWrapper repositoryWrapper)
{
_currentUserService = currentUserService;
_userManager = userManager;
_roleManager = roleManager;
_externalFilesService = externalFilesService;
_repositoryWrapper = repositoryWrapper;
}
@ -258,14 +261,21 @@ public class UserService : IUserService
if (user == null)
throw new AppException("User NotFound", ApiResultStatusCode.NotFound);
var manager = await _repositoryWrapper.SetRepository<Manager>()
.TableNoTracking
.FirstOrDefaultAsync(m => m.UserId == userId, cancellationToken);
var currentVersion = await _externalFilesService.GetAdminChangeLogAsync(cancellationToken);
if (!(user.LatestVersionUsed < currentVersion.VersionNumber)) return currentVersion;
currentVersion.IsNewVersion = true;
user.LatestVersionUsed = currentVersion.VersionNumber;
await _userManager.UpdateAsync(user);
if (manager != null)
{
if (!(manager.LatestVersionUsed < currentVersion.VersionNumber)) return currentVersion;
currentVersion.IsNewVersion = true;
manager.LatestVersionUsed = currentVersion.VersionNumber;
_repositoryWrapper.SetRepository<Manager>()
.Update(manager);
await _repositoryWrapper.SaveChangesAsync(cancellationToken);
}
return currentVersion;
}

View File

@ -0,0 +1,20 @@
namespace NetinaShop.Domain.Dtos.SmallDtos;
public class CustomerSDto : BaseDto<CustomerSDto, Customer>
{
public string PhoneNumber { get; set; } = string.Empty;
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
public DateTime BirthDate { get; set; }
public Gender Gender { get; set; }
public SignUpStatus SignUpStatus { get; set; }
public string NationalId { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string RoleName { get; set; } = string.Empty;
public string FullName => FirstName + " " + LastName;
public List<Guid> RoleIds { get; set; } = new();
public long BirthDateTimeStamp => DateTimeExtensions.DateTimeToUnixTimeStamp(BirthDate);
}

View File

@ -0,0 +1,21 @@
namespace NetinaShop.Domain.Dtos.SmallDtos;
public class ManagerSDto : BaseDto<ManagerSDto, Manager>
{
public string PhoneNumber { get; set; } = string.Empty;
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
public DateTime BirthDate { get; set; }
public Gender Gender { get; set; }
public SignUpStatus SignUpStatus { get; set; }
public string NationalId { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public double LatestVersionUsed { get; set; }
public string RoleName { get; set; } = string.Empty;
public string FullName => FirstName + " " + LastName;
public List<Guid> RoleIds { get; set; } = new();
public long BirthDateTimeStamp => DateTimeExtensions.DateTimeToUnixTimeStamp(BirthDate);
}

View File

@ -0,0 +1,22 @@
namespace NetinaShop.Domain.Dtos.SmallDtos;
public class MarketerSDto : BaseDto<MarketerSDto, Marketer>
{
public string PhoneNumber { get; set; } = string.Empty;
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
public DateTime BirthDate { get; set; }
public Gender Gender { get; set; }
public SignUpStatus SignUpStatus { get; set; }
public string NationalId { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string FatherName { get; set; } = string.Empty;
public string Shaba { get; set; } = string.Empty;
public string RoleName { get; set; } = string.Empty;
public string FullName => FirstName + " " + LastName;
public List<Guid> RoleIds { get; set; } = new();
public long BirthDateTimeStamp => DateTimeExtensions.DateTimeToUnixTimeStamp(BirthDate);
}

View File

@ -71,7 +71,7 @@ public partial class Order : ApiEntity
public string DiscountCode { get; internal set; } = string.Empty;
public Guid UserId { get; internal set; }
public ApplicationUser? User { get; internal set; }
public Customer? User { get; internal set; }
public OrderDelivery? OrderDelivery { get; set; }

View File

@ -10,11 +10,11 @@ public class ApplicationUser : IdentityUser<Guid>
public string LastName { get; set; } = string.Empty;
public string NationalId { get; set; } = string.Empty;
public double LatestVersionUsed { get; set; }
public DateTime BirthDate { get; set; }
public Gender Gender { get; set; }
public SignUpStatus SignUpStatus { get; set; }
public List<UserAddress> Addresses { get; set; } = new();
public Manager? Manager { get; set; }
public Customer? Customer { get; set; }
public Marketer? Marketer { get; set; }
}

View File

@ -0,0 +1,11 @@
namespace NetinaShop.Domain.Entities.Users;
[AdaptTwoWays("[name]SDto", IgnoreAttributes = new[] { typeof(AdaptIgnoreAttribute) }, MapType = MapType.Map | MapType.MapToTarget)]
[AdaptTo("[name]SDto", IgnoreAttributes = new[] { typeof(AdaptIgnoreAttribute) }, MapType = MapType.Projection)]
[GenerateMapper]
public class Customer : ApiEntity
{
public List<UserAddress> Addresses { get; set; } = new();
public Guid UserId { get; set; }
public ApplicationUser? User { get; set; }
}

View File

@ -0,0 +1,11 @@
namespace NetinaShop.Domain.Entities.Users;
[AdaptTwoWays("[name]SDto", IgnoreAttributes = new[] { typeof(AdaptIgnoreAttribute) }, MapType = MapType.Map | MapType.MapToTarget)]
[AdaptTo("[name]SDto", IgnoreAttributes = new[] { typeof(AdaptIgnoreAttribute) }, MapType = MapType.Projection)]
[GenerateMapper]
public class Manager : ApiEntity
{
public double LatestVersionUsed { get; set; }
public Guid UserId { get; set; }
public ApplicationUser? User { get; set; }
}

View File

@ -0,0 +1,12 @@
namespace NetinaShop.Domain.Entities.Users;
[AdaptTwoWays("[name]SDto", IgnoreAttributes = new[] { typeof(AdaptIgnoreAttribute) }, MapType = MapType.Map | MapType.MapToTarget)]
[AdaptTo("[name]SDto", IgnoreAttributes = new[] { typeof(AdaptIgnoreAttribute) }, MapType = MapType.Projection)]
[GenerateMapper]
public class Marketer : ApiEntity
{
public string FatherName { get; set; } = string.Empty;
public string Shaba { get; set; } = string.Empty;
public Guid UserId { get; set; }
public ApplicationUser? User { get; set; }
}

View File

@ -0,0 +1,6 @@
namespace NetinaShop.Domain.Mappers
{
public static partial class CustomerMapper
{
}
}

View File

@ -0,0 +1,98 @@
using System;
using System.Linq.Expressions;
using NetinaShop.Domain.Dtos.SmallDtos;
using NetinaShop.Domain.Entities.Users;
namespace NetinaShop.Domain.Mappers
{
public static partial class ManagerMapper
{
public static Manager AdaptToManager(this ManagerSDto p1)
{
return p1 == null ? null : new Manager()
{
LatestVersionUsed = p1.LatestVersionUsed,
FirstName = p1.FirstName,
LastName = p1.LastName,
NationalId = p1.NationalId,
BirthDate = p1.BirthDate,
Gender = p1.Gender,
SignUpStatus = p1.SignUpStatus,
Id = p1.Id,
Email = p1.Email,
PhoneNumber = p1.PhoneNumber
};
}
public static Manager AdaptTo(this ManagerSDto p2, Manager p3)
{
if (p2 == null)
{
return null;
}
Manager result = p3 ?? new Manager();
result.LatestVersionUsed = p2.LatestVersionUsed;
result.FirstName = p2.FirstName;
result.LastName = p2.LastName;
result.NationalId = p2.NationalId;
result.BirthDate = p2.BirthDate;
result.Gender = p2.Gender;
result.SignUpStatus = p2.SignUpStatus;
result.Id = p2.Id;
result.Email = p2.Email;
result.PhoneNumber = p2.PhoneNumber;
return result;
}
public static ManagerSDto AdaptToSDto(this Manager p4)
{
return p4 == null ? null : new ManagerSDto()
{
PhoneNumber = p4.PhoneNumber,
FirstName = p4.FirstName,
LastName = p4.LastName,
BirthDate = p4.BirthDate,
Gender = p4.Gender,
SignUpStatus = p4.SignUpStatus,
NationalId = p4.NationalId,
Email = p4.Email,
LatestVersionUsed = p4.LatestVersionUsed,
Id = p4.Id
};
}
public static ManagerSDto AdaptTo(this Manager p5, ManagerSDto p6)
{
if (p5 == null)
{
return null;
}
ManagerSDto result = p6 ?? new ManagerSDto();
result.PhoneNumber = p5.PhoneNumber;
result.FirstName = p5.FirstName;
result.LastName = p5.LastName;
result.BirthDate = p5.BirthDate;
result.Gender = p5.Gender;
result.SignUpStatus = p5.SignUpStatus;
result.NationalId = p5.NationalId;
result.Email = p5.Email;
result.LatestVersionUsed = p5.LatestVersionUsed;
result.Id = p5.Id;
return result;
}
public static Expression<Func<Manager, ManagerSDto>> ProjectToSDto => p7 => new ManagerSDto()
{
PhoneNumber = p7.PhoneNumber,
FirstName = p7.FirstName,
LastName = p7.LastName,
BirthDate = p7.BirthDate,
Gender = p7.Gender,
SignUpStatus = p7.SignUpStatus,
NationalId = p7.NationalId,
Email = p7.Email,
LatestVersionUsed = p7.LatestVersionUsed,
Id = p7.Id
};
}
}

View File

@ -0,0 +1,6 @@
namespace NetinaShop.Domain.Mappers
{
public static partial class MarketerMapper
{
}
}

View File

@ -79,5 +79,44 @@ public class MapsterRegister : IRegister
.IgnoreNullValues(false)
.TwoWays();
ConfigUserMappers(config);
}
private void ConfigUserMappers(TypeAdapterConfig config)
{
config.NewConfig<Manager, ManagerSDto>()
.Map("PhoneNumber", o => o.User != null ? o.User.PhoneNumber : string.Empty)
.Map("FirstName", o => o.User != null ? o.User.FirstName : string.Empty)
.Map("LastName", o => o.User != null ? o.User.LastName : string.Empty)
.Map("BirthDate", o => o.User != null ? o.User.BirthDate : DateTime.MinValue)
.Map("Gender", o => o.User != null ? o.User.Gender : 0)
.Map("SignUpStatus", o => o.User != null ? o.User.SignUpStatus : 0)
.Map("NationalId", o => o.User != null ? o.User.NationalId : string.Empty)
.Map("Email", o => o.User != null ? o.User.Email : string.Empty)
.TwoWays();
config.NewConfig<Marketer, MarketerSDto>()
.Map("PhoneNumber", o => o.User != null ? o.User.PhoneNumber : string.Empty)
.Map("FirstName", o => o.User != null ? o.User.FirstName : string.Empty)
.Map("LastName", o => o.User != null ? o.User.LastName : string.Empty)
.Map("BirthDate", o => o.User != null ? o.User.BirthDate : DateTime.MinValue)
.Map("Gender", o => o.User != null ? o.User.Gender : 0)
.Map("SignUpStatus", o => o.User != null ? o.User.SignUpStatus : 0)
.Map("NationalId", o => o.User != null ? o.User.NationalId : string.Empty)
.Map("Email", o => o.User != null ? o.User.Email : string.Empty)
.TwoWays();
config.NewConfig<Customer, CustomerSDto>()
.Map("PhoneNumber", o => o.User != null ? o.User.PhoneNumber : string.Empty)
.Map("FirstName", o => o.User != null ? o.User.FirstName : string.Empty)
.Map("LastName", o => o.User != null ? o.User.LastName : string.Empty)
.Map("BirthDate", o => o.User != null ? o.User.BirthDate : DateTime.MinValue)
.Map("Gender", o => o.User != null ? o.User.Gender : 0)
.Map("SignUpStatus", o => o.User != null ? o.User.SignUpStatus : 0)
.Map("NationalId", o => o.User != null ? o.User.NationalId : string.Empty)
.Map("Email", o => o.User != null ? o.User.Email : string.Empty)
.TwoWays();
}
}

View File

@ -9,6 +9,7 @@ public class SiteSettings
public string StorageBaseUrl { get; set; } = string.Empty;
public RedisSettings MasterRedisConfiguration { get; set; } = new RedisSettings();
public UserSetting UserSetting { get; set; } = new UserSetting();
public UserSetting Manager { get; set; } = new UserSetting();
public string KaveNegarApiKey { get; set; } = string.Empty;
public StorageSettings StorageSetting { get; set; } = new StorageSettings();
}

View File

@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!--<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
@ -14,9 +14,9 @@
<PackageReference Include="MediatR" Version="12.2.0" />
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="8.0.3" />
<PackageReference Include="PropertyChanged.Fody" Version="4.1.0" />
</ItemGroup>
</ItemGroup>-->
<!--<PropertyGroup>
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>10</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
@ -32,7 +32,7 @@
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="5.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.0" />
<PackageReference Include="PropertyChanged.Fody" Version="4.1.0" />
</ItemGroup>-->
</ItemGroup>
<ItemGroup>

View File

@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System.Reflection.Emit;
namespace NetinaShop.Repository.Models;
@ -15,7 +16,6 @@ public class ApplicationContext : IdentityDbContext<ApplicationUser, Application
_projectAssembly = options.GetExtension<DbContextOptionCustomExtensions>().ProjectAssembly;
}
protected override void OnModelCreating(ModelBuilder builder)
{
var stopwatch = new Stopwatch();
@ -31,6 +31,7 @@ public class ApplicationContext : IdentityDbContext<ApplicationUser, Application
builder.RegisterEntityTypeConfiguration(entitiesAssembly);
builder.AddPluralizingTableNameConvention();
builder.AddRestrictDeleteBehaviorConvention();
//builder.AddSequentialGuidForIdConvention();
}

View File

@ -8,6 +8,7 @@ public class DbInitializerService : IDbInitializerService
private readonly IOptionsSnapshot<SiteSettings> _adminUserSeedOptions;
private readonly ApplicationContext _context;
private readonly ILogger<DbInitializerService> _logger;
private readonly IRepositoryWrapper _repositoryWrapper;
private readonly RoleManager<ApplicationRole> _roleManager;
private readonly UserManager<ApplicationUser> _userManager;
@ -16,13 +17,15 @@ public class DbInitializerService : IDbInitializerService
RoleManager<ApplicationRole> roleManager,
UserManager<ApplicationUser> userManager,
IOptionsSnapshot<SiteSettings> adminUserSeedOptions,
ILogger<DbInitializerService> logger)
ILogger<DbInitializerService> logger,
IRepositoryWrapper repositoryWrapper)
{
_context = context;
_roleManager = roleManager;
_userManager = userManager;
_adminUserSeedOptions = adminUserSeedOptions;
_logger = logger;
_repositoryWrapper = repositoryWrapper;
}
public void Initialize()
@ -44,6 +47,7 @@ public class DbInitializerService : IDbInitializerService
{
await SeedRoles();
var seedAdmin = _adminUserSeedOptions.Value.UserSetting;
var manager = _adminUserSeedOptions.Value.Manager;
var user = await _userManager.FindByNameAsync(seedAdmin.Username);
if (user == null)
{
@ -61,26 +65,38 @@ public class DbInitializerService : IDbInitializerService
BirthDate = DateTime.Now.AddYears(-23)
};
var adminUserResult = await _userManager.CreateAsync(adminUser, seedAdmin.Password);
_repositoryWrapper.SetRepository<Manager>()
.Add(new Manager
{
UserId = adminUser.Id
});
await _repositoryWrapper.SaveChangesAsync(default);
if (adminUserResult.Succeeded) await _userManager.AddToRoleAsync(adminUser, seedAdmin.RoleName);
}
var mahanUser = await _userManager.FindByNameAsync("09128387004");
var mahanUser = await _userManager.FindByNameAsync(manager.Username);
if (mahanUser == null)
{
mahanUser = new ApplicationUser
{
UserName = "09128387004",
Email = "mahanmasiha6@gmail.com",
UserName = manager.Username,
Email = manager.Email,
EmailConfirmed = true,
LockoutEnabled = true,
FirstName = "ماهان",
LastName = "",
FirstName = manager.FirstName,
LastName = manager.LastName,
Gender = Gender.Male,
PhoneNumberConfirmed = true,
PhoneNumber = "09128387004",
PhoneNumber = manager.Phone,
BirthDate = DateTime.Now.AddYears(-23)
};
var adminUserResult = await _userManager.CreateAsync(mahanUser, seedAdmin.Password);
_repositoryWrapper.SetRepository<Manager>()
.Add(new Manager
{
UserId = mahanUser.Id
});
await _repositoryWrapper.SaveChangesAsync(default);
if (adminUserResult.Succeeded) await _userManager.AddToRoleAsync(mahanUser, "Manager");
}
}