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