From 8ae360b7e022258c9e6616b6fb4f0f55b8c00972 Mon Sep 17 00:00:00 2001 From: Mykhailo Bilodid Date: Sat, 21 Oct 2023 16:52:54 +0300 Subject: [PATCH] SA-116 initialaizer fixed --- .../PersistanceExtentions/DbInitialaizer.cs | 267 +++++++++--------- 1 file changed, 134 insertions(+), 133 deletions(-) diff --git a/ShoppingAssistantApi.Persistance/PersistanceExtentions/DbInitialaizer.cs b/ShoppingAssistantApi.Persistance/PersistanceExtentions/DbInitialaizer.cs index f96d491..af50479 100644 --- a/ShoppingAssistantApi.Persistance/PersistanceExtentions/DbInitialaizer.cs +++ b/ShoppingAssistantApi.Persistance/PersistanceExtentions/DbInitialaizer.cs @@ -1,12 +1,7 @@ using Microsoft.Extensions.DependencyInjection; using MongoDB.Bson; using MongoDB.Driver; -using ShoppingAssistantApi.Application.GlobalInstances; -using ShoppingAssistantApi.Application.IServices; 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.Enums; using ShoppingAssistantApi.Persistance.Database; @@ -15,34 +10,23 @@ namespace ShoppingAssistantApi.Persistance.PersistanceExtentions; 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 _userCollection; + private readonly IMongoCollection _roleCollection; + private readonly IMongoCollection _wishlistCollection; - + private readonly IMongoCollection _productCollection; - public IEnumerable Roles { get; set; } + private readonly IPasswordHasher _passwordHasher; - public DbInitialaizer(IServiceProvider serviceProvider) + public DbInitialaizer(IServiceProvider serviceProvider, IPasswordHasher passwordHasher) { - _usersService = serviceProvider.GetService(); - _rolesService = serviceProvider.GetService(); - _userManager = serviceProvider.GetService(); - _tokensService = serviceProvider.GetService(); - _wishlistsService = serviceProvider.GetService(); _wishlistCollection = serviceProvider.GetService().Db.GetCollection("Wishlists"); _userCollection = serviceProvider.GetService().Db.GetCollection("Users"); + _roleCollection = serviceProvider.GetService().Db.GetCollection("Roles"); _productCollection = serviceProvider.GetService().Db.GetCollection("Product"); + _passwordHasher = passwordHasher; } public async Task InitialaizeDb(CancellationToken cancellationToken) @@ -55,123 +39,140 @@ public class DbInitialaizer 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 = {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 = + { + 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 = + { + 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 = + { + 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 = + { + 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 - { - 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); + await _userCollection.InsertManyAsync(users); } 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 - { - Name = "Admin" - }; + new Role + { + 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 - { - Name = "Guest" + new Role + { + Id = ObjectId.Parse("6533b59d1b09ab2618af5ff3"), + Name = "Admin", + CreatedById = ObjectId.Parse("6533bded80fbc6e96250575b"), + CreatedDateUtc = DateTime.UtcNow, + LastModifiedById = ObjectId.Parse("6533bded80fbc6e96250575b"), + LastModifiedDateUtc = DateTime.UtcNow, + IsDeleted = false + }, }; - - var dto1 = await _rolesService.AddRoleAsync(role1, cancellationToken); - var dto2 = await _rolesService.AddRoleAsync(role2, cancellationToken); - var dto3 = await _rolesService.AddRoleAsync(role3, cancellationToken); + await _roleCollection.InsertManyAsync(roles); } public async Task AddWishlistsWithMessages(CancellationToken cancellationToken) @@ -249,8 +250,8 @@ public class DbInitialaizer CreatedDateUtc = DateTime.UtcNow, WasOpened = false, WishlistId = ObjectId.Parse("ab79cde6f69abcd3efab65cd") - }, - + }, + new Product() { Name = "Apple MagSafe Battery Pack", @@ -268,7 +269,7 @@ public class DbInitialaizer WasOpened = false, WishlistId = ObjectId.Parse("ab79cde6f69abcd3efab65cd") }, - + new Product() { Name = "Logitech K400 Plus Wireless Touch With Easy Media Control and Built-in Touchpad", @@ -286,7 +287,7 @@ public class DbInitialaizer WasOpened = false, WishlistId = ObjectId.Parse("ab6c2c2d9edf39abcd1ef9ab") }, - + new Product() { Name = "Logitech MX Anywhere 2S Wireless Mouse Use On Any Surface",