Ejemplo n.º 1
0
        public ActionResult EditPost(NoteEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                var noteService = new ElevenNote.Services.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(model);
        }
Ejemplo n.º 2
0
        /// <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.IsFavorite = model.IsFavorite;
                note.ApplicationUserId = userId;

                context.Notes.Add(note);
                var result = context.SaveChanges();
                return result == 1;
            }
        }
Ejemplo n.º 3
0
        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? */;
            }
        }