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

Last change on this file since 414 was 402, checked in by alloc, 22 months ago
  • Major refactoring
  • Using Utf8Json for (de)serialization
  • Moving APIs to REST
  • Removing dependencies from WebServer and MapRenderer to ServerFixes
File size: 4.4 KB
RevLine 
[388]1using System.Collections.Generic;
2using System.Net;
[402]3using JetBrains.Annotations;
4using Utf8Json;
[391]5using Webserver;
6using Webserver.WebAPI;
[388]7
[390]8namespace Examples {
[402]9 [UsedImplicitly]
[391]10 public class Markers : AbsRestApi {
11 private const int numRandomMarkers = 5;
[388]12
[390]13 private readonly Dictionary<string, (int, int)> markers = new Dictionary<string, (int, int)> ();
14
[388]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
[402]26 private static readonly byte[] jsonKeyId = JsonWriter.GetEncodedPropertyNameWithBeginObject ("id");
27 private static readonly byte[] jsonKeyLat = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("lat");
28 private static readonly byte[] jsonKeyLng = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("lng");
29
[388]30 protected override void HandleRestGet (RequestContext _context) {
31 string id = _context.RequestPath;
32
[402]33 PrepareEnvelopedResult (out JsonWriter writer);
34
[388]35 if (string.IsNullOrEmpty (id)) {
[402]36 writer.WriteBeginArray ();
[388]37
[402]38 bool first = true;
[391]39 foreach ((string markerId, (int, int) coordinates) in markers) {
[402]40 if (!first) {
41 writer.WriteValueSeparator ();
42 }
43
44 first = false;
45
46 writeMarkerJson (ref writer, markerId, coordinates);
[388]47 }
[402]48
49 writer.WriteEndArray ();
50 SendEnvelopedResult (_context, ref writer);
[388]51 return;
52 }
53
[390]54 if (!markers.TryGetValue (id, out (int, int) location)) {
[402]55 writer.WriteRaw (JsonEmptyData);
56 SendEnvelopedResult (_context, ref writer, HttpStatusCode.NotFound);
[388]57 return;
58 }
59
60 {
[402]61 writer.WriteBeginArray ();
62
63 writeMarkerJson (ref writer, id, location);
64
65 writer.WriteEndArray ();
66 SendEnvelopedResult (_context, ref writer);
[388]67 }
68 }
69
[402]70 private void writeMarkerJson (ref JsonWriter _writer, string _markerId, (int, int) _coordinates) {
71 _writer.WriteRaw (jsonKeyId);
72 _writer.WriteString (_markerId);
73 _writer.WriteRaw (jsonKeyLat);
74 _writer.WriteInt32 (_coordinates.Item1);
75 _writer.WriteRaw (jsonKeyLng);
76 _writer.WriteInt32 (_coordinates.Item2);
77 _writer.WriteEndObject ();
78 }
[388]79
[402]80 protected override void HandleRestPost (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) {
81 if (!TryGetJsonField (_jsonInput, "lat", out int lat)) {
82 SendErrorResult (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_LAT");
[390]83 return;
84 }
85
[402]86 if (!TryGetJsonField (_jsonInput, "lng", out int lng)) {
87 SendErrorResult (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_LNG");
[390]88 return;
89 }
90
91 string newId = WebUtils.GenerateGuid ();
92 markers.Add (newId, (lat, lng));
93
[402]94 PrepareEnvelopedResult (out JsonWriter writer);
95 writer.WriteString (newId);
96 SendEnvelopedResult (_context, ref writer, HttpStatusCode.Created);
[388]97 }
98
[402]99 protected override void HandleRestPut (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) {
100 if (!TryGetJsonField (_jsonInput, "lat", out int lat)) {
101 SendErrorResult (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_LAT");
[390]102 return;
103 }
104
[402]105 if (!TryGetJsonField (_jsonInput, "lng", out int lng)) {
106 SendErrorResult (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_LNG");
[390]107 return;
108 }
109
110 string id = _context.RequestPath;
111
[391]112 if (!markers.TryGetValue (id, out _)) {
[402]113 SendErrorResult (_context, HttpStatusCode.NotFound, _jsonInputData, "ID_NOT_FOUND");
[390]114 return;
115 }
116
117 markers [id] = (lat, lng);
118
[402]119 PrepareEnvelopedResult (out JsonWriter writer);
120 writer.WriteRaw (jsonKeyId);
121 writer.WriteString (id);
122 writer.WriteRaw (jsonKeyLat);
123 writer.WriteInt32 (lat);
124 writer.WriteRaw (jsonKeyLng);
125 writer.WriteInt32 (lng);
126 writer.WriteEndObject ();
127 SendEnvelopedResult (_context, ref writer);
[388]128 }
129
130 protected override void HandleRestDelete (RequestContext _context) {
[390]131 string id = _context.RequestPath;
132
[402]133 PrepareEnvelopedResult (out JsonWriter writer);
134 writer.WriteRaw (JsonEmptyData);
135 SendEnvelopedResult (_context, ref writer, markers.Remove (id) ? HttpStatusCode.NoContent : HttpStatusCode.NotFound);
[388]136 }
137 }
138}
Note: See TracBrowser for help on using the repository browser.