mirror of
https://github.com/CyberL1/MyMcRealms.git
synced 2024-11-23 14:38:21 -05:00
feat: send errors as json instead of text
This commit is contained in:
parent
a25b93ae9f
commit
6bad1e41eb
@ -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<MinecraftPlayerResponse>($"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<MinecraftPlayerResponse>($"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<string> LeaveRealms(int wId)
|
||||
{
|
||||
return BadRequest("You wish lmao");
|
||||
ErrorResponse errorResponse = new()
|
||||
{
|
||||
ErrorCode = 400,
|
||||
ErrorMsg = "You with lmao"
|
||||
};
|
||||
|
||||
return BadRequest(errorResponse);
|
||||
}
|
||||
}
|
||||
}
|
@ -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<string> GetSubscription(int wId)
|
||||
{
|
||||
return BadRequest("No subscription for you :(");
|
||||
ErrorResponse errorResponse = new()
|
||||
{
|
||||
ErrorCode = 400,
|
||||
ErrorMsg = "No subscription for you :("
|
||||
};
|
||||
|
||||
return BadRequest(errorResponse);
|
||||
}
|
||||
}
|
||||
}
|
@ -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<string> 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<string> 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<string> 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<string> 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")]
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
8
MyMcRealms/Responses/ErrorResponse.cs
Normal file
8
MyMcRealms/Responses/ErrorResponse.cs
Normal file
@ -0,0 +1,8 @@
|
||||
namespace Minecraft_Realms_Emulator.Responses
|
||||
{
|
||||
public class ErrorResponse
|
||||
{
|
||||
public int ErrorCode { get; set; }
|
||||
public string ErrorMsg { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user