-
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathCustomMapsController.cs
More file actions
377 lines (308 loc) · 11.4 KB
/
Copy pathCustomMapsController.cs
File metadata and controls
377 lines (308 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Resgrid.Model;
using Resgrid.Model.Services;
using Resgrid.Web.Areas.User.Models.CustomMaps;
using Microsoft.Extensions.Localization;
using Resgrid.Localization.Areas.User.CustomMaps;
using Resgrid.Web.Helpers;
namespace Resgrid.Web.Areas.User.Controllers
{
[Area("User")]
public class CustomMapsController : SecureBaseController
{
private readonly ICustomMapService _customMapService;
private readonly IStringLocalizer<CustomMaps> _localizer;
public CustomMapsController(ICustomMapService customMapService, IStringLocalizer<CustomMaps> localizer)
{
_customMapService = customMapService;
_localizer = localizer;
}
#region Map CRUD
[HttpGet]
public async Task<IActionResult> Index(int? type)
{
var model = new CustomMapIndexView();
CustomMapType? filterType = type.HasValue ? (CustomMapType)type.Value : null;
model.FilterType = filterType;
model.Maps = await _customMapService.GetCustomMapsForDepartmentAsync(DepartmentId, filterType);
return View(model);
}
[HttpGet]
public IActionResult New(int? type)
{
var model = new CustomMapNewView();
if (type.HasValue)
model.Map.MapType = type.Value;
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> New(CustomMapNewView model, CancellationToken cancellationToken)
{
if (ModelState.IsValid)
{
model.Map.DepartmentId = DepartmentId;
model.Map.AddedById = UserId;
model.Map.AddedOn = DateTime.UtcNow;
model.Map.IsDeleted = false;
await _customMapService.SaveCustomMapAsync(model.Map, cancellationToken);
return RedirectToAction("Index");
}
return View(model);
}
[HttpGet]
public async Task<IActionResult> Edit(string id)
{
var map = await _customMapService.GetCustomMapByIdAsync(id);
if (map == null || map.DepartmentId != DepartmentId)
return RedirectToAction("Index");
var model = new CustomMapNewView();
model.Map = map;
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(CustomMapNewView model, CancellationToken cancellationToken)
{
var existing = await _customMapService.GetCustomMapByIdAsync(model.Map.IndoorMapId);
if (existing == null || existing.DepartmentId != DepartmentId)
return RedirectToAction("Index");
if (ModelState.IsValid)
{
model.Map.DepartmentId = DepartmentId;
model.Map.AddedById = existing.AddedById;
model.Map.AddedOn = existing.AddedOn;
model.Map.UpdatedById = UserId;
model.Map.UpdatedOn = DateTime.UtcNow;
model.Map.IsDeleted = existing.IsDeleted;
await _customMapService.SaveCustomMapAsync(model.Map, cancellationToken);
return RedirectToAction("Index");
}
return View(model);
}
[HttpGet]
public async Task<IActionResult> Delete(string id, CancellationToken cancellationToken)
{
var map = await _customMapService.GetCustomMapByIdAsync(id);
if (map != null && map.DepartmentId == DepartmentId)
{
await _customMapService.DeleteCustomMapAsync(id, cancellationToken);
}
return RedirectToAction("Index");
}
#endregion Map CRUD
#region Layers
[HttpGet]
public async Task<IActionResult> Layers(string id)
{
var map = await _customMapService.GetCustomMapByIdAsync(id);
if (map == null || map.DepartmentId != DepartmentId)
return RedirectToAction("Index");
var model = new CustomMapLayersView();
model.Map = map;
model.Layers = await _customMapService.GetLayersForMapAsync(id);
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> NewLayer(string indoorMapId, string name, int floorOrder, int layerType, IFormFile layerImage, CancellationToken cancellationToken)
{
var map = await _customMapService.GetCustomMapByIdAsync(indoorMapId);
if (map == null || map.DepartmentId != DepartmentId)
return RedirectToAction("Index");
var layer = new IndoorMapFloor();
layer.IndoorMapId = indoorMapId;
layer.Name = name;
layer.FloorOrder = floorOrder;
layer.LayerType = layerType;
layer.Opacity = 0.8m;
layer.IsDeleted = false;
layer.AddedOn = DateTime.UtcNow;
byte[] imageData = null;
if (layerImage != null && layerImage.Length > 0)
{
using (var ms = new MemoryStream())
{
await layerImage.CopyToAsync(ms);
imageData = ms.ToArray();
}
layer.ImageContentType = layerImage.ContentType;
layer.SourceFileSize = layerImage.Length;
}
// Save the layer first to get it in DB
await _customMapService.SaveLayerAsync(layer, cancellationToken);
// Process tiles if image provided
if (imageData != null)
{
await _customMapService.ProcessAndStoreTilesAsync(layer.IndoorMapFloorId, imageData, cancellationToken);
}
return RedirectToAction("Layers", new { id = indoorMapId });
}
[HttpGet]
public async Task<IActionResult> DeleteLayer(string id, CancellationToken cancellationToken)
{
var layer = await _customMapService.GetLayerByIdAsync(id);
if (layer == null)
return RedirectToAction("Index");
var map = await _customMapService.GetCustomMapByIdAsync(layer.IndoorMapId);
if (map == null || map.DepartmentId != DepartmentId)
return RedirectToAction("Index");
await _customMapService.DeleteLayerAsync(id, cancellationToken);
return RedirectToAction("Layers", new { id = layer.IndoorMapId });
}
#endregion Layers
#region Region Editor
[HttpGet]
public async Task<IActionResult> RegionEditor(string id)
{
var layer = await _customMapService.GetLayerByIdAsync(id);
if (layer == null)
return RedirectToAction("Index");
var map = await _customMapService.GetCustomMapByIdAsync(layer.IndoorMapId);
if (map == null || map.DepartmentId != DepartmentId)
return RedirectToAction("Index");
var model = new CustomMapRegionEditorView();
model.Layer = layer;
model.Map = map;
model.Regions = await _customMapService.GetRegionsForLayerAsync(id);
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> SaveRegion([FromBody] IndoorMapZone region, CancellationToken cancellationToken)
{
var layer = await _customMapService.GetLayerByIdAsync(region.IndoorMapFloorId);
if (layer == null)
return Json(new { success = false, message = "Layer not found" });
var map = await _customMapService.GetCustomMapByIdAsync(layer.IndoorMapId);
if (map == null || map.DepartmentId != DepartmentId)
return Json(new { success = false, message = "Unauthorized" });
if (string.IsNullOrWhiteSpace(region.IndoorMapZoneId))
{
region.IndoorMapZoneId = null;
region.AddedOn = DateTime.UtcNow;
}
region.IsDeleted = false;
var saved = await _customMapService.SaveRegionAsync(region, cancellationToken);
return Json(new { success = true, regionId = saved.IndoorMapZoneId });
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteRegion([FromBody] DeleteRegionRequest request, CancellationToken cancellationToken)
{
var region = await _customMapService.GetRegionByIdAsync(request.RegionId);
if (region == null)
return Json(new { success = false });
var layer = await _customMapService.GetLayerByIdAsync(region.IndoorMapFloorId);
if (layer == null)
return Json(new { success = false });
var map = await _customMapService.GetCustomMapByIdAsync(layer.IndoorMapId);
if (map == null || map.DepartmentId != DepartmentId)
return Json(new { success = false });
await _customMapService.DeleteRegionAsync(request.RegionId, cancellationToken);
return Json(new { success = true });
}
#endregion Region Editor
#region Import
[HttpGet]
public async Task<IActionResult> Import(string id)
{
var map = await _customMapService.GetCustomMapByIdAsync(id);
if (map == null || map.DepartmentId != DepartmentId)
return RedirectToAction("Index");
var model = new CustomMapImportView();
model.Map = map;
model.Layers = await _customMapService.GetLayersForMapAsync(id);
model.Imports = await _customMapService.GetImportsForMapAsync(id);
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> ImportUpload(string mapId, string layerId, IFormFile importFile, CancellationToken cancellationToken)
{
var map = await _customMapService.GetCustomMapByIdAsync(mapId);
if (map == null || map.DepartmentId != DepartmentId)
return RedirectToAction("Index");
if (string.IsNullOrWhiteSpace(layerId))
{
var model = new CustomMapImportView();
model.Map = map;
model.Layers = await _customMapService.GetLayersForMapAsync(mapId);
model.Imports = await _customMapService.GetImportsForMapAsync(mapId);
model.Message = _localizer["PleaseSelectTargetLayer"];
return View("Import", model);
}
if (importFile == null || importFile.Length == 0)
return RedirectToAction("Import", new { id = mapId });
var fileName = importFile.FileName.ToLowerInvariant();
if (fileName.EndsWith(".geojson") || fileName.EndsWith(".json"))
{
using (var reader = new StreamReader(importFile.OpenReadStream()))
{
var geoJson = await reader.ReadToEndAsync();
await _customMapService.ImportGeoJsonAsync(mapId, layerId, geoJson, UserId, cancellationToken);
}
}
else if (fileName.EndsWith(".kml"))
{
await _customMapService.ImportKmlAsync(mapId, layerId, importFile.OpenReadStream(), false, UserId, cancellationToken);
}
else if (fileName.EndsWith(".kmz"))
{
await _customMapService.ImportKmlAsync(mapId, layerId, importFile.OpenReadStream(), true, UserId, cancellationToken);
}
return RedirectToAction("Import", new { id = mapId });
}
#endregion Import
#region Search & Image Endpoints
[HttpGet]
public async Task<IActionResult> SearchRegions(string term)
{
var regions = await _customMapService.SearchRegionsAsync(DepartmentId, term);
var results = new List<object>();
foreach (var region in regions)
{
var displayName = await _customMapService.GetRegionDisplayNameAsync(region.IndoorMapZoneId);
results.Add(new { id = region.IndoorMapZoneId, text = displayName, layerId = region.IndoorMapFloorId });
}
return Json(new { results = results });
}
[HttpGet]
public async Task<IActionResult> GetLayerImage(string id)
{
var layer = await _customMapService.GetLayerByIdAsync(id);
if (layer == null || layer.ImageData == null)
return NotFound();
var map = await _customMapService.GetCustomMapByIdAsync(layer.IndoorMapId);
if (map == null || map.DepartmentId != DepartmentId)
return NotFound();
return File(layer.ImageData, layer.ImageContentType ?? "image/png");
}
[HttpGet]
public async Task<IActionResult> GetLayerTile(string id, int z, int x, int y)
{
var layer = await _customMapService.GetLayerByIdAsync(id);
if (layer == null)
return NotFound();
var map = await _customMapService.GetCustomMapByIdAsync(layer.IndoorMapId);
if (map == null || map.DepartmentId != DepartmentId)
return NotFound();
var tile = await _customMapService.GetTileAsync(id, z, x, y);
if (tile == null)
return NotFound();
return File(tile.TileData, tile.TileContentType ?? "image/png");
}
#endregion Search & Image Endpoints
}
public class DeleteRegionRequest
{
public string RegionId { get; set; }
}
}