diff --git a/BeReadyBackend/Endpoints/Users/DeleteUserEndpoint.cs b/BeReadyBackend/Endpoints/Users/DeleteUserEndpoint.cs index 2fb89ec..7813f66 100644 --- a/BeReadyBackend/Endpoints/Users/DeleteUserEndpoint.cs +++ b/BeReadyBackend/Endpoints/Users/DeleteUserEndpoint.cs @@ -1,11 +1,13 @@ -using BeReadyBackend.Repositories; +using BeReadyBackend.Models; +using BeReadyBackend.Repositories; using BeReadyBackend.Services; +using BeReadyBackend.Specifications.Friends; using BeReadyBackend.Specifications.Users; using FastEndpoints; namespace BeReadyBackend.Endpoints.Users; -public class DeleteUserEndpoint(UsersRepository usersRepository, UserService userService) : EndpointWithoutRequest +public class DeleteUserEndpoint(UsersRepository usersRepository, UserFriendsRepository userFriendsRepository, UserService userService) : EndpointWithoutRequest { public override void Configure() { @@ -16,6 +18,9 @@ public class DeleteUserEndpoint(UsersRepository usersRepository, UserService use { int userId = userService.GetUserIdFromToken(); + List friends = await userFriendsRepository.ListAsync(new GetAllFriendsRelationsByIdSpec(userId), ct); + await userFriendsRepository.DeleteRangeAsync(friends, ct); + await usersRepository.DeleteAsync((await usersRepository.SingleOrDefaultAsync(new GetUserByIdSpec(userId), ct))!, ct); await Send.NoContentAsync(ct); } diff --git a/BeReadyBackend/Migrations/BeReadyDbContextModelSnapshot.cs b/BeReadyBackend/Migrations/BeReadyDbContextModelSnapshot.cs index 3762207..81f1b81 100644 --- a/BeReadyBackend/Migrations/BeReadyDbContextModelSnapshot.cs +++ b/BeReadyBackend/Migrations/BeReadyDbContextModelSnapshot.cs @@ -40,7 +40,7 @@ namespace BeReadyBackend.Migrations b.HasKey("Id"); - b.ToTable("Achievements"); + b.ToTable("Achievements", (string)null); }); modelBuilder.Entity("BeReadyBackend.Models.Designation", b => @@ -57,7 +57,7 @@ namespace BeReadyBackend.Migrations b.HasKey("Id"); - b.ToTable("Designations"); + b.ToTable("Designations", (string)null); }); modelBuilder.Entity("BeReadyBackend.Models.Group", b => @@ -77,7 +77,7 @@ namespace BeReadyBackend.Migrations b.HasKey("Id"); - b.ToTable("Groups"); + b.ToTable("Groups", (string)null); }); modelBuilder.Entity("BeReadyBackend.Models.Message", b => @@ -107,7 +107,7 @@ namespace BeReadyBackend.Migrations b.HasIndex("UserId"); - b.ToTable("Messages"); + b.ToTable("Messages", (string)null); }); modelBuilder.Entity("BeReadyBackend.Models.Post", b => @@ -134,7 +134,7 @@ namespace BeReadyBackend.Migrations b.HasIndex("UserId"); - b.ToTable("Posts"); + b.ToTable("Posts", (string)null); }); modelBuilder.Entity("BeReadyBackend.Models.RandomChallenge", b => @@ -161,7 +161,7 @@ namespace BeReadyBackend.Migrations b.HasKey("Id"); - b.ToTable("RandomChallenges"); + b.ToTable("RandomChallenges", (string)null); }); modelBuilder.Entity("BeReadyBackend.Models.User", b => @@ -217,7 +217,7 @@ namespace BeReadyBackend.Migrations b.HasIndex("DesignationId"); - b.ToTable("Users"); + b.ToTable("Users", (string)null); }); modelBuilder.Entity("BeReadyBackend.Models.UserAchievement", b => @@ -232,7 +232,7 @@ namespace BeReadyBackend.Migrations b.HasIndex("AchievementId"); - b.ToTable("UserAchievements"); + b.ToTable("UserAchievements", (string)null); }); modelBuilder.Entity("BeReadyBackend.Models.UserFriend", b => @@ -250,7 +250,7 @@ namespace BeReadyBackend.Migrations b.HasIndex("FriendId"); - b.ToTable("UserFriends"); + b.ToTable("UserFriends", (string)null); }); modelBuilder.Entity("BeReadyBackend.Models.UserGroup", b => @@ -269,7 +269,7 @@ namespace BeReadyBackend.Migrations b.HasIndex("GroupId"); - b.ToTable("UserGroups"); + b.ToTable("UserGroups", (string)null); }); modelBuilder.Entity("BeReadyBackend.Models.UserPost", b => @@ -284,7 +284,7 @@ namespace BeReadyBackend.Migrations b.HasIndex("PostId"); - b.ToTable("UserPosts"); + b.ToTable("UserPosts", (string)null); }); modelBuilder.Entity("BeReadyBackend.Models.UserRandomChallenge", b => @@ -302,7 +302,7 @@ namespace BeReadyBackend.Migrations b.HasIndex("RandomChallengeId"); - b.ToTable("UserRandomChallenges"); + b.ToTable("UserRandomChallenges", (string)null); }); modelBuilder.Entity("BeReadyBackend.Models.Message", b => diff --git a/BeReadyBackend/Specifications/Friends/GetAllFriendsRelationsByIdSpec.cs b/BeReadyBackend/Specifications/Friends/GetAllFriendsRelationsByIdSpec.cs new file mode 100644 index 0000000..0eab1e6 --- /dev/null +++ b/BeReadyBackend/Specifications/Friends/GetAllFriendsRelationsByIdSpec.cs @@ -0,0 +1,13 @@ +using Ardalis.Specification; +using BeReadyBackend.Models; + +namespace BeReadyBackend.Specifications.Friends; + +public class GetAllFriendsRelationsByIdSpec : Specification +{ + public GetAllFriendsRelationsByIdSpec(int userId) + { + Query + .Where(x => x.UserId == userId || x.FriendId == userId); + } +} \ No newline at end of file