Make ChatId in TextMatch optional

This commit is contained in:
Sergey Nazarov 2024-03-22 10:00:41 +04:00
parent 38abbae070
commit 8560bbbf31
8 changed files with 23 additions and 29 deletions

View File

@ -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; }

View File

@ -1,5 +1,3 @@
using Nocr.TextMatcher.AppServices.TextMatches.Services;
namespace Nocr.TextMatcher.AppServices.TextMatches.Repositories;
public interface ITextMatchRepository

View File

@ -4,7 +4,8 @@ namespace Nocr.TextMatcher.AppServices.TextMatches.Services;
public interface ITextMatchService
{
Task<long> Create(long userId, long chatId, string chatUsername, string template, TextMatchRule rule, CancellationToken cancellationToken = default);
Task<long> Create(long userId, string chatUsername, string template, TextMatchRule rule, long? chatId = null,
CancellationToken cancellationToken = default);
Task Delete(long id, CancellationToken cancellationToken = default);

View File

@ -20,15 +20,14 @@ public sealed class TextMatchService : ITextMatchService
_dateProvider = dateProvider ?? throw new ArgumentNullException(nameof(dateProvider));
}
public async Task<long> Create(long userId, long chatId, string chatUsername, string template, TextMatchRule rule,
public async Task<long> 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
};

View File

@ -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)

View File

@ -4,7 +4,5 @@ public class TextMatchCreated : IEvent
{
public Guid Id => Guid.NewGuid();
public long ChatId { get; set; }
public string ChatUsername { get; set; } = null!;
}

View File

@ -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<long> 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);
}
}
}

View File

@ -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");