Make ChatId in TextMatch optional
This commit is contained in:
parent
38abbae070
commit
8560bbbf31
@ -6,7 +6,7 @@ public class CreateTextMatchRequest
|
|||||||
{
|
{
|
||||||
public long UserId { get; set; }
|
public long UserId { get; set; }
|
||||||
|
|
||||||
public long ChatId { get; set; }
|
public long? ChatId { get; set; }
|
||||||
|
|
||||||
public string ChatUsername { get; set; }
|
public string ChatUsername { get; set; }
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,3 @@
|
|||||||
using Nocr.TextMatcher.AppServices.TextMatches.Services;
|
|
||||||
|
|
||||||
namespace Nocr.TextMatcher.AppServices.TextMatches.Repositories;
|
namespace Nocr.TextMatcher.AppServices.TextMatches.Repositories;
|
||||||
|
|
||||||
public interface ITextMatchRepository
|
public interface ITextMatchRepository
|
||||||
|
|||||||
@ -4,7 +4,8 @@ namespace Nocr.TextMatcher.AppServices.TextMatches.Services;
|
|||||||
|
|
||||||
public interface ITextMatchService
|
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);
|
Task Delete(long id, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
|||||||
@ -20,15 +20,14 @@ public sealed class TextMatchService : ITextMatchService
|
|||||||
_dateProvider = dateProvider ?? throw new ArgumentNullException(nameof(dateProvider));
|
_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)
|
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);
|
await _repository.Create(textMatch, cancellationToken);
|
||||||
|
|
||||||
var @event = new TextMatchCreated
|
var @event = new TextMatchCreated
|
||||||
{
|
{
|
||||||
ChatId = chatId,
|
|
||||||
ChatUsername = textMatch.ChatUsername
|
ChatUsername = textMatch.ChatUsername
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -9,7 +9,7 @@ public sealed class TextMatch
|
|||||||
|
|
||||||
public long UserId { get; private set; }
|
public long UserId { get; private set; }
|
||||||
|
|
||||||
public long ChatId { get; private set; }
|
public long? ChatId { get; private set; }
|
||||||
|
|
||||||
public string ChatUsername { get; private set; }
|
public string ChatUsername { get; private set; }
|
||||||
|
|
||||||
@ -20,31 +20,31 @@ public sealed class TextMatch
|
|||||||
public DateTimeOffset CreatedDateTime { get; private set; }
|
public DateTimeOffset CreatedDateTime { get; private set; }
|
||||||
|
|
||||||
private TextMatch(long userId,
|
private TextMatch(long userId,
|
||||||
long chatId,
|
|
||||||
string chatUsername,
|
string chatUsername,
|
||||||
string template,
|
string template,
|
||||||
TextMatchRule rule,
|
TextMatchRule rule,
|
||||||
DateTimeOffset createdDateTime)
|
DateTimeOffset createdDateTime,
|
||||||
|
long? chatId)
|
||||||
{
|
{
|
||||||
UserId = userId;
|
UserId = userId;
|
||||||
ChatId = chatId;
|
|
||||||
ChatUsername = chatUsername;
|
ChatUsername = chatUsername;
|
||||||
Template = template;
|
Template = template;
|
||||||
Rule = rule;
|
Rule = rule;
|
||||||
CreatedDateTime = createdDateTime;
|
CreatedDateTime = createdDateTime;
|
||||||
|
ChatId = chatId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TextMatch Initialize(long userId,
|
public static TextMatch Initialize(long userId,
|
||||||
long chatId,
|
|
||||||
string chatUsername,
|
string chatUsername,
|
||||||
string template,
|
string template,
|
||||||
TextMatchRule rule,
|
TextMatchRule rule,
|
||||||
DateTimeOffset createdDateTime)
|
DateTimeOffset createdDateTime,
|
||||||
|
long? chatId = null)
|
||||||
{
|
{
|
||||||
if (userId <= 0)
|
if (userId <= 0)
|
||||||
throw new ArgumentException("User id should be greater tha 0", nameof(userId));
|
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));
|
throw new ArgumentException("Chat id should be greater tha 0", nameof(chatId));
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(template))
|
if (string.IsNullOrWhiteSpace(template))
|
||||||
@ -53,12 +53,12 @@ public sealed class TextMatch
|
|||||||
if (string.IsNullOrWhiteSpace(chatUsername))
|
if (string.IsNullOrWhiteSpace(chatUsername))
|
||||||
throw new ArgumentException("Chat username should not be empty", nameof(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)
|
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;
|
return false;
|
||||||
|
|
||||||
switch (Rule)
|
switch (Rule)
|
||||||
|
|||||||
@ -4,7 +4,5 @@ public class TextMatchCreated : IEvent
|
|||||||
{
|
{
|
||||||
public Guid Id => Guid.NewGuid();
|
public Guid Id => Guid.NewGuid();
|
||||||
|
|
||||||
public long ChatId { get; set; }
|
|
||||||
|
|
||||||
public string ChatUsername { get; set; } = null!;
|
public string ChatUsername { get; set; } = null!;
|
||||||
}
|
}
|
||||||
@ -1,8 +1,6 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Nocr.TextMatcher.Api.Contracts;
|
using Nocr.TextMatcher.Api.Contracts;
|
||||||
using Nocr.TextMatcher.Api.Contracts.TextMatches;
|
|
||||||
using Nocr.TextMatcher.Api.Contracts.TextMatches.Dto;
|
using Nocr.TextMatcher.Api.Contracts.TextMatches.Dto;
|
||||||
using Nocr.TextMatcher.AppServices.TextMatches;
|
|
||||||
using Nocr.TextMatcher.AppServices.TextMatches.Services;
|
using Nocr.TextMatcher.AppServices.TextMatches.Services;
|
||||||
|
|
||||||
namespace Nocr.TextMatcher.Host.Controllers;
|
namespace Nocr.TextMatcher.Host.Controllers;
|
||||||
@ -21,7 +19,7 @@ public class TextMatchController : ControllerBase
|
|||||||
[HttpPost]
|
[HttpPost]
|
||||||
public Task<long> Create([FromBody] CreateTextMatchRequest request, CancellationToken cancellationToken = default)
|
public Task<long> Create([FromBody] CreateTextMatchRequest request, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
return _textMatchService.Create(request.UserId, request.ChatId, request.ChatUsername, request.Template, request.Rule,
|
return _textMatchService.Create(request.UserId, request.ChatUsername, request.Template, request.Rule,
|
||||||
cancellationToken);
|
request.ChatId, cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -15,7 +15,7 @@ public class TextMatchTests
|
|||||||
public void IsMatches_SameChatId_FullRule_MatchesText()
|
public void IsMatches_SameChatId_FullRule_MatchesText()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var match = TextMatch.Initialize(UserId, ChatId, "Барахолка", "велосипед", TextMatchRule.Full, CreatedDateTime);
|
var match = TextMatch.Initialize(UserId, "Барахолка", "велосипед", TextMatchRule.Full, CreatedDateTime, ChatId);
|
||||||
var text = "Продам снежный велосипед 100 лари. Гудаури.";
|
var text = "Продам снежный велосипед 100 лари. Гудаури.";
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
@ -29,8 +29,8 @@ public class TextMatchTests
|
|||||||
public void IsMatches_SameChatId_AnyWord_MatchesText()
|
public void IsMatches_SameChatId_AnyWord_MatchesText()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var match = TextMatch.Initialize(UserId, ChatId, "Барахолка", "iphone айфон", TextMatchRule.AnyWord,
|
var match = TextMatch.Initialize(UserId, "Барахолка", "iphone айфон", TextMatchRule.AnyWord,
|
||||||
CreatedDateTime);
|
CreatedDateTime, ChatId);
|
||||||
var text = "Продам айфон велосипед 100 лари. Гудаури.";
|
var text = "Продам айфон велосипед 100 лари. Гудаури.";
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
@ -44,8 +44,8 @@ public class TextMatchTests
|
|||||||
public void IsMatches_SameChatId_AllWords_MatchesText()
|
public void IsMatches_SameChatId_AllWords_MatchesText()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var match = TextMatch.Initialize(UserId, ChatId, "Барахолка", "iphone айфон", TextMatchRule.AnyWord,
|
var match = TextMatch.Initialize(UserId, "Барахолка", "iphone айфон", TextMatchRule.AnyWord,
|
||||||
CreatedDateTime);
|
CreatedDateTime, ChatId);
|
||||||
var text = "Гомарджоба. Продам iphone (айфон) 1000 лари. Гудаури.";
|
var text = "Гомарджоба. Продам iphone (айфон) 1000 лари. Гудаури.";
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
@ -59,7 +59,7 @@ public class TextMatchTests
|
|||||||
public void IsMatches_DifferentChatIdAndUserName_NotMatchesText()
|
public void IsMatches_DifferentChatIdAndUserName_NotMatchesText()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var match = TextMatch.Initialize(UserId, ChatId, "Барахолка", "iphone", TextMatchRule.Full, CreatedDateTime);
|
var match = TextMatch.Initialize(UserId, "Барахолка", "iphone", TextMatchRule.Full, CreatedDateTime, ChatId);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = match.IsMatches(0, string.Empty, "iphone");
|
var result = match.IsMatches(0, string.Empty, "iphone");
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user