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

Last change on this file since 426 was 426, checked in by alloc, 19 months ago

*Updated web permissions system
*Fixed webpermissions command
*Moved API "webmods" to "mods", also lists non-webmod mods

File size: 4.7 KB
Line 
1using System.Collections.Generic;
2using System.Net;
3using JetBrains.Annotations;
4using Utf8Json;
5using Webserver;
6using Webserver.Permissions;
7using Webserver.WebAPI;
8
9namespace Examples {
10 [UsedImplicitly]
11 public class Markers : AbsRestApi {
12 private const int numRandomMarkers = 5;
13
14 private readonly Dictionary<string, (int, int)> markers = new Dictionary<string, (int, int)> ();
15
16 public Markers () {
17 GameRandom random = GameRandomManager.Instance.CreateGameRandom ();
18
19 for (int i = 0; i < numRandomMarkers; i++) {
20 int lat = random.RandomRange (-1000, 1001);
21 int lng = random.RandomRange (-1000, 1001);
22
23 markers.Add (WebUtils.GenerateGuid (), (lat, lng));
24 }
25 }
26
27 private static readonly byte[] jsonKeyId = JsonWriter.GetEncodedPropertyNameWithBeginObject ("id");
28 private static readonly byte[] jsonKeyLat = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("lat");
29 private static readonly byte[] jsonKeyLng = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("lng");
30
31 protected override void HandleRestGet (RequestContext _context) {
32 string id = _context.RequestPath;
33
34 PrepareEnvelopedResult (out JsonWriter writer);
35
36 if (string.IsNullOrEmpty (id)) {
37 writer.WriteBeginArray ();
38
39 bool first = true;
40 foreach ((string markerId, (int, int) coordinates) in markers) {
41 if (!first) {
42 writer.WriteValueSeparator ();
43 }
44
45 first = false;
46
47 writeMarkerJson (ref writer, markerId, coordinates);
48 }
49
50 writer.WriteEndArray ();
51 SendEnvelopedResult (_context, ref writer);
52 return;
53 }
54
55 if (!markers.TryGetValue (id, out (int, int) location)) {
56 writer.WriteRaw (JsonEmptyData);
57 SendEnvelopedResult (_context, ref writer, HttpStatusCode.NotFound);
58 return;
59 }
60
61 {
62 writer.WriteBeginArray ();
63
64 writeMarkerJson (ref writer, id, location);
65
66 writer.WriteEndArray ();
67 SendEnvelopedResult (_context, ref writer);
68 }
69 }
70
71 private void writeMarkerJson (ref JsonWriter _writer, string _markerId, (int, int) _coordinates) {
72 _writer.WriteRaw (jsonKeyId);
73 _writer.WriteString (_markerId);
74 _writer.WriteRaw (jsonKeyLat);
75 _writer.WriteInt32 (_coordinates.Item1);
76 _writer.WriteRaw (jsonKeyLng);
77 _writer.WriteInt32 (_coordinates.Item2);
78 _writer.WriteEndObject ();
79 }
80
81 protected override void HandleRestPost (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) {
82 if (!TryGetJsonField (_jsonInput, "lat", out int lat)) {
83 SendErrorResult (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_LAT");
84 return;
85 }
86
87 if (!TryGetJsonField (_jsonInput, "lng", out int lng)) {
88 SendErrorResult (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_LNG");
89 return;
90 }
91
92 string newId = WebUtils.GenerateGuid ();
93 markers.Add (newId, (lat, lng));
94
95 PrepareEnvelopedResult (out JsonWriter writer);
96 writer.WriteString (newId);
97 SendEnvelopedResult (_context, ref writer, HttpStatusCode.Created);
98 }
99
100 protected override void HandleRestPut (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) {
101 if (!TryGetJsonField (_jsonInput, "lat", out int lat)) {
102 SendErrorResult (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_LAT");
103 return;
104 }
105
106 if (!TryGetJsonField (_jsonInput, "lng", out int lng)) {
107 SendErrorResult (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_LNG");
108 return;
109 }
110
111 string id = _context.RequestPath;
112
113 if (!markers.TryGetValue (id, out _)) {
114 SendErrorResult (_context, HttpStatusCode.NotFound, _jsonInputData, "ID_NOT_FOUND");
115 return;
116 }
117
118 markers [id] = (lat, lng);
119
120 PrepareEnvelopedResult (out JsonWriter writer);
121 writer.WriteRaw (jsonKeyId);
122 writer.WriteString (id);
123 writer.WriteRaw (jsonKeyLat);
124 writer.WriteInt32 (lat);
125 writer.WriteRaw (jsonKeyLng);
126 writer.WriteInt32 (lng);
127 writer.WriteEndObject ();
128 SendEnvelopedResult (_context, ref writer);
129 }
130
131 protected override void HandleRestDelete (RequestContext _context) {
132 string id = _context.RequestPath;
133
134 PrepareEnvelopedResult (out JsonWriter writer);
135 writer.WriteRaw (JsonEmptyData);
136 SendEnvelopedResult (_context, ref writer, markers.Remove (id) ? HttpStatusCode.NoContent : HttpStatusCode.NotFound);
137 }
138
139 public override int[] DefaultMethodPermissionLevels () => new[] {
140 AdminWebModules.MethodLevelNotSupported,
141 AdminWebModules.PermissionLevelGuest,
142 AdminWebModules.PermissionLevelUser,
143 AdminWebModules.MethodLevelInheritGlobal,
144 AdminWebModules.MethodLevelInheritGlobal
145 };
146 }
147}
Note: See TracBrowser for help on using the repository browser.