diff --git a/ShoppingAssistantApi.Api/ApiExtentions/GraphQlExtention.cs b/ShoppingAssistantApi.Api/ApiExtentions/GraphQlExtention.cs index 1d67391..9a214e5 100644 --- a/ShoppingAssistantApi.Api/ApiExtentions/GraphQlExtention.cs +++ b/ShoppingAssistantApi.Api/ApiExtentions/GraphQlExtention.cs @@ -16,6 +16,7 @@ public static class GraphQlExtention .AddTypeExtension() .AddTypeExtension() .AddTypeExtension() + .AddTypeExtension() .AddAuthorization() .InitializeOnStartup(keepWarm: true); diff --git a/ShoppingAssistantApi.Api/Mutations/WishlistsMutation.cs b/ShoppingAssistantApi.Api/Mutations/WishlistsMutation.cs new file mode 100644 index 0000000..9c195fb --- /dev/null +++ b/ShoppingAssistantApi.Api/Mutations/WishlistsMutation.cs @@ -0,0 +1,17 @@ +using ShoppingAssistantApi.Application.IServices; +using ShoppingAssistantApi.Application.Models.CreateDtos; +using ShoppingAssistantApi.Application.Models.Dtos; + +namespace ShoppingAssistantApi.Api.Mutations; + +[ExtendObjectType(OperationTypeNames.Mutation)] +public class WishlistsMutation +{ + public Task StartPersonalWishlist(WishlistCreateDto dto, CancellationToken cancellationToken, + [Service] IWishlistsService wishlistsService) + => wishlistsService.StartPersonalWishlistAsync(dto, cancellationToken); + + public Task AddMessageToPersonalWishlist(string wishlistId, MessageCreateDto dto, CancellationToken cancellationToken, + [Service] IWishlistsService wishlistsService) + => wishlistsService.AddMessageToPersonalWishlistAsync(wishlistId, dto, cancellationToken); +} diff --git a/ShoppingAssistantApi.Persistance/PersistanceExtentions/DbInitialaizer.cs b/ShoppingAssistantApi.Persistance/PersistanceExtentions/DbInitialaizer.cs index 0753d58..5832f7a 100644 --- a/ShoppingAssistantApi.Persistance/PersistanceExtentions/DbInitialaizer.cs +++ b/ShoppingAssistantApi.Persistance/PersistanceExtentions/DbInitialaizer.cs @@ -1,41 +1,42 @@ 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; 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 IMongoCollection _wishlistCollection; public IEnumerable Roles { get; set; } public DbInitialaizer(IServiceProvider serviceProvider) { - this._usersService = serviceProvider.GetService(); - this._rolesService = serviceProvider.GetService(); - this._userManager = serviceProvider.GetService(); - this._tokensService = serviceProvider.GetService(); + _usersService = serviceProvider.GetService(); + _rolesService = serviceProvider.GetService(); + _userManager = serviceProvider.GetService(); + _tokensService = serviceProvider.GetService(); + _wishlistCollection = serviceProvider.GetService().Db.GetCollection("Wishlists"); } - public async - Task -InitialaizeDb(CancellationToken cancellationToken) + public async Task InitialaizeDb(CancellationToken cancellationToken) { - await this.AddRoles(cancellationToken); - await this.AddUsers(cancellationToken); + await AddRoles(cancellationToken); + await AddUsers(cancellationToken); + await AddWishlistsWithMessages(cancellationToken); } public async Task AddUsers(CancellationToken cancellationToken) @@ -73,7 +74,7 @@ InitialaizeDb(CancellationToken cancellationToken) _userManager.AccessGuestAsync(guestModel5, cancellationToken) ); - var guests = await this._usersService.GetUsersPageAsync(1, 4, cancellationToken); + var guests = await _usersService.GetUsersPageAsync(1, 4, cancellationToken); var guestsResult = guests.Items.ToList(); var user1 = new UserDto @@ -123,7 +124,7 @@ InitialaizeDb(CancellationToken cancellationToken) RefreshToken = _tokensService.GenerateRefreshToken(), RefreshTokenExpiryDate = DateTime.Now.AddDays(7), }; - + GlobalUser.Id = ObjectId.Parse(user1.Id); await _userManager.UpdateAsync(user1, cancellationToken); @@ -158,4 +159,54 @@ InitialaizeDb(CancellationToken cancellationToken) var dto2 = await _rolesService.AddRoleAsync(role2, cancellationToken); var dto3 = await _rolesService.AddRoleAsync(role3, cancellationToken); } + + public async Task AddWishlistsWithMessages(CancellationToken cancellationToken) + { + var usersPage = await _usersService.GetUsersPageAsync(1, 2, cancellationToken); + var userList = usersPage.Items.ToList(); + + var wishlists = new Wishlist[] + { + new Wishlist + { + Name = "Grandma's Birthday Gift", + Type = WishlistTypes.Gift.ToString(), + UserId = ObjectId.Parse(userList[0].Id), + Messages = new Message[] + { + new Message + { + Text = "Prompt", + Role = MessageRoles.User.ToString(), + }, + new Message + { + Text = "Answer", + Role = MessageRoles.Application.ToString(), + }, + } + }, + new Wishlist + { + Name = "Gaming PC", + Type = WishlistTypes.Product.ToString(), + UserId = ObjectId.Parse(userList[1].Id), + Messages = new Message[] + { + new Message + { + Text = "Prompt", + Role = MessageRoles.User.ToString(), + }, + new Message + { + Text = "Answer", + Role = MessageRoles.Application.ToString(), + }, + } + } + }; + + await _wishlistCollection.InsertManyAsync(wishlists); + } } diff --git a/ShoppingAssistantApi.Tests/Tests/WishlistsTests.cs b/ShoppingAssistantApi.Tests/Tests/WishlistsTests.cs new file mode 100644 index 0000000..e513dc5 --- /dev/null +++ b/ShoppingAssistantApi.Tests/Tests/WishlistsTests.cs @@ -0,0 +1,95 @@ +using ShoppingAssistantApi.Tests.TestExtentions; +using System.Net.Http.Headers; +using System.Net; +using System.Text; +using Xunit; +using Newtonsoft.Json; +using ShoppingAssistantApi.Application.Models.Dtos; +using ShoppingAssistantApi.Domain.Enums; + +namespace ShoppingAssistantApi.Tests.Tests; + +[Collection("Tests")] +public class WishlistsTests : IClassFixture> +{ + private readonly HttpClient _httpClient; + + public WishlistsTests(TestingFactory factory) + { + _httpClient = factory.CreateClient(); + factory.InitialaizeData().GetAwaiter().GetResult(); + } + + [Fact] + public async Task StartPersonalWishlistAndAddMessageAsync_ValidWishlistAndMessageModels_ReturnsNewWishlistAndMessageModels() + { + var tokensModel = await AccessExtention.CreateGuest(new Guid().ToString(), _httpClient); + _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokensModel.AccessToken); + var user = await UserExtention.GetCurrentUser(_httpClient); + + var startPersonalWishlistMutation = new + { + query = "mutation startPersonalWishlist($dto: WishlistCreateDtoInput!) { startPersonalWishlist (dto: $dto) { id, name, type, createdById } }", + variables = new + { + dto = new + { + firstMessageText = "First message", + type = WishlistTypes.Product.ToString() + } + } + }; + + var jsonPayload = JsonConvert.SerializeObject(startPersonalWishlistMutation); + var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json"); + + using var startPersonalWishlistResponse = await _httpClient.PostAsync("graphql", content); + startPersonalWishlistResponse.EnsureSuccessStatusCode(); + Assert.Equal(HttpStatusCode.OK, startPersonalWishlistResponse.StatusCode); + + var responseString = await startPersonalWishlistResponse.Content.ReadAsStringAsync(); + var document = JsonConvert.DeserializeObject(responseString); + + var wishlistId = (string) document.data.startPersonalWishlist.id; + var wishlistCreatedById = (string) document.data.startPersonalWishlist.createdById; + var wishlistType = (string) document.data.startPersonalWishlist.type; + var wishlistName = (string) document.data.startPersonalWishlist.name; + + Assert.Equal(user.Id, wishlistCreatedById); + Assert.Equal(WishlistTypes.Product.ToString(), wishlistType); + Assert.Equal($"{WishlistTypes.Product} Search", wishlistName); + + const string MESSAGE_TEXT = "Second Message"; + + var addMessageToPersonalWishlistMutation = new + { + query = "mutation addMessageToPersonalWishlist($wishlistId: String!, $dto: MessageCreateDtoInput!) { addMessageToPersonalWishlist (wishlistId: $wishlistId, dto: $dto) { role, text, createdById } }", + variables = new + { + wishlistId = wishlistId, + dto = new + { + text = MESSAGE_TEXT, + } + } + }; + + jsonPayload = JsonConvert.SerializeObject(addMessageToPersonalWishlistMutation); + content = new StringContent(jsonPayload, Encoding.UTF8, "application/json"); + + using var addMessageToPersonalWishlistResponse = await _httpClient.PostAsync("graphql", content); + addMessageToPersonalWishlistResponse.EnsureSuccessStatusCode(); + Assert.Equal(HttpStatusCode.OK, addMessageToPersonalWishlistResponse.StatusCode); + + responseString = await addMessageToPersonalWishlistResponse.Content.ReadAsStringAsync(); + document = JsonConvert.DeserializeObject(responseString); + + var messageRole = (string) document.data.addMessageToPersonalWishlist.role; + var messageText = (string) document.data.addMessageToPersonalWishlist.text; + var messageCreatedById = (string) document.data.addMessageToPersonalWishlist.createdById; + + Assert.Equal(MessageRoles.User.ToString(), messageRole); + Assert.Equal(MESSAGE_TEXT, messageText); + Assert.Equal(user.Id, messageCreatedById); + } +}