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