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

Last change on this file since 402 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
Line 
1using System.Collections.Generic;
2using System.Net;
3using JetBrains.Annotations;
4using Utf8Json;
5using Webserver;
6using Webserver.WebAPI;
7
8namespace Examples {
9 [UsedImplicitly]
10 public class Markers : AbsRestApi {
11 private const int numRandomMarkers = 5;
12
13 private readonly Dictionary<string, (int, int)> markers = new Dictionary<string, (int, int)> ();
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 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
30 protected override void HandleRestGet (RequestContext _context) {
31 string id = _context.RequestPath;
32
33 PrepareEnvelopedResult (out JsonWriter writer);
34
35 if (string.IsNullOrEmpty (id)) {
36 writer.WriteBeginArray ();
37
38 bool first = true;
39 foreach ((string markerId, (int, int) coordinates) in markers) {
40 if (!first) {
41 writer.WriteValueSeparator ();
42 }
43
44 first = false;
45
46 writeMarkerJson (ref writer, markerId, coordinates);
47 }
48
49 writer.WriteEndArray ();
50 SendEnvelopedResult (_context, ref writer);
51 return;
52 }
53
54 if (!markers.TryGetValue (id, out (int, int) location)) {
55 writer.WriteRaw (JsonEmptyData);
56 SendEnvelopedResult (_context, ref writer, HttpStatusCode.NotFound);
57 return;
58 }
59
60 {
61 writer.WriteBeginArray ();
62
63 writeMarkerJson (ref writer, id, location);
64
65 writer.WriteEndArray ();
66 SendEnvelopedResult (_context, ref writer);
67 }
68 }
69
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 }
79
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");
83 return;
84 }
85
86 if (!TryGetJsonField (_jsonInput, "lng", out int lng)) {
87 SendErrorResult (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_LNG");
88 return;
89 }
90
91 string newId = WebUtils.GenerateGuid ();
92 markers.Add (newId, (lat, lng));
93
94 PrepareEnvelopedResult (out JsonWriter writer);
95 writer.WriteString (newId);
96 SendEnvelopedResult (_context, ref writer, HttpStatusCode.Created);
97 }
98
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");
102 return;
103 }
104
105 if (!TryGetJsonField (_jsonInput, "lng", out int lng)) {
106 SendErrorResult (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_LNG");
107 return;
108 }
109
110 string id = _context.RequestPath;
111
112 if (!markers.TryGetValue (id, out _)) {
113 SendErrorResult (_context, HttpStatusCode.NotFound, _jsonInputData, "ID_NOT_FOUND");
114 return;
115 }
116
117 markers [id] = (lat, lng);
118
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);
128 }
129
130 protected override void HandleRestDelete (RequestContext _context) {
131 string id = _context.RequestPath;
132
133 PrepareEnvelopedResult (out JsonWriter writer);
134 writer.WriteRaw (JsonEmptyData);
135 SendEnvelopedResult (_context, ref writer, markers.Remove (id) ? HttpStatusCode.NoContent : HttpStatusCode.NotFound);
136 }
137 }
138}
Note: See TracBrowser for help on using the repository browser.