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

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

Added permission management APIs

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