public async Task<ActionResult> Create(MapEntity model) { if (!ModelState.IsValid) { return this.View(model); } model.PartitionKey = MapEntity.MainKey; model.RowKey = ShortGuid.NewGuid().Value; try { // upload image to blob storage. model.ImageAddress = await UploadImageToStorage(model.RowKey, model.ImageAddress); await TableStorage.Insert(model); } catch (StorageException error) { Task.Run(() => RavenClient.CaptureException(error)); ViewBag.Alerts = new List<AlertModel> { new AlertModel(AlertType.Danger, error.Message) }; return this.View(model); } this.TempData["Alerts"] = AlertModel.CreateSingle(AlertType.Success, string.Format("{0} by {1} was created.", model.Title, model.Author)); this.TempData["Highlight"] = new HighlightModel(AlertType.Success, model.PartitionKey, model.RowKey); return this.RedirectToAction("Index"); }
/// <summary> /// Sets the featured Maps cache. /// </summary> /// <param name="table">The Azure table to use.</param> /// <param name="featuredEntity">The currently featured Map to cache.</param> /// <param name="featuredDate">The date to save the featured map for.</param> /// <returns>The saved entity.</returns> public static async Task<MapEntity> SetFeaturedMap(this AzureTable<MapEntity> table, MapEntity featuredEntity, DateTimeOffset featuredDate) { Guard.NotNull(() => table); Guard.NotNull(() => featuredEntity); Guard.NotNull(() => featuredDate); featuredEntity.RowKey = ShortGuid.NewGuid().Value; featuredEntity.PartitionKey = MapEntity.FeaturedKey; featuredEntity.FeaturedDate = featuredDate.ToString("yyyy-MM-dd"); return await table.Insert(featuredEntity); }
/// <summary> /// Converts a MapEntity into a MapModel. /// </summary> /// <param name="entity">The entity to convert.</param> /// <returns>A populated MapModel.</returns> public static MapModel FromEntity(MapEntity entity) { Guard.NotNull(() => entity); return new MapModel { Id = entity.RowKey, Title = entity.Title, Author = entity.Author, Year = entity.Year, ImageAddress = entity.ImageAddress, ReferenceAddress = entity.ReferenceAddress }; }
/// <summary> /// The new map form, pre filled from an existing map. /// </summary> /// <param name="row">The row to copy from.</param> /// <returns>The creation form.</returns> public async Task<ActionResult> Create(string row = "") { MapEntity model = new MapEntity(); if (!string.IsNullOrEmpty(row)) { model = await TableStorage.Get(MapEntity.MainKey, row); } model.PartitionKey = MapEntity.FeaturedKey; model.RowKey = string.Empty; model.FeaturedDate = DateTime.UtcNow.Date.ToString("yyyy-MM-dd"); return this.View(model); }
public async Task<ActionResult> Create(MapEntity model) { if (string.IsNullOrEmpty(model.FeaturedDate)) { ModelState.AddModelError("FeaturedDate", "A featured date is required."); } if (!ModelState.IsValid) { return this.View(model); } model.PartitionKey = MapEntity.FeaturedKey; model.RowKey = ShortGuid.NewGuid().Value; try { await TableStorage.Insert(model); } catch (StorageException error) { Task.Run(() => RavenClient.CaptureException(error)); ViewBag.Alerts = new List<AlertModel> { new AlertModel(AlertType.Danger, error.Message) }; return this.View(model); } string alertMessage = string.Format("{0} by {1} for {2} was created.", model.Title, model.Author, model.FeaturedDate); this.TempData["Alerts"] = AlertModel.CreateSingle(AlertType.Success, alertMessage); this.TempData["Highlight"] = new HighlightModel(AlertType.Success, model.PartitionKey, model.RowKey); return this.RedirectToAction("Index"); }
public async Task<ActionResult> Delete(string row, MapEntity model) { MapEntity entity = await TableStorage.Get(MapEntity.FeaturedKey, row); try { await TableStorage.Delete(entity); } catch (StorageException error) { Task.Run(() => RavenClient.CaptureException(error)); return this.AzureStorageErrorResponse(row, MapEntity.FeaturedKey, error); } string alertMessage = string.Format("{0} by {1} for {2} was deleted.", entity.Title, entity.Author, model.FeaturedDate); this.TempData["Alerts"] = AlertModel.CreateSingle(AlertType.Info, alertMessage); return this.RedirectToAction("Index"); }
public async Task<ActionResult> Edit(string row, MapEntity model) { if (string.IsNullOrEmpty(model.FeaturedDate)) { ModelState.AddModelError("FeaturedDate", "A featured date is required."); } if (!ModelState.IsValid) { return this.View(model); } try { await TableStorage.Replace(model); } catch (StorageException error) { Task.Run(() => RavenClient.CaptureException(error)); return this.AzureStorageErrorResponse(row, MapEntity.FeaturedKey, error); } string alertMessage = string.Format("The featured map for {0} was updated.", model.FeaturedDate); this.TempData["Alerts"] = AlertModel.CreateSingle(AlertType.Success, alertMessage); this.TempData["Highlight"] = new HighlightModel(AlertType.Info, model.PartitionKey, model.RowKey); return this.RedirectToAction("Index"); }
public async Task<ActionResult> Edit(string row, MapEntity model) { if (!ModelState.IsValid) { return this.View(model); } try { await TableStorage.Replace(model); } catch (StorageException error) { Task.Run(() => RavenClient.CaptureException(error)); return this.AzureStorageErrorResponse(row, MapEntity.MainKey, error); } this.TempData["Alerts"] = AlertModel.CreateSingle(AlertType.Success, string.Format("{0} by {1} was updated.", model.Title, model.Author)); this.TempData["Highlight"] = new HighlightModel(AlertType.Info, model.PartitionKey, model.RowKey); return this.RedirectToAction("Index"); }