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? */); } }
/// <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; note.IsFavorite = model.IsFavorite; context.Notes.Add(note); var result = context.SaveChanges(); return result == 1; } }
/// <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 bool Delete(int id, Guid userId) { using (var context = new ElevenNoteDataContext()) { // Attempt to get the note from the database. var note = context.Notes.Where(w => w.Id == id && w.ApplicationUserId == userId).SingleOrDefault(); // Make sure we actually received a note back before updating. if (note == null) return false; // Delete the note. context.Notes.Remove(note); // Save the changes to the database. var result = context.SaveChanges(); // Return the result. return result == 1; } }
public bool ToggleFavorite(int id, Guid userId) { using (var context = new ElevenNoteDataContext()) { // Attempt to get the note from the database. var note = context.Notes.Where(w => w.Id == id && w.ApplicationUserId == userId).SingleOrDefault(); // Make sure we actually received a note back before updating. if (note == null) { return(false); } // Toggle the note IsFavorite flag. note.IsFavorite = !note.IsFavorite; // Save the changes to the database. var result = context.SaveChanges(); // Return the result. return(result == 1); } }
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.DateModified = DateTime.UtcNow; note.IsFavorite = model.IsFavorite; // Save the changes to the database. var result = context.SaveChanges(); return result == 1 /* was 1 record (success) or 0 records (unsuccessful) updated? */; } }