From a2e335e0a452ac9906eba91f74c7c166fe67904e Mon Sep 17 00:00:00 2001 From: Sergey Nazarov Date: Mon, 25 Mar 2024 19:20:33 +0300 Subject: [PATCH] Add Api.Contracts --- .../TextMatches/Dto/TextMatchData.cs | 20 +++++++++++++++++++ .../TextMatches/ITextMatchesController.cs | 4 ++++ .../CreateTextMatchRequest.cs | 0 .../WebRoutes.cs | 2 ++ .../Nocr.TextMatcher.AppServices.csproj | 1 + .../Repositories/ITextMatchRepository.cs | 2 ++ .../InMemoryTextMatchRepository.cs | 6 ++++++ .../TextMatches/Services/ITextMatchService.cs | 3 +++ .../TextMatches/Services/TextMatchService.cs | 16 +++++++++++++++ .../Controllers/TextMatchController.cs | 6 ++++++ 10 files changed, 60 insertions(+) create mode 100644 src/Nocr.TextMatcher.Api.Contracts/TextMatches/Dto/TextMatchData.cs rename src/Nocr.TextMatcher.Api.Contracts/TextMatches/{Dto => Requests}/CreateTextMatchRequest.cs (100%) diff --git a/src/Nocr.TextMatcher.Api.Contracts/TextMatches/Dto/TextMatchData.cs b/src/Nocr.TextMatcher.Api.Contracts/TextMatches/Dto/TextMatchData.cs new file mode 100644 index 0000000..903c318 --- /dev/null +++ b/src/Nocr.TextMatcher.Api.Contracts/TextMatches/Dto/TextMatchData.cs @@ -0,0 +1,20 @@ +using Nocr.TextMatcher.AppServices.Contracts.TextMatches; + +namespace Nocr.TextMatcher.Api.Contracts.TextMatches.Dto; + +public sealed class TextMatchData +{ + public long Id { get; set; } + + 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; } + + public DateTimeOffset CreatedDateTime { 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 index 3e75bfb..e973e83 100644 --- a/src/Nocr.TextMatcher.Api.Contracts/TextMatches/ITextMatchesController.cs +++ b/src/Nocr.TextMatcher.Api.Contracts/TextMatches/ITextMatchesController.cs @@ -6,5 +6,9 @@ namespace Nocr.TextMatcher.Api.Contracts.TextMatches; [BasePath(WebRoutes.TextMatches.Path)] public interface ITextMatchesController { + [Post] Task Create([Body] CreateTextMatchRequest request, CancellationToken cancellationToken = default); + + [Get(WebRoutes.TextMatches.ByUserId)] + Task GetByUserId([Path] long userId, CancellationToken cancellationToken = default); } \ 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/Requests/CreateTextMatchRequest.cs similarity index 100% rename from src/Nocr.TextMatcher.Api.Contracts/TextMatches/Dto/CreateTextMatchRequest.cs rename to src/Nocr.TextMatcher.Api.Contracts/TextMatches/Requests/CreateTextMatchRequest.cs diff --git a/src/Nocr.TextMatcher.Api.Contracts/WebRoutes.cs b/src/Nocr.TextMatcher.Api.Contracts/WebRoutes.cs index e44c47a..a6d9e88 100644 --- a/src/Nocr.TextMatcher.Api.Contracts/WebRoutes.cs +++ b/src/Nocr.TextMatcher.Api.Contracts/WebRoutes.cs @@ -7,5 +7,7 @@ public static class WebRoutes public static class TextMatches { public const string Path = BasePath + "/" + "text-matches"; + + public const string ByUserId = "by-user-id/{userId}"; } } \ 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 ce64e1d..e38b9e1 100644 --- a/src/Nocr.TextMatcher.AppServices/Nocr.TextMatcher.AppServices.csproj +++ b/src/Nocr.TextMatcher.AppServices/Nocr.TextMatcher.AppServices.csproj @@ -12,6 +12,7 @@ + diff --git a/src/Nocr.TextMatcher.AppServices/TextMatches/Repositories/ITextMatchRepository.cs b/src/Nocr.TextMatcher.AppServices/TextMatches/Repositories/ITextMatchRepository.cs index 459b3af..227262a 100644 --- a/src/Nocr.TextMatcher.AppServices/TextMatches/Repositories/ITextMatchRepository.cs +++ b/src/Nocr.TextMatcher.AppServices/TextMatches/Repositories/ITextMatchRepository.cs @@ -7,4 +7,6 @@ public interface ITextMatchRepository Task Delete(long id, CancellationToken cancellationToken = default); Task> Get(CancellationToken cancellationToken = default); + + Task> GetByUserId(long userId, CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/src/Nocr.TextMatcher.AppServices/TextMatches/Repositories/InMemoryTextMatchRepository.cs b/src/Nocr.TextMatcher.AppServices/TextMatches/Repositories/InMemoryTextMatchRepository.cs index f9f2779..da9be80 100644 --- a/src/Nocr.TextMatcher.AppServices/TextMatches/Repositories/InMemoryTextMatchRepository.cs +++ b/src/Nocr.TextMatcher.AppServices/TextMatches/Repositories/InMemoryTextMatchRepository.cs @@ -1,3 +1,4 @@ +using Nocr.TextMatcher.Api.Contracts.TextMatches.Dto; using Nocr.TextMatcher.AppServices.Contracts.TextMatches; namespace Nocr.TextMatcher.AppServices.TextMatches.Repositories; @@ -43,4 +44,9 @@ public sealed class InMemoryTextMatchRepository : ITextMatchRepository { return Task.FromResult>(_textMatches); } + + public Task> GetByUserId(long userId, CancellationToken cancellationToken = default) + { + return Task.FromResult>(_textMatches.Where(x => x.UserId == userId).ToArray()); + } } \ No newline at end of file diff --git a/src/Nocr.TextMatcher.AppServices/TextMatches/Services/ITextMatchService.cs b/src/Nocr.TextMatcher.AppServices/TextMatches/Services/ITextMatchService.cs index d39dbe9..de88b38 100644 --- a/src/Nocr.TextMatcher.AppServices/TextMatches/Services/ITextMatchService.cs +++ b/src/Nocr.TextMatcher.AppServices/TextMatches/Services/ITextMatchService.cs @@ -1,3 +1,4 @@ +using Nocr.TextMatcher.Api.Contracts.TextMatches.Dto; using Nocr.TextMatcher.AppServices.Contracts.TextMatches; namespace Nocr.TextMatcher.AppServices.TextMatches.Services; @@ -10,4 +11,6 @@ public interface ITextMatchService Task Delete(long id, CancellationToken cancellationToken = default); Task> Get(CancellationToken cancellationToken = default); + + Task> GetByUserId(long userId, CancellationToken cancellationToken); } \ No newline at end of file diff --git a/src/Nocr.TextMatcher.AppServices/TextMatches/Services/TextMatchService.cs b/src/Nocr.TextMatcher.AppServices/TextMatches/Services/TextMatchService.cs index 6cd8d7b..94da2f9 100644 --- a/src/Nocr.TextMatcher.AppServices/TextMatches/Services/TextMatchService.cs +++ b/src/Nocr.TextMatcher.AppServices/TextMatches/Services/TextMatchService.cs @@ -1,3 +1,4 @@ +using Nocr.TextMatcher.Api.Contracts.TextMatches.Dto; using Nocr.TextMatcher.AppServices.Contracts.TextMatches; using Nocr.TextMatcher.AppServices.TextMatches.Repositories; using Nocr.TextMatcher.Async.Api.Contracts; @@ -46,4 +47,19 @@ public sealed class TextMatchService : ITextMatchService { return _repository.Get(cancellationToken); } + + public async Task> GetByUserId(long userId, CancellationToken cancellationToken = default) + { + var matches = await _repository.GetByUserId(userId, cancellationToken); + return matches.Select(x => new TextMatchData + { + Id = x.Id, + ChatId = x.ChatId, + ChatUsername = x.ChatUsername, + Template = x.Template, + UserId = x.UserId, + Rule = x.Rule, + CreatedDateTime = x.CreatedDateTime + }).ToArray(); + } } \ 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 6c34654..8f5da47 100644 --- a/src/Nocr.TextMatcher.Host/Controllers/TextMatchController.cs +++ b/src/Nocr.TextMatcher.Host/Controllers/TextMatchController.cs @@ -22,4 +22,10 @@ public class TextMatchController : ControllerBase return _textMatchService.Create(request.UserId, request.ChatUsername, request.Template, request.Rule, request.ChatId, cancellationToken); } + + [HttpGet(WebRoutes.TextMatches.ByUserId)] + public Task> GetByUserId([FromRoute] long userId, CancellationToken cancellationToken = default) + { + return _textMatchService.GetByUserId(userId, cancellationToken); + } } \ No newline at end of file