コード例 #1
0
        public async Task <IActionResult> PutCategory([FromRoute] int id, [FromBody] Category category)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != category.ID)
            {
                return(BadRequest());
            }

            _context.Entry(category).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CategoryExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #2
0
        public async Task <IActionResult> PutNote([FromRoute] string id, [FromBody] Note note)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != note.Title)
            {
                return(BadRequest());
            }

            _context.Entry(note).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!NoteExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #3
0
        public async Task <IActionResult> PutNote(int id, Note note)
        {
            if (id != note.IdNote)
            {
                return(BadRequest());
            }

            _context.Entry(note).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!NoteExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #4
0
        public async Task <Note> AddNote(Note note)
        {
            _context.Note.Add(note);

            await _context.SaveChangesAsync();

            return(note);
        }
コード例 #5
0
        public async Task <IActionResult> Post([FromBody] Note note)
        {
            await _dbContext.Notes.AddAsync(note);

            await _dbContext.SaveChangesAsync(HttpContext.User);

            return(Ok(note.Id));
        }
コード例 #6
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));
        }
コード例 #7
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));
        }
コード例 #8
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));
        }
コード例 #9
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));
        }
コード例 #10
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));
        }
コード例 #11
0
ファイル: NotesController.cs プロジェクト: d3c6e1/notes_api
        public async Task <ActionResult <Note> > Post(Note note)
        {
            if (note == null)
            {
                return(BadRequest());
            }

            _context.Notes.Add(note);
            await _context.SaveChangesAsync();

            return(Ok(note));
        }
コード例 #12
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));
        }
コード例 #13
0
        public Task <Core.Note> AddNoteAsync(Core.Note note)
        {
            if (note.Author is null)
            {
                throw new ArgumentException("Note must have author", nameof(note));
            }

            return(AddNoteInternalAsync(note));

            async Task <Core.Note> AddNoteInternalAsync(Core.Note note)
            {
                User author = await _context.Users.FindAsync(note.Author.Id);

                if (author is null)
                {
                    throw new ArgumentException("Author does not exist", nameof(note));
                }

                List <string> tags = await _context.Tags.Select(t => t.Name).ToListAsync();

                var newNote = new Note
                {
                    Id     = note.Id,
                    Author = author,
                    Text   = note.Text
                };

                _context.Notes.Add(newNote);

                ISet <string> tagSet = tags.ToHashSet();

                for (int i = 0; i < note.Tags.Count; i++)
                {
                    if (!tagSet.Contains(note.Tags[i]))
                    {
                        _context.Tags.Add(new Tag {
                            Name = note.Tags[i]
                        });
                    }

                    newNote.NoteTags.Add(new NoteTag {
                        TagName = note.Tags[i], Order = i
                    });
                }

                await _context.SaveChangesAsync();

                return(MapNoteWithAuthorAndTags(newNote));
            }
        }
コード例 #14
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));
        }
コード例 #15
0
        public static async Task SeedAsync(NotesContext context, ILogger <NotesContextSeed> logger)
        {
            try
            {
                using (context)
                {
                    logger.LogInformation($"Starting migration of database on context {nameof(NotesContext)}");

                    context.Database.Migrate();

                    var retries = 3;
                    var policy  = Policy.Handle <SqlException>()
                                  .WaitAndRetryAsync(
                        retries,
                        (count, ctx) => TimeSpan.FromSeconds(3),
                        (exception, timeSpan, retry, ctx) =>
                    {
                        logger.LogError(exception,
                                        $"[NotesContextSeed] Exception {exception.GetType().Name} with message {exception.Message} on attempt {retry} of {retries}");
                    });

                    await policy.ExecuteAsync(async() =>
                    {
                        if (!context.Tags.Any())
                        {
                            context.Tags.AddRange(Tags);
                            await context.SaveChangesAsync();
                        }

                        if (!context.Notebooks.Any())
                        {
                            Tags.ForEach(tag =>
                            {
                                NoteList1.ForEach(note => note.AddTag(tag));
                                NoteList2.ForEach(note => note.AddTag(tag));
                                NoteList3.ForEach(note => note.AddTag(tag));
                            });
                            context.Notebooks.AddRange(Notebooks);
                            await context.SaveChangesAsync();
                        }
                    });

                    logger.LogInformation($"Finished migrating database on context {nameof(NotesContext)}");
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, $"An error occurred while migrating database on context {nameof(NotesContext)}");
            }
        }
コード例 #16
0
ファイル: EditFunc.cs プロジェクト: HolanDiversity/NotesSite
        public async Task <IActionResult> Edit(Note phone)
        {
            db.Notes.Update(phone);
            await db.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
コード例 #17
0
        public async Task <IActionResult> Create(Note note)
        {
            db.Notes.Add(note);
            await db.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
コード例 #18
0
 public async Task DeleteTodo(ToDo toDo)
 {
     using (var dbContext = new NotesContext())
     {
         dbContext.Remove(toDo);
         await dbContext.SaveChangesAsync();
     }
 }
コード例 #19
0
 public async Task DeleteNote(Note note)
 {
     using (var dbContext = new NotesContext())
     {
         dbContext.Remove(note);
         await dbContext.SaveChangesAsync();
     }
 }
コード例 #20
0
        public async Task <IActionResult> Post([FromBody] Note model)
        {
            try
            {
                _logger.LogInformation($"Creating Note {model.Text}");
                await _context.Notes.AddAsync(model);

                await _context.SaveChangesAsync();

                return(Ok());
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Failed to create Note");
                return(BadRequest());
            }
        }
コード例 #21
0
 public async Task DeleteTodoAsync(Todo todo)
 {
     using (var dbContext = new NotesContext())
     {
         dbContext.Remove(todo);
         await dbContext.SaveChangesAsync().ConfigureAwait(false);
     }
 }
コード例 #22
0
        public async Task <ActionResult <Note> > CreateNote(Note note)
        {
            await _db.Notes.AddAsync(note);

            var result = await _db.SaveChangesAsync();

            return(Created($"api/notes/{note.Id}", note));
        }
コード例 #23
0
        public async Task <IActionResult> PutNote(int id, Note note)
        {
            if (id != note.IdNote)
            {
                return(BadRequest());
            }
            if (!NoteExists(id) || !NoteHistoryExists(id))
            {
                return(NotFound());
            }


            try
            {
                var version = await _context.NoteHistories.Where(n => n.IdNote == id).MaxAsync(n => n.Version);

                var noteHistory = await _context.NoteHistories.FindAsync(id, version);

                ++version;
                _context.NoteHistories.Add(new NoteHistory {
                    IdNote = note.IdNote, Version = version, Title = note.Title, Content = note.Content, Created = noteHistory.Created
                });
                await _context.SaveChangesAsync();

                noteHistory = await _context.NoteHistories.FindAsync(id, version);

                note.Created  = noteHistory.Created;
                note.Modified = DateTime.Now;
                _context.Entry(note).State = EntityState.Modified;

                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!NoteExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #24
0
        private void tagView_DragDrop(object sender, DragEventArgs e)
        {
            TreeNode NewNode;

            if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false))
            {
                Point    pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
                TreeNode DestinationNode = ((TreeView)sender).GetNodeAt(pt);
                NewNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
                using (context.Database.BeginTransaction())
                {
                    if (DestinationNode == null)
                    {
                        ((Model.Tag)NewNode.Tag).ParentTag = null;
                    }
                    else if (CheckNoParent(NewNode, DestinationNode))
                    {
                        //Remove Original Node
                        ((Model.Tag)NewNode.Tag).ParentTag = ((Model.Tag)DestinationNode.Tag);
                    }
                }
                context.SaveChangesAsync();
                UpdateTagList();
            }
        }
コード例 #25
0
        public async Task <IActionResult> Put([FromBody] Note note)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.Entry(note).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }

            return(NoContent());
        }
コード例 #26
0
        public async Task <IActionResult> PutNotes([FromRoute] int id, [FromBody] Notes notes)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != notes.ID)
            {
                return(BadRequest());
            }
            var user     = _context.Users.Find(notes.User.ID);
            var category = _context.Categories.Find(notes.Category.ID);

            notes.User     = user;
            notes.Category = category;

            _context.Entry(notes).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!NotesExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #27
0
        public async Task <IActionResult> PutManagedNotes(long id, ManagedNotesDTO managedNotesDTO)
        {
            if (id != managedNotesDTO.Id)
            {
                return(BadRequest());
            }

            var managedNotes = await _context.Notes.FindAsync(id);

            if (managedNotes == null)
            {
                return(NotFound());
            }

            managedNotes.IsComplete = managedNotesDTO.IsComplete;
            managedNotes.Name       = managedNotesDTO.Name;

            _context.Entry(managedNotes).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ManagedNotesExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutNote([FromRoute] int id, [FromBody] Note note)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != note.ID)
            {
                return(BadRequest());
            }
            //GetNote().Where(i => i.ID == id).ForEachAsync(z => { z = note; });


            _context.Note.Update(note);
            //_context.Entry(note).State = EntityState.Modified;


            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!NoteExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #29
0
        public async Task <IActionResult> Delete(int?id)
        {
            if (id != null)
            {
                Note note = await db.Notes.FirstOrDefaultAsync(p => p.Id == id);

                if (note != null)
                {
                    db.Notes.Remove(note);
                    await db.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }
            }
            return(NotFound());
        }
コード例 #30
0
        public async Task SaveTodoAsync(Todo todo)
        {
            using (var dbContext = new NotesContext())
            {
                if (todo.Id == 0)
                {
                    await dbContext.Todos.AddAsync(todo).ConfigureAwait(false);
                }
                else
                {
                    dbContext.Todos.Update(todo);
                }

                await dbContext.SaveChangesAsync().ConfigureAwait(false);
            }
        }