[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 |
|
---|
[390] | 14 | private readonly Dictionary<string, (int, int)> markers = new Dictionary<string, (int, int)> ();
|
---|
| 15 |
|
---|
[388] | 16 | public Markers () {
|
---|
[390] | 17 | GameRandom random = GameRandomManager.Instance.CreateGameRandom ();
|
---|
| 18 |
|
---|
[391] | 19 | for (int i = 0; i < numRandomMarkers; i++) {
|
---|
[390] | 20 | int lat = random.RandomRange (-1000, 1001);
|
---|
| 21 | int lng = random.RandomRange (-1000, 1001);
|
---|
| 22 |
|
---|
| 23 | markers.Add (WebUtils.GenerateGuid (), (lat, lng));
|
---|
| 24 | }
|
---|
[388] | 25 | }
|
---|
| 26 |
|
---|
[402] | 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 |
|
---|
[388] | 31 | protected override void HandleRestGet (RequestContext _context) {
|
---|
| 32 | string id = _context.RequestPath;
|
---|
| 33 |
|
---|
[402] | 34 | PrepareEnvelopedResult (out JsonWriter writer);
|
---|
| 35 |
|
---|
[388] | 36 | if (string.IsNullOrEmpty (id)) {
|
---|
[402] | 37 | writer.WriteBeginArray ();
|
---|
[388] | 38 |
|
---|
[402] | 39 | bool first = true;
|
---|
[391] | 40 | foreach ((string markerId, (int, int) coordinates) in markers) {
|
---|
[402] | 41 | if (!first) {
|
---|
| 42 | writer.WriteValueSeparator ();
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | first = false;
|
---|
| 46 |
|
---|
| 47 | writeMarkerJson (ref writer, markerId, coordinates);
|
---|
[388] | 48 | }
|
---|
[402] | 49 |
|
---|
| 50 | writer.WriteEndArray ();
|
---|
| 51 | SendEnvelopedResult (_context, ref writer);
|
---|
[388] | 52 | return;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[390] | 55 | if (!markers.TryGetValue (id, out (int, int) location)) {
|
---|
[402] | 56 | writer.WriteRaw (JsonEmptyData);
|
---|
| 57 | SendEnvelopedResult (_context, ref writer, HttpStatusCode.NotFound);
|
---|
[388] | 58 | return;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | {
|
---|
[402] | 62 | writer.WriteBeginArray ();
|
---|
| 63 |
|
---|
| 64 | writeMarkerJson (ref writer, id, location);
|
---|
| 65 |
|
---|
| 66 | writer.WriteEndArray ();
|
---|
| 67 | SendEnvelopedResult (_context, ref writer);
|
---|
[388] | 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[402] | 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 | }
|
---|
[388] | 80 |
|
---|
[402] | 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");
|
---|
[390] | 84 | return;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[402] | 87 | if (!TryGetJsonField (_jsonInput, "lng", out int lng)) {
|
---|
| 88 | SendErrorResult (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_LNG");
|
---|
[390] | 89 | return;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | string newId = WebUtils.GenerateGuid ();
|
---|
| 93 | markers.Add (newId, (lat, lng));
|
---|
| 94 |
|
---|
[402] | 95 | PrepareEnvelopedResult (out JsonWriter writer);
|
---|
| 96 | writer.WriteString (newId);
|
---|
| 97 | SendEnvelopedResult (_context, ref writer, HttpStatusCode.Created);
|
---|
[388] | 98 | }
|
---|
| 99 |
|
---|
[402] | 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");
|
---|
[390] | 103 | return;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[402] | 106 | if (!TryGetJsonField (_jsonInput, "lng", out int lng)) {
|
---|
| 107 | SendErrorResult (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_LNG");
|
---|
[390] | 108 | return;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | string id = _context.RequestPath;
|
---|
| 112 |
|
---|
[391] | 113 | if (!markers.TryGetValue (id, out _)) {
|
---|
[402] | 114 | SendErrorResult (_context, HttpStatusCode.NotFound, _jsonInputData, "ID_NOT_FOUND");
|
---|
[390] | 115 | return;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | markers [id] = (lat, lng);
|
---|
| 119 |
|
---|
[402] | 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);
|
---|
[388] | 129 | }
|
---|
| 130 |
|
---|
| 131 | protected override void HandleRestDelete (RequestContext _context) {
|
---|
[390] | 132 | string id = _context.RequestPath;
|
---|
| 133 |
|
---|
[402] | 134 | PrepareEnvelopedResult (out JsonWriter writer);
|
---|
| 135 | writer.WriteRaw (JsonEmptyData);
|
---|
| 136 | SendEnvelopedResult (_context, ref writer, markers.Remove (id) ? HttpStatusCode.NoContent : HttpStatusCode.NotFound);
|
---|
[388] | 137 | }
|
---|
[426] | 138 |
|
---|
| 139 | public override int[] DefaultMethodPermissionLevels () => new[] {
|
---|
| 140 | AdminWebModules.MethodLevelNotSupported,
|
---|
| 141 | AdminWebModules.PermissionLevelGuest,
|
---|
| 142 | AdminWebModules.PermissionLevelUser,
|
---|
| 143 | AdminWebModules.MethodLevelInheritGlobal,
|
---|
| 144 | AdminWebModules.MethodLevelInheritGlobal
|
---|
| 145 | };
|
---|
[388] | 146 | }
|
---|
| 147 | }
|
---|