示例#1
0
        public Task Create(Domain.Dto.Note note)
        {
            var toAdd = DbModel.Note.From(note);

            _dbContext.Notes.Add(toAdd);
            return(_dbContext.SaveChangesAsync());
        }
示例#2
0
        public async Task Update(Domain.Dto.Note note)
        {
            var toUpdate = await FindOrException(note.Id);

            toUpdate.Title   = note.Title;
            toUpdate.Content = note.Content;
            await _dbContext.SaveChangesAsync();
        }
示例#3
0
        public void GivenANonExistingNote_WhenUpdate_ThenAnExceptionIsThrown()
        {
            var id = Guid.NewGuid();

            using (var dbContext = GetFreshDbContext())
            {
                var note = new Domain.Dto.Note(id, "title", "content");

                var         sut    = GetSut(dbContext);
                Func <Task> update = async() => await sut.Update(note);

                update.ShouldThrow <Exception>().WithMessage($"Note with id {id} could not be found.");
            }
        }
示例#4
0
        public async Task GivenANoteWithAnIdThatAlreadyExists_WhenCreate_ThenAnExceptionIsThrown()
        {
            var id = await CreateNote();

            using (var dbContext = GetFreshDbContext())
            {
                var note = new Domain.Dto.Note(id, "title", "content");

                var         sut    = GetSut(dbContext);
                Func <Task> create = async() => await sut.Create(note);

                create.ShouldThrow <ArgumentException>().WithMessage($"An item with the same key has already been added. Key: {id}");
            }
        }
示例#5
0
        public async Task GivenANote_WhenUpdate_ThenTheNoteIsUpdated()
        {
            var id = await CreateNote();

            using (var dbContext = GetFreshDbContext())
            {
                var note = new Domain.Dto.Note(id, "newTitle", "newContent");
                var sut  = GetSut(dbContext);
                await sut.Update(note);
            }

            using (var dbContext = GetFreshDbContext())
            {
                var updatedNote = await dbContext.Notes.FirstAsync();

                updatedNote.ShouldBeEquivalentTo(new DbModel.Note {
                    Id = id, Title = "newTitle", Content = "newContent"
                });
            }
        }
示例#6
0
        public async Task GivenANote_WhenCreate_ThenANoteIsCreated()
        {
            var id = Guid.NewGuid();

            using (var dbContext = GetFreshDbContext())
            {
                var note = new Domain.Dto.Note(id, "title", "content");

                var sut = GetSut(dbContext);
                await sut.Create(note);
            }

            using (var dbContext = GetFreshDbContext())
            {
                var createdNote = await dbContext.Notes.FirstAsync();

                createdNote.ShouldBeEquivalentTo(new DbModel.Note {
                    Id = id, Title = "title", Content = "content"
                });
            }
        }
示例#7
0
 internal static Note From(Domain.Dto.Note note)
 {
     return(new Note {
         Id = note.Id, Title = note.Title, Content = note.Content
     });
 }