DataController.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Configuration;
  5. using Newtonsoft.Json;
  6. namespace jsonapi.Controllers
  7. {
  8. [ApiController]
  9. [Route("data")]
  10. [Produces("application/json")]
  11. public class DataController : ControllerBase
  12. {
  13. private IConfiguration configuration;
  14. private Library.SHA256HashGenerator sha256HashGenerator;
  15. private Library.JsonFileReader jsonFileReader;
  16. public DataController(IConfiguration config)
  17. {
  18. configuration = config;
  19. sha256HashGenerator = Library.SHA256HashGenerator.GetInstance();
  20. jsonFileReader = Library.JsonFileReader.GetInstance();
  21. }
  22. [HttpGet("{key}")]
  23. public IActionResult Get(string key)
  24. {
  25. string worldPath = configuration.GetSection("jsonapi").GetValue<string>("world");
  26. string filePath = worldPath + "/data/" + sha256HashGenerator.Get(key) + ".json";
  27. if(!System.IO.File.Exists(filePath)) {
  28. dynamic errorObject = new
  29. {
  30. errorCode = StatusCodes.Status404NotFound,
  31. errorMessage = "404 Error: That key has no associated data.",
  32. filePath = filePath
  33. };
  34. return StatusCode(errorObject.errorCode, errorObject);
  35. }
  36. try
  37. {
  38. var json = jsonFileReader.Read(filePath);
  39. return Ok(json);
  40. }
  41. catch (Exception)
  42. {
  43. dynamic errorObject = new
  44. {
  45. errorCode = StatusCodes.Status500InternalServerError,
  46. errorMessage = "500 Error: Something went wrong. Please contact the administrator for assistance.",
  47. filePath = filePath
  48. };
  49. return StatusCode(errorObject.errorCode, errorObject);
  50. }
  51. }
  52. [HttpPost("{key}")]
  53. public IActionResult Save(string key, [FromBody] dynamic body)
  54. {
  55. string worldPath = configuration.GetSection("jsonapi").GetValue<string>("world");
  56. string filePath = worldPath + "/data/" + sha256HashGenerator.Get(key) + ".json";
  57. dynamic json = body;
  58. if(System.IO.File.Exists(filePath)) {
  59. try
  60. {
  61. json = jsonFileReader.Read(filePath);
  62. foreach (var prop in body)
  63. {
  64. json[prop.Name] = prop.Value;
  65. }
  66. }
  67. catch (Exception e)
  68. {
  69. Console.WriteLine("Save Error: " + e.Message);
  70. dynamic errorObject = new
  71. {
  72. errorCode = StatusCodes.Status500InternalServerError,
  73. errorMessage = "500 Error: Something went wrong. Please contact the administrator for assistance.",
  74. filePath = filePath
  75. };
  76. return StatusCode(errorObject.errorCode, errorObject);
  77. }
  78. }
  79. System.IO.File.WriteAllText(filePath, JsonConvert.SerializeObject(json, Formatting.Indented));
  80. return Ok(json);
  81. }
  82. [HttpPut("{key}")]
  83. public IActionResult Put(string key, [FromBody] dynamic body)
  84. {
  85. string worldPath = configuration.GetSection("jsonapi").GetValue<string>("world");
  86. string filePath = worldPath + "/data/" + sha256HashGenerator.Get(key) + ".json";
  87. System.IO.File.WriteAllText(filePath, JsonConvert.SerializeObject(body, Formatting.Indented));
  88. return Ok(body);
  89. }
  90. [HttpDelete("{key}")]
  91. public IActionResult Delete(string key)
  92. {
  93. string worldPath = configuration.GetSection("jsonapi").GetValue<string>("world");
  94. string filePath = worldPath + "/data/" + sha256HashGenerator.Get(key) + ".json";
  95. System.IO.File.Delete(filePath);
  96. return Ok();
  97. }
  98. }
  99. }