Esempio n. 1
0
        public async Task <IActionResult> EditText(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var noteItem = await _context.NoteItems.Include(m => m.Note)
                           .FirstOrDefaultAsync(m => m.Id == id);

            if (noteItem == null)
            {
                return(NotFound());
            }

            if (noteItem.Note.Owner != GetUserId())
            {
                return(NotFound());
            }

            var model = new EditTextNoteItemViewModel()
            {
                Id          = noteItem.Id,
                NoteId      = noteItem.NoteId,
                Type        = noteItem.Type,
                TextContent = noteItem.Content
            };

            return(View(model));
        }
Esempio n. 2
0
        public async Task <IActionResult> EditText(int id, EditTextNoteItemViewModel model)
        {
            if (id != model.Id)
            {
                return(NotFound());
            }

            var noteItem = await _context.NoteItems.Include(m => m.Note)
                           .FirstOrDefaultAsync(m => m.Id == id);

            if (noteItem == null)
            {
                return(NotFound());
            }

            if (noteItem.Note.Owner != GetUserId())
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    noteItem.Content              = model.TextContent;
                    noteItem.Note.LastUpdatedBy   = GetUserId();
                    noteItem.Note.LastUpdatedDate = DateTime.UtcNow;
                    _context.Update(noteItem);
                    _context.Update(noteItem.Note);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    throw;
                }

                return(RedirectToAction(nameof(Details), new { id = noteItem.NoteId }));
            }
            return(View(model));
        }