From e9c3b57fc4e95efad1903fb75071e8dca37e6e2e Mon Sep 17 00:00:00 2001 From: stasex Date: Thu, 12 Oct 2023 20:55:34 +0300 Subject: [PATCH 1/5] added product entity and initial data for it --- .../Entities/Product.cs | 19 ++++++++ .../PersistanceExtentions/DbInitialaizer.cs | 45 +++++++++++++++++++ .../Repositories/ProductsRepository.cs | 9 ++++ 3 files changed, 73 insertions(+) create mode 100644 ShoppingAssistantApi.Domain/Entities/Product.cs create mode 100644 ShoppingAssistantApi.Persistance/Repositories/ProductsRepository.cs diff --git a/ShoppingAssistantApi.Domain/Entities/Product.cs b/ShoppingAssistantApi.Domain/Entities/Product.cs new file mode 100644 index 0000000..c581196 --- /dev/null +++ b/ShoppingAssistantApi.Domain/Entities/Product.cs @@ -0,0 +1,19 @@ +using MongoDB.Bson; +using ShoppingAssistantApi.Domain.Common; + +namespace ShoppingAssistantApi.Domain.Entities; + +public class Product : EntityBase +{ + public ObjectId WishlistId { get; set; } + + public string? Url { get; set; } + + public string? Name { get; set; } + + public string? Description { get; set; } + + public double Rating { get; set; } + + public string[]? ImagesUrls { get; set; } +} \ No newline at end of file diff --git a/ShoppingAssistantApi.Persistance/PersistanceExtentions/DbInitialaizer.cs b/ShoppingAssistantApi.Persistance/PersistanceExtentions/DbInitialaizer.cs index a63c8f7..651e374 100644 --- a/ShoppingAssistantApi.Persistance/PersistanceExtentions/DbInitialaizer.cs +++ b/ShoppingAssistantApi.Persistance/PersistanceExtentions/DbInitialaizer.cs @@ -23,9 +23,13 @@ public class DbInitialaizer private readonly ITokensService _tokensService; + private readonly IWishlistsService _wishlistsService; + private readonly IMongoCollection _userCollection; private readonly IMongoCollection _wishlistCollection; + + private readonly IMongoCollection _productCollection; public IEnumerable Roles { get; set; } @@ -35,8 +39,10 @@ public class DbInitialaizer _rolesService = serviceProvider.GetService(); _userManager = serviceProvider.GetService(); _tokensService = serviceProvider.GetService(); + _wishlistsService = serviceProvider.GetService(); _wishlistCollection = serviceProvider.GetService().Db.GetCollection("Wishlists"); _userCollection = serviceProvider.GetService().Db.GetCollection("Users"); + _productCollection = serviceProvider.GetService().Db.GetCollection("Product"); } public async Task InitialaizeDb(CancellationToken cancellationToken) @@ -213,4 +219,43 @@ public class DbInitialaizer await _wishlistCollection.InsertManyAsync(wishlists); } + + public async Task AddProducts(CancellationToken cancellationToken) + { + var wishList1 = await _wishlistCollection.FindAsync(w => w.Name == "Gaming PC"); + var wishList2 = await _wishlistCollection.FindAsync(w => w.Name == "Generic Wishlist Name"); + + var products = new Product[] + { + new Product() + { + Id = ObjectId.Parse("ab6c2c2d9edf39abcd1ef9ab"), + Url = "url", + Name = "Thermaltake Glacier", + Description = "Something", + Rating = 4.1, + ImagesUrls = new string[] + { + "url1", + "url2" + } + }, + + new Product() + { + Id = ObjectId.Parse("ab6c2c2d9edf39abcd1ef9ab"), + Url = "url", + Name = "Mac", + Description = "very very cool laptop", + Rating = 4.9, + ImagesUrls = new string[] + { + "url1", + "url2" + } + } + + }; + + } } diff --git a/ShoppingAssistantApi.Persistance/Repositories/ProductsRepository.cs b/ShoppingAssistantApi.Persistance/Repositories/ProductsRepository.cs new file mode 100644 index 0000000..5de13b9 --- /dev/null +++ b/ShoppingAssistantApi.Persistance/Repositories/ProductsRepository.cs @@ -0,0 +1,9 @@ +using ShoppingAssistantApi.Domain.Entities; +using ShoppingAssistantApi.Persistance.Database; + +namespace ShoppingAssistantApi.Persistance.Repositories; + +public class ProductsRepository : BaseRepository +{ + public ProductsRepository(MongoDbContext db) : base(db, "Products") { } +} \ No newline at end of file From 80c9bc2b3289ce000ef86733f41b8bde85f72895 Mon Sep 17 00:00:00 2001 From: stasex Date: Sun, 15 Oct 2023 16:27:29 +0300 Subject: [PATCH 2/5] Small changes for DbInitialaizer with products collection --- .../PersistanceExtentions/DbInitialaizer.cs | 74 +++++++++++++------ 1 file changed, 53 insertions(+), 21 deletions(-) diff --git a/ShoppingAssistantApi.Persistance/PersistanceExtentions/DbInitialaizer.cs b/ShoppingAssistantApi.Persistance/PersistanceExtentions/DbInitialaizer.cs index 651e374..9d88eb1 100644 --- a/ShoppingAssistantApi.Persistance/PersistanceExtentions/DbInitialaizer.cs +++ b/ShoppingAssistantApi.Persistance/PersistanceExtentions/DbInitialaizer.cs @@ -222,40 +222,72 @@ public class DbInitialaizer public async Task AddProducts(CancellationToken cancellationToken) { - var wishList1 = await _wishlistCollection.FindAsync(w => w.Name == "Gaming PC"); - var wishList2 = await _wishlistCollection.FindAsync(w => w.Name == "Generic Wishlist Name"); - var products = new Product[] { new Product() { - Id = ObjectId.Parse("ab6c2c2d9edf39abcd1ef9ab"), - Url = "url", - Name = "Thermaltake Glacier", - Description = "Something", - Rating = 4.1, + Name = "Thermaltake Glacier 360 Liquid-Cooled PC", + Description = "Cool PC for any task!", + Rating = 4.3, + Url = "https://www.amazon.com/Thermaltake-Liquid-Cooled-ToughRAM-Computer-S3WT-B550-G36-LCS/dp" + + "/B09FYNM2GW/ref=sr_1_1?crid=391KAS4JFJSFF&keywords=gaming%2Bpc&qid=1697132083&sprefix=gaming%2Bpc%2Caps%2C209&sr=8-1&th=1", ImagesUrls = new string[] { - "url1", - "url2" - } + "https://m.media-amazon.com/images/I/61cXu9yGldL._AC_SL1200_.jpg", + "https://m.media-amazon.com/images/I/615gxSGp42L._AC_SL1200_.jpg" + }, + CreatedDateUtc = DateTime.UtcNow }, new Product() { - Id = ObjectId.Parse("ab6c2c2d9edf39abcd1ef9ab"), - Url = "url", - Name = "Mac", - Description = "very very cool laptop", - Rating = 4.9, + Name = "Apple MagSafe Battery Pack", + Description = "Portable Charger with Fast Charging Capability, Power Bank Compatible with iPhone", + Rating = 4.3, + Url = "https://www.amazon.com/Apple-MJWY3AM-A-MagSafe-Battery/dp/" + + "B099BWY7WT/ref=sr_1_1?keywords=apple+power+bank&qid=1697375350&sr=8-1", ImagesUrls = new string[] { - "url1", - "url2" - } - } + "https://m.media-amazon.com/images/I/418SjFMB1wL._AC_SX679_.jpg", + "https://m.media-amazon.com/images/I/51v4pgChtLL._AC_SX679_.jpg", + "https://m.media-amazon.com/images/I/61mJ0z7uYQL._AC_SX679_.jpg" + }, + CreatedDateUtc = DateTime.UtcNow + }, + new Product() + { + Name = "Logitech K400 Plus Wireless Touch With Easy Media Control and Built-in Touchpad", + Description = "Reliable membrane keyboard with touchpad!", + Rating = 4.5, + Url = "https://www.amazon.com/Logitech-Wireless-Keyboard-Touchpad-PC-connected/dp/B014EUQOGK/" + + "ref=sr_1_11?crid=BU2PHZKHKD65&keywords=keyboard+wireless&qid=1697375559&sprefix=keyboard+wir%2Caps%2C195&sr=8-11", + ImagesUrls = new string[] + { + "https://m.media-amazon.com/images/I/51yjnWJ5urL._AC_SX466_.jpg", + "https://m.media-amazon.com/images/I/71al70zP7QL._AC_SX466_.jpg", + "https://m.media-amazon.com/images/I/71+JXDDY01L._AC_SX466_.jpg" + }, + CreatedDateUtc = DateTime.UtcNow + }, + + new Product() + { + Name = "Logitech MX Anywhere 2S Wireless Mouse Use On Any Surface", + Description = "Cross computer control: Game changing capacity to navigate seamlessly on three computers," + + " and copy paste text, images, and files from one to the other using Logitech Flow", + Rating = 4.6, + Url = "https://www.amazon.com/Logitech-Hyper-Fast-Scrolling-Rechargeable-Computers/dp/B08P2JFPQC/ref=sr_1_8?" + + "crid=2BL6Z14W2TPP3&keywords=mouse%2Bwireless&qid=1697375784&sprefix=mousewireless%2Caps%2C197&sr=8-8&th=1", + ImagesUrls = new string[] + { + "https://m.media-amazon.com/images/I/6170mJHIsYL._AC_SX466_.jpg", + "https://m.media-amazon.com/images/I/71a5As76MDL._AC_SX466_.jpg" + }, + CreatedDateUtc = DateTime.UtcNow + } }; - + + await _productCollection.InsertManyAsync(products); } } From 004033f3076cb42c402eda941a289a5cfb59bac3 Mon Sep 17 00:00:00 2001 From: stasex Date: Sun, 15 Oct 2023 16:38:20 +0300 Subject: [PATCH 3/5] added the missing property to the product entity --- ShoppingAssistantApi.Domain/Entities/Product.cs | 2 ++ .../PersistanceExtentions/DbInitialaizer.cs | 12 ++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/ShoppingAssistantApi.Domain/Entities/Product.cs b/ShoppingAssistantApi.Domain/Entities/Product.cs index c581196..f3b2ccd 100644 --- a/ShoppingAssistantApi.Domain/Entities/Product.cs +++ b/ShoppingAssistantApi.Domain/Entities/Product.cs @@ -16,4 +16,6 @@ public class Product : EntityBase public double Rating { get; set; } public string[]? ImagesUrls { get; set; } + + public bool WasOpened { get; set; } } \ No newline at end of file diff --git a/ShoppingAssistantApi.Persistance/PersistanceExtentions/DbInitialaizer.cs b/ShoppingAssistantApi.Persistance/PersistanceExtentions/DbInitialaizer.cs index 9d88eb1..dbba926 100644 --- a/ShoppingAssistantApi.Persistance/PersistanceExtentions/DbInitialaizer.cs +++ b/ShoppingAssistantApi.Persistance/PersistanceExtentions/DbInitialaizer.cs @@ -236,7 +236,8 @@ public class DbInitialaizer "https://m.media-amazon.com/images/I/61cXu9yGldL._AC_SL1200_.jpg", "https://m.media-amazon.com/images/I/615gxSGp42L._AC_SL1200_.jpg" }, - CreatedDateUtc = DateTime.UtcNow + CreatedDateUtc = DateTime.UtcNow, + WasOpened = false }, new Product() @@ -252,7 +253,8 @@ public class DbInitialaizer "https://m.media-amazon.com/images/I/51v4pgChtLL._AC_SX679_.jpg", "https://m.media-amazon.com/images/I/61mJ0z7uYQL._AC_SX679_.jpg" }, - CreatedDateUtc = DateTime.UtcNow + CreatedDateUtc = DateTime.UtcNow, + WasOpened = false }, new Product() @@ -268,7 +270,8 @@ public class DbInitialaizer "https://m.media-amazon.com/images/I/71al70zP7QL._AC_SX466_.jpg", "https://m.media-amazon.com/images/I/71+JXDDY01L._AC_SX466_.jpg" }, - CreatedDateUtc = DateTime.UtcNow + CreatedDateUtc = DateTime.UtcNow, + WasOpened = false }, new Product() @@ -284,7 +287,8 @@ public class DbInitialaizer "https://m.media-amazon.com/images/I/6170mJHIsYL._AC_SX466_.jpg", "https://m.media-amazon.com/images/I/71a5As76MDL._AC_SX466_.jpg" }, - CreatedDateUtc = DateTime.UtcNow + CreatedDateUtc = DateTime.UtcNow, + WasOpened = false } }; From ba876b30fe78f2f94614a6fb257394aedc0d2809 Mon Sep 17 00:00:00 2001 From: stasex Date: Sun, 15 Oct 2023 20:08:21 +0300 Subject: [PATCH 4/5] added a method call --- .../PersistanceExtentions/DbInitialaizer.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/ShoppingAssistantApi.Persistance/PersistanceExtentions/DbInitialaizer.cs b/ShoppingAssistantApi.Persistance/PersistanceExtentions/DbInitialaizer.cs index dbba926..b64f784 100644 --- a/ShoppingAssistantApi.Persistance/PersistanceExtentions/DbInitialaizer.cs +++ b/ShoppingAssistantApi.Persistance/PersistanceExtentions/DbInitialaizer.cs @@ -50,6 +50,7 @@ public class DbInitialaizer await AddRoles(cancellationToken); await AddUsers(cancellationToken); await AddWishlistsWithMessages(cancellationToken); + await AddProducts(cancellationToken); } public async Task AddUsers(CancellationToken cancellationToken) From 250e493fc24a0a782af460deabd9c77b3c49c69f Mon Sep 17 00:00:00 2001 From: stasex Date: Sun, 15 Oct 2023 20:26:34 +0300 Subject: [PATCH 5/5] added a method call --- .../PersistanceExtentions/DbInitialaizer.cs | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/ShoppingAssistantApi.Persistance/PersistanceExtentions/DbInitialaizer.cs b/ShoppingAssistantApi.Persistance/PersistanceExtentions/DbInitialaizer.cs index dbba926..f96d491 100644 --- a/ShoppingAssistantApi.Persistance/PersistanceExtentions/DbInitialaizer.cs +++ b/ShoppingAssistantApi.Persistance/PersistanceExtentions/DbInitialaizer.cs @@ -50,6 +50,7 @@ public class DbInitialaizer await AddRoles(cancellationToken); await AddUsers(cancellationToken); await AddWishlistsWithMessages(cancellationToken); + await AddProducts(cancellationToken); } public async Task AddUsers(CancellationToken cancellationToken) @@ -192,11 +193,17 @@ public class DbInitialaizer { Text = "Prompt", Role = MessageRoles.User.ToString(), + WishlistId = ObjectId.Parse("ab79cde6f69abcd3efab65cd"), + CreatedById = user1.Id, + CreatedDateUtc = DateTime.UtcNow }, new Message { Text = "Answer", Role = MessageRoles.Application.ToString(), + WishlistId = ObjectId.Parse("ab79cde6f69abcd3efab65cd"), + CreatedById = user1.Id, + CreatedDateUtc = DateTime.UtcNow }, } }, @@ -212,6 +219,9 @@ public class DbInitialaizer { Text = "Prompt", Role = MessageRoles.User.ToString(), + WishlistId = ObjectId.Parse("ab6c2c2d9edf39abcd1ef9ab"), + CreatedById = user1.Id, + CreatedDateUtc = DateTime.UtcNow } } } @@ -237,7 +247,8 @@ public class DbInitialaizer "https://m.media-amazon.com/images/I/615gxSGp42L._AC_SL1200_.jpg" }, CreatedDateUtc = DateTime.UtcNow, - WasOpened = false + WasOpened = false, + WishlistId = ObjectId.Parse("ab79cde6f69abcd3efab65cd") }, new Product() @@ -254,7 +265,8 @@ public class DbInitialaizer "https://m.media-amazon.com/images/I/61mJ0z7uYQL._AC_SX679_.jpg" }, CreatedDateUtc = DateTime.UtcNow, - WasOpened = false + WasOpened = false, + WishlistId = ObjectId.Parse("ab79cde6f69abcd3efab65cd") }, new Product() @@ -271,7 +283,8 @@ public class DbInitialaizer "https://m.media-amazon.com/images/I/71+JXDDY01L._AC_SX466_.jpg" }, CreatedDateUtc = DateTime.UtcNow, - WasOpened = false + WasOpened = false, + WishlistId = ObjectId.Parse("ab6c2c2d9edf39abcd1ef9ab") }, new Product() @@ -288,7 +301,8 @@ public class DbInitialaizer "https://m.media-amazon.com/images/I/71a5As76MDL._AC_SX466_.jpg" }, CreatedDateUtc = DateTime.UtcNow, - WasOpened = false + WasOpened = false, + WishlistId = ObjectId.Parse("ab6c2c2d9edf39abcd1ef9ab") } };