コード例 #1
0
ファイル: NoteDao.cs プロジェクト: romanzes/yoba
 public Task DeleteNote(YobaNote note, CancellationToken cancel = default)
 {
     using (var db = _factory.Create())
         return(db.Notes
                .Where(x => x.NoteName == note.Name)
                .DeleteAsync(cancel));
 }
コード例 #2
0
ファイル: NoteDao.cs プロジェクト: romanzes/yoba
        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);
            }
        }
コード例 #3
0
ファイル: NoteController.cs プロジェクト: stdray/yoba
 async Task Save(YobaNote note, CancellationToken cancel)
 {
     if (string.IsNullOrEmpty(note.Content))
     {
         await _dao.DeleteNote(note, cancel);
     }
     else
     {
         await _dao.AddOrUpdateNote(note, cancel);
     }
 }
コード例 #4
0
ファイル: NoteController.cs プロジェクト: stdray/yoba
 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)));
     });
 }
コード例 #5
0
ファイル: NoteController.cs プロジェクト: stdray/yoba
 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)));
     });
 }
コード例 #6
0
ファイル: NoteController.cs プロジェクト: stdray/yoba
    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));
    }
コード例 #7
0
ファイル: NoteControllerTests.cs プロジェクト: romanzes/yoba
        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();
            }
        }
コード例 #8
0
ファイル: NoteControllerTests.cs プロジェクト: romanzes/yoba
 public NoteControllerTests(NoteFixture fixture)
 {
     _controller = fixture.Scope.ServiceProvider.GetService <NoteController>();
     _dao        = fixture.Scope.ServiceProvider.GetService <INoteDao>();
     _note       = fixture.Note;
 }
コード例 #9
0
ファイル: NoteDaoTests.cs プロジェクト: romanzes/yoba
 public NoteDaoTests(NoteFixture fixture)
 {
     _dao  = fixture.Scope.ServiceProvider.GetService <INoteDao>();
     _note = fixture.Note;
 }