123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- using System;
- using System.Reflection;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- using Newtonsoft.Json;
- namespace jsonapi.Controllers
- {
- [ApiController]
- [Route("data")]
- [Produces("application/json")]
- public class DataController : ControllerBase
- {
- private IConfiguration configuration;
- private Library.SHA256HashGenerator sha256HashGenerator;
- private Library.JsonFileReader jsonFileReader;
- public DataController(IConfiguration config)
- {
- configuration = config;
- sha256HashGenerator = Library.SHA256HashGenerator.GetInstance();
- jsonFileReader = Library.JsonFileReader.GetInstance();
- }
- [HttpGet("{key}")]
- public IActionResult Get(string key)
- {
- string worldPath = configuration.GetSection("jsonapi").GetValue<string>("world");
- string filePath = worldPath + "/data/" + sha256HashGenerator.Get(key) + ".json";
- if(!System.IO.File.Exists(filePath)) {
- dynamic errorObject = new
- {
- errorCode = StatusCodes.Status404NotFound,
- errorMessage = "404 Error: That key has no associated data.",
- filePath = filePath
- };
- return StatusCode(errorObject.errorCode, errorObject);
- }
- 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);
- }
- }
- [HttpPost("{key}")]
- public IActionResult Save(string key, [FromBody] dynamic body)
- {
- string worldPath = configuration.GetSection("jsonapi").GetValue<string>("world");
- string filePath = worldPath + "/data/" + sha256HashGenerator.Get(key) + ".json";
- dynamic json = body;
- if(System.IO.File.Exists(filePath)) {
- try
- {
- json = jsonFileReader.Read(filePath);
- foreach (var prop in body)
- {
- json[prop.Name] = prop.Value;
- }
- }
- catch (Exception) { }
- }
-
- System.IO.File.WriteAllText(filePath, JsonConvert.SerializeObject(json, Formatting.Indented));
- return Ok(json);
- }
- [HttpPut("{key}")]
- public IActionResult Put(string key, [FromBody] dynamic body)
- {
- string worldPath = configuration.GetSection("jsonapi").GetValue<string>("world");
- string filePath = worldPath + "/data/" + sha256HashGenerator.Get(key) + ".json";
- System.IO.File.WriteAllText(filePath, JsonConvert.SerializeObject(body, Formatting.Indented));
- return Ok(body);
- }
- [HttpDelete("{key}")]
- public IActionResult Delete(string key)
- {
- string worldPath = configuration.GetSection("jsonapi").GetValue<string>("world");
- string filePath = worldPath + "/data/" + sha256HashGenerator.Get(key) + ".json";
- System.IO.File.Delete(filePath);
- return Ok();
- }
- }
- }
|