using System; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Newtonsoft.Json.Linq; namespace jsonapi.Controllers { [ApiController] [Route("user")] [Produces("application/json")] public class UserController : ControllerBase { private IConfiguration configuration; private Library.JsonFileReader jsonFileReader; public UserController(IConfiguration config) { configuration = config; jsonFileReader = Library.JsonFileReader.GetInstance(); } [HttpGet("{id}")] public IActionResult Get(string id) { string worldPath = configuration.GetSection("jsonapi").GetValue("world"); var fileName = worldPath + "/users.json"; try { var json = jsonFileReader.Read(fileName); string userFilePath = worldPath + "/users/" + json[id] + ".json"; try { var userJson = jsonFileReader.Read(userFilePath); dynamic simpleUser = new { name = userJson["name"], }; return Ok(simpleUser); } catch (Exception) { dynamic errorObject = new { errorCode = StatusCodes.Status404NotFound, errorMessage = "404 Error: Invalid user data." }; return StatusCode(errorObject.errorCode, errorObject); } } catch (Exception) { dynamic errorObject = new { errorCode = StatusCodes.Status500InternalServerError, errorMessage = "500 Error: Something went wrong. Please contact the administrator for assistance." }; return StatusCode(errorObject.errorCode, errorObject); } } } }