mirror of
https://github.com/Shchoholiev/shopping-assistant-api.git
synced 2025-04-12 01:48:49 +00:00
add ability to delete wishlist
This commit is contained in:
parent
236153d486
commit
e26177528b
@ -14,4 +14,8 @@ public class WishlistsMutation
|
|||||||
public Task<MessageDto> AddMessageToPersonalWishlist(string wishlistId, MessageCreateDto dto, CancellationToken cancellationToken,
|
public Task<MessageDto> AddMessageToPersonalWishlist(string wishlistId, MessageCreateDto dto, CancellationToken cancellationToken,
|
||||||
[Service] IWishlistsService wishlistsService)
|
[Service] IWishlistsService wishlistsService)
|
||||||
=> wishlistsService.AddMessageToPersonalWishlistAsync(wishlistId, dto, cancellationToken);
|
=> wishlistsService.AddMessageToPersonalWishlistAsync(wishlistId, dto, cancellationToken);
|
||||||
|
|
||||||
|
public Task<WishlistDto> DeletePersonalWishlistAsync(string wishlistId, CancellationToken cancellationToken,
|
||||||
|
[Service] IWishlistsService wishlistsService)
|
||||||
|
=> wishlistsService.DeletePersonalWishlistAsync(wishlistId, cancellationToken);
|
||||||
}
|
}
|
||||||
|
@ -13,4 +13,6 @@ public interface IWishlistsService
|
|||||||
Task<PagedList<WishlistDto>> GetPersonalWishlistsPageAsync(int pageNumber, int pageSize, CancellationToken cancellationToken);
|
Task<PagedList<WishlistDto>> GetPersonalWishlistsPageAsync(int pageNumber, int pageSize, CancellationToken cancellationToken);
|
||||||
|
|
||||||
Task<WishlistDto> GetPersonalWishlistAsync(string wishlistId, CancellationToken cancellationToken);
|
Task<WishlistDto> GetPersonalWishlistAsync(string wishlistId, CancellationToken cancellationToken);
|
||||||
|
|
||||||
|
Task<WishlistDto> DeletePersonalWishlistAsync(string wishlistId, CancellationToken cancellationToken);
|
||||||
}
|
}
|
||||||
|
@ -67,12 +67,7 @@ public class WishlistsService : IWishlistsService
|
|||||||
newMessage.CreatedById = (ObjectId) GlobalUser.Id;
|
newMessage.CreatedById = (ObjectId) GlobalUser.Id;
|
||||||
newMessage.CreatedDateUtc = DateTime.UtcNow;
|
newMessage.CreatedDateUtc = DateTime.UtcNow;
|
||||||
|
|
||||||
var relatedWishlist = await _wishlistsRepository.GetWishlistAsync(x => x.Id == wishlistObjectId && x.CreatedById == GlobalUser.Id, cancellationToken);
|
await TryGetPersonalWishlist(wishlistObjectId, cancellationToken);
|
||||||
|
|
||||||
if (relatedWishlist == null)
|
|
||||||
{
|
|
||||||
throw new UnAuthorizedException<Wishlist>();
|
|
||||||
}
|
|
||||||
|
|
||||||
var createdMessage = await _messagesRepository.AddAsync(newMessage, cancellationToken);
|
var createdMessage = await _messagesRepository.AddAsync(newMessage, cancellationToken);
|
||||||
|
|
||||||
@ -93,15 +88,43 @@ public class WishlistsService : IWishlistsService
|
|||||||
{
|
{
|
||||||
throw new InvalidDataException("Provided id is invalid.");
|
throw new InvalidDataException("Provided id is invalid.");
|
||||||
}
|
}
|
||||||
var entity = await _wishlistsRepository.GetWishlistAsync(x => x.Id == wishlistObjectId && x.CreatedById == GlobalUser.Id, cancellationToken);
|
|
||||||
|
|
||||||
Console.WriteLine(" WISHLIST: " + entity.CreatedById + " " + GlobalUser.Id);
|
var entity = await TryGetPersonalWishlist(wishlistObjectId, cancellationToken);
|
||||||
|
|
||||||
if (entity == null)
|
|
||||||
{
|
|
||||||
throw new UnAuthorizedException<Wishlist>();
|
|
||||||
}
|
|
||||||
|
|
||||||
return _mapper.Map<WishlistDto>(entity);
|
return _mapper.Map<WishlistDto>(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<WishlistDto> DeletePersonalWishlistAsync(string wishlistId, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
if (!ObjectId.TryParse(wishlistId, out var wishlistObjectId))
|
||||||
|
{
|
||||||
|
throw new InvalidDataException("Provided id is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var entity = await TryGetPersonalWishlist(wishlistObjectId, cancellationToken);
|
||||||
|
|
||||||
|
entity.LastModifiedById = GlobalUser.Id;
|
||||||
|
entity.LastModifiedDateUtc = DateTime.UtcNow;
|
||||||
|
|
||||||
|
await _wishlistsRepository.DeleteAsync(entity, cancellationToken);
|
||||||
|
|
||||||
|
return _mapper.Map<WishlistDto>(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<Wishlist> TryGetPersonalWishlist(ObjectId wishlistId, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var entity = await _wishlistsRepository.GetWishlistAsync(x => x.Id == wishlistId, cancellationToken);
|
||||||
|
|
||||||
|
if (entity.CreatedById != GlobalUser.Id)
|
||||||
|
{
|
||||||
|
throw new UnAuthorizedException<Wishlist>();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (entity == null)
|
||||||
|
{
|
||||||
|
throw new EntityNotFoundException<Wishlist>();
|
||||||
|
}
|
||||||
|
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,15 @@ public class WishlistsTests : IClassFixture<TestingFactory<Program>>
|
|||||||
|
|
||||||
private const string WISHLIST_TESTING_USER_PASSWORD = "Yuiop12345";
|
private const string WISHLIST_TESTING_USER_PASSWORD = "Yuiop12345";
|
||||||
|
|
||||||
private const string TESTING_WISHLIST_ID = "ab79cde6f69abcd3efab65cd";
|
private const string WISHLIST_TESTING_VALID_WISHLIST_ID = "ab79cde6f69abcd3efab65cd";
|
||||||
|
|
||||||
|
private const string WISHLIST_TESTING_VALID_WISHLIST_NAME = "Gaming PC";
|
||||||
|
|
||||||
|
private const WishlistTypes WISHLIST_TESTING_VALID_WISHLIST_TYPE = WishlistTypes.Product;
|
||||||
|
|
||||||
|
private const string WISHLIST_TESTING_INVALID_WISHLIST_ID = "1234567890abcdef12345678";
|
||||||
|
|
||||||
|
private const string WISHLIST_TESTING_OTHER_USER_WISHLIST_ID = "ab6c2c2d9edf39abcd1ef9ab";
|
||||||
|
|
||||||
public WishlistsTests(TestingFactory<Program> factory)
|
public WishlistsTests(TestingFactory<Program> factory)
|
||||||
{
|
{
|
||||||
@ -111,7 +119,7 @@ public class WishlistsTests : IClassFixture<TestingFactory<Program>>
|
|||||||
query = "query personalWishlist($wishlistId: String!) { personalWishlist(wishlistId: $wishlistId) { createdById, id, name, type } }",
|
query = "query personalWishlist($wishlistId: String!) { personalWishlist(wishlistId: $wishlistId) { createdById, id, name, type } }",
|
||||||
variables = new
|
variables = new
|
||||||
{
|
{
|
||||||
wishlistId = TESTING_WISHLIST_ID
|
wishlistId = WISHLIST_TESTING_VALID_WISHLIST_ID
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -130,9 +138,9 @@ public class WishlistsTests : IClassFixture<TestingFactory<Program>>
|
|||||||
var personalWishlistType = (string) document.data.personalWishlist.type;
|
var personalWishlistType = (string) document.data.personalWishlist.type;
|
||||||
var personalWishlistCreatedById = (string) document.data.personalWishlist.createdById;
|
var personalWishlistCreatedById = (string) document.data.personalWishlist.createdById;
|
||||||
|
|
||||||
Assert.Equal(TESTING_WISHLIST_ID, personalWishlistId);
|
Assert.Equal(WISHLIST_TESTING_VALID_WISHLIST_ID, personalWishlistId);
|
||||||
Assert.Equal("Gaming PC", personalWishlistName);
|
Assert.Equal(WISHLIST_TESTING_VALID_WISHLIST_NAME, personalWishlistName);
|
||||||
Assert.Equal(WishlistTypes.Product.ToString(), personalWishlistType);
|
Assert.Equal(WISHLIST_TESTING_VALID_WISHLIST_TYPE.ToString(), personalWishlistType);
|
||||||
Assert.Equal(user.Id, personalWishlistCreatedById);
|
Assert.Equal(user.Id, personalWishlistCreatedById);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -150,7 +158,7 @@ public class WishlistsTests : IClassFixture<TestingFactory<Program>>
|
|||||||
query = "mutation addMessageToPersonalWishlist($wishlistId: String!, $dto: MessageCreateDtoInput!) { addMessageToPersonalWishlist (wishlistId: $wishlistId, dto: $dto) { role, text, createdById } }",
|
query = "mutation addMessageToPersonalWishlist($wishlistId: String!, $dto: MessageCreateDtoInput!) { addMessageToPersonalWishlist (wishlistId: $wishlistId, dto: $dto) { role, text, createdById } }",
|
||||||
variables = new
|
variables = new
|
||||||
{
|
{
|
||||||
wishlistId = TESTING_WISHLIST_ID,
|
wishlistId = WISHLIST_TESTING_VALID_WISHLIST_ID,
|
||||||
dto = new
|
dto = new
|
||||||
{
|
{
|
||||||
text = MESSAGE_TEXT
|
text = MESSAGE_TEXT
|
||||||
@ -177,6 +185,42 @@ public class WishlistsTests : IClassFixture<TestingFactory<Program>>
|
|||||||
Assert.Equal(user.Id, messageCreatedById);
|
Assert.Equal(user.Id, messageCreatedById);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task DeletePersonalWishlist_ValidWishlistIdOrAuthorizedAccess_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 = "mutation deletePersonalWishlist($wishlistId: String!) { deletePersonalWishlist (wishlistId: $wishlistId) { createdById, id, name, type } }",
|
||||||
|
variables = new
|
||||||
|
{
|
||||||
|
wishlistId = WISHLIST_TESTING_VALID_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.OK, response.StatusCode);
|
||||||
|
|
||||||
|
var responseString = await response.Content.ReadAsStringAsync();
|
||||||
|
var document = JsonConvert.DeserializeObject<dynamic>(responseString);
|
||||||
|
|
||||||
|
var personalWishlistId = (string) document.data.deletePersonalWishlist.id;
|
||||||
|
var personalWishlistName = (string) document.data.deletePersonalWishlist.name;
|
||||||
|
var personalWishlistType = (string) document.data.deletePersonalWishlist.type;
|
||||||
|
var personalWishlistCreatedById = (string) document.data.deletePersonalWishlist.createdById;
|
||||||
|
|
||||||
|
Assert.Equal(WISHLIST_TESTING_VALID_WISHLIST_ID, personalWishlistId);
|
||||||
|
Assert.Equal(WISHLIST_TESTING_VALID_WISHLIST_NAME, personalWishlistName);
|
||||||
|
Assert.Equal(WISHLIST_TESTING_VALID_WISHLIST_TYPE.ToString(), personalWishlistType);
|
||||||
|
Assert.Equal(user.Id, personalWishlistCreatedById);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task StartPersonalWishlistAsync_InvalidWishlistModel_ReturnsInternalServerError()
|
public async Task StartPersonalWishlistAsync_InvalidWishlistModel_ReturnsInternalServerError()
|
||||||
{
|
{
|
||||||
@ -216,7 +260,7 @@ public class WishlistsTests : IClassFixture<TestingFactory<Program>>
|
|||||||
query = "query personalWishlist($wishlistId: String!) { personalWishlist(wishlistId: $wishlistId) { createdById, id, name, type } }",
|
query = "query personalWishlist($wishlistId: String!) { personalWishlist(wishlistId: $wishlistId) { createdById, id, name, type } }",
|
||||||
variables = new
|
variables = new
|
||||||
{
|
{
|
||||||
wishlistId = "1234567890abcdef12345678" // Invalid wishlistId
|
wishlistId = WISHLIST_TESTING_INVALID_WISHLIST_ID
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -239,7 +283,7 @@ public class WishlistsTests : IClassFixture<TestingFactory<Program>>
|
|||||||
query = "query personalWishlist($wishlistId: String!) { personalWishlist(wishlistId: $wishlistId) { createdById, id, name, type } }",
|
query = "query personalWishlist($wishlistId: String!) { personalWishlist(wishlistId: $wishlistId) { createdById, id, name, type } }",
|
||||||
variables = new
|
variables = new
|
||||||
{
|
{
|
||||||
wishlistId = "ab6c2c2d9edf39abcd1ef9ab" // Other user's wishlist
|
wishlistId = WISHLIST_TESTING_OTHER_USER_WISHLIST_ID
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -264,7 +308,7 @@ public class WishlistsTests : IClassFixture<TestingFactory<Program>>
|
|||||||
query = "mutation addMessageToPersonalWishlist($wishlistId: String!, $dto: MessageCreateDtoInput!) { addMessageToPersonalWishlist (wishlistId: $wishlistId, dto: $dto) { role, text, createdById } }",
|
query = "mutation addMessageToPersonalWishlist($wishlistId: String!, $dto: MessageCreateDtoInput!) { addMessageToPersonalWishlist (wishlistId: $wishlistId, dto: $dto) { role, text, createdById } }",
|
||||||
variables = new
|
variables = new
|
||||||
{
|
{
|
||||||
wishlistId = "8125jad7g12", // Invalid wishlistId
|
wishlistId = WISHLIST_TESTING_INVALID_WISHLIST_ID,
|
||||||
dto = new
|
dto = new
|
||||||
{
|
{
|
||||||
text = MESSAGE_TEXT,
|
text = MESSAGE_TEXT,
|
||||||
@ -278,4 +322,27 @@ public class WishlistsTests : IClassFixture<TestingFactory<Program>>
|
|||||||
using var response = await _httpClient.PostAsync("graphql", content);
|
using var response = await _httpClient.PostAsync("graphql", content);
|
||||||
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
|
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task DeletePersonalWishlist_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 = "mutation deletePersonalWishlist($wishlistId: String!) { deletePersonalWishlist (wishlistId: $wishlistId) { createdById, id, name, type } }",
|
||||||
|
variables = new
|
||||||
|
{
|
||||||
|
wishlistId = WISHLIST_TESTING_INVALID_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