public async Task <IActionResult> AddImage(int?id, int?position, int?relItemId) { if (id == null) { return(NotFound()); } var note = await _context.Notes .FirstOrDefaultAsync(m => m.Id == (int)id && m.Owner == GetUserId()); if (note == null) { return(NotFound()); } var model = new NewImageNoteItemViewModel() { NoteId = note.Id, Type = NoteItemType.Text, Position = position, RelativeItemId = relItemId }; return(View(model)); }
private string UploadedImage(NewImageNoteItemViewModel model) { string uniqueFileName = null; if (model.Image != null) { string uploadsFolder = Path.Combine(_webHostEnvironment.WebRootPath, "content/img"); uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Image.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { model.Image.CopyTo(fileStream); } } return(uniqueFileName); }
public async Task <IActionResult> AddImage(int?id, NewImageNoteItemViewModel model) { if (id == null) { return(NotFound()); } if (id != model.NoteId) { return(NotFound()); } if (ModelState.IsValid) { var note = await _context.Notes .FirstOrDefaultAsync(m => m.Id == (int)id && m.Owner == GetUserId()); if (note == null) { return(NotFound()); } var fileName = UploadedImage(model); var noteItem = new NoteItem() { Content = fileName, NoteId = (int)id, Type = NoteItemType.Picture, Order = 0 }; _context.NoteItems.Add(noteItem); await _context.SaveChangesAsync(); await UpdateNoteItemsOrderAfterInsertAsync(note.Id, model.Position, model.RelativeItemId, noteItem.Id); return(RedirectToAction(nameof(Details), new { id = note.Id })); } return(View(model)); }