Skip to content

Commit

Permalink
Add API endpoint to purge all reviews by the given user
Browse files Browse the repository at this point in the history
  • Loading branch information
Beyley committed Nov 2, 2024
1 parent f64450a commit 9fdea17
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Refresh.GameServer/Database/GameDatabaseContext.Relations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,19 @@ public void AddReviewToLevel(GameReview review, GameLevel level)

this.AddSequentialObject(review, level.Reviews);
}

public void DeleteReviewsPostedByUser(GameUser user)
{
IEnumerable<GameReview> reviews = this.GameReviews.Where(s => s.Publisher == user);

this.Write(() =>
{
foreach (GameReview review in reviews)
{
this.DeleteReview(review);
}
});
}

public DatabaseList<GameReview> GetReviewsByUser(GameUser user, int count, int skip)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 9fdea17

Please sign in to comment.