DataController.cs 2.9 KB

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