Example #1
0
        public async void handlerShouldExecuteCorrectlyWhenNotAuthorized()
        {
            var noteMock = new Mock <Note>();

            noteMock.Setup(obj => obj.IsOwnedBy(It.IsAny <string>())).Returns(false);

            var notesRepo = new Mock <DbSet <Note> >();

            notesRepo.Setup(obj => obj.Find(It.IsAny <int>())).Returns(noteMock.Object);

            var context = new Mock <NotepadContext>(new DbContextOptions <NotepadContext>());

            context.SetupGet(obj => obj.Notes).Returns(notesRepo.Object);

            var handler = new UpdateNoteHandler(context.Object);

            var command = new UpdateNote(1, "mytitle", "mydescription");

            command.UserId = "myuserid";

            var result = await handler.Handle(command, new CancellationToken());

            noteMock.Verify(obj => obj.IsOwnedBy(command.UserId), Times.Once());

            notesRepo.Verify(obj => obj.Find(1), Times.Once());

            Assert.False(result);
        }
        public async Task Update_Note_Successfully()
        {
            var noteId = Guid.NewGuid().ToString();

            var updateNote = new UpdateNote
            {
                Title   = "Apples",
                Content = "Oranges"
            };

            noteService.Setup(x => x.UpdateNoteAsync(noteId, updateNote.Title, updateNote.Content)).ReturnsAsync(
                new Note
            {
                Id       = noteId,
                Title    = "Apples",
                Content  = "Oranges",
                Modified = DateTime.Now,
                Created  = DateTime.Now.AddDays(-7)
            }).Verifiable();

            var result = (await controller.Update(noteId, updateNote)).Result as OkObjectResult;

            var updatedNote = (ViewModels.Note)result?.Value;

            updatedNote.Should().NotBeNull();

            noteService.Verify();
        }
Example #3
0
        public async void handlerShouldExecuteCorrectlyWhenExists()
        {
            var noteMock = new Mock <Note>();

            noteMock.Setup(obj => obj.changeTitle(It.IsAny <string>())).Verifiable();
            noteMock.Setup(obj => obj.changeDescription(It.IsAny <string>())).Verifiable();
            noteMock.Setup(obj => obj.IsOwnedBy(It.IsAny <string>())).Returns(true).Verifiable();

            var notesRepo = new Mock <DbSet <Note> >();

            notesRepo.Setup(obj => obj.Find(It.IsAny <int>())).Returns(noteMock.Object).Verifiable();

            var context = new Mock <NotepadContext>(new DbContextOptions <NotepadContext>());

            context.SetupGet(obj => obj.Notes).Returns(notesRepo.Object).Verifiable();
            context.Setup(obj => obj.MarkAsModified(It.IsAny <Note>())).Verifiable();
            context.Setup <Task <int> >(obj => obj.SaveChangesAsync(It.IsAny <CancellationToken>())).ReturnsAsync(1);

            var handler = new UpdateNoteHandler(context.Object);

            var command = new UpdateNote(1, "mytitle", "mydescription");

            command.UserId = "myuserid";

            var result = await handler.Handle(command, new CancellationToken());

            noteMock.Verify();
            notesRepo.Verify();
            context.Verify();

            Assert.True(result);
        }
Example #4
0
        public async Task UpdateAsync(UpdateNote command)
        {
            var note = await GetAsync(command.Id);

            note.Update(command.Title, command.Content);

            await _context.SaveChangesAsync();
        }
Example #5
0
 public IActionResult UpdateNote(UpdateNote Note)
 {
     if (ModelState.IsValid)
     {
         Console.WriteLine("I am here");
         string update_query = $@"UPDATE notes SET description='{Note.description}', updated_at=Now()
                                 WHERE  id = '{Note.id}'";
         DbConnector.Execute(update_query);
         return(RedirectToAction("Index"));
     }
     return(View("Index", Note));
 }
Example #6
0
        private void Button_Click_EditNote(object sender, RoutedEventArgs e)
        {
            if (Repository.GetAllName().Count == 0)
            {
                MessageBox.Show("В базе не найденно не одной записи");
                return;
            }

            var window = new UpdateNote();

            window.ShowDialog();
        }
Example #7
0
        public ActionResult <Note> Patch([FromRoute] int noteId, [FromBody] UpdateNote patchNote)
        {
            var note = _database.Notes.Find(noteId);

            if (note == null)
            {
                return(NotFound($"Note with noteId {noteId} not found"));
            }

            note.Content = patchNote.Content;
            _database.SaveChanges();

            return(Ok(note));
        }
Example #8
0
        private async Task EditNoteAsync(object id)
        {
            var noteId = (int)id;

            var context = new UpdateNote(Notes.SingleOrDefault(x => x.Id == noteId));
            var dialog  = DialogHelper.GetContentDialog(DialogEnum.EditNoteDialog, context);
            var result  = await dialog.ShowAsync();

            if (result == ContentDialogResult.Primary)
            {
                context = (UpdateNote)dialog.DataContext;
                await _noteService.UpdateAsync(context);
            }
        }
Example #9
0
        public IActionResult Put(int id, [FromBody] UpdateNote noteToUpdate)
        {
            var note = new Note(id, noteToUpdate.Text);

            try
            {
                _repository.UpdateNote(note);
                return(Ok());
            }
            catch (ResourceNotFoundException)
            {
                return(NotFound());
            }
        }
Example #10
0
        public async Task <ActionResult <Note> > Update(string id, [FromBody] UpdateNote note)
        {
            logger.LogDebug($"Updating note: {id}");

            var updatedNote = await notesService.UpdateNoteAsync(id, note.Title, note.Content);

            if (updatedNote == null)
            {
                logger.LogDebug($"Unable to find note: {id}");
                return(NotFound());
            }

            logger.LogDebug($"Updated note: {id}");

            return(Ok(new Note(updatedNote)));
        }
        public async Task Update_Note_Returns_Not_Found()
        {
            var noteId = Guid.NewGuid().ToString();

            var updateNote = new UpdateNote
            {
                Title   = "Apples",
                Content = "Oranges"
            };

            noteService.Setup(x => x.UpdateNoteAsync(noteId, updateNote.Title, updateNote.Content)).ReturnsAsync(() => null).Verifiable();

            var result = (await controller.Update(noteId, updateNote)).Result;

            result.GetType().Should().Be(typeof(NotFoundResult));

            noteService.Verify();
        }
Example #12
0
        public async Task <IActionResult> Put(int id, [FromBody] UpdateNote command)
        {
            var user = await _userManager.GetUserAsync(HttpContext.User);

            command.UserId = user.Id;

            var result = await _mediator.Send(command);

            if (result == false)
            {
                return(NotFound());
            }

            await _mediator.Publish(new NoteUpdated(
                                        id,
                                        command.Title,
                                        command.Description
                                        ));

            return(NoContent());
        }
Example #13
0
        public async Task PutChangedNote3NotesReturnsSuccessAndChangesNote()
        {
            InMemoryNotesRepository.Notes.Clear();
            InMemoryNotesRepository.Notes.AddRange(new[]
            {
                new Note(1, "Test1"),
                new Note(2, "Test2"),
                new Note(3, "Test3"),
            });
            var newNote = new UpdateNote {
                Text = "Changed2"
            };
            var bodyContent = new StringContent(JsonConvert.SerializeObject(newNote), Encoding.Default, "application/json");

            var response = await _client.PutAsync("/api/notes/2", bodyContent);

            response.EnsureSuccessStatusCode();
            var result = InMemoryNotesRepository.Notes.Single(n => n.Id == 2);

            Assert.AreEqual("Changed2", result.Text);
        }
Example #14
0
        public async Task <IActionResult> UpdateNoteAsync([Required] long?noteId, [FromBody][Required] UpdateNote data, CancellationToken cancellationToken = default(CancellationToken))
        {
            var hierarchy = await this._documentSession.LoadOrCreateHierarchyAsync(this.User.Identity.Name, cancellationToken);

            if (hierarchy.HasNote(this._documentSession.ToStringId <Note>(noteId)) == false)
            {
                return(this.NotFound());
            }

            var note = await this._documentSession.LoadAsync <Note>(noteId, cancellationToken);

            if (data.HasTitle())
            {
                note.Title = data.Title;
                hierarchy.UpdateNoteTitle(note.Id, note.Title);
            }

            if (data.HasContent())
            {
                note.Content = data.Content;
            }

            if (data.HasFolderId())
            {
                hierarchy.UpdateFolderId(note.Id, this._documentSession.ToStringId <Folder>(data.FolderId));
            }

            if (hierarchy.Validate() == false)
            {
                return(this.BadRequest());
            }

            await this._documentSession.SaveChangesAsync(cancellationToken);

            var noteDTO = await this._noteToNoteDTOMapper.MapAsync(note, cancellationToken);

            return(this.Ok(noteDTO));
        }
Example #15
0
        //public async void LoadNotesFromServer()
        //{
        //	try
        //	{
        //		var notes = await firebaseClient.Child("notes").OrderByValue().StartAt(curUser.Email).OnceAsync<NoteData>();

        //		if (notes.Count > 0)
        //		{
        //			foreach (var item in notes)
        //			{
        //				UpdateNoteEventArgs args = new UpdateNoteEventArgs
        //				{
        //					Data = item.Object
        //				};
        //				OnUpdateNote(args);
        //			}
        //		}
        //	}
        //	catch (Exception ex)
        //	{
        //		Console.WriteLine(ex);
        //	}
        //}

        protected virtual void OnUpdateNote(UpdateNoteEventArgs e)
        {
            UpdateNote?.Invoke(this, e);
        }