Exemplo n.º 1
0
        public async Task <UpdateResult> UpdateNote(Note note
                                                    , IEnumerable <SelectableTag> selectableTags
                                                    , IEnumerable <Comment> comments)
        {
            var now = DateTime.Now;

            Assert(selectableTags != null);
            Assert(comments != null);
            if (note.NoteTags == null)
            {
                note.NoteTags = new List <NoteTag>();
            }
            try
            {
                _context.Database.ExecuteSqlCommand("delete from NoteTag where Noteid = {0}", note.Id);
                _context.Database.ExecuteSqlCommand("delete from Comments where Noteid = {0}", note.Id);
                var user = new User {
                    Id = 1
                };
                _context.User.Attach(user);
                note.User = user;
                if (IsNewNote(note))
                {
                    _context.Add(note);
                }
                else
                {
                    _context.Update(note);
                }
                _context.Entry(note).Property(Constants.LastUpdated).CurrentValue = now;
                IncludeSelectedTagsInNote(note, selectableTags);
                foreach (NoteTag ntt in note.NoteTags)
                {
                    _context.NoteTag.Add(ntt);
                    _context.Entry(ntt).Property(Constants.LastUpdated).CurrentValue = now;
                    _context.Entry(ntt).Property(Constants.UserId).CurrentValue      = user.Id;
                }
                note.Comments.Clear();
                foreach (var cmt in comments)
                {
                    Comment newComment = new Comment {
                        Payload = cmt.Payload
                    };
                    note.Comments.Add(newComment);
                    _context.CommentSet.Add(newComment);
                    _context.Entry(newComment).Property(Constants.LastUpdated).CurrentValue = now;
                    _context.Entry(newComment).Property(Constants.UserId).CurrentValue      = user.Id;
                }
                int x = await _context.SaveChangesAsync();

                return(UpdateResult.Success);
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!NoteExists(note.Id))
                {
                    return(UpdateResult.NoteAlreadyDeleted);
                }
                else
                {
                    return(UpdateResult.ConcurrencyConflict);
                }
            }
        }