diff --git a/MyMcRealms/Controllers/InvitesController.cs b/MyMcRealms/Controllers/InvitesController.cs index 244886c..89bad12 100644 --- a/MyMcRealms/Controllers/InvitesController.cs +++ b/MyMcRealms/Controllers/InvitesController.cs @@ -2,6 +2,7 @@ using MyMcRealms.Responses; using MyMcRealms.Attributes; using MyMcRealms.Requests; +using Minecraft_Realms_Emulator.Responses; namespace MyMcRealms.Controllers { @@ -22,7 +23,16 @@ namespace MyMcRealms.Controllers var _api = new MyMcAPI.Wrapper(Environment.GetEnvironmentVariable("MYMC_API_KEY")); var world = (await _api.GetAllServers()).Servers[wId]; - if (world == null) return NotFound("World not found"); + if (world == null) + { + ErrorResponse errorResponse = new() + { + ErrorCode = 404, + ErrorMsg = "World not found" + }; + + return NotFound(errorResponse); + } var api = new MyMcAPI.Wrapper(world.OwnersToken); var whitelist = await api.GetWhitelist(); @@ -30,7 +40,16 @@ namespace MyMcRealms.Controllers // Get player name var playerInfo = await new HttpClient().GetFromJsonAsync($"https://api.mojang.com/users/profiles/minecraft/{body.Name}"); - if (whitelist.Result.Any(p => p.Name == body.Name)) return BadRequest("Player already whitelisted"); + if (whitelist.Result.Any(p => p.Name == body.Name)) + { + ErrorResponse errorResponse = new() + { + ErrorCode = 400, + ErrorMsg = "Player already whitelisteed" + }; + + return BadRequest(errorResponse); + } api.ExecuteCommand($"whitelist add {body.Name}"); @@ -95,7 +114,16 @@ namespace MyMcRealms.Controllers var _api = new MyMcAPI.Wrapper(Environment.GetEnvironmentVariable("MYMC_API_KEY")); var world = (await _api.GetAllServers()).Servers[wId]; - if (world == null) return NotFound("World not found"); + if (world == null) + { + ErrorResponse errorResponse = new() + { + ErrorCode = 404, + ErrorMsg = "World not found" + }; + + return NotFound(errorResponse); + } var api = new MyMcAPI.Wrapper(world.OwnersToken); var whitelist = await api.GetWhitelist(); @@ -105,7 +133,16 @@ namespace MyMcRealms.Controllers // Get player name var playerInfo = await new HttpClient().GetFromJsonAsync($"https://sessionserver.mojang.com/session/minecraft/profile/{uuid}"); - if (!whitelist.Result.Any(p => p.Uuid.Replace("-", "") == uuid)) return BadRequest("Player not whitelisted"); + if (!whitelist.Result.Any(p => p.Uuid.Replace("-", "") == uuid)) + { + ErrorResponse errorResponse = new() + { + ErrorCode = 400, + ErrorMsg = "Player not whitelisted" + }; + + return BadRequest(errorResponse); + } api.ExecuteCommand($"whitelist remove {player.Name}"); @@ -115,7 +152,13 @@ namespace MyMcRealms.Controllers [HttpDelete("{wId}")] public ActionResult LeaveRealms(int wId) { - return BadRequest("You wish lmao"); + ErrorResponse errorResponse = new() + { + ErrorCode = 400, + ErrorMsg = "You with lmao" + }; + + return BadRequest(errorResponse); } } } \ No newline at end of file diff --git a/MyMcRealms/Controllers/SubscriptionsController.cs b/MyMcRealms/Controllers/SubscriptionsController.cs index 782bc58..0ab1238 100644 --- a/MyMcRealms/Controllers/SubscriptionsController.cs +++ b/MyMcRealms/Controllers/SubscriptionsController.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Mvc; +using Minecraft_Realms_Emulator.Responses; using MyMcRealms.Attributes; namespace Minecraft_Realms_Emulator.Controllers @@ -12,7 +13,13 @@ namespace Minecraft_Realms_Emulator.Controllers [CheckRealmOwner] public ActionResult GetSubscription(int wId) { - return BadRequest("No subscription for you :("); + ErrorResponse errorResponse = new() + { + ErrorCode = 400, + ErrorMsg = "No subscription for you :(" + }; + + return BadRequest(errorResponse); } } } \ No newline at end of file diff --git a/MyMcRealms/Controllers/WorldsController.cs b/MyMcRealms/Controllers/WorldsController.cs index 99f450d..60a06aa 100644 --- a/MyMcRealms/Controllers/WorldsController.cs +++ b/MyMcRealms/Controllers/WorldsController.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Mvc; +using Minecraft_Realms_Emulator.Responses; using MyMcRealms.Attributes; using MyMcRealms.MyMcAPI.Responses; using MyMcRealms.Responses; @@ -92,7 +93,16 @@ namespace MyMcRealms.Controllers var api = new MyMcAPI.Wrapper(world.OwnersToken); var whitelist = await api.GetWhitelist(); - if (whitelist == null) return BadRequest($"Cannot get data for world {wId}"); + if (whitelist == null) + { + ErrorResponse errorResponse = new() + { + ErrorCode = 400, + ErrorMsg = $"Cannot get data for world {wId}" + }; + + return BadRequest(errorResponse); + } string worldOwnerName = world.Ops.ToArray().Length == 0 ? "Owner" : world.Ops[0].Name; string worldOwnerUuid = world.Ops.ToArray().Length == 0 ? "069a79f444e94726a5befca90e38aaf5" : world.Ops[0].Uuid; @@ -162,14 +172,26 @@ namespace MyMcRealms.Controllers [CheckRealmOwner] public ActionResult UpdateRealms(int wId) { - return BadRequest("You can change the MOTD trough server.properties file"); + ErrorResponse errorResponse = new() + { + ErrorCode = 400, + ErrorMsg = "You can change the MOTD trough server.properties file" + }; + + return BadRequest(errorResponse); } [HttpPost("{wId}/reset")] [CheckRealmOwner] public ActionResult ChangeSlot(int wId) { - return BadRequest("lol nice try"); + ErrorResponse errorResponse = new() + { + ErrorCode = 400, + ErrorMsg = "lol nice try" + }; + + return BadRequest(errorResponse); } [HttpPut("{wId}/open")] @@ -181,7 +203,16 @@ namespace MyMcRealms.Controllers var world = (await _api.GetAllServers()).Servers[wId]; var api = new MyMcAPI.Wrapper(world.OwnersToken); - if (world == null) return NotFound("World not found"); + if (world == null) + { + ErrorResponse errorResponse = new() + { + ErrorCode = 404, + ErrorMsg = "World not found" + }; + + return NotFound(errorResponse); + } api.ExecuteCommand("whitelist off"); @@ -197,7 +228,16 @@ namespace MyMcRealms.Controllers var world = (await _api.GetAllServers()).Servers[wId]; var api = new MyMcAPI.Wrapper(world.OwnersToken); - if (world == null) return NotFound("World not found"); + if (world == null) + { + ErrorResponse errorResponse = new() + { + ErrorCode = 404, + ErrorMsg = "World not found" + }; + + return NotFound(errorResponse); + } api.ExecuteCommand("whitelist on"); @@ -208,14 +248,26 @@ namespace MyMcRealms.Controllers [CheckRealmOwner] public ActionResult UpdateSlot(int wId, int sId) { - return BadRequest("no."); + ErrorResponse errorResponse = new() + { + ErrorCode = 400, + ErrorMsg = "no." + }; + + return BadRequest(errorResponse); } [HttpGet("{wId}/slot/{sId}/download")] [CheckRealmOwner] public ActionResult GetBackups(int wId, int sId) { - return BadRequest("Wouldn't it be nice if you could download your world to singleplayer? Well I think that too"); + ErrorResponse errorResponse = new() + { + ErrorCode = 400, + ErrorMsg = "Wouldn't it be nice if you could download your world to singleplayer? Well I think that too" + }; + + return BadRequest(errorResponse); } [HttpGet("v1/{wId}/join/pc")] diff --git a/MyMcRealms/Middlewares/CheckRealmOwnerMiddleware.cs b/MyMcRealms/Middlewares/CheckRealmOwnerMiddleware.cs index 6d866f3..033e624 100644 --- a/MyMcRealms/Middlewares/CheckRealmOwnerMiddleware.cs +++ b/MyMcRealms/Middlewares/CheckRealmOwnerMiddleware.cs @@ -1,4 +1,5 @@ -using MyMcRealms.Attributes; +using Minecraft_Realms_Emulator.Responses; +using MyMcRealms.Attributes; using MyMcRealms.MyMcAPI.Responses; namespace Minecraft_Realms_Emulator.Middlewares @@ -25,21 +26,39 @@ namespace Minecraft_Realms_Emulator.Middlewares if (server == null) { + ErrorResponse errorResponse = new() + { + ErrorCode = 404, + ErrorMsg = "World not found" + }; + httpContext.Response.StatusCode = 404; - await httpContext.Response.WriteAsync("World not found"); + await httpContext.Response.WriteAsJsonAsync(errorResponse); return; } if (server.Ops.Count == 0) { + ErrorResponse errorResponse = new() + { + ErrorCode = 403, + ErrorMsg = "This world isn't owner by anyone" + }; + httpContext.Response.StatusCode = 403; - await httpContext.Response.WriteAsync("This world isn't owned by anyone"); + await httpContext.Response.WriteAsJsonAsync(errorResponse); return; } if (!attribute.IsRealmOwner(playerUUID, server.Ops[0].Uuid.Replace("-", ""))) { + ErrorResponse errorResponse = new() + { + ErrorCode = 403, + ErrorMsg = "You don't own this world" + }; + httpContext.Response.StatusCode = 403; - await httpContext.Response.WriteAsync("You don't own this world"); + await httpContext.Response.WriteAsJsonAsync(errorResponse); return; } diff --git a/MyMcRealms/Responses/ErrorResponse.cs b/MyMcRealms/Responses/ErrorResponse.cs new file mode 100644 index 0000000..304bffc --- /dev/null +++ b/MyMcRealms/Responses/ErrorResponse.cs @@ -0,0 +1,8 @@ +namespace Minecraft_Realms_Emulator.Responses +{ + public class ErrorResponse + { + public int ErrorCode { get; set; } + public string ErrorMsg { get; set; } = string.Empty; + } +}