diff --git a/Refresh.GameServer/Database/GameDatabaseContext.Relations.cs b/Refresh.GameServer/Database/GameDatabaseContext.Relations.cs index ac93e966..1a8f91e4 100644 --- a/Refresh.GameServer/Database/GameDatabaseContext.Relations.cs +++ b/Refresh.GameServer/Database/GameDatabaseContext.Relations.cs @@ -332,6 +332,19 @@ public void AddReviewToLevel(GameReview review, GameLevel level) this.AddSequentialObject(review, level.Reviews); } + + public void DeleteReviewsPostedByUser(GameUser user) + { + IEnumerable reviews = this.GameReviews.Where(s => s.Publisher == user); + + this.Write(() => + { + foreach (GameReview review in reviews) + { + this.DeleteReview(review); + } + }); + } public DatabaseList GetReviewsByUser(GameUser user, int count, int skip) { diff --git a/Refresh.GameServer/Endpoints/ApiV3/Admin/AdminReviewApiEndpoints.cs b/Refresh.GameServer/Endpoints/ApiV3/Admin/AdminReviewApiEndpoints.cs new file mode 100644 index 00000000..5ceb676a --- /dev/null +++ b/Refresh.GameServer/Endpoints/ApiV3/Admin/AdminReviewApiEndpoints.cs @@ -0,0 +1,40 @@ +using AttribDoc.Attributes; +using Bunkum.Core; +using Bunkum.Core.Endpoints; +using Bunkum.Protocols.Http; +using Refresh.GameServer.Database; +using Refresh.GameServer.Endpoints.ApiV3.ApiTypes; +using Refresh.GameServer.Endpoints.ApiV3.ApiTypes.Errors; +using Refresh.GameServer.Types.Roles; +using Refresh.GameServer.Types.UserData; + +namespace Refresh.GameServer.Endpoints.ApiV3.Admin; + +public class AdminReviewApiEndpoints : EndpointGroup +{ + [ApiV3Endpoint("admin/users/uuid/{uuid}/reviews", HttpMethods.Delete), MinimumRole(GameUserRole.Admin)] + [DocSummary("Deletes all reviews posted by a user. Gets user by their UUID.")] + [DocError(typeof(ApiNotFoundError), ApiNotFoundError.UserMissingErrorWhen)] + public ApiOkResponse DeleteReviewsPostedByUuid(RequestContext context, GameDatabaseContext database, + [DocSummary("The UUID of the user")] string uuid) + { + GameUser? user = database.GetUserByUuid(uuid); + if (user == null) return ApiNotFoundError.UserMissingError; + + database.DeleteReviewsPostedByUser(user); + return new ApiOkResponse(); + } + + [ApiV3Endpoint("admin/users/name/{username}/reviews", HttpMethods.Delete), MinimumRole(GameUserRole.Admin)] + [DocSummary("Deletes all reviews posted by a user. Gets user by their username.")] + [DocError(typeof(ApiNotFoundError), ApiNotFoundError.UserMissingErrorWhen)] + public ApiOkResponse DeleteReviewsPostedByUsername(RequestContext context, GameDatabaseContext database, + [DocSummary("The username of the user")] string username) + { + GameUser? user = database.GetUserByUsername(username); + if (user == null) return ApiNotFoundError.UserMissingError; + + database.DeleteReviewsPostedByUser(user); + return new ApiOkResponse(); + } +} \ No newline at end of file