diff --git a/src/Nocr.TextMatcher.Api.Contracts/TextMatches/Dto/CreateTextMatchRequest.cs b/src/Nocr.TextMatcher.Api.Contracts/TextMatches/Dto/CreateTextMatchRequest.cs index bff2dab..ae08618 100644 --- a/src/Nocr.TextMatcher.Api.Contracts/TextMatches/Dto/CreateTextMatchRequest.cs +++ b/src/Nocr.TextMatcher.Api.Contracts/TextMatches/Dto/CreateTextMatchRequest.cs @@ -6,7 +6,7 @@ public class CreateTextMatchRequest { public long UserId { get; set; } - public long ChatId { get; set; } + public long? ChatId { get; set; } public string ChatUsername { get; set; } diff --git a/src/Nocr.TextMatcher.AppServices/TextMatches/Repositories/ITextMatchRepository.cs b/src/Nocr.TextMatcher.AppServices/TextMatches/Repositories/ITextMatchRepository.cs index 33c9a4f..459b3af 100644 --- a/src/Nocr.TextMatcher.AppServices/TextMatches/Repositories/ITextMatchRepository.cs +++ b/src/Nocr.TextMatcher.AppServices/TextMatches/Repositories/ITextMatchRepository.cs @@ -1,5 +1,3 @@ -using Nocr.TextMatcher.AppServices.TextMatches.Services; - namespace Nocr.TextMatcher.AppServices.TextMatches.Repositories; public interface ITextMatchRepository diff --git a/src/Nocr.TextMatcher.AppServices/TextMatches/Services/ITextMatchService.cs b/src/Nocr.TextMatcher.AppServices/TextMatches/Services/ITextMatchService.cs index 0d2bbd8..d39dbe9 100644 --- a/src/Nocr.TextMatcher.AppServices/TextMatches/Services/ITextMatchService.cs +++ b/src/Nocr.TextMatcher.AppServices/TextMatches/Services/ITextMatchService.cs @@ -4,7 +4,8 @@ namespace Nocr.TextMatcher.AppServices.TextMatches.Services; public interface ITextMatchService { - Task Create(long userId, long chatId, string chatUsername, string template, TextMatchRule rule, CancellationToken cancellationToken = default); + Task Create(long userId, string chatUsername, string template, TextMatchRule rule, long? chatId = null, + 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 856f793..63a116d 100644 --- a/src/Nocr.TextMatcher.AppServices/TextMatches/Services/TextMatchService.cs +++ b/src/Nocr.TextMatcher.AppServices/TextMatches/Services/TextMatchService.cs @@ -20,15 +20,14 @@ public sealed class TextMatchService : ITextMatchService _dateProvider = dateProvider ?? throw new ArgumentNullException(nameof(dateProvider)); } - public async Task Create(long userId, long chatId, string chatUsername, string template, TextMatchRule rule, + public async Task Create(long userId, string chatUsername, string template, TextMatchRule rule, long? chatId = null, CancellationToken cancellationToken = default) { - var textMatch = TextMatch.Initialize(userId, chatId, chatUsername, template, rule, _dateProvider.UtcNow); + var textMatch = TextMatch.Initialize(userId, chatUsername, template, rule, _dateProvider.UtcNow, chatId); await _repository.Create(textMatch, cancellationToken); var @event = new TextMatchCreated { - ChatId = chatId, ChatUsername = textMatch.ChatUsername }; diff --git a/src/Nocr.TextMatcher.AppServices/TextMatches/TextMatch.cs b/src/Nocr.TextMatcher.AppServices/TextMatches/TextMatch.cs index 06d82b9..9a38a9f 100644 --- a/src/Nocr.TextMatcher.AppServices/TextMatches/TextMatch.cs +++ b/src/Nocr.TextMatcher.AppServices/TextMatches/TextMatch.cs @@ -9,7 +9,7 @@ public sealed class TextMatch public long UserId { get; private set; } - public long ChatId { get; private set; } + public long? ChatId { get; private set; } public string ChatUsername { get; private set; } @@ -20,31 +20,31 @@ public sealed class TextMatch public DateTimeOffset CreatedDateTime { get; private set; } private TextMatch(long userId, - long chatId, string chatUsername, string template, TextMatchRule rule, - DateTimeOffset createdDateTime) + DateTimeOffset createdDateTime, + long? chatId) { UserId = userId; - ChatId = chatId; ChatUsername = chatUsername; Template = template; Rule = rule; CreatedDateTime = createdDateTime; + ChatId = chatId; } public static TextMatch Initialize(long userId, - long chatId, string chatUsername, string template, TextMatchRule rule, - DateTimeOffset createdDateTime) + DateTimeOffset createdDateTime, + long? chatId = null) { if (userId <= 0) throw new ArgumentException("User id should be greater tha 0", nameof(userId)); - if (chatId <= 0) + if (chatId is <= 0) throw new ArgumentException("Chat id should be greater tha 0", nameof(chatId)); if (string.IsNullOrWhiteSpace(template)) @@ -53,12 +53,12 @@ public sealed class TextMatch if (string.IsNullOrWhiteSpace(chatUsername)) throw new ArgumentException("Chat username should not be empty", nameof(chatUsername)); - return new TextMatch(userId, chatId, chatUsername, template, rule, createdDateTime); + return new TextMatch(userId, chatUsername, template, rule, createdDateTime,chatId); } public bool IsMatches(long chatId, string chatUsername, string text) { - if (ChatId != chatId && !string.Equals(ChatUsername, chatUsername, StringComparison.OrdinalIgnoreCase)) + if (ChatId.HasValue && 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 42c8f38..060d3da 100644 --- a/src/Nocr.TextMatcher.Async.Api.Contracts/TextMatchCreated.cs +++ b/src/Nocr.TextMatcher.Async.Api.Contracts/TextMatchCreated.cs @@ -4,7 +4,5 @@ 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 ad2d05e..6c34654 100644 --- a/src/Nocr.TextMatcher.Host/Controllers/TextMatchController.cs +++ b/src/Nocr.TextMatcher.Host/Controllers/TextMatchController.cs @@ -1,8 +1,6 @@ using Microsoft.AspNetCore.Mvc; using Nocr.TextMatcher.Api.Contracts; -using Nocr.TextMatcher.Api.Contracts.TextMatches; using Nocr.TextMatcher.Api.Contracts.TextMatches.Dto; -using Nocr.TextMatcher.AppServices.TextMatches; using Nocr.TextMatcher.AppServices.TextMatches.Services; namespace Nocr.TextMatcher.Host.Controllers; @@ -21,7 +19,7 @@ public class TextMatchController : ControllerBase [HttpPost] public Task Create([FromBody] CreateTextMatchRequest request, CancellationToken cancellationToken = default) { - return _textMatchService.Create(request.UserId, request.ChatId, request.ChatUsername, request.Template, request.Rule, - cancellationToken); + return _textMatchService.Create(request.UserId, request.ChatUsername, request.Template, request.Rule, + request.ChatId, cancellationToken); } -} +} \ No newline at end of file diff --git a/tests/Nocr.TextMatcher.AppServices.UnitTests/TextMatchTests.cs b/tests/Nocr.TextMatcher.AppServices.UnitTests/TextMatchTests.cs index 94fd7bf..c0a2a3a 100644 --- a/tests/Nocr.TextMatcher.AppServices.UnitTests/TextMatchTests.cs +++ b/tests/Nocr.TextMatcher.AppServices.UnitTests/TextMatchTests.cs @@ -15,7 +15,7 @@ public class TextMatchTests public void IsMatches_SameChatId_FullRule_MatchesText() { // Arrange - var match = TextMatch.Initialize(UserId, ChatId, "Барахолка", "велосипед", TextMatchRule.Full, CreatedDateTime); + var match = TextMatch.Initialize(UserId, "Барахолка", "велосипед", TextMatchRule.Full, CreatedDateTime, ChatId); var text = "Продам снежный велосипед 100 лари. Гудаури."; // Act @@ -29,8 +29,8 @@ public class TextMatchTests public void IsMatches_SameChatId_AnyWord_MatchesText() { // Arrange - var match = TextMatch.Initialize(UserId, ChatId, "Барахолка", "iphone айфон", TextMatchRule.AnyWord, - CreatedDateTime); + var match = TextMatch.Initialize(UserId, "Барахолка", "iphone айфон", TextMatchRule.AnyWord, + CreatedDateTime, ChatId); var text = "Продам айфон велосипед 100 лари. Гудаури."; // Act @@ -44,8 +44,8 @@ public class TextMatchTests public void IsMatches_SameChatId_AllWords_MatchesText() { // Arrange - var match = TextMatch.Initialize(UserId, ChatId, "Барахолка", "iphone айфон", TextMatchRule.AnyWord, - CreatedDateTime); + var match = TextMatch.Initialize(UserId, "Барахолка", "iphone айфон", TextMatchRule.AnyWord, + CreatedDateTime, ChatId); var text = "Гомарджоба. Продам iphone (айфон) 1000 лари. Гудаури."; // Act @@ -59,7 +59,7 @@ public class TextMatchTests public void IsMatches_DifferentChatIdAndUserName_NotMatchesText() { // Arrange - var match = TextMatch.Initialize(UserId, ChatId, "Барахолка", "iphone", TextMatchRule.Full, CreatedDateTime); + var match = TextMatch.Initialize(UserId, "Барахолка", "iphone", TextMatchRule.Full, CreatedDateTime, ChatId); // Act var result = match.IsMatches(0, string.Empty, "iphone");