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

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

Major refactoring/cleanup

File size: 3.9 KB
Line 
1using System.Collections.Generic;
2using System.Net;
3using AllocsFixes.JSON;
4using Webserver;
5using Webserver.WebAPI;
6
7namespace Examples {
8 public class Markers : AbsRestApi {
9 private const int numRandomMarkers = 5;
10
11 private readonly Dictionary<string, (int, int)> markers = new Dictionary<string, (int, int)> ();
12
13 private static readonly JsonArray emptyResult = new JsonArray ();
14
15 public Markers () {
16 GameRandom random = GameRandomManager.Instance.CreateGameRandom ();
17
18 for (int i = 0; i < numRandomMarkers; i++) {
19 int lat = random.RandomRange (-1000, 1001);
20 int lng = random.RandomRange (-1000, 1001);
21
22 markers.Add (WebUtils.GenerateGuid (), (lat, lng));
23 }
24 }
25
26 protected override void HandleRestGet (RequestContext _context) {
27 string id = _context.RequestPath;
28
29 if (string.IsNullOrEmpty (id)) {
30 JsonArray result = new JsonArray ();
31
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));
37 result.Add (marker);
38 }
39
40 SendEnvelopedResult (_context, result);
41 return;
42 }
43
44 if (!markers.TryGetValue (id, out (int, int) location)) {
45 SendEnvelopedResult (_context, emptyResult, HttpStatusCode.NotFound);
46 return;
47 }
48
49 {
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));
55 result.Add (marker);
56 SendEnvelopedResult (_context, result);
57 }
58 }
59
60 protected override void HandleRestPost (RequestContext _context, JsonNode _jsonBody) {
61 if (!(_jsonBody is JsonObject bodyObject)) {
62 SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, _jsonBody, "BODY_NOT_OBJECT");
63 return;
64 }
65
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
79 JsonString result = new JsonString (newId);
80 SendEnvelopedResult (_context, result, HttpStatusCode.Created);
81 }
82
83 protected override void HandleRestPut (RequestContext _context, JsonNode _jsonBody) {
84 if (!(_jsonBody is JsonObject bodyObject)) {
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
101 if (!markers.TryGetValue (id, out _)) {
102 SendEnvelopedResult (_context, null, HttpStatusCode.NotFound, _jsonBody, "ID_NOT_FOUND");
103 return;
104 }
105
106 markers [id] = (lat, lng);
107
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));
112 SendEnvelopedResult (_context, result);
113 }
114
115 protected override void HandleRestDelete (RequestContext _context) {
116 string id = _context.RequestPath;
117
118 SendEnvelopedResult (_context, null, markers.Remove (id) ? HttpStatusCode.NoContent : HttpStatusCode.NotFound);
119 }
120 }
121}
Note: See TracBrowser for help on using the repository browser.