1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- using System;
- using System.Collections;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- using Newtonsoft.Json;
- namespace jsonapi.Controllers
- {
- [ApiController]
- [Route("room")]
- [Produces("application/json")]
- public class RoomController : ControllerBase
- {
- private IConfiguration configuration;
- private Library.SHA256HashGenerator sha256HashGenerator;
- private Library.JsonFileReader jsonFileReader;
- public RoomController(IConfiguration config)
- {
- configuration = config;
- sha256HashGenerator = Library.SHA256HashGenerator.GetInstance();
- jsonFileReader = Library.JsonFileReader.GetInstance();
- }
- [HttpGet]
- public IActionResult Get()
- {
- try
- {
- var json = jsonFileReader.Read("Static/room.json");
- return Ok(json);
- }
- 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);
- }
- }
- [HttpGet("{x},{y},{z}")]
- public IActionResult Get(int x, int y, int z)
- {
- string worldPath = configuration.GetSection("jsonapi").GetValue<string>("world");
- string filePath = worldPath + "/rooms/" + z + "/" + x + "," + y + ".json";
- if(!System.IO.File.Exists(filePath)) {
- var generator = Library.RoomGenerator.GetInstance();
- var body = generator.Generate(x, y, z);
- System.IO.File.WriteAllText(filePath, JsonConvert.SerializeObject(body, Formatting.Indented));
- return Ok(body);
- }
- try
- {
- var json = jsonFileReader.Read(filePath);
- return Ok(json);
- }
- catch (Exception)
- {
- dynamic errorObject = new
- {
- errorCode = StatusCodes.Status500InternalServerError,
- errorMessage = "500 Error: Something went wrong. Please contact the administrator for assistance.",
- filePath = filePath
- };
- return StatusCode(errorObject.errorCode, errorObject);
- }
- }
- }
- }
|