예제 #1
0
        public async Task <IActionResult> UpdateAsync(int?id, [FromBody] NoteData note)
        {
            Note oldNote = _context.Note.Where(n => n.Idnote == id).FirstOrDefault();

            if (oldNote == null)
            {
                Note deletedNote = new Note();
                await TryUpdateModelAsync(deletedNote);

                return(StatusCode(404, "Note was deleted"));;
            }
            oldNote = _context.Note.Include(i => i.NoteCategory).ThenInclude(noteCategories => noteCategories.IdcategoryNavigation).FirstOrDefault(n => n.Idnote == oldNote.Idnote);
            if (_context.Note.Where(n => n.Title == note.Title && n.Idnote != id).Any())
            {
                return(StatusCode(400, "Note with title - " + note.Title + " - already exists"));
            }
            _context.Entry(oldNote).Property("Timestamp").OriginalValue = note.Timestamp;
            try
            {
                oldNote.Title       = note.Title;
                oldNote.Description = note.Text;
                oldNote.Date        = note.Date;
                oldNote.IsMarkdown  = Convert.ToInt16(note.Markdown);
                await _context.SaveChangesAsync();

                updateCategories(oldNote.Idnote, note.NoteCategories);
            }
            catch (DbUpdateConcurrencyException ex)
            {
                var entry         = ex.Entries.Single();
                var clientValues  = (Note)entry.Entity;
                var databaseEntry = entry.GetDatabaseValues();
                if (databaseEntry == null)
                {
                    return(StatusCode(404, "Note was deleted"));;
                }
                else
                {
                    var    databaseValues = (Note)databaseEntry.ToObject();
                    string error          = getConcurrencyErrors(clientValues, databaseValues);
                    return(StatusCode(403, "The record you attempted to edit "
                                      + "was modified by another user after you got the original value. The "
                                      + "edit operation was canceled and the current values in the database "
                                      + "have been displayed. Click the Back to List hyperlink and if you "
                                      + "still want to edit please re-enter Note. Current values:\n" + error));
                }
            }
            catch (DbUpdateException ex)
            {
                return(StatusCode(500, ex.InnerException.Message));
            }
            return(Ok());
        }
예제 #2
0
        public async Task <IActionResult> UpdateAsync(int?id, [FromBody] DomainModel.Note note)
        {
            if (id == null)
            {
                return(NotFound());
            }

            using (var context = new NTR2019ZContext()){
                var noteToUpdate = await context.Note.Include(i => i.NoteCategory).ThenInclude(noteCategories => noteCategories.IdcategoryNavigation).FirstOrDefaultAsync(note => note.Idnote == id);

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

                context.Entry(noteToUpdate).Property("Timestamp").OriginalValue = note.Timestamp;

                try
                {
                    noteToUpdate.Date        = note.Date;
                    noteToUpdate.Title       = note.Title;
                    noteToUpdate.Description = note.Description;
                    noteToUpdate.IsMarkdown  = note.IsMarkdown;

                    foreach (string category in note.Categories)
                    {
                        var cat = await context.Category.Where(categoryObj => categoryObj.Name == category).FirstOrDefaultAsync();

                        if (cat == null)
                        {
                            cat = new Category {
                                Name = category
                            };
                            context.Category.Add(cat);
                        }
                        var noteCategory = await context.NoteCategory.Where(nc => nc.IdcategoryNavigation.Name == category && nc.Idnote == noteToUpdate.Idnote).FirstOrDefaultAsync();

                        if (noteCategory == null)
                        {
                            context.Add(new NoteCategory {
                                Idnote = noteToUpdate.Idnote, IdcategoryNavigation = cat
                            });
                        }
                    }

                    await context.SaveChangesAsync();

                    return(Ok());
                } catch (DbUpdateConcurrencyException ex)
                {
                    var exceptionEntry = ex.Entries.Single();
                    var databaseEntry  = exceptionEntry.GetDatabaseValues();
                    if (databaseEntry == null)
                    {
                        return(StatusCode(500, "The record you attempted to edit "
                                          + "was deleted."));
                    }
                    else
                    {
                        noteToUpdate.Timestamp = (byte[])noteToUpdate.Timestamp;
                        return(StatusCode(403, "The record you attempted to edit "
                                          + "was modified by another user after you got the original value. The "
                                          + "edit operation was canceled and the current values in the database "
                                          + "have been displayed. Refresh the page or click Back to List hyperlink."));
                    }
                } catch (DbUpdateException ex)
                {
                    return(StatusCode(500, ex.InnerException.Message));
                }
            }
        }