public async Task <bool> AddNote(
            string username,
            Note note,
            IReadOnlyList <string> tags = null)
        {
            if (string.IsNullOrWhiteSpace(username))
            {
                var message = "Cannot add note."
                              + $" The specified username '{username}'"
                              + " may not be null, empty. or whitespace";

                throw new ArgumentException(message, nameof(username));
            }

            var user = await _userManager.FindByNameAsync(username);

            if (user == null)
            {
                var message = "Cannot add note."
                              + $" A user having username '{username}' does not exist.";

                throw new InvalidOperationException(message);
            }

            note.UserId = user.Id;

            if (tags == null || !tags.Any())
            {
                return(await _addNoteCommand.ExecuteAsync(note));
            }

            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var addTagsResult = await _addTagsCommand.ExecuteAsync(tags);

                foreach (var tag in addTagsResult)
                {
                    note.NoteTags.Add(new NoteTag {
                        TagId = tag.Id
                    });
                }

                var addNoteResult = await _addNoteCommand.ExecuteAsync(note);

                if (!addNoteResult)
                {
                    return(false);
                }

                scope.Complete();
                return(true);
            }
        }
Exemple #2
0
        public async Task <bool> ExecuteAsync(Note note, IReadOnlyList <string> tags)
        {
            if (note == null)
            {
                throw new ArgumentNullException(nameof(note));
            }

            var noteFromDb = await FindNoteAsync(note.Id);

            noteFromDb.Description = note.Description;
            noteFromDb.Title       = note.Title;
            noteFromDb.ModifiedAt  = DateTimeOffset.Now;

            using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                if (tags != null && tags.Any())
                {
                    // if (noteFromDb.NoteTags.Any())
                    //    _context.NoteTags.RemoveRange(noteFromDb.NoteTags);

                    noteFromDb.NoteTags = new List <NoteTag>();

                    var addTagsResult = await _addTagsCommand.ExecuteAsync(tags);

                    foreach (var tag in addTagsResult)
                    {
                        noteFromDb.NoteTags.Add(new NoteTag {
                            TagId = tag.Id
                        });
                    }
                }

                var result = await _context.SaveChangesAsync();

                if (result < 1)
                {
                    throw new InvalidOperationException("Note update failed.");
                }

                transaction.Complete();
            }

            return(true);
        }