From a6b70a06867c62ce3a2bad98b2e80e87f192fa91 Mon Sep 17 00:00:00 2001 From: CyberL1 Date: Tue, 11 Jun 2024 17:45:00 +0200 Subject: [PATCH] feat: error handling in `POST /worlds/{id}` and `POST /worlds/{wId}/slot/{sId}` --- .../Modes/External/WorldsController.cs | 37 +++++++++- .../Realms/Controllers/WorldsController.cs | 70 ++++++++++++++++++- 2 files changed, 103 insertions(+), 4 deletions(-) diff --git a/Minecraft-Realms-Emulator/Modes/External/WorldsController.cs b/Minecraft-Realms-Emulator/Modes/External/WorldsController.cs index 1406fc8..93ed3b2 100644 --- a/Minecraft-Realms-Emulator/Modes/External/WorldsController.cs +++ b/Minecraft-Realms-Emulator/Modes/External/WorldsController.cs @@ -324,8 +324,30 @@ namespace Minecraft_Realms_Emulator.Modes.External [HttpPost("{wId}")] [CheckForWorld] [CheckRealmOwner] - public async Task> UpdateWorld(int wId, WorldCreateRequest body) + public async Task> UpdateWorld(int wId, WorldCreateRequest body) { + if (body.Name.Length > 32) + { + ErrorResponse errorResponse = new() + { + ErrorCode = 400, + ErrorMsg = "World name cannot exceed 32 characters" + }; + + return BadRequest(errorResponse); + } + + if (body.Description.Length > 32) + { + ErrorResponse errorResponse = new() + { + ErrorCode = 400, + ErrorMsg = "World description cannot exceed 32 characters" + }; + + return BadRequest(errorResponse); + } + var worlds = await _context.Worlds.ToListAsync(); var world = worlds.Find(w => w.Id == wId); @@ -341,8 +363,19 @@ namespace Minecraft_Realms_Emulator.Modes.External [HttpPost("{wId}/slot/{sId}")] [CheckForWorld] [CheckRealmOwner] - public async Task> UpdateSlotAsync(int wId, int sId, SlotOptionsRequest body) + public async Task> UpdateSlot(int wId, int sId, SlotOptionsRequest body) { + if (body.SlotName.Length > 10) + { + ErrorResponse errorResponse = new() + { + ErrorCode = 400, + ErrorMsg = "Slot name cannot exceed 10 characters" + }; + + return BadRequest(errorResponse); + } + var slots = await _context.Slots.Where(s => s.World.Id == wId).ToListAsync(); var slot = slots.Find(s => s.SlotId == sId); diff --git a/Minecraft-Realms-Emulator/Modes/Realms/Controllers/WorldsController.cs b/Minecraft-Realms-Emulator/Modes/Realms/Controllers/WorldsController.cs index 8a12027..528eb72 100644 --- a/Minecraft-Realms-Emulator/Modes/Realms/Controllers/WorldsController.cs +++ b/Minecraft-Realms-Emulator/Modes/Realms/Controllers/WorldsController.cs @@ -347,8 +347,30 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers [CheckForWorld] [CheckRealmOwner] [CheckActiveSubscription] - public async Task> UpdateWorld(int wId, WorldCreateRequest body) + public async Task> UpdateWorld(int wId, WorldCreateRequest body) { + if (body.Name.Length > 32) + { + ErrorResponse errorResponse = new() + { + ErrorCode = 400, + ErrorMsg = "World name cannot exceed 32 characters" + }; + + return BadRequest(errorResponse); + } + + if (body.Description.Length > 32) + { + ErrorResponse errorResponse = new() + { + ErrorCode = 400, + ErrorMsg = "World description cannot exceed 32 characters" + }; + + return BadRequest(errorResponse); + } + var worlds = await _context.Worlds.ToListAsync(); var world = worlds.Find(w => w.Id == wId); @@ -365,8 +387,52 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers [CheckForWorld] [CheckRealmOwner] [CheckActiveSubscription] - public async Task> UpdateSlotAsync(int wId, int sId, SlotOptionsRequest body) + public async Task> UpdateSlot(int wId, int sId, SlotOptionsRequest body) { + if (body.SlotName.Length > 10) + { + ErrorResponse errorResponse = new() + { + ErrorCode = 400, + ErrorMsg = "Slot name cannot exceed 10 characters" + }; + + return BadRequest(errorResponse); + } + + if (body.SpawnProtection < 0 || body.SpawnProtection > 16) + { + ErrorResponse errorResponse = new() + { + ErrorCode = 400, + ErrorMsg = "Spawn protection can only be between 0 and 16" + }; + + return BadRequest(errorResponse); + } + + if (!new List{ 0, 1, 2 }.Contains(body.GameMode)) + { + ErrorResponse errorResponse = new() + { + ErrorCode = 400, + ErrorMsg = "Gamemode can only be one of 0, 1, 2" + }; + + return BadRequest(errorResponse); + } + + if (!new List { 0, 1, 2, 3 }.Contains(body.Difficulty)) + { + ErrorResponse errorResponse = new() + { + ErrorCode = 400, + ErrorMsg = "Difficulty can only be one of 0, 1, 2, 3" + }; + + return BadRequest(errorResponse); + } + var slots = await _context.Slots.Where(s => s.World.Id == wId).ToListAsync(); var slot = slots.Find(s => s.SlotId == sId);