mirror of
https://github.com/Shchoholiev/shopping-assistant-api.git
synced 2025-04-04 16:49:36 +00:00
add ability to view personal wishlists (get pages of messages starting from last messages)
This commit is contained in:
parent
e26177528b
commit
4f67175ff7
@ -7,11 +7,11 @@ namespace ShoppingAssistantApi.Api.Mutations;
|
||||
[ExtendObjectType(OperationTypeNames.Mutation)]
|
||||
public class WishlistsMutation
|
||||
{
|
||||
public Task<WishlistDto> StartPersonalWishlist(WishlistCreateDto dto, CancellationToken cancellationToken,
|
||||
public Task<WishlistDto> StartPersonalWishlistAsync(WishlistCreateDto dto, CancellationToken cancellationToken,
|
||||
[Service] IWishlistsService wishlistsService)
|
||||
=> wishlistsService.StartPersonalWishlistAsync(dto, cancellationToken);
|
||||
|
||||
public Task<MessageDto> AddMessageToPersonalWishlist(string wishlistId, MessageCreateDto dto, CancellationToken cancellationToken,
|
||||
public Task<MessageDto> AddMessageToPersonalWishlistAsync(string wishlistId, MessageCreateDto dto, CancellationToken cancellationToken,
|
||||
[Service] IWishlistsService wishlistsService)
|
||||
=> wishlistsService.AddMessageToPersonalWishlistAsync(wishlistId, dto, cancellationToken);
|
||||
|
||||
|
@ -9,12 +9,17 @@ namespace ShoppingAssistantApi.Api.Queries;
|
||||
public class WishlistsQuery
|
||||
{
|
||||
[Authorize]
|
||||
public Task<PagedList<WishlistDto>> GetPersonalWishlistsPageAsync(int pageNumber, int pageSize, CancellationToken cancellationToken,
|
||||
[Service] IWishlistsService wishlistsService)
|
||||
public Task<PagedList<WishlistDto>> GetPersonalWishlistsPageAsync(int pageNumber, int pageSize,
|
||||
CancellationToken cancellationToken, [Service] IWishlistsService wishlistsService)
|
||||
=> wishlistsService.GetPersonalWishlistsPageAsync(pageNumber, pageSize, cancellationToken);
|
||||
|
||||
[Authorize]
|
||||
public Task<WishlistDto> GetPersonalWishlistAsync(string wishlistId, CancellationToken cancellationToken,
|
||||
[Service] IWishlistsService wishlistsService)
|
||||
[Service] IWishlistsService wishlistsService)
|
||||
=> wishlistsService.GetPersonalWishlistAsync(wishlistId, cancellationToken);
|
||||
|
||||
[Authorize]
|
||||
public Task<PagedList<MessageDto>> GetMessagesPageFromPersonalWishlistAsync(string wishlistId, int pageNumber, int pageSize,
|
||||
CancellationToken cancellationToken, [Service] IWishlistsService wishlistsService)
|
||||
=> wishlistsService.GetMessagesPageFromPersonalWishlistAsync(wishlistId, pageNumber, pageSize, cancellationToken);
|
||||
}
|
||||
|
@ -1,5 +1,9 @@
|
||||
using System.Linq.Expressions;
|
||||
using ShoppingAssistantApi.Domain.Entities;
|
||||
|
||||
namespace ShoppingAssistantApi.Application.IRepositories;
|
||||
|
||||
public interface IMessagesRepository : IBaseRepository<Message> { }
|
||||
public interface IMessagesRepository : IBaseRepository<Message>
|
||||
{
|
||||
Task<List<Message>> GetPageStartingFromEndAsync(int pageNumber, int pageSize, Expression<Func<Message, bool>> predicate, CancellationToken cancellationToken);
|
||||
}
|
||||
|
@ -14,5 +14,7 @@ public interface IWishlistsService
|
||||
|
||||
Task<WishlistDto> GetPersonalWishlistAsync(string wishlistId, CancellationToken cancellationToken);
|
||||
|
||||
Task<PagedList<MessageDto>> GetMessagesPageFromPersonalWishlistAsync(string wishlistId, int pageNumber, int pageSize, CancellationToken cancellationToken);
|
||||
|
||||
Task<WishlistDto> DeletePersonalWishlistAsync(string wishlistId, CancellationToken cancellationToken);
|
||||
}
|
||||
|
@ -47,6 +47,8 @@ public class WishlistsService : IWishlistsService
|
||||
{
|
||||
Text = dto.FirstMessageText,
|
||||
Role = MessageRoles.User.ToString(),
|
||||
CreatedById = (ObjectId) GlobalUser.Id,
|
||||
CreatedDateUtc = DateTime.UtcNow,
|
||||
WishlistId = createdWishlist.Id
|
||||
};
|
||||
var createdMessage = await _messagesRepository.AddAsync(newMessage, cancellationToken);
|
||||
@ -62,10 +64,11 @@ public class WishlistsService : IWishlistsService
|
||||
{
|
||||
throw new InvalidDataException("Provided id is invalid.");
|
||||
}
|
||||
newMessage.WishlistId = wishlistObjectId;
|
||||
|
||||
newMessage.Role = MessageRoles.User.ToString();
|
||||
newMessage.CreatedById = (ObjectId) GlobalUser.Id;
|
||||
newMessage.CreatedDateUtc = DateTime.UtcNow;
|
||||
newMessage.WishlistId = wishlistObjectId;
|
||||
|
||||
await TryGetPersonalWishlist(wishlistObjectId, cancellationToken);
|
||||
|
||||
@ -94,6 +97,22 @@ public class WishlistsService : IWishlistsService
|
||||
return _mapper.Map<WishlistDto>(entity);
|
||||
}
|
||||
|
||||
public async Task<PagedList<MessageDto>> GetMessagesPageFromPersonalWishlistAsync(string wishlistId, int pageNumber, int pageSize, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!ObjectId.TryParse(wishlistId, out var wishlistObjectId))
|
||||
{
|
||||
throw new InvalidDataException("Provided id is invalid.");
|
||||
}
|
||||
|
||||
await TryGetPersonalWishlist(wishlistObjectId, cancellationToken);
|
||||
|
||||
var entities = await _messagesRepository.GetPageStartingFromEndAsync(pageNumber, pageSize, x => x.WishlistId == wishlistObjectId, cancellationToken);
|
||||
|
||||
var dtos = _mapper.Map<List<MessageDto>>(entities);
|
||||
var count = await _messagesRepository.GetCountAsync(x => x.WishlistId == wishlistObjectId, cancellationToken);
|
||||
return new PagedList<MessageDto>(dtos, pageNumber, pageSize, count);
|
||||
}
|
||||
|
||||
public async Task<WishlistDto> DeletePersonalWishlistAsync(string wishlistId, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!ObjectId.TryParse(wishlistId, out var wishlistObjectId))
|
||||
|
@ -27,6 +27,8 @@ public class DbInitialaizer
|
||||
|
||||
private readonly IMongoCollection<Wishlist> _wishlistCollection;
|
||||
|
||||
private readonly IMongoCollection<Message> _messageCollection;
|
||||
|
||||
public IEnumerable<RoleDto> Roles { get; set; }
|
||||
|
||||
public DbInitialaizer(IServiceProvider serviceProvider)
|
||||
@ -35,8 +37,9 @@ public class DbInitialaizer
|
||||
_rolesService = serviceProvider.GetService<IRolesService>();
|
||||
_userManager = serviceProvider.GetService<IUserManager>();
|
||||
_tokensService = serviceProvider.GetService<ITokensService>();
|
||||
_wishlistCollection = serviceProvider.GetService<MongoDbContext>().Db.GetCollection<Wishlist>("Wishlists");
|
||||
_userCollection = serviceProvider.GetService<MongoDbContext>().Db.GetCollection<User>("Users");
|
||||
_wishlistCollection = serviceProvider.GetService<MongoDbContext>().Db.GetCollection<Wishlist>("Wishlists");
|
||||
_messageCollection = serviceProvider.GetService<MongoDbContext>().Db.GetCollection<Message>("Messages");
|
||||
}
|
||||
|
||||
public async Task InitialaizeDb(CancellationToken cancellationToken)
|
||||
@ -172,45 +175,79 @@ public class DbInitialaizer
|
||||
var user1 = await (await _userCollection.FindAsync(x => x.Email.Equals("shopping.assistant.team@gmail.com"))).FirstAsync();
|
||||
var user2 = await (await _userCollection.FindAsync(x => x.Email.Equals("mykhailo.bilodid@nure.ua"))).FirstAsync();
|
||||
|
||||
var wishlistId1 = ObjectId.Parse("ab79cde6f69abcd3efab65cd");
|
||||
var wishlistId2 = ObjectId.Parse("ab6c2c2d9edf39abcd1ef9ab");
|
||||
|
||||
var wishlists = new Wishlist[]
|
||||
{
|
||||
new Wishlist
|
||||
{
|
||||
Id = ObjectId.Parse("ab79cde6f69abcd3efab65cd"),
|
||||
Id = wishlistId1,
|
||||
Name = "Gaming PC",
|
||||
Type = WishlistTypes.Product.ToString(),
|
||||
CreatedById = user1.Id,
|
||||
Messages = new Message[]
|
||||
{
|
||||
new Message
|
||||
{
|
||||
Text = "Prompt",
|
||||
Role = MessageRoles.User.ToString(),
|
||||
},
|
||||
new Message
|
||||
{
|
||||
Text = "Answer",
|
||||
Role = MessageRoles.Application.ToString(),
|
||||
},
|
||||
}
|
||||
},
|
||||
new Wishlist
|
||||
{
|
||||
Id = ObjectId.Parse("ab6c2c2d9edf39abcd1ef9ab"),
|
||||
Id = wishlistId2,
|
||||
Name = "Generic Wishlist Name",
|
||||
Type = WishlistTypes.Product.ToString(),
|
||||
CreatedById = user2.Id,
|
||||
Messages = new Message[]
|
||||
{
|
||||
new Message
|
||||
{
|
||||
Text = "Prompt",
|
||||
Role = MessageRoles.User.ToString(),
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
await _wishlistCollection.InsertManyAsync(wishlists);
|
||||
|
||||
var messages = new Message[]
|
||||
{
|
||||
new Message
|
||||
{
|
||||
Text = "Message 1",
|
||||
Role = MessageRoles.User.ToString(),
|
||||
WishlistId = wishlistId1,
|
||||
CreatedById = user1.Id,
|
||||
},
|
||||
new Message
|
||||
{
|
||||
Text = "Message 2",
|
||||
Role = MessageRoles.Application.ToString(),
|
||||
WishlistId = wishlistId1,
|
||||
},
|
||||
new Message
|
||||
{
|
||||
Text = "Message 3",
|
||||
Role = MessageRoles.User.ToString(),
|
||||
WishlistId = wishlistId1,
|
||||
CreatedById = user1.Id,
|
||||
},
|
||||
new Message
|
||||
{
|
||||
Text = "Message 4",
|
||||
Role = MessageRoles.Application.ToString(),
|
||||
WishlistId = wishlistId1,
|
||||
},
|
||||
new Message
|
||||
{
|
||||
Text = "Message 5",
|
||||
Role = MessageRoles.User.ToString(),
|
||||
WishlistId = wishlistId1,
|
||||
CreatedById = user1.Id,
|
||||
},
|
||||
new Message
|
||||
{
|
||||
Text = "Message 6",
|
||||
Role = MessageRoles.Application.ToString(),
|
||||
WishlistId = wishlistId1,
|
||||
},
|
||||
new Message
|
||||
{
|
||||
Text = "Prompt",
|
||||
Role = MessageRoles.User.ToString(),
|
||||
WishlistId = wishlistId2,
|
||||
CreatedById = user2.Id,
|
||||
}
|
||||
};
|
||||
|
||||
await _messageCollection.InsertManyAsync(messages);
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
using System.Linq.Expressions;
|
||||
using MongoDB.Driver;
|
||||
using ShoppingAssistantApi.Application.IRepositories;
|
||||
using ShoppingAssistantApi.Domain.Entities;
|
||||
using ShoppingAssistantApi.Persistance.Database;
|
||||
@ -7,4 +9,14 @@ namespace ShoppingAssistantApi.Persistance.Repositories;
|
||||
public class MessagesRepository : BaseRepository<Message>, IMessagesRepository
|
||||
{
|
||||
public MessagesRepository(MongoDbContext db) : base(db, "Messages") { }
|
||||
|
||||
public async Task<List<Message>> GetPageStartingFromEndAsync(int pageNumber, int pageSize, Expression<Func<Message, bool>> predicate, CancellationToken cancellationToken)
|
||||
{
|
||||
var messageCount = await GetCountAsync(predicate, cancellationToken);
|
||||
|
||||
return await _collection.Find(predicate)
|
||||
.Skip((messageCount / pageSize - pageNumber) * pageSize)
|
||||
.Limit(pageSize)
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
|
@ -185,6 +185,44 @@ public class WishlistsTests : IClassFixture<TestingFactory<Program>>
|
||||
Assert.Equal(user.Id, messageCreatedById);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetMessagesPageFromPersonalWishlist_ValidPageNumberAndSizeValidWishlistIdOrAuthorizedAccess_ReturnsWishlistModel()
|
||||
{
|
||||
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 = "query messagesPageFromPersonalWishlist($wishlistId: String!, $pageNumber: Int!, $pageSize: Int!) { messagesPageFromPersonalWishlist (wishlistId: $wishlistId, pageNumber: $pageNumber, pageSize: $pageSize) { hasNextPage, hasPreviousPage, items { id, text, role, createdById }, pageNumber, pageSize, totalItems, totalPages } }",
|
||||
variables = new
|
||||
{
|
||||
wishlistId = WISHLIST_TESTING_VALID_WISHLIST_ID,
|
||||
pageNumber = 1,
|
||||
pageSize = 2
|
||||
}
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
Console.WriteLine(document.data.messagesPageFromPersonalWishlist);
|
||||
|
||||
var messagesPageFromPersonalWishlist = Enumerable.ToList(document.data.messagesPageFromPersonalWishlist.items);
|
||||
var firstMessageInPage = messagesPageFromPersonalWishlist[0];
|
||||
var secondMessageInPage = messagesPageFromPersonalWishlist[1];
|
||||
|
||||
Assert.Equal("Message 5", (string) firstMessageInPage.text);
|
||||
Assert.Equal(MessageRoles.User.ToString(), (string) firstMessageInPage.role);
|
||||
Assert.Equal(user.Id, (string) firstMessageInPage.createdById);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeletePersonalWishlist_ValidWishlistIdOrAuthorizedAccess_ReturnsWishlistModel()
|
||||
{
|
||||
@ -323,6 +361,106 @@ public class WishlistsTests : IClassFixture<TestingFactory<Program>>
|
||||
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetMessagesPageFromPersonalWishlist_InValidPageNumber_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);
|
||||
|
||||
var mutation = new
|
||||
{
|
||||
query = "query messagesPageFromPersonalWishlist($wishlistId: String!, $pageNumber: Int!, $pageSize: Int!) { messagesPageFromPersonalWishlist (wishlistId: $wishlistId, pageNumber: $pageNumber, pageSize: $pageSize) { hasNextPage, hasPreviousPage, items { id, text, role, createdById }, pageNumber, pageSize, totalItems, totalPages } }",
|
||||
variables = new
|
||||
{
|
||||
wishlistId = WISHLIST_TESTING_VALID_WISHLIST_ID,
|
||||
pageNumber = 4,
|
||||
pageSize = 2
|
||||
}
|
||||
};
|
||||
|
||||
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 GetMessagesPageFromPersonalWishlist_InValidPageSize_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);
|
||||
|
||||
var mutation = new
|
||||
{
|
||||
query = "query messagesPageFromPersonalWishlist($wishlistId: String!, $pageNumber: Int!, $pageSize: Int!) { messagesPageFromPersonalWishlist (wishlistId: $wishlistId, pageNumber: $pageNumber, pageSize: $pageSize) { hasNextPage, hasPreviousPage, items { id, text, role, createdById }, pageNumber, pageSize, totalItems, totalPages } }",
|
||||
variables = new
|
||||
{
|
||||
wishlistId = WISHLIST_TESTING_VALID_WISHLIST_ID,
|
||||
pageNumber = 1,
|
||||
pageSize = 10
|
||||
}
|
||||
};
|
||||
|
||||
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 GetMessagesPageFromPersonalWishlist_InValidWishlistId_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);
|
||||
|
||||
var mutation = new
|
||||
{
|
||||
query = "query messagesPageFromPersonalWishlist($wishlistId: String!, $pageNumber: Int!, $pageSize: Int!) { messagesPageFromPersonalWishlist (wishlistId: $wishlistId, pageNumber: $pageNumber, pageSize: $pageSize) { hasNextPage, hasPreviousPage, items { id, text, role, createdById }, pageNumber, pageSize, totalItems, totalPages } }",
|
||||
variables = new
|
||||
{
|
||||
wishlistId = WISHLIST_TESTING_INVALID_WISHLIST_ID,
|
||||
pageNumber = 1,
|
||||
pageSize = 2
|
||||
}
|
||||
};
|
||||
|
||||
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 GetMessagesPageFromPersonalWishlist_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);
|
||||
|
||||
var mutation = new
|
||||
{
|
||||
query = "query messagesPageFromPersonalWishlist($wishlistId: String!, $pageNumber: Int!, $pageSize: Int!) { messagesPageFromPersonalWishlist (wishlistId: $wishlistId, pageNumber: $pageNumber, pageSize: $pageSize) { hasNextPage, hasPreviousPage, items { id, text, role, createdById }, pageNumber, pageSize, totalItems, totalPages } }",
|
||||
variables = new
|
||||
{
|
||||
wishlistId = WISHLIST_TESTING_OTHER_USER_WISHLIST_ID,
|
||||
pageNumber = 1,
|
||||
pageSize = 2
|
||||
}
|
||||
};
|
||||
|
||||
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 DeletePersonalWishlist_InValidWishlistId_ReturnsInternalServerError()
|
||||
{
|
||||
@ -345,4 +483,27 @@ public class WishlistsTests : IClassFixture<TestingFactory<Program>>
|
||||
using var response = await _httpClient.PostAsync("graphql", content);
|
||||
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeletePersonalWishlist_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);
|
||||
|
||||
var mutation = new
|
||||
{
|
||||
query = "mutation deletePersonalWishlist($wishlistId: String!) { deletePersonalWishlist (wishlistId: $wishlistId) { createdById, id, name, type } }",
|
||||
variables = new
|
||||
{
|
||||
wishlistId = WISHLIST_TESTING_OTHER_USER_WISHLIST_ID
|
||||
}
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user