source: binary-improvements2/MarkersMod/src/Markers.cs@ 401

Last change on this file since 401 was 391, checked in by alloc, 2 years ago

Major refactoring/cleanup

File size: 3.9 KB
RevLine 
[388]1using System.Collections.Generic;
2using System.Net;
3using AllocsFixes.JSON;
[391]4using Webserver;
5using Webserver.WebAPI;
[388]6
[390]7namespace Examples {
[391]8 public class Markers : AbsRestApi {
9 private const int numRandomMarkers = 5;
[388]10
[390]11 private readonly Dictionary<string, (int, int)> markers = new Dictionary<string, (int, int)> ();
12
[391]13 private static readonly JsonArray emptyResult = new JsonArray ();
[388]14
15 public Markers () {
[390]16 GameRandom random = GameRandomManager.Instance.CreateGameRandom ();
17
[391]18 for (int i = 0; i < numRandomMarkers; i++) {
[390]19 int lat = random.RandomRange (-1000, 1001);
20 int lng = random.RandomRange (-1000, 1001);
21
22 markers.Add (WebUtils.GenerateGuid (), (lat, lng));
23 }
[388]24 }
25
26 protected override void HandleRestGet (RequestContext _context) {
27 string id = _context.RequestPath;
28
29 if (string.IsNullOrEmpty (id)) {
[391]30 JsonArray result = new JsonArray ();
[388]31
[391]32 foreach ((string markerId, (int, int) coordinates) in markers) {
33 JsonObject marker = new JsonObject ();
34 marker.Add ("id", new JsonString (markerId));
35 marker.Add ("lat", new JsonNumber (coordinates.Item1));
36 marker.Add ("lng", new JsonNumber (coordinates.Item2));
[388]37 result.Add (marker);
38 }
39
40 SendEnvelopedResult (_context, result);
41 return;
42 }
43
[390]44 if (!markers.TryGetValue (id, out (int, int) location)) {
[388]45 SendEnvelopedResult (_context, emptyResult, HttpStatusCode.NotFound);
46 return;
47 }
48
49 {
[391]50 JsonArray result = new JsonArray ();
51 JsonObject marker = new JsonObject ();
52 marker.Add ("id", new JsonString (id));
53 marker.Add ("lat", new JsonNumber (location.Item1));
54 marker.Add ("lng", new JsonNumber (location.Item2));
[388]55 result.Add (marker);
56 SendEnvelopedResult (_context, result);
57 }
58 }
59
[391]60 protected override void HandleRestPost (RequestContext _context, JsonNode _jsonBody) {
61 if (!(_jsonBody is JsonObject bodyObject)) {
[390]62 SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, _jsonBody, "BODY_NOT_OBJECT");
63 return;
[388]64 }
65
[390]66 if (!TryGetJsonField (bodyObject, "lat", out int lat)) {
67 SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, _jsonBody, "NO_OR_INVALID_LAT");
68 return;
69 }
70
71 if (!TryGetJsonField (bodyObject, "lng", out int lng)) {
72 SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, _jsonBody, "NO_OR_INVALID_LNG");
73 return;
74 }
75
76 string newId = WebUtils.GenerateGuid ();
77 markers.Add (newId, (lat, lng));
78
[391]79 JsonString result = new JsonString (newId);
80 SendEnvelopedResult (_context, result, HttpStatusCode.Created);
[388]81 }
82
[391]83 protected override void HandleRestPut (RequestContext _context, JsonNode _jsonBody) {
84 if (!(_jsonBody is JsonObject bodyObject)) {
[390]85 SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, _jsonBody, "BODY_NOT_OBJECT");
86 return;
87 }
88
89 if (!TryGetJsonField (bodyObject, "lat", out int lat)) {
90 SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, _jsonBody, "NO_OR_INVALID_LAT");
91 return;
92 }
93
94 if (!TryGetJsonField (bodyObject, "lng", out int lng)) {
95 SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, _jsonBody, "NO_OR_INVALID_LNG");
96 return;
97 }
98
99 string id = _context.RequestPath;
100
[391]101 if (!markers.TryGetValue (id, out _)) {
[390]102 SendEnvelopedResult (_context, null, HttpStatusCode.NotFound, _jsonBody, "ID_NOT_FOUND");
103 return;
104 }
105
106 markers [id] = (lat, lng);
107
[391]108 JsonObject result = new JsonObject ();
109 result.Add ("id", new JsonString (id));
110 result.Add ("lat", new JsonNumber (lat));
111 result.Add ("lng", new JsonNumber (lng));
[390]112 SendEnvelopedResult (_context, result);
[388]113 }
114
115 protected override void HandleRestDelete (RequestContext _context) {
[390]116 string id = _context.RequestPath;
117
118 SendEnvelopedResult (_context, null, markers.Remove (id) ? HttpStatusCode.NoContent : HttpStatusCode.NotFound);
[388]119 }
120 }
121}
Note: See TracBrowser for help on using the repository browser.