public async Task SaveAct(ActInfo actModel) { var act = await repository.GetOrInsertAct(actModel.ActGuid); var lastActDescription = act.Descriptions .OrderByDescending(description => description.ModifiedDate) .FirstOrDefault(); if (lastActDescription == null || lastActDescription.Title != actModel.Title || lastActDescription.ImageHash != actModel.ImageHash) { var modifiedTicks = lastActDescription?.ModifiedDate.Ticks ?? 0; if (modifiedTicks != actModel.LastModifiedTicks) { throw new DbUpdateConcurrencyException("A new update has occurred since you loaded the page. Please refresh and try again."); } await repository.AddAsync(new ActDescription { ModifiedDate = DateTime.UtcNow, Act = act, Title = actModel.Title, ImageHash = actModel.ImageHash }); await repository.SaveChangesAsync(); } }
public async Task <IActionResult> Edit(Guid id, [Bind("Title,Image,ImageHash,LastModifiedTicks")] ActInfo act) { var imageHash = await SaveImageContent(act); if (ModelState.IsValid) { act.ActGuid = id; act.ImageHash = imageHash; await actCommands.SaveAct(act); return(RedirectToAction(nameof(Index))); } return(View(act)); }
private async Task <string> SaveImageContent(ActInfo act) { if (act.Image != null) { using var imageReadStream = act.Image.OpenReadStream(); using var imageMemoryStream = new MemoryStream(); await imageReadStream.CopyToAsync(imageMemoryStream); var imageHash = await contentCommands.SaveContent(imageMemoryStream.ToArray(), act.Image.ContentType); return(imageHash); } else { return(act.ImageHash); } }