public Task Create(Domain.Dto.Note note) { var toAdd = DbModel.Note.From(note); _dbContext.Notes.Add(toAdd); return(_dbContext.SaveChangesAsync()); }
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(); }
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."); } }
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}"); } }
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" }); } }
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" }); } }
internal static Note From(Domain.Dto.Note note) { return(new Note { Id = note.Id, Title = note.Title, Content = note.Content }); }