public async Task <ActionResult <Map> > Delete( int id, [FromServices] IMapService MapService) { try { var map = await MapService.GetById(id); if (map == null) { return(NotFound(new { message = Messages.MAPA_NAO_ENCONTRADO })); } await MapService.Delete(map); return(Ok(new { message = Messages.MAPA_REMOVIDO_COM_SUCESSO })); } catch (InvalidOperationException ex) { return(BadRequest(new { message = ex.Message })); } catch { return(BadRequest(new { message = Messages.OCORREU_UM_ERRO_INESPERADO })); } }
public MapsModule(IMapService service, IUserService userService, IDownloadService downloadService, INotificationService notificationService) : base("maps") { this.RequiresAuthentication(); Get("/{id}/preview", x => HttpStatusCode.NotFound); Get("/", x => { Debug.Log($"Listing maps"); try { int page = Request.Query["page"]; // TODO: Items per page should be read from personal user settings. // This value should be independent for each module: maps, vehicles and simulation. // But for now 5 is just an arbitrary value to ensure that we don't try and Page a count of 0 int count = Request.Query["count"] > 0 ? Request.Query["count"] : Config.DefaultPageSize; return(service.List(page, count, this.Context.CurrentUser.Identity.Name).Select(MapResponse.Create).ToArray()); } catch (Exception ex) { Debug.LogException(ex); return(Response.AsJson(new { error = $"Failed to list maps: {ex.Message}" }, HttpStatusCode.InternalServerError)); } }); Get("/{id:long}", x => { long id = x.id; Debug.Log($"Getting map with id {id}"); try { var map = service.Get(id, this.Context.CurrentUser.Identity.Name); return(MapResponse.Create(map)); } catch (IndexOutOfRangeException) { Debug.Log($"Map with id {id} does not exist"); return(Response.AsJson(new { error = $"Map with id {id} does not exist" }, HttpStatusCode.NotFound)); } catch (Exception ex) { Debug.LogException(ex); return(Response.AsJson(new { error = $"Failed to get map with id {id}: {ex.Message}" }, HttpStatusCode.InternalServerError)); } }); Post("/", x => { Debug.Log($"Adding new map"); try { var req = this.BindAndValidate <MapRequest>(); if (!ModelValidationResult.IsValid) { var message = ModelValidationResult.Errors.First().Value.First().ErrorMessage; Debug.Log($"Validation for adding map failed: {message}"); return(Response.AsJson(new { error = $"Failed to add map: {message}" }, HttpStatusCode.BadRequest)); } var map = req.ToModel(this.Context.CurrentUser.Identity.Name); var uri = new Uri(map.Url); if (uri.IsFile) { map.Status = "Valid"; map.LocalPath = uri.LocalPath; } else { map.Status = "Downloading"; map.LocalPath = WebUtilities.GenerateLocalPath("Maps"); } long id = service.Add(map); Debug.Log($"Map added with id {id}"); map.Id = id; SIM.LogWeb(SIM.Web.MapAddName, map.Name); SIM.LogWeb(SIM.Web.MapAddURL, map.Url); if (!uri.IsFile) { SIM.LogWeb(SIM.Web.MapDownloadStart, map.Name); downloadService.AddDownload( uri, map.LocalPath, progress => notificationService.Send("MapDownload", new { map.Id, progress }, map.Owner), success => { var updatedModel = service.Get(id, map.Owner); updatedModel.Status = success && Validation.BeValidAssetBundle(updatedModel.LocalPath) ? "Valid" : "Invalid"; service.Update(updatedModel); notificationService.Send("MapDownloadComplete", updatedModel, map.Owner); SIM.LogWeb(SIM.Web.MapDownloadFinish, map.Name); } ); } return(MapResponse.Create(map)); } catch (Exception ex) { Debug.LogException(ex); return(Response.AsJson(new { error = $"Failed to add map: {ex.Message}" }, HttpStatusCode.InternalServerError)); } }); Put("/{id:long}", x => { long id = x.id; Debug.Log($"Updating map with id {id}"); try { var req = this.BindAndValidate <MapRequest>(); if (!ModelValidationResult.IsValid) { var message = ModelValidationResult.Errors.First().Value.First().ErrorMessage; Debug.Log($"Validation for updating map failed: {message}"); return(Response.AsJson(new { error = $"Failed to update map: {message}" }, HttpStatusCode.BadRequest)); } var map = service.Get(id, this.Context.CurrentUser.Identity.Name); map.Name = req.name; if (map.Url != req.url) { Uri uri = new Uri(req.url); if (uri.IsFile) { map.Status = "Valid"; map.LocalPath = uri.LocalPath; } else { map.Status = "Downloading"; map.LocalPath = WebUtilities.GenerateLocalPath("Maps"); SIM.LogWeb(SIM.Web.MapDownloadStart, map.Name); downloadService.AddDownload( uri, map.LocalPath, progress => notificationService.Send("MapDownload", new { map.Id, progress }, map.Owner), success => { var updatedModel = service.Get(id, map.Owner); updatedModel.Status = success && Validation.BeValidAssetBundle(updatedModel.LocalPath) ? "Valid" : "Invalid"; service.Update(updatedModel); notificationService.Send("MapDownloadComplete", updatedModel, map.Owner); SIM.LogWeb(SIM.Web.MapDownloadFinish, map.Name); } ); } map.Url = req.url; } int result = service.Update(map); SIM.LogWeb(SIM.Web.MapEditName, map.Name); SIM.LogWeb(SIM.Web.MapEditURL, map.Url); if (result > 1) { throw new Exception($"More than one map has id {id}"); } else if (result < 1) { throw new IndexOutOfRangeException(); } return(MapResponse.Create(map)); } catch (IndexOutOfRangeException) { Debug.Log($"Map with id {id} does not exist"); return(Response.AsJson(new { error = $"Map with id {id} does not exist" }, HttpStatusCode.NotFound)); } catch (Exception ex) { Debug.LogException(ex); return(Response.AsJson(new { error = $"Failed to update map with id {id}: {ex.Message}" }, HttpStatusCode.InternalServerError)); } }); Delete("/{id:long}", x => { long id = x.id; Debug.Log($"Removing map with id {id}"); try { MapModel map = service.Get(id, this.Context.CurrentUser.Identity.Name); if (map.Status == "Downloading") { downloadService.StopDownload(map.Url); SIM.LogWeb(SIM.Web.MapDownloadStop, map.Name); } if (!new Uri(map.Url).IsFile&& File.Exists(map.LocalPath)) { Debug.Log($"Deleting file at path: {map.LocalPath}"); File.Delete(map.LocalPath); } int result = service.Delete(id, map.Owner); SIM.LogWeb(SIM.Web.MapDeleteName, map.Name); SIM.LogWeb(SIM.Web.MapDeleteURL, map.Url); if (result > 1) { throw new Exception($"More than one map has id {id}"); } else if (result < 1) { throw new IndexOutOfRangeException(); } return(new { }); } catch (IndexOutOfRangeException) { Debug.Log($"Map with id {id} does not exist"); return(Response.AsJson(new { error = $"Map with id {id} does not exist" }, HttpStatusCode.NotFound)); } catch (Exception ex) { Debug.LogException(ex); return(Response.AsJson(new { error = $"Failed to remove map with id {id}: {ex.Message}" }, HttpStatusCode.InternalServerError)); } }); Put("/{id:long}/cancel", x => { long id = x.id; Debug.Log($"Cancelling download of map with id {id}"); try { MapModel map = service.Get(id, this.Context.CurrentUser.Identity.Name); if (map.Status == "Downloading") { downloadService.StopDownload(map.Url); map.Status = "Invalid"; service.Update(map); SIM.LogWeb(SIM.Web.MapDownloadStop, map.Name); } else { throw new Exception($"Failed to cancel map download: map with id {id} is not currently downloading"); } return(new { }); } catch (IndexOutOfRangeException) { Debug.Log($"Map with id {id} does not exist"); return(Response.AsJson(new { error = $"Map with id {id} does not exist" }, HttpStatusCode.NotFound)); } catch (Exception ex) { Debug.LogException(ex); return(Response.AsJson(new { error = $"Failed to cancel download of map with id {id}: {ex.Message}" }, HttpStatusCode.InternalServerError)); } }); Put("/{id:long}/download", x => { long id = x.id; Debug.Log($"Restarting download of map with id {id}"); try { MapModel map = service.Get(id, this.Context.CurrentUser.Identity.Name); Uri uri = new Uri(map.Url); if (!uri.IsFile) { if (map.Status == "Invalid") { map.Status = "Downloading"; downloadService.AddDownload( uri, map.LocalPath, progress => { Debug.Log($"Map Download at {progress}%"); notificationService.Send("MapDownload", new { map.Id, progress }, map.Owner); }, success => { var updatedModel = service.Get(id, map.Owner); updatedModel.Status = success && Validation.BeValidAssetBundle(updatedModel.LocalPath) ? "Valid" : "Invalid"; service.Update(updatedModel); notificationService.Send("MapDownloadComplete", updatedModel, map.Owner); SIM.LogWeb(SIM.Web.MapDownloadFinish, map.Name); } ); } else { throw new Exception($"Failed to restart download of map: map is not in invalid state"); } } else { throw new Exception($"Failed to restart download of map: file URL is not remote"); } int result = service.Update(map); if (result > 1) { throw new Exception($"More than one map has id {id}"); } else if (result < 1) { throw new IndexOutOfRangeException(); } return(MapResponse.Create(map)); } catch (IndexOutOfRangeException) { Debug.Log($"Map with id {id} does not exist"); return(Response.AsJson(new { error = $"Map with id {id} does not exist" }, HttpStatusCode.NotFound)); } catch (Exception ex) { Debug.LogException(ex); return(Response.AsJson(new { error = $"Failed to cancel download of map with id {id}: {ex.Message}" }, HttpStatusCode.InternalServerError)); } }); }