Ejemplo n.º 1
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));
                }
            }
        }