From 38abbae0702d53ab5dcfcbf4e8cab74c712591cd Mon Sep 17 00:00:00 2001 From: Sergey Nazarov Date: Fri, 22 Mar 2024 00:38:37 +0400 Subject: [PATCH] Add ChatUsername --- Directory.Packages.props | 10 +++--- .../TextMatches/Dto/CreateTextMatchRequest.cs | 13 +++---- .../TextMatches/ITextMatchesController.cs | 10 ++++++ .../Nocr.TextMatcher.AppServices.csproj | 14 ++++---- .../TextMatchers/MessageReceivedHandler.cs | 2 +- .../TextMatches/Services/ITextMatchService.cs | 2 +- .../TextMatches/Services/TextMatchService.cs | 9 ++--- .../TextMatches/TextMatch.cs | 14 ++++++-- .../TextMatchCreated.cs | 2 ++ .../Controllers/TextMatchController.cs | 2 +- .../TextMatchTests.cs | 34 ++++++++++--------- 11 files changed, 65 insertions(+), 47 deletions(-) create mode 100644 src/Nocr.TextMatcher.Api.Contracts/TextMatches/ITextMatchesController.cs diff --git a/Directory.Packages.props b/Directory.Packages.props index e118f0e..0193467 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -9,7 +9,7 @@ - + @@ -26,9 +26,9 @@ - - - - + + + + \ No newline at end of file diff --git a/src/Nocr.TextMatcher.Api.Contracts/TextMatches/Dto/CreateTextMatchRequest.cs b/src/Nocr.TextMatcher.Api.Contracts/TextMatches/Dto/CreateTextMatchRequest.cs index d20ac87..bff2dab 100644 --- a/src/Nocr.TextMatcher.Api.Contracts/TextMatches/Dto/CreateTextMatchRequest.cs +++ b/src/Nocr.TextMatcher.Api.Contracts/TextMatches/Dto/CreateTextMatchRequest.cs @@ -1,21 +1,16 @@ using Nocr.TextMatcher.AppServices.Contracts.TextMatches; -using RestEase; namespace Nocr.TextMatcher.Api.Contracts.TextMatches.Dto; public class CreateTextMatchRequest { public long UserId { get; set; } - + public long ChatId { get; set; } + + public string ChatUsername { get; set; } public string Template { get; set; } - - public TextMatchRule Rule { get; set; } -} -[BasePath(WebRoutes.TextMatches.Path)] -public interface ITextMatcherController -{ - Task Create([Body] CreateTextMatchRequest request, CancellationToken cancellationToken = default); + public TextMatchRule Rule { get; set; } } \ No newline at end of file diff --git a/src/Nocr.TextMatcher.Api.Contracts/TextMatches/ITextMatchesController.cs b/src/Nocr.TextMatcher.Api.Contracts/TextMatches/ITextMatchesController.cs new file mode 100644 index 0000000..3e75bfb --- /dev/null +++ b/src/Nocr.TextMatcher.Api.Contracts/TextMatches/ITextMatchesController.cs @@ -0,0 +1,10 @@ +using Nocr.TextMatcher.Api.Contracts.TextMatches.Dto; +using RestEase; + +namespace Nocr.TextMatcher.Api.Contracts.TextMatches; + +[BasePath(WebRoutes.TextMatches.Path)] +public interface ITextMatchesController +{ + Task Create([Body] CreateTextMatchRequest request, CancellationToken cancellationToken = default); +} \ No newline at end of file diff --git a/src/Nocr.TextMatcher.AppServices/Nocr.TextMatcher.AppServices.csproj b/src/Nocr.TextMatcher.AppServices/Nocr.TextMatcher.AppServices.csproj index 0885807..ce64e1d 100644 --- a/src/Nocr.TextMatcher.AppServices/Nocr.TextMatcher.AppServices.csproj +++ b/src/Nocr.TextMatcher.AppServices/Nocr.TextMatcher.AppServices.csproj @@ -5,16 +5,16 @@ - - - - + + + + - - - + + + diff --git a/src/Nocr.TextMatcher.AppServices/TextMatchers/MessageReceivedHandler.cs b/src/Nocr.TextMatcher.AppServices/TextMatchers/MessageReceivedHandler.cs index 6ef020c..a2f39ec 100644 --- a/src/Nocr.TextMatcher.AppServices/TextMatchers/MessageReceivedHandler.cs +++ b/src/Nocr.TextMatcher.AppServices/TextMatchers/MessageReceivedHandler.cs @@ -34,7 +34,7 @@ public sealed class MessageReceivedHandler : IHandleMessages foreach (var match in matches) { - if (match.IsMatches(message.ChatId, message.Text)) + if (match.IsMatches(message.ChatId, message.ChatUserName, message.Text)) { _logger.LogInformation("Message {@Message} matched {@Match}", message, match); var @event = new TextMatchMatched diff --git a/src/Nocr.TextMatcher.AppServices/TextMatches/Services/ITextMatchService.cs b/src/Nocr.TextMatcher.AppServices/TextMatches/Services/ITextMatchService.cs index a6ed2a1..0d2bbd8 100644 --- a/src/Nocr.TextMatcher.AppServices/TextMatches/Services/ITextMatchService.cs +++ b/src/Nocr.TextMatcher.AppServices/TextMatches/Services/ITextMatchService.cs @@ -4,7 +4,7 @@ namespace Nocr.TextMatcher.AppServices.TextMatches.Services; public interface ITextMatchService { - Task Create(long userId, long chatId, string template, TextMatchRule rule, CancellationToken cancellationToken = default); + Task Create(long userId, long chatId, string chatUsername, string template, TextMatchRule rule, CancellationToken cancellationToken = default); Task Delete(long id, CancellationToken cancellationToken = default); diff --git a/src/Nocr.TextMatcher.AppServices/TextMatches/Services/TextMatchService.cs b/src/Nocr.TextMatcher.AppServices/TextMatches/Services/TextMatchService.cs index a25aea9..856f793 100644 --- a/src/Nocr.TextMatcher.AppServices/TextMatches/Services/TextMatchService.cs +++ b/src/Nocr.TextMatcher.AppServices/TextMatches/Services/TextMatchService.cs @@ -20,19 +20,20 @@ public sealed class TextMatchService : ITextMatchService _dateProvider = dateProvider ?? throw new ArgumentNullException(nameof(dateProvider)); } - public async Task Create(long userId, long chatId, string template, TextMatchRule rule, + public async Task Create(long userId, long chatId, string chatUsername, string template, TextMatchRule rule, CancellationToken cancellationToken = default) { - var textMatch = TextMatch.Initialize(userId, chatId, template, rule, _dateProvider.UtcNow); + var textMatch = TextMatch.Initialize(userId, chatId, chatUsername, template, rule, _dateProvider.UtcNow); await _repository.Create(textMatch, cancellationToken); var @event = new TextMatchCreated { - ChatId = chatId + ChatId = chatId, + ChatUsername = textMatch.ChatUsername }; await _bus.Advanced.Topics.Publish("nocr.text.matcher", @event); - + return textMatch.Id; } diff --git a/src/Nocr.TextMatcher.AppServices/TextMatches/TextMatch.cs b/src/Nocr.TextMatcher.AppServices/TextMatches/TextMatch.cs index 7744169..06d82b9 100644 --- a/src/Nocr.TextMatcher.AppServices/TextMatches/TextMatch.cs +++ b/src/Nocr.TextMatcher.AppServices/TextMatches/TextMatch.cs @@ -11,6 +11,8 @@ public sealed class TextMatch public long ChatId { get; private set; } + public string ChatUsername { get; private set; } + public string Template { get; private set; } public TextMatchRule Rule { get; private set; } @@ -19,12 +21,14 @@ public sealed class TextMatch private TextMatch(long userId, long chatId, + string chatUsername, string template, TextMatchRule rule, DateTimeOffset createdDateTime) { UserId = userId; ChatId = chatId; + ChatUsername = chatUsername; Template = template; Rule = rule; CreatedDateTime = createdDateTime; @@ -32,6 +36,7 @@ public sealed class TextMatch public static TextMatch Initialize(long userId, long chatId, + string chatUsername, string template, TextMatchRule rule, DateTimeOffset createdDateTime) @@ -45,12 +50,15 @@ public sealed class TextMatch if (string.IsNullOrWhiteSpace(template)) throw new ArgumentException("Template should not be empty", nameof(template)); - return new TextMatch(userId, chatId, template, rule, createdDateTime); + if (string.IsNullOrWhiteSpace(chatUsername)) + throw new ArgumentException("Chat username should not be empty", nameof(chatUsername)); + + return new TextMatch(userId, chatId, chatUsername, template, rule, createdDateTime); } - public bool IsMatches(long chatId, string text) + public bool IsMatches(long chatId, string chatUsername, string text) { - if (ChatId != chatId) + if (ChatId != chatId && !string.Equals(ChatUsername, chatUsername, StringComparison.OrdinalIgnoreCase)) return false; switch (Rule) diff --git a/src/Nocr.TextMatcher.Async.Api.Contracts/TextMatchCreated.cs b/src/Nocr.TextMatcher.Async.Api.Contracts/TextMatchCreated.cs index 6544e5b..42c8f38 100644 --- a/src/Nocr.TextMatcher.Async.Api.Contracts/TextMatchCreated.cs +++ b/src/Nocr.TextMatcher.Async.Api.Contracts/TextMatchCreated.cs @@ -5,4 +5,6 @@ public class TextMatchCreated : IEvent public Guid Id => Guid.NewGuid(); public long ChatId { get; set; } + + public string ChatUsername { get; set; } = null!; } \ No newline at end of file diff --git a/src/Nocr.TextMatcher.Host/Controllers/TextMatchController.cs b/src/Nocr.TextMatcher.Host/Controllers/TextMatchController.cs index 509388c..ad2d05e 100644 --- a/src/Nocr.TextMatcher.Host/Controllers/TextMatchController.cs +++ b/src/Nocr.TextMatcher.Host/Controllers/TextMatchController.cs @@ -21,7 +21,7 @@ public class TextMatchController : ControllerBase [HttpPost] public Task Create([FromBody] CreateTextMatchRequest request, CancellationToken cancellationToken = default) { - return _textMatchService.Create(request.UserId, request.ChatId, request.Template, request.Rule, + return _textMatchService.Create(request.UserId, request.ChatId, request.ChatUsername, request.Template, request.Rule, cancellationToken); } } diff --git a/tests/Nocr.TextMatcher.AppServices.UnitTests/TextMatchTests.cs b/tests/Nocr.TextMatcher.AppServices.UnitTests/TextMatchTests.cs index 2a2e3f7..94fd7bf 100644 --- a/tests/Nocr.TextMatcher.AppServices.UnitTests/TextMatchTests.cs +++ b/tests/Nocr.TextMatcher.AppServices.UnitTests/TextMatchTests.cs @@ -15,52 +15,54 @@ public class TextMatchTests public void IsMatches_SameChatId_FullRule_MatchesText() { // Arrange - var match = TextMatch.Initialize(UserId, ChatId, "велосипед", TextMatchRule.Full, CreatedDateTime); + var match = TextMatch.Initialize(UserId, ChatId, "Барахолка", "велосипед", TextMatchRule.Full, CreatedDateTime); var text = "Продам снежный велосипед 100 лари. Гудаури."; - + // Act - var result = match.IsMatches(ChatId, text); + var result = match.IsMatches(ChatId, "Барахолка", text); // Assert Assert.True(result); } - + [Fact] public void IsMatches_SameChatId_AnyWord_MatchesText() { // Arrange - var match = TextMatch.Initialize(UserId, ChatId, "iphone айфон", TextMatchRule.AnyWord, CreatedDateTime); + var match = TextMatch.Initialize(UserId, ChatId, "Барахолка", "iphone айфон", TextMatchRule.AnyWord, + CreatedDateTime); var text = "Продам айфон велосипед 100 лари. Гудаури."; - + // Act - var result = match.IsMatches(ChatId, text); + var result = match.IsMatches(ChatId, "Барахолка", text); // Assert Assert.True(result); } - + [Fact] public void IsMatches_SameChatId_AllWords_MatchesText() { // Arrange - var match = TextMatch.Initialize(UserId, ChatId, "iphone айфон", TextMatchRule.AnyWord, CreatedDateTime); + var match = TextMatch.Initialize(UserId, ChatId, "Барахолка", "iphone айфон", TextMatchRule.AnyWord, + CreatedDateTime); var text = "Гомарджоба. Продам iphone (айфон) 1000 лари. Гудаури."; - + // Act - var result = match.IsMatches(ChatId, text); + var result = match.IsMatches(ChatId, "Барахолка", text); // Assert Assert.True(result); } - + [Fact] - public void IsMatches_DifferentChatId_NotMatchesText() + public void IsMatches_DifferentChatIdAndUserName_NotMatchesText() { // Arrange - var match = TextMatch.Initialize(UserId, ChatId, "iphone", TextMatchRule.Full, CreatedDateTime); - + var match = TextMatch.Initialize(UserId, ChatId, "Барахолка", "iphone", TextMatchRule.Full, CreatedDateTime); + // Act - var result = match.IsMatches(ChatId, string.Empty); + var result = match.IsMatches(0, string.Empty, "iphone"); // Assert Assert.False(result);