source: TFP-WebServer/MarkersMod/src/Markers.cs@ 453

Last change on this file since 453 was 434, checked in by alloc, 18 months ago

Added permission management APIs

File size: 5.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 const string defaultIcon =
15 "https://upload.wikimedia.org/wikipedia/commons/thumb/1/11/Blue_question_mark_icon.svg/1200px-Blue_question_mark_icon.svg.png";
16
17 private readonly Dictionary<string, (int, int, string)> markers = new Dictionary<string, (int, int, string)> ();
18
19 public Markers () {
20 GameRandom random = GameRandomManager.Instance.CreateGameRandom ();
21
22 for (int i = 0; i < numRandomMarkers; i++) {
23 int lat = random.RandomRange (-1000, 1001);
24 int lng = random.RandomRange (-1000, 1001);
25
26 markers.Add (WebUtils.GenerateGuid (), (lat, lng, null));
27 }
28
29 GameRandomManager.Instance.FreeGameRandom (random);
30 }
31
32 private static readonly byte[] jsonKeyId = JsonWriter.GetEncodedPropertyNameWithBeginObject ("id");
33 private static readonly byte[] jsonKeyLat = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("lat");
34 private static readonly byte[] jsonKeyLng = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("lng");
35 private static readonly byte[] jsonKeyIcon = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("icon");
36
37 protected override void HandleRestGet (RequestContext _context) {
38 string id = _context.RequestPath;
39
40 PrepareEnvelopedResult (out JsonWriter writer);
41
42 if (string.IsNullOrEmpty (id)) {
43 writer.WriteBeginArray ();
44
45 bool first = true;
46 foreach ((string markerId, (int, int, string) properties) in markers) {
47 if (!first) {
48 writer.WriteValueSeparator ();
49 }
50
51 first = false;
52
53 writeMarkerJson (ref writer, markerId, properties);
54 }
55
56 writer.WriteEndArray ();
57 SendEnvelopedResult (_context, ref writer);
58 return;
59 }
60
61 if (!markers.TryGetValue (id, out (int, int, string) properties2)) {
62 writer.WriteRaw (WebUtils.JsonEmptyData);
63 SendEnvelopedResult (_context, ref writer, HttpStatusCode.NotFound);
64 return;
65 }
66
67 {
68 writer.WriteBeginArray ();
69
70 writeMarkerJson (ref writer, id, properties2);
71
72 writer.WriteEndArray ();
73 SendEnvelopedResult (_context, ref writer);
74 }
75 }
76
77 private void writeMarkerJson (ref JsonWriter _writer, string _markerId, (int, int, string) _properties) {
78 _writer.WriteRaw (jsonKeyId);
79 _writer.WriteString (_markerId);
80 _writer.WriteRaw (jsonKeyLat);
81 (int lat, int lng, string icon) = _properties;
82 _writer.WriteInt32 (lat);
83 _writer.WriteRaw (jsonKeyLng);
84 _writer.WriteInt32 (lng);
85 _writer.WriteRaw (jsonKeyIcon);
86 _writer.WriteString (icon ?? defaultIcon);
87 _writer.WriteEndObject ();
88 }
89
90 protected override void HandleRestPost (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) {
91 if (!JsonCommons.TryGetJsonField (_jsonInput, "lat", out int lat)) {
92 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_LAT");
93 return;
94 }
95
96 if (!JsonCommons.TryGetJsonField (_jsonInput, "lng", out int lng)) {
97 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_LNG");
98 return;
99 }
100
101 JsonCommons.TryGetJsonField (_jsonInput, "icon", out string icon);
102 if (string.IsNullOrEmpty (icon)) {
103 icon = null;
104 }
105
106 string newId = WebUtils.GenerateGuid ();
107 markers.Add (newId, (lat, lng, icon));
108
109 PrepareEnvelopedResult (out JsonWriter writer);
110 writer.WriteString (newId);
111 SendEnvelopedResult (_context, ref writer, HttpStatusCode.Created);
112 }
113
114 protected override void HandleRestPut (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) {
115 if (!JsonCommons.TryGetJsonField (_jsonInput, "lat", out int lat)) {
116 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_LAT");
117 return;
118 }
119
120 if (!JsonCommons.TryGetJsonField (_jsonInput, "lng", out int lng)) {
121 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_LNG");
122 return;
123 }
124
125 bool keepIcon = !_jsonInput.TryGetValue ("icon", out _);
126 JsonCommons.TryGetJsonField (_jsonInput, "icon", out string icon);
127 if (string.IsNullOrEmpty (icon)) {
128 icon = null;
129 }
130
131 string id = _context.RequestPath;
132
133 if (!markers.TryGetValue (id, out (int, int, string) properties)) {
134 SendEmptyResponse (_context, HttpStatusCode.NotFound, _jsonInputData, "ID_NOT_FOUND");
135 return;
136 }
137
138 if (keepIcon) {
139 icon = properties.Item3;
140 }
141 markers [id] = (lat, lng, icon);
142
143 PrepareEnvelopedResult (out JsonWriter writer);
144 writer.WriteRaw (jsonKeyId);
145 writer.WriteString (id);
146 writer.WriteRaw (jsonKeyLat);
147 writer.WriteInt32 (lat);
148 writer.WriteRaw (jsonKeyLng);
149 writer.WriteInt32 (lng);
150 writer.WriteRaw (jsonKeyIcon);
151 writer.WriteString (icon);
152 writer.WriteEndObject ();
153 SendEnvelopedResult (_context, ref writer);
154 }
155
156 protected override void HandleRestDelete (RequestContext _context) {
157 string id = _context.RequestPath;
158
159 SendEmptyResponse (_context, markers.Remove (id) ? HttpStatusCode.NoContent : HttpStatusCode.NotFound);
160 }
161
162 public override int[] DefaultMethodPermissionLevels () => new[] {
163 AdminWebModules.MethodLevelNotSupported,
164 AdminWebModules.PermissionLevelGuest,
165 AdminWebModules.PermissionLevelUser,
166 AdminWebModules.MethodLevelInheritGlobal,
167 AdminWebModules.MethodLevelInheritGlobal
168 };
169 }
170}
Note: See TracBrowser for help on using the repository browser.