add ability to add product to wishlist

This commit is contained in:
cuqmbr 2023-10-15 14:04:57 +03:00
parent 1412c07fa2
commit 2096df2c34
Signed by: cuqmbr
GPG Key ID: 2D72ED98B6CB200F
6 changed files with 184 additions and 19 deletions

View File

@ -15,6 +15,10 @@ public class WishlistsMutation
[Service] IWishlistsService wishlistsService) [Service] IWishlistsService wishlistsService)
=> wishlistsService.AddMessageToPersonalWishlistAsync(wishlistId, dto, cancellationToken); => wishlistsService.AddMessageToPersonalWishlistAsync(wishlistId, dto, cancellationToken);
public Task<ProductDto> AddProductToPersonalWishlistAsync(string wishlistId, ProductCreateDto dto, CancellationToken cancellationToken,
[Service] IWishlistsService wishlistsService)
=> wishlistsService.AddProductToPersonalWishlistAsync(wishlistId, dto, cancellationToken);
public Task<WishlistDto> DeletePersonalWishlistAsync(string wishlistId, CancellationToken cancellationToken, public Task<WishlistDto> DeletePersonalWishlistAsync(string wishlistId, CancellationToken cancellationToken,
[Service] IWishlistsService wishlistsService) [Service] IWishlistsService wishlistsService)
=> wishlistsService.DeletePersonalWishlistAsync(wishlistId, cancellationToken); => wishlistsService.DeletePersonalWishlistAsync(wishlistId, cancellationToken);

View File

@ -16,6 +16,8 @@ public interface IWishlistsService
Task<PagedList<MessageDto>> GetMessagesPageFromPersonalWishlistAsync(string wishlistId, int pageNumber, int pageSize, CancellationToken cancellationToken); Task<PagedList<MessageDto>> GetMessagesPageFromPersonalWishlistAsync(string wishlistId, int pageNumber, int pageSize, CancellationToken cancellationToken);
Task<ProductDto> AddProductToPersonalWishlistAsync(string wishlistId, ProductCreateDto dto, CancellationToken cancellationToken);
Task<PagedList<ProductDto>> GetProductsPageFromPersonalWishlistAsync(string wishlistId, int pageNumber, int pageSize, CancellationToken cancellationToken); Task<PagedList<ProductDto>> GetProductsPageFromPersonalWishlistAsync(string wishlistId, int pageNumber, int pageSize, CancellationToken cancellationToken);
Task<WishlistDto> DeletePersonalWishlistAsync(string wishlistId, CancellationToken cancellationToken); Task<WishlistDto> DeletePersonalWishlistAsync(string wishlistId, CancellationToken cancellationToken);

View File

@ -13,6 +13,4 @@ public class ProductCreateDto
public required string[] ImagesUrls { get; set; } public required string[] ImagesUrls { get; set; }
public required bool WasOpened { get; set; } public required bool WasOpened { get; set; }
public required string WishlistId { get; set; }
} }

View File

@ -18,7 +18,7 @@ public class WishlistsService : IWishlistsService
private readonly IMessagesRepository _messagesRepository; private readonly IMessagesRepository _messagesRepository;
private readonly IProductsRepository _productRepository; private readonly IProductsRepository _productsRepository;
private readonly IMapper _mapper; private readonly IMapper _mapper;
@ -26,7 +26,7 @@ public class WishlistsService : IWishlistsService
{ {
_wishlistsRepository = wishlistRepository; _wishlistsRepository = wishlistRepository;
_messagesRepository = messageRepository; _messagesRepository = messageRepository;
_productRepository = productRepository; _productsRepository = productRepository;
_mapper = mapper; _mapper = mapper;
} }
@ -116,6 +116,26 @@ public class WishlistsService : IWishlistsService
return new PagedList<MessageDto>(dtos, pageNumber, pageSize, count); return new PagedList<MessageDto>(dtos, pageNumber, pageSize, count);
} }
public async Task<ProductDto> AddProductToPersonalWishlistAsync(string wishlistId, ProductCreateDto dto, CancellationToken cancellationToken)
{
var newProduct = _mapper.Map<Product>(dto);
if (!ObjectId.TryParse(wishlistId, out var wishlistObjectId))
{
throw new InvalidDataException("Provided id is invalid.");
}
await TryGetPersonalWishlist(wishlistObjectId, cancellationToken);
newProduct.CreatedById = (ObjectId) GlobalUser.Id;
newProduct.CreatedDateUtc = DateTime.UtcNow;
newProduct.WishlistId = wishlistObjectId;
var createdProduct = await _productsRepository.AddAsync(newProduct, cancellationToken);
return _mapper.Map<ProductDto>(createdProduct);
}
public async Task<PagedList<ProductDto>> GetProductsPageFromPersonalWishlistAsync(string wishlistId, int pageNumber, int pageSize, CancellationToken cancellationToken) public async Task<PagedList<ProductDto>> GetProductsPageFromPersonalWishlistAsync(string wishlistId, int pageNumber, int pageSize, CancellationToken cancellationToken)
{ {
if (!ObjectId.TryParse(wishlistId, out var wishlistObjectId)) if (!ObjectId.TryParse(wishlistId, out var wishlistObjectId))
@ -125,15 +145,10 @@ public class WishlistsService : IWishlistsService
await TryGetPersonalWishlist(wishlistObjectId, cancellationToken); await TryGetPersonalWishlist(wishlistObjectId, cancellationToken);
var entities = await _productRepository.GetPageAsync(pageNumber, pageSize, x => x.WishlistId == wishlistObjectId, cancellationToken); var entities = await _productsRepository.GetPageAsync(pageNumber, pageSize, x => x.WishlistId == wishlistObjectId, cancellationToken);
foreach (var e in entities)
{
Console.WriteLine(e.Name);
}
var dtos = _mapper.Map<List<ProductDto>>(entities); var dtos = _mapper.Map<List<ProductDto>>(entities);
var count = await _productRepository.GetCountAsync(x => x.WishlistId == wishlistObjectId, cancellationToken); var count = await _productsRepository.GetCountAsync(x => x.WishlistId == wishlistObjectId, cancellationToken);
return new PagedList<ProductDto>(dtos, pageNumber, pageSize, count); return new PagedList<ProductDto>(dtos, pageNumber, pageSize, count);
} }

View File

@ -221,6 +221,51 @@ public class WishlistsTests : IClassFixture<TestingFactory<Program>>
Assert.Equal(user.Id, (string) firstMessageInPage.createdById); Assert.Equal(user.Id, (string) firstMessageInPage.createdById);
} }
[Fact]
public async Task AddProductToPersonalWishlist_ValidMessageModel_ReturnsNewProductModel()
{
var tokensModel = await AccessExtention.Login(WISHLIST_TESTING_USER_EMAIL, WISHLIST_TESTING_USER_PASSWORD, _httpClient);
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokensModel.AccessToken);
var user = await UserExtention.GetCurrentUser(_httpClient);
var mutation = new
{
query = "mutation addProductToPersonalWishlist($wishlistId: String!, $dto: ProductCreateDtoInput!) { addProductToPersonalWishlist (wishlistId: $wishlistId, dto: $dto) { url, name, description, rating, imagesUrls, wasOpened } }",
variables = new
{
wishlistId = WISHLIST_TESTING_VALID_WISHLIST_ID,
dto = new
{
url = "https://www.amazon.com/url",
name = "Generic name",
description = "Generic description",
rating = 4.8,
imagesUrls = new string[]
{
"https://www.amazon.com/image-url-1",
"https://www.amazon.com/image-url-2"
},
wasOpened = false
}
}
};
var jsonPayload = JsonConvert.SerializeObject(mutation);
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
using var response = await _httpClient.PostAsync("graphql", content);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var responseString = await response.Content.ReadAsStringAsync();
var document = JsonConvert.DeserializeObject<dynamic>(responseString);
Assert.Equal("https://www.amazon.com/url", (string) document.data.addProductToPersonalWishlist.url);
Assert.Equal("Generic name", (string) document.data.addProductToPersonalWishlist.name);
Assert.Equal("Generic description", (string) document.data.addProductToPersonalWishlist.description);
Assert.Equal(4.8, (double) document.data.addProductToPersonalWishlist.rating);
Assert.Equal("https://www.amazon.com/image-url-1", (string) document.data.addProductToPersonalWishlist.imagesUrls[0]);
}
[Fact] [Fact]
public async Task GetProductsPageFromPersonalWishlist_ValidPageNumberAndSizeValidWishlistIdOrAuthorizedAccess_ReturnsPage() public async Task GetProductsPageFromPersonalWishlist_ValidPageNumberAndSizeValidWishlistIdOrAuthorizedAccess_ReturnsPage()
{ {
@ -435,7 +480,7 @@ public class WishlistsTests : IClassFixture<TestingFactory<Program>>
} }
[Fact] [Fact]
public async Task AddMessageToPersonalWishlist_InvalidMessageModel_ReturnsInternalServerError() public async Task AddMessageToPersonalWishlist_InvalidWishlistId_ReturnsInternalServerError()
{ {
var tokensModel = await AccessExtention.Login(WISHLIST_TESTING_USER_EMAIL, WISHLIST_TESTING_USER_PASSWORD, _httpClient); var tokensModel = await AccessExtention.Login(WISHLIST_TESTING_USER_EMAIL, WISHLIST_TESTING_USER_PASSWORD, _httpClient);
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokensModel.AccessToken); _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokensModel.AccessToken);
@ -463,6 +508,35 @@ public class WishlistsTests : IClassFixture<TestingFactory<Program>>
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode); Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
} }
[Fact]
public async Task AddMessageToPersonalWishlist_UnAuthorizedAccess_ReturnsInternalServerError()
{
var tokensModel = await AccessExtention.Login(WISHLIST_TESTING_USER_EMAIL, WISHLIST_TESTING_USER_PASSWORD, _httpClient);
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokensModel.AccessToken);
var user = await UserExtention.GetCurrentUser(_httpClient);
const string MESSAGE_TEXT = "Second Message";
var mutation = new
{
query = "mutation addMessageToPersonalWishlist($wishlistId: String!, $dto: MessageCreateDtoInput!) { addMessageToPersonalWishlist (wishlistId: $wishlistId, dto: $dto) { role, text, createdById } }",
variables = new
{
wishlistId = WISHLIST_TESTING_OTHER_USER_WISHLIST_ID,
dto = new
{
text = MESSAGE_TEXT,
}
}
};
var jsonPayload = JsonConvert.SerializeObject(mutation);
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
using var response = await _httpClient.PostAsync("graphql", content);
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
}
[Fact] [Fact]
public async Task GetMessagesPageFromPersonalWishlist_InValidPageNumber_ReturnsEmptyList() public async Task GetMessagesPageFromPersonalWishlist_InValidPageNumber_ReturnsEmptyList()
{ {
@ -587,6 +661,78 @@ public class WishlistsTests : IClassFixture<TestingFactory<Program>>
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode); Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
} }
[Fact]
public async Task AddProductToPersonalWishlist_InValidWishlistId_RturnsInternalServerError()
{
var tokensModel = await AccessExtention.Login(WISHLIST_TESTING_USER_EMAIL, WISHLIST_TESTING_USER_PASSWORD, _httpClient);
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokensModel.AccessToken);
var user = await UserExtention.GetCurrentUser(_httpClient);
var mutation = new
{
query = "mutation addProductToPersonalWishlist($wishlistId: String!, $dto: ProductCreateDtoInput!) { addProductToPersonalWishlist (wishlistId: $wishlistId, dto: $dto) { url, name, description, rating, imagesUrls, wasOpened } }",
variables = new
{
wishlistId = WISHLIST_TESTING_INVALID_WISHLIST_ID,
dto = new
{
url = "https://www.amazon.com/url",
name = "Generic name",
description = "Generic description",
rating = 4.8,
imagesUrls = new string[]
{
"https://www.amazon.com/image-url-1",
"https://www.amazon.com/image-url-2"
},
wasOpened = false
}
}
};
var jsonPayload = JsonConvert.SerializeObject(mutation);
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
using var response = await _httpClient.PostAsync("graphql", content);
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
}
[Fact]
public async Task AddProductToPersonalWishlist_UnAuthorizedAccess_RturnsInternalServerError()
{
var tokensModel = await AccessExtention.Login(WISHLIST_TESTING_USER_EMAIL, WISHLIST_TESTING_USER_PASSWORD, _httpClient);
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokensModel.AccessToken);
var user = await UserExtention.GetCurrentUser(_httpClient);
var mutation = new
{
query = "mutation addProductToPersonalWishlist($wishlistId: String!, $dto: ProductCreateDtoInput!) { addProductToPersonalWishlist (wishlistId: $wishlistId, dto: $dto) { url, name, description, rating, imagesUrls, wasOpened } }",
variables = new
{
wishlistId = WISHLIST_TESTING_OTHER_USER_WISHLIST_ID,
dto = new
{
url = "https://www.amazon.com/url",
name = "Generic name",
description = "Generic description",
rating = 4.8,
imagesUrls = new string[]
{
"https://www.amazon.com/image-url-1",
"https://www.amazon.com/image-url-2"
},
wasOpened = false
}
}
};
var jsonPayload = JsonConvert.SerializeObject(mutation);
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
using var response = await _httpClient.PostAsync("graphql", content);
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
}
[Fact] [Fact]
public async Task GetProductsPageFromPersonalWishlist_InValidPageNumber_ReturnsInternalServerError() public async Task GetProductsPageFromPersonalWishlist_InValidPageNumber_ReturnsInternalServerError()
{ {