mirror of
https://github.com/Shchoholiev/shopping-assistant-api.git
synced 2025-04-12 01:48:49 +00:00
Merge pull request #8 from Shchoholiev/bug/SA-116-fix-db-initialaizer
Bug/sa 116 fix db initialaizer
This commit is contained in:
commit
1ded33452e
@ -36,14 +36,12 @@ app.AddGlobalUserMiddleware();
|
|||||||
app.MapGraphQL();
|
app.MapGraphQL();
|
||||||
|
|
||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
/*
|
|
||||||
using var scope = app.Services.CreateScope();
|
// using var scope = app.Services.CreateScope();
|
||||||
var serviceProvider = scope.ServiceProvider;
|
// var serviceProvider = scope.ServiceProvider;
|
||||||
using var cancellationTokenSource = new CancellationTokenSource();
|
// var initializer = new DbInitialaizer(serviceProvider);
|
||||||
var cancellationToken = cancellationTokenSource.Token;
|
// await initializer.InitialaizeDb(CancellationToken.None);
|
||||||
var initializer = new DbInitialaizer(serviceProvider);
|
|
||||||
initializer.InitialaizeDb(cancellationToken);
|
|
||||||
*/
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|
||||||
public partial class Program { }
|
public partial class Program { }
|
@ -1,47 +1,33 @@
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using MongoDB.Bson;
|
using MongoDB.Bson;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
using ShoppingAssistantApi.Application.GlobalInstances;
|
|
||||||
using ShoppingAssistantApi.Application.IServices;
|
|
||||||
using ShoppingAssistantApi.Application.IServices.Identity;
|
using ShoppingAssistantApi.Application.IServices.Identity;
|
||||||
using ShoppingAssistantApi.Application.Models.CreateDtos;
|
|
||||||
using ShoppingAssistantApi.Application.Models.Dtos;
|
|
||||||
using ShoppingAssistantApi.Application.Models.Identity;
|
|
||||||
using ShoppingAssistantApi.Domain.Entities;
|
using ShoppingAssistantApi.Domain.Entities;
|
||||||
using ShoppingAssistantApi.Domain.Enums;
|
using ShoppingAssistantApi.Domain.Enums;
|
||||||
|
using ShoppingAssistantApi.Infrastructure.Services.Identity;
|
||||||
using ShoppingAssistantApi.Persistance.Database;
|
using ShoppingAssistantApi.Persistance.Database;
|
||||||
|
|
||||||
namespace ShoppingAssistantApi.Persistance.PersistanceExtentions;
|
namespace ShoppingAssistantApi.Persistance.PersistanceExtentions;
|
||||||
|
|
||||||
public class DbInitialaizer
|
public class DbInitialaizer
|
||||||
{
|
{
|
||||||
private readonly IUsersService _usersService;
|
|
||||||
|
|
||||||
private readonly IUserManager _userManager;
|
|
||||||
|
|
||||||
private readonly IRolesService _rolesService;
|
|
||||||
|
|
||||||
private readonly ITokensService _tokensService;
|
|
||||||
|
|
||||||
private readonly IWishlistsService _wishlistsService;
|
|
||||||
|
|
||||||
private readonly IMongoCollection<User> _userCollection;
|
private readonly IMongoCollection<User> _userCollection;
|
||||||
|
|
||||||
|
private readonly IMongoCollection<Role> _roleCollection;
|
||||||
|
|
||||||
private readonly IMongoCollection<Wishlist> _wishlistCollection;
|
private readonly IMongoCollection<Wishlist> _wishlistCollection;
|
||||||
|
|
||||||
private readonly IMongoCollection<Product> _productCollection;
|
private readonly IMongoCollection<Product> _productCollection;
|
||||||
|
|
||||||
public IEnumerable<RoleDto> Roles { get; set; }
|
private readonly PasswordHasher passwordHasher;
|
||||||
|
|
||||||
public DbInitialaizer(IServiceProvider serviceProvider)
|
public DbInitialaizer(IServiceProvider serviceProvider)
|
||||||
{
|
{
|
||||||
_usersService = serviceProvider.GetService<IUsersService>();
|
passwordHasher = new PasswordHasher(new Logger<PasswordHasher>(new LoggerFactory()));
|
||||||
_rolesService = serviceProvider.GetService<IRolesService>();
|
|
||||||
_userManager = serviceProvider.GetService<IUserManager>();
|
|
||||||
_tokensService = serviceProvider.GetService<ITokensService>();
|
|
||||||
_wishlistsService = serviceProvider.GetService<IWishlistsService>();
|
|
||||||
_wishlistCollection = serviceProvider.GetService<MongoDbContext>().Db.GetCollection<Wishlist>("Wishlists");
|
_wishlistCollection = serviceProvider.GetService<MongoDbContext>().Db.GetCollection<Wishlist>("Wishlists");
|
||||||
_userCollection = serviceProvider.GetService<MongoDbContext>().Db.GetCollection<User>("Users");
|
_userCollection = serviceProvider.GetService<MongoDbContext>().Db.GetCollection<User>("Users");
|
||||||
|
_roleCollection = serviceProvider.GetService<MongoDbContext>().Db.GetCollection<Role>("Roles");
|
||||||
_productCollection = serviceProvider.GetService<MongoDbContext>().Db.GetCollection<Product>("Product");
|
_productCollection = serviceProvider.GetService<MongoDbContext>().Db.GetCollection<Product>("Product");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,123 +41,140 @@ public class DbInitialaizer
|
|||||||
|
|
||||||
public async Task AddUsers(CancellationToken cancellationToken)
|
public async Task AddUsers(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var guestModel1 = new AccessGuestModel
|
var userRole = await (await _roleCollection.FindAsync(x => x.Name.Equals("User"))).FirstAsync();
|
||||||
|
var guestRole = await (await _roleCollection.FindAsync(x => x.Name.Equals("Guest"))).FirstAsync();
|
||||||
|
var adminRole = await (await _roleCollection.FindAsync(x => x.Name.Equals("Admin"))).FirstAsync();
|
||||||
|
|
||||||
|
var users = new User[]
|
||||||
{
|
{
|
||||||
GuestId = Guid.NewGuid(),
|
new User
|
||||||
|
{
|
||||||
|
Id = ObjectId.Parse("6533bb29c8c22b038c71cf46"),
|
||||||
|
GuestId = Guid.NewGuid(),
|
||||||
|
Roles = new List<Role> {guestRole},
|
||||||
|
CreatedById = ObjectId.Parse("6533bb29c8c22b038c71cf46"),
|
||||||
|
CreatedDateUtc = DateTime.UtcNow,
|
||||||
|
LastModifiedById = ObjectId.Parse("6533bb29c8c22b038c71cf46"),
|
||||||
|
LastModifiedDateUtc = DateTime.UtcNow,
|
||||||
|
IsDeleted = false
|
||||||
|
},
|
||||||
|
|
||||||
|
new User
|
||||||
|
{
|
||||||
|
Id = ObjectId.Parse("6533bde5755745116be42ce7"),
|
||||||
|
GuestId = Guid.NewGuid(),
|
||||||
|
Roles = new List<Role>
|
||||||
|
{
|
||||||
|
guestRole,
|
||||||
|
userRole
|
||||||
|
},
|
||||||
|
Phone = "+380953326869",
|
||||||
|
Email = "mykhailo.bilodid@nure.ua",
|
||||||
|
PasswordHash = this.passwordHasher.Hash("Yuiop12345"),
|
||||||
|
CreatedById = ObjectId.Parse("6533bde5755745116be42ce7"),
|
||||||
|
CreatedDateUtc = DateTime.UtcNow,
|
||||||
|
LastModifiedById = ObjectId.Parse("6533bde5755745116be42ce7"),
|
||||||
|
LastModifiedDateUtc = DateTime.UtcNow,
|
||||||
|
IsDeleted = false
|
||||||
|
},
|
||||||
|
|
||||||
|
new User
|
||||||
|
{
|
||||||
|
Id = ObjectId.Parse("6533bded80fbc6e96250575b"),
|
||||||
|
GuestId = Guid.NewGuid(),
|
||||||
|
Roles = new List<Role>
|
||||||
|
{
|
||||||
|
guestRole,
|
||||||
|
userRole,
|
||||||
|
adminRole
|
||||||
|
},
|
||||||
|
Phone = "+380953826869",
|
||||||
|
Email = "shopping.assistant.team@gmail.com",
|
||||||
|
PasswordHash = this.passwordHasher.Hash("Yuiop12345"),
|
||||||
|
CreatedById = ObjectId.Parse("6533bded80fbc6e96250575b"),
|
||||||
|
CreatedDateUtc = DateTime.UtcNow,
|
||||||
|
LastModifiedById = ObjectId.Parse("6533bded80fbc6e96250575b"),
|
||||||
|
LastModifiedDateUtc = DateTime.UtcNow,
|
||||||
|
IsDeleted = false },
|
||||||
|
|
||||||
|
new User
|
||||||
|
{
|
||||||
|
Id = ObjectId.Parse("6533bdf9efaca5bb0894f992"),
|
||||||
|
GuestId = Guid.NewGuid(),
|
||||||
|
Roles = new List<Role>
|
||||||
|
{
|
||||||
|
guestRole,
|
||||||
|
userRole
|
||||||
|
},
|
||||||
|
Phone = "+380983326869",
|
||||||
|
Email = "vitalii.krasnorutski@nure.ua",
|
||||||
|
PasswordHash = this.passwordHasher.Hash("Yuiop12345"),
|
||||||
|
CreatedById = ObjectId.Parse("6533bdf9efaca5bb0894f992"),
|
||||||
|
CreatedDateUtc = DateTime.UtcNow,
|
||||||
|
LastModifiedById = ObjectId.Parse("6533bdf9efaca5bb0894f992"),
|
||||||
|
LastModifiedDateUtc = DateTime.UtcNow,
|
||||||
|
IsDeleted = false },
|
||||||
|
|
||||||
|
new User
|
||||||
|
{
|
||||||
|
Id = ObjectId.Parse("6533be06d1b78a76c664ddae"),
|
||||||
|
GuestId = Guid.NewGuid(),
|
||||||
|
Roles = new List<Role>
|
||||||
|
{
|
||||||
|
guestRole,
|
||||||
|
userRole
|
||||||
|
},
|
||||||
|
Phone = "+380953326888",
|
||||||
|
Email = "serhii.shchoholiev@nure.ua",
|
||||||
|
PasswordHash = this.passwordHasher.Hash("Yuiop12345"),
|
||||||
|
CreatedById = ObjectId.Parse("6533be06d1b78a76c664ddae"),
|
||||||
|
CreatedDateUtc = DateTime.UtcNow,
|
||||||
|
LastModifiedById = ObjectId.Parse("6533be06d1b78a76c664ddae"),
|
||||||
|
LastModifiedDateUtc = DateTime.UtcNow,
|
||||||
|
IsDeleted = false }
|
||||||
};
|
};
|
||||||
|
|
||||||
var guestModel2 = new AccessGuestModel
|
await _userCollection.InsertManyAsync(users);
|
||||||
{
|
|
||||||
GuestId = Guid.NewGuid(),
|
|
||||||
};
|
|
||||||
|
|
||||||
var guestModel3 = new AccessGuestModel
|
|
||||||
{
|
|
||||||
GuestId = Guid.NewGuid(),
|
|
||||||
};
|
|
||||||
|
|
||||||
var guestModel4 = new AccessGuestModel
|
|
||||||
{
|
|
||||||
GuestId = Guid.NewGuid(),
|
|
||||||
};
|
|
||||||
|
|
||||||
var guestModel5 = new AccessGuestModel
|
|
||||||
{
|
|
||||||
GuestId = Guid.NewGuid(),
|
|
||||||
};
|
|
||||||
|
|
||||||
Task.WaitAll(
|
|
||||||
_userManager.AccessGuestAsync(guestModel1, cancellationToken),
|
|
||||||
_userManager.AccessGuestAsync(guestModel2, cancellationToken),
|
|
||||||
_userManager.AccessGuestAsync(guestModel3, cancellationToken),
|
|
||||||
_userManager.AccessGuestAsync(guestModel4, cancellationToken),
|
|
||||||
_userManager.AccessGuestAsync(guestModel5, cancellationToken)
|
|
||||||
);
|
|
||||||
|
|
||||||
var guests = await _usersService.GetUsersPageAsync(1, 4, cancellationToken);
|
|
||||||
var guestsResult = guests.Items.ToList();
|
|
||||||
|
|
||||||
var user1 = new UserDto
|
|
||||||
{
|
|
||||||
Id = guestsResult[0].Id,
|
|
||||||
GuestId = guestsResult[0].GuestId,
|
|
||||||
Roles = guestsResult[0].Roles,
|
|
||||||
Phone = "+380953326869",
|
|
||||||
Email = "mykhailo.bilodid@nure.ua",
|
|
||||||
Password = "Yuiop12345",
|
|
||||||
RefreshToken = _tokensService.GenerateRefreshToken(),
|
|
||||||
RefreshTokenExpiryDate = DateTime.Now.AddDays(7),
|
|
||||||
};
|
|
||||||
|
|
||||||
var user2 = new UserDto
|
|
||||||
{
|
|
||||||
Id = guestsResult[1].Id,
|
|
||||||
GuestId = guestsResult[1].GuestId,
|
|
||||||
Roles = guestsResult[1].Roles,
|
|
||||||
Phone = "+380953326888",
|
|
||||||
Email = "serhii.shchoholiev@nure.ua",
|
|
||||||
Password = "Yuiop12345",
|
|
||||||
RefreshToken = _tokensService.GenerateRefreshToken(),
|
|
||||||
RefreshTokenExpiryDate = DateTime.Now.AddDays(7),
|
|
||||||
};
|
|
||||||
|
|
||||||
var user3 = new UserDto
|
|
||||||
{
|
|
||||||
Id = guestsResult[2].Id,
|
|
||||||
GuestId = guestsResult[2].GuestId,
|
|
||||||
Roles = guestsResult[2].Roles,
|
|
||||||
Phone = "+380983326869",
|
|
||||||
Email = "vitalii.krasnorutski@nure.ua",
|
|
||||||
Password = "Yuiop12345",
|
|
||||||
RefreshToken = _tokensService.GenerateRefreshToken(),
|
|
||||||
RefreshTokenExpiryDate = DateTime.Now.AddDays(7),
|
|
||||||
};
|
|
||||||
|
|
||||||
var user4 = new UserDto
|
|
||||||
{
|
|
||||||
Id = guestsResult[3].Id,
|
|
||||||
GuestId = guestsResult[3].GuestId,
|
|
||||||
Roles = guestsResult[3].Roles,
|
|
||||||
Phone = "+380953826869",
|
|
||||||
Email = "shopping.assistant.team@gmail.com",
|
|
||||||
Password = "Yuiop12345",
|
|
||||||
RefreshToken = _tokensService.GenerateRefreshToken(),
|
|
||||||
RefreshTokenExpiryDate = DateTime.Now.AddDays(7),
|
|
||||||
};
|
|
||||||
|
|
||||||
GlobalUser.Id = ObjectId.Parse(user1.Id);
|
|
||||||
await _userManager.UpdateAsync(user1, cancellationToken);
|
|
||||||
|
|
||||||
GlobalUser.Id = ObjectId.Parse(user2.Id);
|
|
||||||
await _userManager.UpdateAsync(user2, cancellationToken);
|
|
||||||
|
|
||||||
GlobalUser.Id = ObjectId.Parse(user3.Id);
|
|
||||||
await _userManager.UpdateAsync(user3, cancellationToken);
|
|
||||||
|
|
||||||
GlobalUser.Id = ObjectId.Parse(user4.Id);
|
|
||||||
await _userManager.UpdateAsync(user4, cancellationToken);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task AddRoles(CancellationToken cancellationToken)
|
public async Task AddRoles(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var role1 = new RoleCreateDto
|
var roles = new Role[]
|
||||||
{
|
{
|
||||||
Name = "User"
|
new Role
|
||||||
};
|
{
|
||||||
|
Id = ObjectId.Parse("6533b5882e7867b8b21e7b27"),
|
||||||
|
Name = "User",
|
||||||
|
CreatedById = ObjectId.Parse("6533bded80fbc6e96250575b"),
|
||||||
|
CreatedDateUtc = DateTime.UtcNow,
|
||||||
|
LastModifiedById = ObjectId.Parse("6533bded80fbc6e96250575b"),
|
||||||
|
LastModifiedDateUtc = DateTime.UtcNow,
|
||||||
|
IsDeleted = false
|
||||||
|
},
|
||||||
|
|
||||||
var role2 = new RoleCreateDto
|
new Role
|
||||||
{
|
{
|
||||||
Name = "Admin"
|
Id = ObjectId.Parse("6533b591a7f31776cd2d50fc"),
|
||||||
};
|
Name = "Guest",
|
||||||
|
CreatedById = ObjectId.Parse("6533bded80fbc6e96250575b"),
|
||||||
|
CreatedDateUtc = DateTime.UtcNow,
|
||||||
|
LastModifiedById = ObjectId.Parse("6533bded80fbc6e96250575b"),
|
||||||
|
LastModifiedDateUtc = DateTime.UtcNow,
|
||||||
|
IsDeleted = false
|
||||||
|
},
|
||||||
|
|
||||||
var role3 = new RoleCreateDto
|
new Role
|
||||||
{
|
{
|
||||||
Name = "Guest"
|
Id = ObjectId.Parse("6533b59d1b09ab2618af5ff3"),
|
||||||
|
Name = "Admin",
|
||||||
|
CreatedById = ObjectId.Parse("6533bded80fbc6e96250575b"),
|
||||||
|
CreatedDateUtc = DateTime.UtcNow,
|
||||||
|
LastModifiedById = ObjectId.Parse("6533bded80fbc6e96250575b"),
|
||||||
|
LastModifiedDateUtc = DateTime.UtcNow,
|
||||||
|
IsDeleted = false
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
await _roleCollection.InsertManyAsync(roles);
|
||||||
var dto1 = await _rolesService.AddRoleAsync(role1, cancellationToken);
|
|
||||||
var dto2 = await _rolesService.AddRoleAsync(role2, cancellationToken);
|
|
||||||
var dto3 = await _rolesService.AddRoleAsync(role3, cancellationToken);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task AddWishlistsWithMessages(CancellationToken cancellationToken)
|
public async Task AddWishlistsWithMessages(CancellationToken cancellationToken)
|
||||||
@ -249,8 +252,8 @@ public class DbInitialaizer
|
|||||||
CreatedDateUtc = DateTime.UtcNow,
|
CreatedDateUtc = DateTime.UtcNow,
|
||||||
WasOpened = false,
|
WasOpened = false,
|
||||||
WishlistId = ObjectId.Parse("ab79cde6f69abcd3efab65cd")
|
WishlistId = ObjectId.Parse("ab79cde6f69abcd3efab65cd")
|
||||||
},
|
},
|
||||||
|
|
||||||
new Product()
|
new Product()
|
||||||
{
|
{
|
||||||
Name = "Apple MagSafe Battery Pack",
|
Name = "Apple MagSafe Battery Pack",
|
||||||
@ -268,7 +271,7 @@ public class DbInitialaizer
|
|||||||
WasOpened = false,
|
WasOpened = false,
|
||||||
WishlistId = ObjectId.Parse("ab79cde6f69abcd3efab65cd")
|
WishlistId = ObjectId.Parse("ab79cde6f69abcd3efab65cd")
|
||||||
},
|
},
|
||||||
|
|
||||||
new Product()
|
new Product()
|
||||||
{
|
{
|
||||||
Name = "Logitech K400 Plus Wireless Touch With Easy Media Control and Built-in Touchpad",
|
Name = "Logitech K400 Plus Wireless Touch With Easy Media Control and Built-in Touchpad",
|
||||||
@ -286,7 +289,7 @@ public class DbInitialaizer
|
|||||||
WasOpened = false,
|
WasOpened = false,
|
||||||
WishlistId = ObjectId.Parse("ab6c2c2d9edf39abcd1ef9ab")
|
WishlistId = ObjectId.Parse("ab6c2c2d9edf39abcd1ef9ab")
|
||||||
},
|
},
|
||||||
|
|
||||||
new Product()
|
new Product()
|
||||||
{
|
{
|
||||||
Name = "Logitech MX Anywhere 2S Wireless Mouse Use On Any Surface",
|
Name = "Logitech MX Anywhere 2S Wireless Mouse Use On Any Surface",
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\ShoppingAssistantApi.Application\ShoppingAssistantApi.Application.csproj" />
|
<ProjectReference Include="..\ShoppingAssistantApi.Application\ShoppingAssistantApi.Application.csproj" />
|
||||||
<ProjectReference Include="..\ShoppingAssistantApi.Domain\ShoppingAssistantApi.Domain.csproj" />
|
<ProjectReference Include="..\ShoppingAssistantApi.Domain\ShoppingAssistantApi.Domain.csproj" />
|
||||||
|
<ProjectReference Include="..\ShoppingAssistantApi.Infrastructure\ShoppingAssistantApi.Infrastructure.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
Loading…
Reference in New Issue
Block a user