public bool Update(NoteEditViewModel model, Guid userId) { using (var context = new ElevenNoteDataContext()) { // Attempt to get the note from the database. var note = context.Notes.Where(w => w.Id == model.Id && w.ApplicationUserId == userId).SingleOrDefault(); // Functionally equivalent expressive syntax: //var note2 = (from w in context.Notes // where w.Id == model.Id && w.ApplicationUserId == userId // select w).SingleOrDefault(); // Make sure we actually received a note back before updating. if (note == null) { return(false); } // Update the note. note.Contents = model.Contents; note.Title = model.Title; note.IsFavorite = model.IsFavorite; note.DateModified = DateTime.UtcNow; // Save the changes to the database. var result = context.SaveChanges(); return(result == 1 /* was 1 record (success) or 0 records (unsuccessful) updated? */); } }
public ActionResult Create(NoteEditViewModel model) { if (ModelState.IsValid) { var files = new List <BinaryFile>(); string serverPath = Server.MapPath($"~/Uploaded Files/{ User.Identity.Name }/"); if (!Directory.Exists(serverPath)) { Directory.CreateDirectory(serverPath); } foreach (var file in model.Files) { if (file != null) { string fileName = Path.GetFileName(file.FileName); string filePath = Path.Combine(serverPath, fileName); file.SaveAs(filePath); files.Add(new BinaryFile(fileName, filePath)); } } var user = userRepository.GetCurrentUser(User); var note = new Note { Title = model.Title, Text = model.Text, Tags = model.Tags, Author = user, Files = files }; noteRepository.Save(note); return(RedirectToAction("Index")); } return(View(model)); }
public ActionResult Edit(int id, NoteEditViewModel model) { if (model.NoteId != id) { ModelState.AddModelError("", "Nice try"); model.NoteId = id; return(View(model)); } if (!ModelState.IsValid) { return(View(model)); } if (!CreateNoteService().UpdateNote(model)) { ModelState.AddModelError("", "Unable to update note"); return(View(model)); } // like session, but stored for a shorter period TempData["SaveResult"] = "Your note was saved"; return(RedirectToAction("Index")); }
public async Task <GenericResponse <bool> > Put(Guid id, [FromBody] NoteEditViewModel value) { var editNoteData = Mapper.Map <Note>(value); editNoteData.Id = id; return(await _noteService.EditNote(editNoteData)); }
public ActionResult Create(NoteEditViewModel model, HttpPostedFileBase uploadedFile) { if (ModelState.IsValid) { try { IMapper mapper = new MapperConfiguration(c => c.CreateMap <NoteEditViewModel, NoteDTO>()).CreateMapper(); NoteDTO note = mapper.Map <NoteEditViewModel, NoteDTO>(model); if (uploadedFile != null) { using (BinaryReader reader = new BinaryReader(uploadedFile.InputStream)) { note.Image = reader.ReadBytes(uploadedFile.ContentLength); } note.PictureMimeType = uploadedFile.ContentType; } else { note.Image = null; note.PictureMimeType = null; } _bl.Notes.Create(note); return(RedirectToAction(nameof(Index))); } catch (Exception e) { return(View("Error", e.Message)); } } else { return(View("Error")); } }
public ActionResult Edit(int id) { if (id > 0) { try { NoteDTO note = _bl.Notes.GetItemById(id); if (note != null) { IMapper mapper = new MapperConfiguration(c => c.CreateMap <NoteDTO, NoteEditViewModel>()).CreateMapper(); NoteEditViewModel model = mapper.Map <NoteDTO, NoteEditViewModel>(note); model.Categories = _bl.Categories.GetList().OrderBy(c => c.Name).ToList(); model.Owner = _bl.Users.GetItemById(model.OwnerId)?.NameOrLogin; return(PartialView(model)); } else { return(PartialView("Error", new ErrorViewModel("Заметка не найдена"))); } } catch (NoteCustomException e) { ErrorViewModel errorModel = new ErrorViewModel(e.Message); return(PartialView("Error", errorModel)); } catch { return(RedirectToAction(nameof(Index))); } } else { return(PartialView("Error", new ErrorViewModel("Неверные параметры"))); } }
public ActionResult Edit(NoteEditViewModel model, HttpPostedFileBase image) { if ((model != null) && (ModelState.IsValid)) { try { IMapper mapper = new MapperConfiguration(c => c.CreateMap <NoteEditViewModel, NoteDTO>()).CreateMapper(); NoteDTO note = mapper.Map <NoteEditViewModel, NoteDTO>(model); if (image != null) { using (BinaryReader reader = new BinaryReader(image.InputStream)) { note.Image = reader.ReadBytes(image.ContentLength); } } else { note.Image = null; } _bl.Notes.Update(note); return(RedirectToAction(nameof(Index))); } catch (NoteCustomException e) { return(View("Error", e.Message)); } } else { return(PartialView(model)); } }
public IActionResult Add(NoteEditViewModel model, List <NoteCategory> noteCategories) { if (ModelState.IsValid) { if (model.Note.Title == "write some title" || model.Note.Title == "") { model.Note.NoteCategories = noteCategories; ModelState.AddModelError("Title error", "Wrong title"); return(View(model)); } else if (Notes.Where(m => m.Title == model.Note.Title).Any()) { model.Note.NoteCategories = noteCategories; ModelState.AddModelError("Title error", "Title already taken"); return(View(model)); } using (var transaction = _context.Database.BeginTransaction()) { _context.Notes.Add(model.Note); _context.SaveChanges(); transaction.Commit(); } foreach (var n in noteCategories) { Category newCategory; if (!_context.Categories.Where(m => m.Title == n.Category.Title).Any()) { using (var transaction = _context.Database.BeginTransaction()) { newCategory = new Category { Title = n.Category.Title }; _context.Categories.Add(newCategory); _context.SaveChanges(); transaction.Commit(); } } else { newCategory = _context.Categories.Where(m => m.Title == n.Category.Title).FirstOrDefault(); } using (var transaction = _context.Database.BeginTransaction()) { _context.NoteCategories.Add(new NoteCategory { NoteID = model.Note.NoteID, CategoryID = newCategory.CategoryID }); _context.SaveChanges(); transaction.Commit(); } } } var errors = ModelState .Where(x => x.Value.Errors.Count > 0) .Select(x => new { x.Key, x.Value.Errors }) .ToArray(); return(ReturnToIndex()); }
public IActionResult Edit(int gameID, int id) { var model = new NoteEditViewModel(); model.Note = NoteModel.GenerateNoteModelFromDTO(work.NoteRepository.GetDTO(id)); model.NoteTypes = work.NoteTypeRepository.GetAll().Select(e => NoteTypeModel.GenerateNoteTypeModelFromDTO(e)); return(View(model)); }
public IActionResult AddCategory(NoteEditViewModel model) { Note prevNote = model.Note = Notes.Where(m => m.Title == model.Note.Title).FirstOrDefault(); model.Note.Categories = model.Note.Categories.Append(model.NewCategory).ToArray(); Notes[Notes.IndexOf(prevNote)] = model.Note; model.NewCategory = ""; return(View("Edit", model)); }
public IActionResult RemoveCategories(NoteEditViewModel model) { Note prevNote = model.Note = Notes.Where(m => m.Title == model.Note.Title).FirstOrDefault(); foreach (var c in model.CategoriesToRemove ?? new string[] { }) { model.Note.Categories = model.Note.Categories.Where(v => v != c).ToArray(); } Notes[Notes.IndexOf(prevNote)] = model.Note; return(View("Edit", model)); }
public ActionResult EditPost(NoteEditViewModel model) { if (ModelState.IsValid) { var noteService = new NoteService(); var userId = Guid.Parse(User.Identity.GetUserId()); var result = noteService.Update(model, userId); TempData.Add("Result", result ? "Note updated." : "Note not updated."); return(RedirectToAction("Index")); } return(View()); }
public IActionResult Edit(int id) { Note editNote = _noteRepository.GetNote(id); NoteEditViewModel model = new NoteEditViewModel() { ID = editNote.ID, Content = editNote.Content, Author = editNote.Author, NewPhotoPath = editNote.PhotoPath, }; return(View(model)); }
public ActionResult Edit(int id) { var detailModel = CreateNoteService().GetNoteById(id); var editModel = new NoteEditViewModel { NoteId = detailModel.NoteId, Title = detailModel.Title, Content = detailModel.Content }; return(View(editModel)); }
public ActionResult CreatePost(NoteEditViewModel model) { if (ModelState.IsValid) { var noteService = new ElevenNote.Services.NoteService(); var userID = Guid.Parse(User.Identity.GetUserId()); var result = noteService.Create(model, userID); TempData.Add("Result", result ? "Note Added." : "Note not added."); return(RedirectToAction("Index")); } return(View(model)); }
public IActionResult Edit(NoteEditViewModel model) { if (!ModelState.IsValid) { model.NoteTypes = work.NoteTypeRepository.GetAll().Select(e => NoteTypeModel.GenerateNoteTypeModelFromDTO(e)); return(View(model)); } work.NoteRepository.Edit(NoteModel.GenerateNoteDTOFromModel(model.Note)); work.Save(); return(RedirectToAction("Index", new { gameId = model.Note.GameID })); }
public ActionResult Edit(int id) { var detail = _svc.Value.GetNoteById(id); var note = new NoteEditViewModel { NoteId = detail.NoteId, Title = detail.Title, Content = detail.Content, IsStarred = detail.IsStarred }; return(View(note)); }
/// <summary> /// Create a note. /// </summary> /// <param name="model"></param> /// <param name="userId"></param> /// <returns></returns> public bool Create(NoteEditViewModel model, Guid userId) { using (var context = new ElevenNoteDataContext()) { var note = new Note(); note.Title = model.Title; note.Contents = model.Contents; note.DateCreated = DateTime.UtcNow; note.ApplicationUserId = userId; context.Notes.Add(note); var result = context.SaveChanges(); return(result == 1); } }
public ActionResult Edit(NoteEditViewModel vm) { if (!ModelState.IsValid) { return(View(vm)); } if (!_svc.Value.UpdateNote(vm)) { ModelState.AddModelError("", "Unable to update note"); return(View(vm)); } return(RedirectToAction("Index")); }
public ActionResult Create() { try { NoteEditViewModel model = new NoteEditViewModel(); model.OwnerId = ((UserPrinciple)(HttpContext.User)).Id; model.Owner = _bl.Users.GetItemById(model.OwnerId)?.NameOrLogin; model.CreationDate = DateTime.Now; model.Categories = _bl.Categories.GetList().OrderBy(c => c.Name).ToList(); return(PartialView(model)); } catch (Exception e) { return(PartialView("Error", new ErrorViewModel(e.Message))); } }
public IActionResult RemoveCategories(NoteEditViewModel model, List <NoteCategory> noteCategories) { model.Note = _context.Notes.Include(i => i.NoteCategories).ThenInclude(nc => nc.Category).FirstOrDefault(note => note.NoteID == model.Note.NoteID); foreach (var c in model.CategoriesToRemove ?? new string[] { }) { model.Note.NoteCategories = model.Note.NoteCategories.Where(v => v.Category.CategoryID.ToString() != c).ToArray(); } model.CategoriesToRemove = new string[] {}; if (Notes.Where(m => m.Title == model.Note.Title).Any()) { return(View("Edit", new NoteEditViewModel(model.Note))); } else { return(View("Add", new NoteEditViewModel(model.Note))); } }
public IActionResult AddCategory(NoteEditViewModel model, List <NoteCategory> noteCategories) { model.Note.NoteCategories = noteCategories; if (ModelState.IsValid) { if (String.IsNullOrEmpty(model.NewCategory)) { ModelState.AddModelError("Category error", "Empty category"); if (_context.Notes.Where(m => m.Title == model.Note.Title).Any()) { return(View("Edit", model)); } else { return(View("Add", model)); } } else if (noteCategories.Where(m => m.Category.Title == model.NewCategory).Any()) { ModelState.AddModelError("Category error", "Category already exists"); if (_context.Notes.Where(m => m.Title == model.Note.Title).Any()) { return(View("Edit", model)); } else { return(View("Add", model)); } } model.Note.NoteCategories = noteCategories.Append(new NoteCategory { Note = model.Note, Category = new Category(model.NewCategory) }).ToArray(); model.NewCategory = ""; } if (model.Note.Timestamp != null) { return(View("Edit", model)); } else { return(View("Add", model)); } }
private bool SetStarState(int noteId, bool newState) { var userId = Guid.Parse(User.Identity.GetUserId()); var service = new NoteService(userId); var detail = service.GetNoteById(noteId); var updatedNote = new NoteEditViewModel { NoteId = detail.NoteId, Title = detail.Title, Content = detail.Content, IsStarred = newState }; return(service.UpdateNote(updatedNote)); }
private bool SetStarState(int noteId, bool state) { var detail = _svc .Value .GetNoteById(noteId); var note = new NoteEditViewModel { NoteId = detail.NoteId, Title = detail.Title, Content = detail.Content, IsStarred = state }; return(_svc.Value.UpdateNote(note)); }
public bool UpdateNote(NoteEditViewModel vm) { using (var ctx = new ElevenNoteDbContext()) { var entity = ctx .Notes .SingleOrDefault(e => e.OwnerId == _userId && e.NoteId == vm.NoteId); // TODO: Handle note not found entity.Title = vm.Title; entity.Content = vm.Content; entity.IsStarred = vm.IsStarred; entity.ModifiedUtc = DateTimeOffset.UtcNow; return(ctx.SaveChanges() == 1); } }
public bool UpdateNote(NoteEditViewModel model) { using (var context = new ElevenNoteDbContext()) { var entity = context .Notes .SingleOrDefault(e => e.NoteId == model.NoteId && e.OwnerId == _userId); if (entity == null) { return(false); } entity.Title = model.Title; entity.Content = model.Content; entity.ModifiedUtc = DateTimeOffset.Now; entity.IsStarred = model.IsStarred; return(context.SaveChanges() == 1); } }
public async Task <IActionResult> Edit(int id, NoteEditViewModel vm) { if (id != vm.Note.Id) { return(NotFound()); } if (ModelState.IsValid) { try { vm.Note.ApplicationUserId = GetCurrentUserId(); if (string.IsNullOrEmpty(vm.Note.CodeContent)) { vm.Note.HasCode = false; } else { vm.Note.HasCode = true; } vm.Note.LastModified = DateTime.UtcNow; _context.Update(vm.Note); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!NoteExists(vm.Note.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(vm)); }
// GET: Notes/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var note = await _context.Notes .Include(x => x.CodingLanguage) .SingleOrDefaultAsync(x => x.Id == id); if (note == null) { return(NotFound()); } var vm = new NoteEditViewModel { Note = note, CodingLanguages = new SelectList(_context.CodingLanguages.ToList(), "Id", "Name") }; return(View(vm)); }
public IActionResult EditPost(NoteEditViewModel model) { if (ModelState.IsValid) { string uniqueFileName = ProcessUploadedFile(model); Note editNote = _noteRepository.GetNote(model.ID); editNote.Content = model.Content; editNote.Author = model.Author; if (model.Photo != null) { string filePath = Path.Combine(__webHostEnvironment.WebRootPath, "images", model.NewPhotoPath); System.IO.File.Delete(filePath); } editNote.PhotoPath = ProcessUploadedFile(model); Note note = _noteRepository.Update(editNote); return(RedirectToAction("index")); } return(View()); }
public bool Put(int id, NoteEditViewModel vm) { return(_svc.Value.UpdateNote(vm)); }