예제 #1
0
        public async Task <IActionResult> CreateAsync([FromBody] DomainModel.Note note)
        {
            using (var context = new NTR2019ZContext()) {
                if (context.Note.Any(n => n.Title == note.Title))
                {
                    return(StatusCode(400, "A note with title=\"" + note.Title + "\" already exists in the database"));
                }

                Note newNote = new Note()
                {
                    Title       = note.Title,
                    Description = note.Description,
                    Date        = note.Date,
                    IsMarkdown  = note.IsMarkdown,
                };
                context.Note.Add(newNote);
                await context.SaveChangesAsync();

                Array.ForEach(note.Categories.ToArray(), c => {
                    try{
                        Category cat;
                        using (var transaction = context.Database.BeginTransaction()){
                            var occurances = context.Category.Where(categoryObj => categoryObj.Name == c).ToList();
                            if (occurances.Count() == 0)
                            {
                                cat = new Category {
                                    Name = c
                                };
                                context.Category.Add(cat);
                                context.SaveChanges();
                            }
                            else
                            {
                                cat = occurances.FirstOrDefault();
                            }
                            context.NoteCategory.Add(new NoteCategory {
                                Idnote     = newNote.Idnote,
                                Idcategory = cat.Idcategory
                            });
                            context.SaveChanges();
                            transaction.Commit();
                        }
                    } catch (DbUpdateException e) {
                        Console.WriteLine(e.Message);
                    }
                });
                await context.SaveChangesAsync();

                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));
                }
            }
        }