private static async Task <string> RemovePitstopImagesFromBlob(PitstopTableEntity pitstop, CloudBlobContainer container) { string smallImageName = pitstop.PhotoSmallUrl; string mediumImageName = pitstop.PhotoMediumUrl; string largeImageName = pitstop.PhotoLargeUrl; CloudBlockBlob smallImage = container.GetBlockBlobReference(smallImageName); CloudBlockBlob mediumImage = container.GetBlockBlobReference(mediumImageName); CloudBlockBlob largeImage = container.GetBlockBlobReference(largeImageName); using (var deleteStream = await smallImage.OpenReadAsync()) { } await smallImage.DeleteIfExistsAsync(); using (var deleteStream = await mediumImage.OpenReadAsync()) { } await mediumImage.DeleteIfExistsAsync(); using (var deleteStream = await largeImage.OpenReadAsync()) { } await largeImage.DeleteIfExistsAsync(); return($"Deleted images {smallImageName}, {mediumImageName} and {largeImageName}"); }
public async Task <ActionResult <string> > PostPitstop(NewPitstop newPitstop) { string UserID = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value; //string UserID = "666"; List <Pitstop> pitstopList = new List <Pitstop>(); var tripList = new List <TripTableEntity>(); //TripEntity from trip table var tripQuery = new TableQuery <TripTableEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, UserID)); TableContinuationToken tokenTrip = null; do { TableQuerySegment <TripTableEntity> resultSegment = await _tableTrip.ExecuteQuerySegmentedAsync(tripQuery, tokenTrip); tokenTrip = resultSegment.ContinuationToken; foreach (TripTableEntity entity in resultSegment.Results) { tripList.Add(entity); } } while (tokenTrip != null); //Get pitstops List <PitstopTableEntity> pitstopList2 = new List <PitstopTableEntity>(); TableQuery <PitstopTableEntity> pitstopQuery = new TableQuery <PitstopTableEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, newPitstop.TripId.ToString() + ";" + UserID)); TableContinuationToken tokenPitstop = null; do { TableQuerySegment <PitstopTableEntity> resultSegment = await _tablePitstop.ExecuteQuerySegmentedAsync(pitstopQuery, tokenPitstop); tokenPitstop = resultSegment.ContinuationToken; foreach (PitstopTableEntity entity in resultSegment.Results) { pitstopList2.Add(entity); } } while (tokenPitstop != null); //Check if trip exists in trip table (if not return not found) if (tripList.Where(a => a.TripId == newPitstop.TripId).Count() != 0) { var nextPitsop = 1; if (pitstopList2.Count != 0) { nextPitsop = pitstopList2.Select(a => a.PitstopId).Max() + 1; } string photoName = await StorePicture(newPitstop.picture); Pitstop pitstop = new Pitstop(); pitstop.PersonId = UserID; pitstop.PitstopId = nextPitsop; pitstop.Title = newPitstop.Title; pitstop.Note = newPitstop.Note; pitstop.PitstopDate = newPitstop.PitstopDate; if (pitstop.PitstopDate.Year.Equals(0001)) { pitstop.PitstopDate = DateTime.Now; } pitstop.PhotoLargeUrl = photoName; // will be replaced with the url to the resized image. pitstop.PhotoMediumUrl = string.Empty; // will be updated when the image has been resized. pitstop.PhotoSmallUrl = string.Empty; // will be updated when the image has been resized. pitstop.TripId = newPitstop.TripId; pitstop.pitstopPosition = newPitstop.pitstopPosition; pitstop.Address = newPitstop.Address; PitstopTableEntity pitstopTable = new PitstopTableEntity(pitstop); TableOperation insertOperation = TableOperation.Insert(pitstopTable); await _tablePitstop.ExecuteAsync(insertOperation); await AddQueueItem(new QueueParam { PartitionKey = pitstopTable.PartitionKey, RowKey = pitstopTable.RowKey, PictureUri = photoName }); return(Ok($"Pitstop created under trip {pitstop.TripId}, id: {pitstop.PitstopId}")); } return(NotFound()); }