public Task DeleteNote(YobaNote note, CancellationToken cancel = default) { using (var db = _factory.Create()) return(db.Notes .Where(x => x.NoteName == note.Name) .DeleteAsync(cancel)); }
public async Task AddOrUpdateNote(YobaNote note, CancellationToken cancel = default) { var now = DateTime.Now; using (var db = _factory.Create()) { var count = await db.Notes .Where(x => x.NoteName == note.Name) .Set(x => x.Content, _ => note.Content) .Set(x => x.Updated, _ => now) .UpdateAsync(cancel); if (count == 1) { return; } if (count > 1) { throw new InvalidOperationException("Conflict"); } await db.Notes.InsertAsync(() => new Note { NoteName = note.Name, DisplayNoteName = note.DisplayName, Content = note.Content, Added = now, Updated = now, }, cancel); } }
async Task Save(YobaNote note, CancellationToken cancel) { if (string.IsNullOrEmpty(note.Content)) { await _dao.DeleteNote(note, cancel); } else { await _dao.AddOrUpdateNote(note, cancel); } }
void ShowNote() { this.AddReRule( bot + s + "покажи" + s + "заметку" + phrase("name"), async(request, match, cancel) => { var name = YobaNote.MakePkName(match.Value("name")); var note = await _dao.FindNote(name, cancel); if (note == null) { return(Ok(await _telegram.ReplyAsync(request, "Заметка не найдена", cancel))); } var lines = Lines(note.Content).Select((x, i) => $"{i + 1}. {x}"); var text = string.Join(Environment.NewLine, lines); return(Ok(await _telegram.ReplyAsync(request, text, cancel))); }); }
void DelNoteLine() { this.AddReRule( bot + s + "удали" + (s + "строку").opt + s + digit.oneOrMore.group("line") + s + "из" + (s + "заметки").opt + phrase("name"), async(request, match, cancel) => { var line = int.Parse(match.Value("line")); var name = YobaNote.MakePkName(match.Value("name")); var note = await _dao.FindNote(name, cancel); if (note == null) { return(Ok(await _telegram.ReplyAsync(request, "Заметка не найдена", cancel))); } var lines = Lines(note.Content) .Select((x, i) => new { Line = i + 1, Data = x }) .Where(x => x.Line != line) .Select(x => x.Data); note.Content = string.Join(Environment.NewLine, lines); await Save(note, cancel); return(Ok(await _telegram.ReplyAsync(request, "Ок", cancel))); }); }
void AddOrUpdateNote() { MatchHandle <Message> MakeHandle(bool update) => async(request, match, cancel) => { var data = match.Value("data"); var displayName = match.Value("name").Trim(); var name = YobaNote.MakePkName(displayName); var note = await _dao.FindNote(name, cancel); if (note == null) { note = new YobaNote { Content = data, Created = DateTime.Now, DisplayName = displayName }; } else { var content = update ? data : note.Content + Environment.NewLine + data; note.Content = string.Join(Environment.NewLine, Lines(content)); } await Save(note, cancel); return(Ok(await _telegram.ReplyAsync(request, "Ок", cancel))); }; this.AddReRule( bot + (s + "добавь").opt + (s + "в").opt + s + "заметку" + phrase("name") + ":" + phrase("data"), MakeHandle(false)); this.AddReRule( bot + s + "обнови" + s + "заметку" + phrase("name") + ":" + phrase("data"), MakeHandle(true)); }
public async Task Test() { //AddOrUpdateNote { //Create new note from line await Handle($"ёба добавь в заметку {_note.DisplayName} : {_note.Content}"); var note = await _dao.FindNote(_note.Name); note.Content.Should().Be(_note.Content); //Append line to note var line = Guid.NewGuid().ToString(); await Handle($"ёба добавь в заметку {_note.DisplayName} : {line}"); note = await _dao.FindNote(_note.Name); _note.Content.Should().ContainAll(_note.Content.Split('\r', '\n')); note.Content.Should().Contain(line); //Reset note to original content await Handle($"ёба обнови заметку {_note.DisplayName} : {_note.Content}"); note = await _dao.FindNote(_note.Name); note.Content.Should().Be(_note.Content); } //ShowNote { var result = await Handle($"ёба покажи заметку {_note.DisplayName}"); result.Response.Text.Should().ContainAll(_note.Content.Split('\r', '\n')); } var note2 = new YobaNote { Content = "aaaaa" + Environment.NewLine + "bbb", DisplayName = "baz", Created = DateTime.Now, Updated = DateTime.Now }; await _dao.AddOrUpdateNote(note2); //ListNotes { async Task checkList(string msg) { var result = await Handle(msg); result.Response.Text.Should().Contain(note2.DisplayName); } await checkList("ёба покажи список заметок"); await checkList("ёба покажи заметки"); } //DelNoteLine { await Handle($"ёба удали 1 из {note2.DisplayName}"); var tmp = await _dao.FindNote(note2.Name); tmp.Content.Should().Contain("bbb"); tmp.Content.Should().NotContain("aaaaa"); //Empty note should be deleted await Handle($"ёба удали 1 из {note2.DisplayName}"); tmp = await _dao.FindNote(note2.Name); tmp.Should().BeNull(); } }
public NoteControllerTests(NoteFixture fixture) { _controller = fixture.Scope.ServiceProvider.GetService <NoteController>(); _dao = fixture.Scope.ServiceProvider.GetService <INoteDao>(); _note = fixture.Note; }
public NoteDaoTests(NoteFixture fixture) { _dao = fixture.Scope.ServiceProvider.GetService <INoteDao>(); _note = fixture.Note; }