コード例 #1
0
        public ActionResult CreatePost(NoteDetailViewModel vm)
        {
            if (!ModelState.IsValid) return View(vm);

            var svc = new NoteService();

            var result = svc.Create(vm, _userId.Value);

            TempData.Add(
                "Result",
                result
                    ? "Note added"
                    : "Note not added");

            return RedirectToAction("Index");
        }
コード例 #2
0
        public bool Create(NoteDetailViewModel vm, Guid userId)
        {
            using (var ctx = new ElevenNoteDbContext())
            {
                var note =
                    new Note
                    {
                        Title = vm.Title,
                        Contents = vm.Contents,
                        DateCreated = DateTime.UtcNow,
                        ApplicationUserId = userId,
                        IsFavorite = vm.IsFavorite
                    };

                ctx.Notes.Add(note);

                return ctx.SaveChanges() == 1;
            }
        }
コード例 #3
0
        public bool Update(NoteDetailViewModel vm, Guid userId)
        {
            using (var ctx = new ElevenNoteDbContext())
            {
                var note =
                    ctx
                        .Notes
                        .Where(
                            n => n.Id == vm.Id
                                 && n.ApplicationUserId == userId)
                        .SingleOrDefault();

                if (note == null) return false;

                note.Contents = vm.Contents;
                note.Title = vm.Title;
                note.IsFavorite = vm.IsFavorite;
                note.DateModified = DateTime.UtcNow;

                return ctx.SaveChanges() == 1;
            }
        }