Changeset 430 for binary-improvements2/MarkersMod/src
- Timestamp:
- Apr 28, 2023, 4:39:34 PM (19 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/MarkersMod/src/Markers.cs
r426 r430 12 12 private const int numRandomMarkers = 5; 13 13 14 private readonly Dictionary<string, (int, int)> markers = new Dictionary<string, (int, int)> (); 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)> (); 15 18 16 19 public Markers () { … … 21 24 int lng = random.RandomRange (-1000, 1001); 22 25 23 markers.Add (WebUtils.GenerateGuid (), (lat, lng ));26 markers.Add (WebUtils.GenerateGuid (), (lat, lng, null)); 24 27 } 25 28 } … … 28 31 private static readonly byte[] jsonKeyLat = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("lat"); 29 32 private static readonly byte[] jsonKeyLng = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("lng"); 33 private static readonly byte[] jsonKeyIcon = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("icon"); 30 34 31 35 protected override void HandleRestGet (RequestContext _context) { … … 38 42 39 43 bool first = true; 40 foreach ((string markerId, (int, int ) coordinates) in markers) {44 foreach ((string markerId, (int, int, string) properties) in markers) { 41 45 if (!first) { 42 46 writer.WriteValueSeparator (); … … 45 49 first = false; 46 50 47 writeMarkerJson (ref writer, markerId, coordinates);51 writeMarkerJson (ref writer, markerId, properties); 48 52 } 49 53 … … 53 57 } 54 58 55 if (!markers.TryGetValue (id, out (int, int ) location)) {59 if (!markers.TryGetValue (id, out (int, int, string) properties2)) { 56 60 writer.WriteRaw (JsonEmptyData); 57 61 SendEnvelopedResult (_context, ref writer, HttpStatusCode.NotFound); … … 62 66 writer.WriteBeginArray (); 63 67 64 writeMarkerJson (ref writer, id, location);68 writeMarkerJson (ref writer, id, properties2); 65 69 66 70 writer.WriteEndArray (); … … 69 73 } 70 74 71 private void writeMarkerJson (ref JsonWriter _writer, string _markerId, (int, int ) _coordinates) {75 private void writeMarkerJson (ref JsonWriter _writer, string _markerId, (int, int, string) _properties) { 72 76 _writer.WriteRaw (jsonKeyId); 73 77 _writer.WriteString (_markerId); 74 78 _writer.WriteRaw (jsonKeyLat); 75 _writer.WriteInt32 (_ coordinates.Item1);79 _writer.WriteInt32 (_properties.Item1); 76 80 _writer.WriteRaw (jsonKeyLng); 77 _writer.WriteInt32 (_coordinates.Item2); 81 _writer.WriteInt32 (_properties.Item2); 82 _writer.WriteRaw (jsonKeyIcon); 83 _writer.WriteString (_properties.Item3 ?? defaultIcon); 78 84 _writer.WriteEndObject (); 79 85 } … … 90 96 } 91 97 98 TryGetJsonField (_jsonInput, "icon", out string icon); 99 if (string.IsNullOrEmpty (icon)) { 100 icon = null; 101 } 102 92 103 string newId = WebUtils.GenerateGuid (); 93 markers.Add (newId, (lat, lng ));104 markers.Add (newId, (lat, lng, icon)); 94 105 95 106 PrepareEnvelopedResult (out JsonWriter writer); … … 109 120 } 110 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 111 128 string id = _context.RequestPath; 112 129 113 if (!markers.TryGetValue (id, out _)) {130 if (!markers.TryGetValue (id, out (int, int, string) properties)) { 114 131 SendErrorResult (_context, HttpStatusCode.NotFound, _jsonInputData, "ID_NOT_FOUND"); 115 132 return; 116 133 } 117 118 markers [id] = (lat, lng); 134 135 if (keepIcon) { 136 icon = properties.Item3; 137 } 138 markers [id] = (lat, lng, icon); 119 139 120 140 PrepareEnvelopedResult (out JsonWriter writer); … … 125 145 writer.WriteRaw (jsonKeyLng); 126 146 writer.WriteInt32 (lng); 147 writer.WriteRaw (jsonKeyIcon); 148 writer.WriteString (icon); 127 149 writer.WriteEndObject (); 128 150 SendEnvelopedResult (_context, ref writer);
Note:
See TracChangeset
for help on using the changeset viewer.