Exemple #1
0
        public Task <Note> PatchAsync(NotePatchInfo patchInfo, CancellationToken cancelltionToken)
        {
            if (patchInfo == null)
            {
                throw new ArgumentNullException(nameof(patchInfo));
            }

            cancelltionToken.ThrowIfCancellationRequested();

            var searchResult = notes.Find(x => x.Id == patchInfo.NoteId);

            if (!searchResult.Any())
            {
                throw new NoteNotFoundExcepction(patchInfo.NoteId);
            }

            var updated = false;
            var note    = searchResult.First();

            if (patchInfo.Title != null)
            {
                note.Title = patchInfo.Title;
                updated    = true;
            }

            if (patchInfo.Text != null)
            {
                note.Text = patchInfo.Text;
                updated   = true;
            }

            if (patchInfo.Favorite != null)
            {
                note.Favorite = patchInfo.Favorite.Value;
                updated       = true;
            }

            if (updated)
            {
                note.LastUpdatedAt = DateTime.UtcNow;
                notes.ReplaceOne(x => x.Id == note.Id, note);
            }

            return(Task.FromResult(note));
        }
Exemple #2
0
        /// <summary>
        /// Изменить заметку
        /// </summary>
        /// <param name="patchInfo">Описание изменений заметки</param>
        /// <param name="cancelltionToken">Токен отмены операции</param>
        /// <returns>Задача, представляющая асинхронный запрос на изменение заметки. Результат выполнения - актуальное состояние заметки</returns>
        public Task <Note> PatchAsync(NotePatchInfo patchInfo, CancellationToken cancelltionToken)
        {
            if (patchInfo == null)
            {
                throw new ArgumentNullException(nameof(patchInfo));
            }

            cancelltionToken.ThrowIfCancellationRequested();

            if (!this.primaryKeyIndex.TryGetValue(patchInfo.NoteId, out var note))
            {
                throw new NoteNotFoundExcepction(patchInfo.NoteId);
            }

            var updated = false;

            if (patchInfo.Title != null)
            {
                note.Title = patchInfo.Title;
                updated    = true;
            }

            if (patchInfo.Text != null)
            {
                note.Text = patchInfo.Text;
                updated   = true;
            }

            if (patchInfo.Favorite != null)
            {
                note.Favorite = patchInfo.Favorite.Value;
                updated       = true;
            }

            if (updated)
            {
                note.LastUpdatedAt = DateTime.UtcNow;
            }

            return(Task.FromResult(note));
        }