コード例 #1
0
        public Note Add(Note note)
        {
            var entry = context.Add(note);

            context.SaveChanges();

            return(entry.Entity);
        }
コード例 #2
0
        public void EditNote(int id, NoteWithCategories newNote)
        {
            var oldNote = _context.Notes.Where(n => n.NoteID == id &&
                                               n.Timestamp == newNote.Timestamp).FirstOrDefault();

            if (oldNote == null)
            {
                throw new Exception("The file does not exist or has been changed");
            }
            // Find differences in standard fields and replace if needed
            if (oldNote.Title != newNote.Title ||
                oldNote.Description != newNote.Description ||
                oldNote.NoteDate != newNote.NoteDate ||
                oldNote.IsMarkdown != newNote.IsMarkdown)
            {
                oldNote.Title       = newNote.Title;
                oldNote.Description = newNote.Description;
                oldNote.NoteDate    = newNote.NoteDate;
                oldNote.IsMarkdown  = newNote.IsMarkdown;
            }

            // Find differences in categories

            var categoriesInDb = _context.NoteCategories
                                 .Where(nc => nc.Note == oldNote)
                                 .Include(nc => nc.Category);

            var newCategories = newNote.Categories
                                .Except(categoriesInDb.Select(c => c.Category.Title));

            foreach (var newCategory in newCategories)
            {
                var categoryInDb = _context.Categories
                                   .Where(c => c.Title == newCategory)
                                   .FirstOrDefault();

                if (categoryInDb == null)
                {
                    categoryInDb = new Category
                    {
                        Title = newCategory
                    };
                }
                _context.Add(new NoteCategory
                {
                    Note     = oldNote,
                    Category = categoryInDb
                });
            }

            var deletedCategories = categoriesInDb
                                    .Where(c => !newNote.Categories.Contains(c.Category.Title));

            _context.RemoveRange(deletedCategories);
            _context.SaveChanges();
        }
コード例 #3
0
        public Note CreateNote(Note createdNote)
        {
            var note = db.Add(createdNote);

            db.SaveChanges();
            return(note.Entity);
        }
コード例 #4
0
        public async Task <IActionResult> Create([Bind("ID,Title,Body")] Note note)
        {
            if (ModelState.IsValid)
            {
                _context.Add(note);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(note));
        }
コード例 #5
0
        public async Task <IActionResult> Create([Bind("ID,Name")] Category category)
        {
            if (ModelState.IsValid)
            {
                _context.Add(category);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }
コード例 #6
0
        public async Task <IActionResult> Create([Bind("Id,Owner,NoteTitle,NoteContent")] Notes notes)
        {
            if (ModelState.IsValid)
            {
                _context.Add(notes);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(notes));
        }
コード例 #7
0
        public async Task <IActionResult> Create([Bind("ID,Email,Name,CreatedOn")] User user)
        {
            if (ModelState.IsValid)
            {
                _context.Add(user);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(user));
        }
コード例 #8
0
        public async Task <IActionResult> Create([Bind("Id,Name,UrlSlug,Description")] Tag tag)
        {
            if (ModelState.IsValid)
            {
                _context.Add(tag);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(tag));
        }
コード例 #9
0
        public async Task <IActionResult> Create([Bind("ID,Title,Notes,CreatedOn,CategoryId,UserId,IsDeleted")] Note note)
        {
            if (ModelState.IsValid)
            {
                _context.Add(note);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CategoryId"] = new SelectList(_context.Categories, "ID", "ID", note.CategoryId);
            ViewData["UserId"]     = new SelectList(_context.Users, "ID", "ID", note.UserId);
            return(View(note));
        }
コード例 #10
0
 public bool Salvar(Notes notes)
 {
     if (notes.Id != null)
     {
         _notesContext.Update(notes);
     }
     else
     {
         _notesContext.Add(notes);
     }
     _notesContext.SaveChanges();
     return(true);
 }
コード例 #11
0
 public bool Salvar(Users dados)
 {
     if (dados.Id > 0)
     {
         _notesContext.Update(dados);
     }
     else
     {
         _notesContext.Add(dados);
     }
     _notesContext.SaveChanges();
     return(true);
 }
コード例 #12
0
 public IActionResult Create([Bind("ID,Title,Content,FinishDate,Finished,Importance")] Note note)
 {
     if (ModelState.IsValid)
     {
         if (note.Finished)
         {
             note.FinishedDate = DateTime.Now;
         }
         _context.Add(note);
         _context.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(BadRequest());
 }
コード例 #13
0
        public async Task <IActionResult> Create([Bind("Id,Name,Text,Date_system,UrlNote,TagID,CategoryID")] Note note)
        {
            if (ModelState.IsValid)
            {
                _context.Add(note);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CategoryID"] = new SelectList(_context.Category, "Id", "Name", note.CategoryID);

            ViewData["TagID"] = new SelectList(_context.Tag, "Id", "Name", note.TagID);
            return(View(note));
        }