diff --git a/Refresh.GameServer/Database/GameDatabaseContext.Photos.cs b/Refresh.GameServer/Database/GameDatabaseContext.Photos.cs index d1f622e5..5d6b34d2 100644 --- a/Refresh.GameServer/Database/GameDatabaseContext.Photos.cs +++ b/Refresh.GameServer/Database/GameDatabaseContext.Photos.cs @@ -1,4 +1,5 @@ using JetBrains.Annotations; +using Refresh.GameServer.Types.Activity; using Refresh.GameServer.Types.Levels; using Refresh.GameServer.Types.Photos; using Refresh.GameServer.Types.UserData; @@ -57,6 +58,13 @@ public void RemovePhoto(GamePhoto photo) { this.Write(() => { + IQueryable photoEvents = this.Events + .Where(e => e._StoredDataType == (int)EventDataType.Photo && e.StoredSequentialId == photo.PhotoId); + + // Remove all events referencing the photo + this.Events.RemoveRange(photoEvents); + + // Remove the photo this.GamePhotos.Remove(photo); }); } @@ -106,4 +114,17 @@ public DatabaseList GetPhotosInLevel(GameLevel level, int count, int [Pure] public int GetTotalPhotosInLevel(GameLevel level) => this.GamePhotos.Count(p => p.LevelId == level.LevelId); + + public void DeletePhotosPostedByUser(GameUser user) + { + IEnumerable photos = this.GamePhotos.Where(s => s.Publisher == user); + + this.Write(() => + { + foreach (GamePhoto photo in photos) + { + this.RemovePhoto(photo); + } + }); + } } \ No newline at end of file diff --git a/Refresh.GameServer/Endpoints/ApiV3/Admin/AdminPhotoApiEndpoints.cs b/Refresh.GameServer/Endpoints/ApiV3/Admin/AdminPhotoApiEndpoints.cs new file mode 100644 index 00000000..217f7f82 --- /dev/null +++ b/Refresh.GameServer/Endpoints/ApiV3/Admin/AdminPhotoApiEndpoints.cs @@ -0,0 +1,53 @@ +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.Photos; +using Refresh.GameServer.Types.Roles; +using Refresh.GameServer.Types.UserData; + +namespace Refresh.GameServer.Endpoints.ApiV3.Admin; + +public class AdminPhotoApiEndpoints : EndpointGroup +{ + [ApiV3Endpoint("admin/users/uuid/{uuid}/photos", HttpMethods.Delete), MinimumRole(GameUserRole.Admin)] + [DocSummary("Deletes all photos posted by a user. Gets user by their UUID.")] + [DocError(typeof(ApiNotFoundError), ApiNotFoundError.UserMissingErrorWhen)] + public ApiOkResponse DeletePhotosPostedByUuid(RequestContext context, GameDatabaseContext database, + [DocSummary("The UUID of the user")] string uuid) + { + GameUser? user = database.GetUserByUuid(uuid); + if (user == null) return ApiNotFoundError.UserMissingError; + + database.DeletePhotosPostedByUser(user); + return new ApiOkResponse(); + } + + [ApiV3Endpoint("admin/users/name/{username}/photos", HttpMethods.Delete), MinimumRole(GameUserRole.Admin)] + [DocSummary("Deletes all photos posted by a user. Gets user by their username.")] + [DocError(typeof(ApiNotFoundError), ApiNotFoundError.UserMissingErrorWhen)] + public ApiOkResponse DeletePhotosPostedByUsername(RequestContext context, GameDatabaseContext database, + [DocSummary("The username of the user")] string username) + { + GameUser? user = database.GetUserByUsername(username); + if (user == null) return ApiNotFoundError.UserMissingError; + + database.DeletePhotosPostedByUser(user); + return new ApiOkResponse(); + } + + [ApiV3Endpoint("admin/photos/id/{id}", HttpMethods.Delete), MinimumRole(GameUserRole.Admin)] + [DocSummary("Deletes a photo.")] + [DocError(typeof(ApiNotFoundError), ApiNotFoundError.PhotoMissingErrorWhen)] + public ApiOkResponse DeletePhoto(RequestContext context, GameDatabaseContext database, int id) + { + GamePhoto? photo = database.GetPhotoById(id); + if (photo == null) return ApiNotFoundError.PhotoMissingError; + + database.RemovePhoto(photo); + return new ApiOkResponse(); + } +} \ No newline at end of file