diff --git a/ShoppingAssistantApi.Api/Mutations/UsersMutation.cs b/ShoppingAssistantApi.Api/Mutations/UsersMutation.cs index 6beaccf..9a6102c 100644 --- a/ShoppingAssistantApi.Api/Mutations/UsersMutation.cs +++ b/ShoppingAssistantApi.Api/Mutations/UsersMutation.cs @@ -2,6 +2,7 @@ using ShoppingAssistantApi.Application.Models.Dtos; using ShoppingAssistantApi.Application.Models.Operations; using HotChocolate.Authorization; +using ShoppingAssistantApi.Application.IServices; namespace ShoppingAssistantApi.Api.Mutations; @@ -27,4 +28,12 @@ public class UsersMutation public Task RemoveFromRoleAsync(string roleName, string userId, CancellationToken cancellationToken, [Service] IUserManager userManager) => userManager.RemoveFromRoleAsync(roleName, userId, cancellationToken); -} \ No newline at end of file + + [Authorize] + public async Task DeletePersonalUserAsync(string guestId, CancellationToken cancellationToken, + [Service] IUsersService usersService) + { + await usersService.DeletePersonalUserAsync(guestId, cancellationToken); + return true; + } +} diff --git a/ShoppingAssistantApi.Application/IServices/IUsersService.cs b/ShoppingAssistantApi.Application/IServices/IUsersService.cs index 25fcab4..0357178 100644 --- a/ShoppingAssistantApi.Application/IServices/IUsersService.cs +++ b/ShoppingAssistantApi.Application/IServices/IUsersService.cs @@ -12,4 +12,6 @@ public interface IUsersService Task GetUserAsync(string id, CancellationToken cancellationToken); Task UpdateUserAsync(UserDto dto, CancellationToken cancellationToken); -} \ No newline at end of file + + Task DeletePersonalUserAsync(string guestId, CancellationToken cancellationToken); +} diff --git a/ShoppingAssistantApi.Infrastructure/Services/UsersService.cs b/ShoppingAssistantApi.Infrastructure/Services/UsersService.cs index b3df02b..2ae7654 100644 --- a/ShoppingAssistantApi.Infrastructure/Services/UsersService.cs +++ b/ShoppingAssistantApi.Infrastructure/Services/UsersService.cs @@ -59,4 +59,26 @@ public class UsersService : IUsersService entity.LastModifiedDateUtc = DateTime.UtcNow; await _repository.UpdateUserAsync(entity, cancellationToken); } + + public async Task DeletePersonalUserAsync(string guestId, CancellationToken cancellationToken) + { + if (!Guid.TryParse(guestId, out var guid)) + { + throw new InvalidDataException("Provided id is invalid."); + } + + var entity = await _repository.GetUserAsync(u => u.GuestId == guid, cancellationToken); + + if (entity.Id != GlobalUser.Id) + { + throw new UnAuthorizedException(); + } + + if (entity == null) + { + throw new EntityNotFoundException(); + } + + await _repository.DeleteAsync(entity, cancellationToken); + } }