From 83203f967d63f7991a0553950ca5b1ded77e9cd6 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sun, 23 Jul 2023 22:50:03 -0700 Subject: [PATCH 01/13] Moved domain objects from the API to Domain --- .../Domain/Results/AuthenticationResult.cs | 12 ------------ .../Newsbot.Collector.Domain.csproj | 2 ++ .../Requests/NewRoleRequest.cs | 2 +- .../Requests/RegisterUserRequest.cs | 2 +- .../Requests/UserLoginRequest.cs | 2 +- .../Requests/UserRefreshTokenRequest.cs | 2 +- .../Response/AuthFailedResponse.cs | 2 +- .../Response/AuthSuccessfulResponse.cs | 2 +- .../Results/AuthenticationResult.cs | 7 +++++++ 9 files changed, 15 insertions(+), 18 deletions(-) delete mode 100644 Newsbot.Collector.Api/Domain/Results/AuthenticationResult.cs rename {Newsbot.Collector.Api/Domain => Newsbot.Collector.Domain}/Requests/NewRoleRequest.cs (69%) rename {Newsbot.Collector.Api/Domain => Newsbot.Collector.Domain}/Requests/RegisterUserRequest.cs (81%) rename {Newsbot.Collector.Api/Domain => Newsbot.Collector.Domain}/Requests/UserLoginRequest.cs (78%) rename {Newsbot.Collector.Api/Domain => Newsbot.Collector.Domain}/Requests/UserRefreshTokenRequest.cs (71%) rename {Newsbot.Collector.Api/Domain => Newsbot.Collector.Domain}/Response/AuthFailedResponse.cs (64%) rename {Newsbot.Collector.Api/Domain => Newsbot.Collector.Domain}/Response/AuthSuccessfulResponse.cs (79%) create mode 100644 Newsbot.Collector.Domain/Results/AuthenticationResult.cs diff --git a/Newsbot.Collector.Api/Domain/Results/AuthenticationResult.cs b/Newsbot.Collector.Api/Domain/Results/AuthenticationResult.cs deleted file mode 100644 index 4cd4516..0000000 --- a/Newsbot.Collector.Api/Domain/Results/AuthenticationResult.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System.Collections; - -namespace Newsbot.Collector.Api.Domain.Results; - -public class AuthenticationResult -{ - public string? Token { get; set; } - public string? RefreshToken { get; set; } - - public bool IsSuccessful { get; set; } - public IEnumerable? ErrorMessage { get; set; } -} \ No newline at end of file diff --git a/Newsbot.Collector.Domain/Newsbot.Collector.Domain.csproj b/Newsbot.Collector.Domain/Newsbot.Collector.Domain.csproj index b355dd0..462ff5b 100644 --- a/Newsbot.Collector.Domain/Newsbot.Collector.Domain.csproj +++ b/Newsbot.Collector.Domain/Newsbot.Collector.Domain.csproj @@ -4,6 +4,8 @@ net7.0 enable enable + + true diff --git a/Newsbot.Collector.Api/Domain/Requests/NewRoleRequest.cs b/Newsbot.Collector.Domain/Requests/NewRoleRequest.cs similarity index 69% rename from Newsbot.Collector.Api/Domain/Requests/NewRoleRequest.cs rename to Newsbot.Collector.Domain/Requests/NewRoleRequest.cs index 706ebef..b15867d 100644 --- a/Newsbot.Collector.Api/Domain/Requests/NewRoleRequest.cs +++ b/Newsbot.Collector.Domain/Requests/NewRoleRequest.cs @@ -1,4 +1,4 @@ -namespace Newsbot.Collector.Api.Domain.Requests; +namespace Newsbot.Collector.Domain.Requests; public class AddRoleRequest { diff --git a/Newsbot.Collector.Api/Domain/Requests/RegisterUserRequest.cs b/Newsbot.Collector.Domain/Requests/RegisterUserRequest.cs similarity index 81% rename from Newsbot.Collector.Api/Domain/Requests/RegisterUserRequest.cs rename to Newsbot.Collector.Domain/Requests/RegisterUserRequest.cs index 461615c..bde90c6 100644 --- a/Newsbot.Collector.Api/Domain/Requests/RegisterUserRequest.cs +++ b/Newsbot.Collector.Domain/Requests/RegisterUserRequest.cs @@ -1,6 +1,6 @@ using System.ComponentModel.DataAnnotations; -namespace Newsbot.Collector.Api.Domain.Requests; +namespace Newsbot.Collector.Domain.Requests; public class RegisterUserRequest { diff --git a/Newsbot.Collector.Api/Domain/Requests/UserLoginRequest.cs b/Newsbot.Collector.Domain/Requests/UserLoginRequest.cs similarity index 78% rename from Newsbot.Collector.Api/Domain/Requests/UserLoginRequest.cs rename to Newsbot.Collector.Domain/Requests/UserLoginRequest.cs index 8b34017..6386e4f 100644 --- a/Newsbot.Collector.Api/Domain/Requests/UserLoginRequest.cs +++ b/Newsbot.Collector.Domain/Requests/UserLoginRequest.cs @@ -1,6 +1,6 @@ using System.ComponentModel.DataAnnotations; -namespace Newsbot.Collector.Api.Domain.Requests; +namespace Newsbot.Collector.Domain.Requests; public class UserLoginRequest { diff --git a/Newsbot.Collector.Api/Domain/Requests/UserRefreshTokenRequest.cs b/Newsbot.Collector.Domain/Requests/UserRefreshTokenRequest.cs similarity index 71% rename from Newsbot.Collector.Api/Domain/Requests/UserRefreshTokenRequest.cs rename to Newsbot.Collector.Domain/Requests/UserRefreshTokenRequest.cs index 9556e49..9c20338 100644 --- a/Newsbot.Collector.Api/Domain/Requests/UserRefreshTokenRequest.cs +++ b/Newsbot.Collector.Domain/Requests/UserRefreshTokenRequest.cs @@ -1,4 +1,4 @@ -namespace Newsbot.Collector.Api.Domain.Requests; +namespace Newsbot.Collector.Domain.Requests; public class UserRefreshTokenRequest { diff --git a/Newsbot.Collector.Api/Domain/Response/AuthFailedResponse.cs b/Newsbot.Collector.Domain/Response/AuthFailedResponse.cs similarity index 64% rename from Newsbot.Collector.Api/Domain/Response/AuthFailedResponse.cs rename to Newsbot.Collector.Domain/Response/AuthFailedResponse.cs index 79ecf83..3c3e6f0 100644 --- a/Newsbot.Collector.Api/Domain/Response/AuthFailedResponse.cs +++ b/Newsbot.Collector.Domain/Response/AuthFailedResponse.cs @@ -1,4 +1,4 @@ -namespace Newsbot.Collector.Api.Domain.Response; +namespace Newsbot.Collector.Domain.Response; public class AuthFailedResponse { diff --git a/Newsbot.Collector.Api/Domain/Response/AuthSuccessfulResponse.cs b/Newsbot.Collector.Domain/Response/AuthSuccessfulResponse.cs similarity index 79% rename from Newsbot.Collector.Api/Domain/Response/AuthSuccessfulResponse.cs rename to Newsbot.Collector.Domain/Response/AuthSuccessfulResponse.cs index dcb42ef..ebdcf2a 100644 --- a/Newsbot.Collector.Api/Domain/Response/AuthSuccessfulResponse.cs +++ b/Newsbot.Collector.Domain/Response/AuthSuccessfulResponse.cs @@ -1,4 +1,4 @@ -namespace Newsbot.Collector.Api.Domain.Response; +namespace Newsbot.Collector.Domain.Response; public class AuthSuccessfulResponse { diff --git a/Newsbot.Collector.Domain/Results/AuthenticationResult.cs b/Newsbot.Collector.Domain/Results/AuthenticationResult.cs new file mode 100644 index 0000000..127d4d5 --- /dev/null +++ b/Newsbot.Collector.Domain/Results/AuthenticationResult.cs @@ -0,0 +1,7 @@ +namespace Newsbot.Collector.Domain.Results; + +public class AuthenticationResult : BaseResult +{ + public string? Token { get; set; } + public string? RefreshToken { get; set; } +} \ No newline at end of file From 9b86f9e84d0c5ea64df0e9f69a646c154c3b8834 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sun, 23 Jul 2023 22:50:41 -0700 Subject: [PATCH 02/13] Cleaned up the consts and moved the Authorization.cs to the correct namespace --- .../Consts}/Authorization.cs | 0 .../Consts/ConfigConnectionStringsConst.cs | 10 ---------- Newsbot.Collector.Domain/Consts/ConfigTwitchConst.cs | 11 ----------- Newsbot.Collector.Domain/Consts/ConfigYoutubeConst.cs | 10 ---------- 4 files changed, 31 deletions(-) rename {Newsbot.Collector.Api/Domain => Newsbot.Collector.Domain/Consts}/Authorization.cs (100%) delete mode 100644 Newsbot.Collector.Domain/Consts/ConfigConnectionStringsConst.cs delete mode 100644 Newsbot.Collector.Domain/Consts/ConfigTwitchConst.cs delete mode 100644 Newsbot.Collector.Domain/Consts/ConfigYoutubeConst.cs diff --git a/Newsbot.Collector.Api/Domain/Authorization.cs b/Newsbot.Collector.Domain/Consts/Authorization.cs similarity index 100% rename from Newsbot.Collector.Api/Domain/Authorization.cs rename to Newsbot.Collector.Domain/Consts/Authorization.cs diff --git a/Newsbot.Collector.Domain/Consts/ConfigConnectionStringsConst.cs b/Newsbot.Collector.Domain/Consts/ConfigConnectionStringsConst.cs deleted file mode 100644 index 85f9db6..0000000 --- a/Newsbot.Collector.Domain/Consts/ConfigConnectionStringsConst.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Newsbot.Collector.Domain.Consts; - -/// -/// This class contains const entries to access keys within IConfiguration. -/// -public static class ConfigConnectionStringConst -{ - public const string Database = "ConnectionStrings:Database"; - public const string OpenTelemetry = "ConnectionStrings:OpenTelemetry"; -} \ No newline at end of file diff --git a/Newsbot.Collector.Domain/Consts/ConfigTwitchConst.cs b/Newsbot.Collector.Domain/Consts/ConfigTwitchConst.cs deleted file mode 100644 index 24b02fd..0000000 --- a/Newsbot.Collector.Domain/Consts/ConfigTwitchConst.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Newsbot.Collector.Domain.Consts; - -/// -/// This class contains const entries to access keys within IConfiguration. -/// -public class ConfigTwitchConst -{ - public const string IsEnabled = "Twitch:IsEnabled"; - public const string ClientID = "Twitch:ClientID"; - public const string ClientSecret = "Twitch:ClientSecret"; -} \ No newline at end of file diff --git a/Newsbot.Collector.Domain/Consts/ConfigYoutubeConst.cs b/Newsbot.Collector.Domain/Consts/ConfigYoutubeConst.cs deleted file mode 100644 index 41012f7..0000000 --- a/Newsbot.Collector.Domain/Consts/ConfigYoutubeConst.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Newsbot.Collector.Domain.Consts; - -/// -/// This class contains const entries to access keys within IConfiguration. -/// -public class ConfigYoutubeConst -{ - public const string IsEnable = "Youtube:IsEnabled"; - public const string Debug = "Youtube:Debug"; -} \ No newline at end of file From d7242c12c855105cc2a30912daafd45b5887c4e3 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sun, 23 Jul 2023 22:51:22 -0700 Subject: [PATCH 03/13] Created a master config to match the appsettings --- Newsbot.Collector.Domain/Models/Config.cs | 47 ++++++++++++++++--- .../Sources/ConfigSectionRedditModel.cs | 9 ---- 2 files changed, 41 insertions(+), 15 deletions(-) delete mode 100644 Newsbot.Collector.Domain/Models/Config/Sources/ConfigSectionRedditModel.cs diff --git a/Newsbot.Collector.Domain/Models/Config.cs b/Newsbot.Collector.Domain/Models/Config.cs index 77dd469..f9e9cd4 100644 --- a/Newsbot.Collector.Domain/Models/Config.cs +++ b/Newsbot.Collector.Domain/Models/Config.cs @@ -1,13 +1,24 @@ +using Newsbot.Collector.Domain.Models.Config; + namespace Newsbot.Collector.Domain.Models; public class ConfigModel { - public string? ServerAddress { get; set; } - public string? SqlConnectionString { get; set; } - public RedditConfigModel? Reddit { get; set; } + public ConnectionStrings? ConnectionStrings { get; set; } + public RedditConfig? Reddit { get; set; } + public YoutubeConfig? Youtube { get; set; } + public TwitchConfig? Twitch { get; set; } + public BasicSourceConfig? FinalFantasyXiv { get; set; } + public BasicSourceConfig? Rss { get; set; } + public BasicSourceConfig? CodeProjects { get; set; } + public NotificationsConfig? Notifications { get; set; } + public bool EnableSwagger { get; set; } + public bool RunDatabaseMigrationsOnStartup { get; set; } + public List? ApiKeys { get; set; } + public JwtSettings? JwtSettings { get; set; } } -public class RedditConfigModel +public class RedditConfig { public bool IsEnabled { get; set; } public bool PullHot { get; set; } @@ -17,5 +28,29 @@ public class RedditConfigModel public class ConnectionStrings { - public string Database { get; set; } = ""; -} \ No newline at end of file + public string? Database { get; set; } + public string? OpenTelemetry { get; set; } +} + +public class BasicSourceConfig +{ + public bool IsEnabled { get; set; } +} + +public class YoutubeConfig +{ + public bool IsEnabled { get; set; } + public bool Debug { get; set; } +} + +public class TwitchConfig +{ + public bool IsEnabled { get; set; } + public string? ClientId { get; set; } + public string? ClientSecret { get; set; } +} + +public class NotificationsConfig +{ + public BasicSourceConfig? Discord { get; set; } +} diff --git a/Newsbot.Collector.Domain/Models/Config/Sources/ConfigSectionRedditModel.cs b/Newsbot.Collector.Domain/Models/Config/Sources/ConfigSectionRedditModel.cs deleted file mode 100644 index e647630..0000000 --- a/Newsbot.Collector.Domain/Models/Config/Sources/ConfigSectionRedditModel.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace Newsbot.Collector.Domain.Models.Config.Sources; - -public class ConfigSectionRedditModel -{ - public bool IsEnabled { get; set; } - public bool PullHot { get; set; } - public bool PullNsfw { get; set; } - public bool PullTop { get; set; } -} \ No newline at end of file From 7b24ba16f7cb0f0e875448207ef273e2bafb7bab Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sun, 23 Jul 2023 22:52:18 -0700 Subject: [PATCH 04/13] moved the IdentityService.cs to services and warnings are now errors --- .../IdentityService.cs | 8 ++++---- .../Newsbot.Collector.Services.csproj | 5 +++++ 2 files changed, 9 insertions(+), 4 deletions(-) rename {Newsbot.Collector.Api/Services => Newsbot.Collector.Services}/IdentityService.cs (96%) diff --git a/Newsbot.Collector.Api/Services/IdentityService.cs b/Newsbot.Collector.Services/IdentityService.cs similarity index 96% rename from Newsbot.Collector.Api/Services/IdentityService.cs rename to Newsbot.Collector.Services/IdentityService.cs index bd84b33..3d587de 100644 --- a/Newsbot.Collector.Api/Services/IdentityService.cs +++ b/Newsbot.Collector.Services/IdentityService.cs @@ -3,13 +3,13 @@ using System.Security.Claims; using System.Text; using Microsoft.AspNetCore.Identity; using Microsoft.IdentityModel.Tokens; -using Newsbot.Collector.Api.Domain.Results; -using Newsbot.Collector.Database; +using Newsbot.Collector.Domain.Results; using Newsbot.Collector.Domain.Entities; using Newsbot.Collector.Domain.Interfaces; using Newsbot.Collector.Domain.Models.Config; +using JwtRegisteredClaimNames = Microsoft.IdentityModel.JsonWebTokens.JwtRegisteredClaimNames; -namespace Newsbot.Collector.Api.Services; +namespace Newsbot.Collector.Services; public interface IIdentityService { @@ -60,7 +60,7 @@ public class IdentityService : IIdentityService { return new AuthenticationResult { - ErrorMessage = new[] { createdUser.Result.Errors.Select(x => x.Description).ToString() } + ErrorMessage = new List(createdUser.Result.Errors.Select(x => x.Description)) }; } diff --git a/Newsbot.Collector.Services/Newsbot.Collector.Services.csproj b/Newsbot.Collector.Services/Newsbot.Collector.Services.csproj index 132e6a8..3081955 100644 --- a/Newsbot.Collector.Services/Newsbot.Collector.Services.csproj +++ b/Newsbot.Collector.Services/Newsbot.Collector.Services.csproj @@ -11,11 +11,14 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + + + @@ -23,6 +26,8 @@ net7.0 enable enable + + true From 5df29969473d2c7cbb4a76a33b10e12f1abc87c3 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sun, 23 Jul 2023 22:52:59 -0700 Subject: [PATCH 05/13] Database project now has warnings converted to errors --- Newsbot.Collector.Database/DatabaseContext.cs | 4 ++-- Newsbot.Collector.Database/Newsbot.Collector.Database.csproj | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Newsbot.Collector.Database/DatabaseContext.cs b/Newsbot.Collector.Database/DatabaseContext.cs index a3e0fdc..338da9c 100644 --- a/Newsbot.Collector.Database/DatabaseContext.cs +++ b/Newsbot.Collector.Database/DatabaseContext.cs @@ -17,8 +17,8 @@ public class DatabaseContext : IdentityDbContext public DbSet Sources { get; set; } = null!; public DbSet UserSourceSubscription { get; set; } = null!; - - public DbSet RefreshTokens { get; set; } + + public DbSet RefreshTokens { get; set; } = null!; private string ConnectionString { get; set; } = ""; diff --git a/Newsbot.Collector.Database/Newsbot.Collector.Database.csproj b/Newsbot.Collector.Database/Newsbot.Collector.Database.csproj index 68f1436..47f2b44 100644 --- a/Newsbot.Collector.Database/Newsbot.Collector.Database.csproj +++ b/Newsbot.Collector.Database/Newsbot.Collector.Database.csproj @@ -21,6 +21,9 @@ net7.0 enable enable + + true + 8981 From 2bc99afe6360d0a6adeaff2143829fedfa73face Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sun, 23 Jul 2023 22:54:00 -0700 Subject: [PATCH 06/13] Moved controllers to v1 namespace and updated routes --- .../{ => v1}/DiscordWebHooksController.cs | 22 ++++++++++++------- .../Controllers/{ => v1}/RssController.cs | 4 ++-- .../Controllers/{ => v1}/SourcesController.cs | 0 .../Controllers/{ => v1}/YoutubeController.cs | 4 ++-- 4 files changed, 18 insertions(+), 12 deletions(-) rename Newsbot.Collector.Api/Controllers/{ => v1}/DiscordWebHooksController.cs (81%) rename Newsbot.Collector.Api/Controllers/{ => v1}/RssController.cs (95%) rename Newsbot.Collector.Api/Controllers/{ => v1}/SourcesController.cs (100%) rename Newsbot.Collector.Api/Controllers/{ => v1}/YoutubeController.cs (95%) diff --git a/Newsbot.Collector.Api/Controllers/DiscordWebHooksController.cs b/Newsbot.Collector.Api/Controllers/v1/DiscordWebHooksController.cs similarity index 81% rename from Newsbot.Collector.Api/Controllers/DiscordWebHooksController.cs rename to Newsbot.Collector.Api/Controllers/v1/DiscordWebHooksController.cs index b33806d..a0d745d 100644 --- a/Newsbot.Collector.Api/Controllers/DiscordWebHooksController.cs +++ b/Newsbot.Collector.Api/Controllers/v1/DiscordWebHooksController.cs @@ -1,18 +1,17 @@ -using System.Net; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; -using Newsbot.Collector.Database.Repositories; +using Newsbot.Collector.Api.Middleware; using Newsbot.Collector.Domain.Dto; using Newsbot.Collector.Domain.Entities; using Newsbot.Collector.Domain.Interfaces; using Newsbot.Collector.Domain.Models; -namespace Newsbot.Collector.Api.Controllers; +namespace Newsbot.Collector.Api.Controllers.v1; [ApiController] -[Route("api/discord/webhooks")] +[Route("api/v1/discord/webhooks")] [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] public class DiscordWebHookController : ControllerBase { @@ -26,22 +25,29 @@ public class DiscordWebHookController : ControllerBase } [HttpGet(Name = "GetDiscordWebhooks")] - public IEnumerable Get(int page) + public ActionResult> Get(int page) { + var userId = HttpContext.GetUserId(); + if (userId.Equals(string.Empty)) + { + _logger.LogWarning("Unable to find the user ID in the JWD Token"); + return new BadRequestResult(); + } + var items = new List(); - var res = _webhooks.List(page); + var res = _webhooks.ListByUserId(userId, page); foreach (var item in res) { items.Add(DiscordWebHookDto.Convert(item)); } - return items; + + return new OkObjectResult(items); } [HttpPost(Name = "New")] public DiscordWebHookDto New(string url, string server, string channel) { var exists = _webhooks.GetByUrl(url); - // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract if (exists.Id != Guid.Empty) { return DiscordWebHookDto.Convert(exists); diff --git a/Newsbot.Collector.Api/Controllers/RssController.cs b/Newsbot.Collector.Api/Controllers/v1/RssController.cs similarity index 95% rename from Newsbot.Collector.Api/Controllers/RssController.cs rename to Newsbot.Collector.Api/Controllers/v1/RssController.cs index bfaaa7d..9b31637 100644 --- a/Newsbot.Collector.Api/Controllers/RssController.cs +++ b/Newsbot.Collector.Api/Controllers/v1/RssController.cs @@ -8,10 +8,10 @@ using Newsbot.Collector.Domain.Models.Config; using Newsbot.Collector.Domain.Models.Config.Sources; using Newsbot.Collector.Services.Jobs; -namespace Newsbot.Collector.Api.Controllers; +namespace Newsbot.Collector.Api.Controllers.v1; [ApiController] -[Route("api/rss")] +[Route("api/v1/rss")] [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] public class RssController { diff --git a/Newsbot.Collector.Api/Controllers/SourcesController.cs b/Newsbot.Collector.Api/Controllers/v1/SourcesController.cs similarity index 100% rename from Newsbot.Collector.Api/Controllers/SourcesController.cs rename to Newsbot.Collector.Api/Controllers/v1/SourcesController.cs diff --git a/Newsbot.Collector.Api/Controllers/YoutubeController.cs b/Newsbot.Collector.Api/Controllers/v1/YoutubeController.cs similarity index 95% rename from Newsbot.Collector.Api/Controllers/YoutubeController.cs rename to Newsbot.Collector.Api/Controllers/v1/YoutubeController.cs index b3f34e1..66ea171 100644 --- a/Newsbot.Collector.Api/Controllers/YoutubeController.cs +++ b/Newsbot.Collector.Api/Controllers/v1/YoutubeController.cs @@ -9,10 +9,10 @@ using Newsbot.Collector.Domain.Models.Config.Sources; using Newsbot.Collector.Services.Jobs; using ILogger = Grpc.Core.Logging.ILogger; -namespace Newsbot.Collector.Api.Controllers; +namespace Newsbot.Collector.Api.Controllers.v1; [ApiController] -[Route("api/youtube")] +[Route("api/v1/youtube")] [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] public class YoutubeController { From fa14562fd462a8c82c073ba32ba435c835caf0aa Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sun, 23 Jul 2023 22:55:20 -0700 Subject: [PATCH 07/13] Updated namespaces, consts, startup and warnings are now errors in the api project --- Newsbot.Collector.Api/Controllers/v1/UserController.cs | 1 + Newsbot.Collector.Api/Middleware/JwtUserIdExtension.cs | 7 +------ Newsbot.Collector.Api/Newsbot.Collector.Api.csproj | 2 ++ Newsbot.Collector.Api/Program.cs | 4 ++-- Newsbot.Collector.Api/Startup/DatabaseStartup.cs | 2 +- 5 files changed, 7 insertions(+), 9 deletions(-) diff --git a/Newsbot.Collector.Api/Controllers/v1/UserController.cs b/Newsbot.Collector.Api/Controllers/v1/UserController.cs index 9125463..04285b3 100644 --- a/Newsbot.Collector.Api/Controllers/v1/UserController.cs +++ b/Newsbot.Collector.Api/Controllers/v1/UserController.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Newsbot.Collector.Api.Authentication; +using Newsbot.Collector.Api.Middleware; using Newsbot.Collector.Domain.Entities; using Newsbot.Collector.Domain.Interfaces; diff --git a/Newsbot.Collector.Api/Middleware/JwtUserIdExtension.cs b/Newsbot.Collector.Api/Middleware/JwtUserIdExtension.cs index 80c0e09..041440a 100644 --- a/Newsbot.Collector.Api/Middleware/JwtUserIdExtension.cs +++ b/Newsbot.Collector.Api/Middleware/JwtUserIdExtension.cs @@ -1,14 +1,9 @@ -namespace Newsbot.Collector.Api.Authentication; +namespace Newsbot.Collector.Api.Middleware; public static class JwtUserIdExtension { public static string GetUserId(this HttpContext context) { - if (context.User == null) - { - return string.Empty; - } - return context.User.Claims.Single(x => x.Type == "id").Value; } } \ No newline at end of file diff --git a/Newsbot.Collector.Api/Newsbot.Collector.Api.csproj b/Newsbot.Collector.Api/Newsbot.Collector.Api.csproj index 969a610..76badf8 100644 --- a/Newsbot.Collector.Api/Newsbot.Collector.Api.csproj +++ b/Newsbot.Collector.Api/Newsbot.Collector.Api.csproj @@ -4,6 +4,8 @@ net7.0 enable enable + + true diff --git a/Newsbot.Collector.Api/Program.cs b/Newsbot.Collector.Api/Program.cs index e3ae5fc..f35dbc3 100644 --- a/Newsbot.Collector.Api/Program.cs +++ b/Newsbot.Collector.Api/Program.cs @@ -35,7 +35,7 @@ GlobalConfiguration.Configuration.UseSerilogLogProvider(); // Build Health Checks builder.Services.AddHealthChecks() - .AddNpgSql(config.GetValue(ConfigConnectionStringConst.Database) ?? ""); + .AddNpgSql(config.GetValue(ConfigConst.ConnectionStringDatabase) ?? ""); builder.Services.AddControllers(); @@ -109,7 +109,7 @@ static IConfiguration GetConfiguration() static ILogger GetLogger(IConfiguration configuration) { - var otel = configuration.GetValue(ConfigConnectionStringConst.OpenTelemetry) ?? ""; + var otel = configuration.GetValue(ConfigConst.ConnectionStringOpenTelemetry) ?? ""; if (otel == "") return Log.Logger = new LoggerConfiguration() diff --git a/Newsbot.Collector.Api/Startup/DatabaseStartup.cs b/Newsbot.Collector.Api/Startup/DatabaseStartup.cs index 04495ae..f5f60c3 100644 --- a/Newsbot.Collector.Api/Startup/DatabaseStartup.cs +++ b/Newsbot.Collector.Api/Startup/DatabaseStartup.cs @@ -1,10 +1,10 @@ using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Newsbot.Collector.Api.Domain; -using Newsbot.Collector.Api.Services; using Newsbot.Collector.Database; using Newsbot.Collector.Database.Repositories; using Newsbot.Collector.Domain.Interfaces; +using Newsbot.Collector.Services; namespace Newsbot.Collector.Api.Startup; From 19d8e3e925273e1e4ceff14a3397cfab7ff904f5 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sun, 23 Jul 2023 22:56:06 -0700 Subject: [PATCH 08/13] ArticlesController.cs was cleaned up and supports results with details now --- .../Controllers/ArticlesController.cs | 60 ------------- .../Controllers/v1/ArticlesController.cs | 84 +++++++++++++++++++ .../Interfaces/IArticlesRepository.cs | 2 +- .../Results/ArticleDetailsResult.cs | 8 ++ .../Results/ArticleResult.cs | 8 ++ .../Results/BaseResult.cs | 7 ++ 6 files changed, 108 insertions(+), 61 deletions(-) delete mode 100644 Newsbot.Collector.Api/Controllers/ArticlesController.cs create mode 100644 Newsbot.Collector.Api/Controllers/v1/ArticlesController.cs create mode 100644 Newsbot.Collector.Domain/Results/ArticleDetailsResult.cs create mode 100644 Newsbot.Collector.Domain/Results/ArticleResult.cs create mode 100644 Newsbot.Collector.Domain/Results/BaseResult.cs diff --git a/Newsbot.Collector.Api/Controllers/ArticlesController.cs b/Newsbot.Collector.Api/Controllers/ArticlesController.cs deleted file mode 100644 index e4626ba..0000000 --- a/Newsbot.Collector.Api/Controllers/ArticlesController.cs +++ /dev/null @@ -1,60 +0,0 @@ -using Microsoft.AspNetCore.Authentication.JwtBearer; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Options; -using Newsbot.Collector.Domain.Dto; -using Newsbot.Collector.Domain.Interfaces; -using Newsbot.Collector.Domain.Models; - -namespace Newsbot.Collector.Api.Controllers; - -[ApiController] -[Route("api/articles")] -[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] -public class ArticlesController : ControllerBase -{ - private readonly ILogger _logger; - private readonly IArticlesRepository _articles; - private readonly ISourcesRepository _sources; - - public ArticlesController(ILogger logger, IArticlesRepository articles, ISourcesRepository sources) - { - _logger = logger; - _articles = articles; - _sources = sources; - } - - [HttpGet(Name = "GetArticles")] - public IEnumerable Get() - { - var res = new List(); - var items = _articles.List(0, 25); - foreach (var item in items) res.Add(ArticleDto.Convert(item)); - return res; - } - - [HttpGet("{id:guid}")] - [EndpointDescription("Returns the article based on the Id value given.")] - public ArticleDto GetById(Guid id) - { - var item = _articles.GetById(id); - return ArticleDto.Convert(item); - } - - [HttpGet("{id:guid}/details")] - public ArticleDetailsDto GetDetailsById(Guid id) - { - var item = _articles.GetById(id); - var sourceItem = _sources.GetById(item.SourceId); - return ArticleDetailsDto.Convert(item, sourceItem); - } - - [HttpGet("by/{sourceId:guid}")] - public IEnumerable GetBySourceId(Guid sourceId, int page = 0, int count = 25) - { - var res = new List(); - var items = _articles.ListBySourceId(sourceId, page, count); - foreach (var item in items) res.Add(ArticleDto.Convert(item)); - return res; - } -} \ No newline at end of file diff --git a/Newsbot.Collector.Api/Controllers/v1/ArticlesController.cs b/Newsbot.Collector.Api/Controllers/v1/ArticlesController.cs new file mode 100644 index 0000000..b8ed6d5 --- /dev/null +++ b/Newsbot.Collector.Api/Controllers/v1/ArticlesController.cs @@ -0,0 +1,84 @@ +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Newsbot.Collector.Domain.Dto; +using Newsbot.Collector.Domain.Interfaces; +using Newsbot.Collector.Domain.Results; + +namespace Newsbot.Collector.Api.Controllers.v1; + +[ApiController] +[Route("api/v1/articles")] +[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] +public class ArticlesController : ControllerBase +{ + //private readonly ILogger _logger; + private readonly IArticlesRepository _articles; + private readonly ISourcesRepository _sources; + + public ArticlesController(IArticlesRepository articles, ISourcesRepository sources) + { + _articles = articles; + _sources = sources; + } + + [HttpGet(Name = "GetArticles")] + public ActionResult Get() + { + var res = new List(); + var items = _articles.List(0); + foreach (var item in items) + { + res.Add(ArticleDto.Convert(item)); + } + + return new OkObjectResult(new ArticleResult + { + IsSuccessful = true, + Items = res + }); + } + + [HttpGet("{id:guid}")] + [EndpointDescription("Returns the article based on the Id value given.")] + public ActionResult GetById(Guid id) + { + var item = _articles.GetById(id); + return new OkObjectResult(new ArticleResult + { + IsSuccessful = true, + Items = new List + { + ArticleDto.Convert(item) + } + }); + } + + [HttpGet("{id:guid}/details")] + public ActionResult GetDetailsById(Guid id) + { + var item = _articles.GetById(id); + var sourceItem = _sources.GetById(item.SourceId); + + return new OkObjectResult(new ArticleDetailsResult + { + IsSuccessful = true, + Item = ArticleDetailsDto.Convert(item, sourceItem) + }); + } + + [HttpGet("by/{sourceId:guid}")] + public ActionResult GetBySourceId(Guid sourceId, int page = 0) + { + var res = new List(); + var items = _articles.ListBySourceId(sourceId, page); + foreach (var item in items) res.Add(ArticleDto.Convert(item)); + + return new OkObjectResult(new ArticleResult + { + IsSuccessful = true, + Items = res + }); + + } +} \ No newline at end of file diff --git a/Newsbot.Collector.Domain/Interfaces/IArticlesRepository.cs b/Newsbot.Collector.Domain/Interfaces/IArticlesRepository.cs index 4cabce7..c9f86c6 100644 --- a/Newsbot.Collector.Domain/Interfaces/IArticlesRepository.cs +++ b/Newsbot.Collector.Domain/Interfaces/IArticlesRepository.cs @@ -5,7 +5,7 @@ namespace Newsbot.Collector.Domain.Interfaces; public interface IArticlesRepository : ITableRepository { - List List(int page, int count); + List List(int page, int count = 25); List ListBySourceId(Guid id, int page = 0, int count = 25); ArticlesEntity GetById(Guid id); ArticlesEntity GetByUrl(string url); diff --git a/Newsbot.Collector.Domain/Results/ArticleDetailsResult.cs b/Newsbot.Collector.Domain/Results/ArticleDetailsResult.cs new file mode 100644 index 0000000..c97271f --- /dev/null +++ b/Newsbot.Collector.Domain/Results/ArticleDetailsResult.cs @@ -0,0 +1,8 @@ +using Newsbot.Collector.Domain.Dto; + +namespace Newsbot.Collector.Domain.Results; + +public class ArticleDetailsResult : BaseResult +{ + public ArticleDetailsDto? Item { get; set; } +} \ No newline at end of file diff --git a/Newsbot.Collector.Domain/Results/ArticleResult.cs b/Newsbot.Collector.Domain/Results/ArticleResult.cs new file mode 100644 index 0000000..0b5fb78 --- /dev/null +++ b/Newsbot.Collector.Domain/Results/ArticleResult.cs @@ -0,0 +1,8 @@ +using Newsbot.Collector.Domain.Dto; + +namespace Newsbot.Collector.Domain.Results; + +public class ArticleResult : BaseResult +{ + public IEnumerable? Items { get; set; } +} \ No newline at end of file diff --git a/Newsbot.Collector.Domain/Results/BaseResult.cs b/Newsbot.Collector.Domain/Results/BaseResult.cs new file mode 100644 index 0000000..d9e22e1 --- /dev/null +++ b/Newsbot.Collector.Domain/Results/BaseResult.cs @@ -0,0 +1,7 @@ +namespace Newsbot.Collector.Domain.Results; + +public class BaseResult +{ + public bool IsSuccessful { get; set; } + public IEnumerable? ErrorMessage { get; set; } +} \ No newline at end of file From 7344d2bdd2485b6f7c7112546bbe9c026eb50b45 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sun, 23 Jul 2023 22:56:26 -0700 Subject: [PATCH 09/13] CodeProjectController.cs was updated to support ActionResults and new route --- .../Controllers/{ => v1}/CodeProjectController.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) rename Newsbot.Collector.Api/Controllers/{ => v1}/CodeProjectController.cs (88%) diff --git a/Newsbot.Collector.Api/Controllers/CodeProjectController.cs b/Newsbot.Collector.Api/Controllers/v1/CodeProjectController.cs similarity index 88% rename from Newsbot.Collector.Api/Controllers/CodeProjectController.cs rename to Newsbot.Collector.Api/Controllers/v1/CodeProjectController.cs index 4929d39..d33cba1 100644 --- a/Newsbot.Collector.Api/Controllers/CodeProjectController.cs +++ b/Newsbot.Collector.Api/Controllers/v1/CodeProjectController.cs @@ -7,10 +7,10 @@ using Newsbot.Collector.Api.Domain; using Newsbot.Collector.Domain.Models.Config; using Newsbot.Collector.Services.Jobs; -namespace Newsbot.Collector.Api.Controllers; +namespace Newsbot.Collector.Api.Controllers.v1; [ApiController] -[Route("api/codeprojects")] +[Route("api/v1/codeprojects")] [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] public class CodeProjectController { @@ -26,7 +26,7 @@ public class CodeProjectController [HttpPost("check")] [Authorize(Roles = Authorization.AdministratorsRole)] - public void PullNow() + public ActionResult PullNow() { BackgroundJob.Enqueue(x => x.InitAndExecute(new CodeProjectWatcherJobOptions { @@ -34,5 +34,7 @@ public class CodeProjectController FeaturePullReleases = true, FeaturePullCommits = true })); + + return new AcceptedResult(); } } \ No newline at end of file From a41674626968acbee70c62ccc9a590293e71f61f Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sun, 23 Jul 2023 22:57:35 -0700 Subject: [PATCH 10/13] DiscordNotificationController.cs got a new route, moved over to ActionResults, records are now tracking the userId and returning common result classes --- .../DiscordNotificationController.cs | 130 ---------- .../v1/DiscordNotificationController.cs | 239 ++++++++++++++++++ .../Repositories/DiscordNotificationTable.cs | 65 ++--- .../Entities/DiscordWebhookEntity.cs | 7 + .../IDiscordNotificationRepository.cs | 20 +- .../Requests/NewDiscordNotificationRequest.cs | 10 + .../DiscordNotificationDetailsResult.cs | 8 + .../Results/DiscordNotificationResult.cs | 8 + 8 files changed, 322 insertions(+), 165 deletions(-) delete mode 100644 Newsbot.Collector.Api/Controllers/DiscordNotificationController.cs create mode 100644 Newsbot.Collector.Api/Controllers/v1/DiscordNotificationController.cs create mode 100644 Newsbot.Collector.Domain/Requests/NewDiscordNotificationRequest.cs create mode 100644 Newsbot.Collector.Domain/Results/DiscordNotificationDetailsResult.cs create mode 100644 Newsbot.Collector.Domain/Results/DiscordNotificationResult.cs diff --git a/Newsbot.Collector.Api/Controllers/DiscordNotificationController.cs b/Newsbot.Collector.Api/Controllers/DiscordNotificationController.cs deleted file mode 100644 index c2c8e55..0000000 --- a/Newsbot.Collector.Api/Controllers/DiscordNotificationController.cs +++ /dev/null @@ -1,130 +0,0 @@ -using Microsoft.AspNetCore.Authentication.JwtBearer; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; -using Newsbot.Collector.Domain.Dto; -using Newsbot.Collector.Domain.Entities; -using Newsbot.Collector.Domain.Interfaces; - -namespace Newsbot.Collector.Api.Controllers; - -[ApiController] -[Route("api/subscriptions")] -[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] -public class DiscordNotificationController : ControllerBase -{ - private readonly ILogger _logger; - private readonly IDiscordWebHooksRepository _discord; - private readonly ISourcesRepository _sources; - private readonly IDiscordNotificationRepository _discordNotification; - - public DiscordNotificationController(ILogger logger, IDiscordWebHooksRepository discord, ISourcesRepository sources, IDiscordNotificationRepository discordNotification) - { - _logger = logger; - _discord = discord; - _sources = sources; - _discordNotification = discordNotification; - } - - [HttpGet(Name = "ListSubscriptions")] - public IEnumerable List(int page) - { - var res = new List(); - var items = _discordNotification.List(page); - foreach (var item in items) res.Add(DiscordNotificationDto.Convert(item)); - return res; - } - - [HttpGet("{id}")] - public DiscordNotificationDto GetById(Guid id) - { - return DiscordNotificationDto.Convert(_discordNotification.GetById(id)); - } - - [HttpGet("{id}/details")] - public DiscordNotificationDetailsDto GetDetailsById(Guid id) - { - var sub = _discordNotification.GetById(id); - var webhook = _discord.GetById(sub.DiscordWebHookId); - var source = _sources.GetById(sub.SourceId); - - return DiscordNotificationDetailsDto.Convert(sub, source, webhook); - } - - [HttpPost("{id}/delete")] - public void DeleteById(Guid id) - { - _discordNotification.Delete(id); - } - - [HttpGet("by/discordId/{id}")] - public IEnumerable GetByDiscordId(Guid id) - { - var res = new List(); - var items = _discordNotification.ListByWebhook(id); - foreach (var item in items) res.Add(DiscordNotificationDto.Convert(item)); - return res; - } - - [HttpGet("by/sourceId/{id}")] - public IEnumerable GetBySourceId(Guid id) - { - var res = new List(); - var items = _discordNotification.ListBySourceId(id); - foreach (var item in items) res.Add(DiscordNotificationDto.Convert(item)); - return res; - } - - [HttpPost(Name = "New Subscription")] - public ActionResult New(Guid sourceId, Guid discordId) - { - if (sourceId == Guid.Empty) return new BadRequestResult(); - if (discordId == Guid.Empty) return new BadRequestResult(); - - var exists = _discordNotification.GetByWebhookAndSource(discordId, sourceId); - if (exists.Id != Guid.Empty) return DiscordNotificationDto.Convert(exists); - - var discord = _discord.GetById(discordId); - if (discord.Id == Guid.Empty) return new BadRequestResult(); - - var source = _sources.GetById(sourceId); - if (source.Id == Guid.Empty) return new BadRequestResult(); - - var item = _discordNotification.New(new DiscordNotificationEntity - { - Id = Guid.NewGuid(), - SourceId = sourceId, - DiscordWebHookId = discordId, - CodeAllowCommits = false, - CodeAllowReleases = false - }); - - return DiscordNotificationDto.Convert(item); - } - - [HttpPost("new/codeproject")] - public ActionResult NewCodeProjectSubscription(Guid sourceId, Guid discordId, bool allowReleases, - bool allowCommits) - { - if (sourceId == Guid.Empty) return new BadRequestResult(); - if (discordId == Guid.Empty) return new BadRequestResult(); - - var exists = _discordNotification.GetByWebhookAndSource(discordId, sourceId); - if (exists.Id != Guid.Empty) return DiscordNotificationDto.Convert(exists); - - var discord = _discord.GetById(discordId); - if (discord.Id == Guid.Empty) return new BadRequestResult(); - - var source = _sources.GetById(sourceId); - if (source.Id == Guid.Empty) return new BadRequestResult(); - - var sub = _discordNotification.New(new DiscordNotificationEntity - { - DiscordWebHookId = discordId, - SourceId = sourceId, - CodeAllowCommits = allowCommits, - CodeAllowReleases = allowReleases - }); - - return new DiscordNotificationDto(); - } -} \ No newline at end of file diff --git a/Newsbot.Collector.Api/Controllers/v1/DiscordNotificationController.cs b/Newsbot.Collector.Api/Controllers/v1/DiscordNotificationController.cs new file mode 100644 index 0000000..c66951d --- /dev/null +++ b/Newsbot.Collector.Api/Controllers/v1/DiscordNotificationController.cs @@ -0,0 +1,239 @@ +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Newsbot.Collector.Api.Middleware; +using Newsbot.Collector.Domain.Dto; +using Newsbot.Collector.Domain.Entities; +using Newsbot.Collector.Domain.Interfaces; +using Newsbot.Collector.Domain.Requests; +using Newsbot.Collector.Domain.Results; + +namespace Newsbot.Collector.Api.Controllers.v1; + +[ApiController] +[Route("api/v1/subscriptions")] +[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] +public class DiscordNotificationController : ControllerBase +{ + private readonly ILogger _logger; + private readonly IDiscordWebHooksRepository _discord; + private readonly ISourcesRepository _sources; + private readonly IDiscordNotificationRepository _discordNotification; + + public DiscordNotificationController(ILogger logger, IDiscordWebHooksRepository discord, ISourcesRepository sources, IDiscordNotificationRepository discordNotification) + { + _logger = logger; + _discord = discord; + _sources = sources; + _discordNotification = discordNotification; + } + + [HttpGet(Name = "ListSubscriptions")] + public ActionResult List(int page) + { + var userId = HttpContext.GetUserId(); + if (userId.Equals(string.Empty)) + { + return new BadRequestObjectResult(new DiscordNotificationResult + { + IsSuccessful = false, + ErrorMessage = new List { "User Id is missing from the request." } + }); + } + + var res = new List(); + var items = _discordNotification.List(userId, page); + foreach (var item in items) res.Add(DiscordNotificationDto.Convert(item)); + + return new OkObjectResult(new DiscordNotificationResult + { + IsSuccessful = true, + Items = res + }); + } + + [HttpGet("{id}")] + public ActionResult GetById(Guid id) + { + var userId = HttpContext.GetUserId(); + if (userId.Equals(string.Empty)) + { + return new BadRequestObjectResult(new DiscordNotificationResult + { + IsSuccessful = false, + ErrorMessage = new List { "User Id is missing from the request." } + }); + } + + var res = DiscordNotificationDto.Convert(_discordNotification.GetById(userId, id)); + + return new OkObjectResult(new DiscordNotificationResult + { + IsSuccessful = true, + Items = new List + { + res + } + }); + } + + [HttpGet("{id}/details")] + public ActionResult GetDetailsById(Guid id) + { + var userId = HttpContext.GetUserId(); + if (userId.Equals(string.Empty)) + { + return new BadRequestObjectResult(new DiscordNotificationResult + { + IsSuccessful = false, + ErrorMessage = new List { "User Id is missing from the request." } + }); + } + + var sub = _discordNotification.GetById(userId, id); + var webhook = _discord.GetById(sub.DiscordWebHookId); + var source = _sources.GetById(sub.SourceId); + + return new OkObjectResult(new DiscordNotificationDetailsResult + { + IsSuccessful = true, + Item = DiscordNotificationDetailsDto.Convert(sub, source, webhook) + }); + } + + [HttpPost("{id}/delete")] + public ActionResult DeleteById(Guid id) + { + var userId = HttpContext.GetUserId(); + if (userId.Equals(string.Empty)) + { + return new BadRequestObjectResult(new DiscordNotificationResult + { + IsSuccessful = false, + ErrorMessage = new List { "User Id is missing from the request." } + }); + } + + var rowsUpdated = _discordNotification.Delete(userId, id); + if (rowsUpdated == -1) + { + return new OkObjectResult(new DiscordNotificationResult + { + IsSuccessful = false, + ErrorMessage = new List { "Record was not own by requested user." } + }); + } + + return new OkObjectResult(new DiscordNotificationResult + { + IsSuccessful = true + }); + } + + [HttpGet("by/discordId/{id}")] + public ActionResult GetByDiscordId(Guid id) + { + var userId = HttpContext.GetUserId(); + if (userId.Equals(string.Empty)) + { + return new BadRequestObjectResult(new DiscordNotificationResult + { + IsSuccessful = false, + ErrorMessage = new List { "User Id is missing from the request." } + }); + } + + var res = new List(); + var items = _discordNotification.ListByWebhook(userId, id); + foreach (var item in items) res.Add(DiscordNotificationDto.Convert(item)); + + return new OkObjectResult(new DiscordNotificationResult + { + IsSuccessful = true, + Items = res + }); + } + + [HttpGet("by/sourceId/{id}")] + public ActionResult GetBySourceId(Guid id) + { + var userId = HttpContext.GetUserId(); + if (userId.Equals(string.Empty)) + { + return new BadRequestObjectResult(new DiscordNotificationResult + { + IsSuccessful = false, + ErrorMessage = new List { "User Id is missing from the request." } + }); + } + + var res = new List(); + var items = _discordNotification.ListBySourceId(userId, id); + foreach (var item in items) res.Add(DiscordNotificationDto.Convert(item)); + + return new OkObjectResult(new DiscordNotificationResult + { + IsSuccessful = true, + Items = res + }); + + } + + [HttpPost(Name = "New Subscription")] + public ActionResult New([FromBody] NewDiscordNotificationRequest request) + { + var userId = HttpContext.GetUserId(); + if (userId.Equals(string.Empty)) + { + return new BadRequestObjectResult(new DiscordNotificationResult + { + IsSuccessful = false, + ErrorMessage = new List { "User Id is missing from the request." } + }); + } + + if (request.SourceId == Guid.Empty) return new BadRequestObjectResult(new DiscordNotificationResult + { + IsSuccessful = false, + ErrorMessage = new List { "SourceId is missing from the request." } + }); + if (request.DiscordId == Guid.Empty) return new BadRequestObjectResult(new DiscordNotificationResult + { + IsSuccessful = false, + ErrorMessage = new List { "DiscordId is missing from the request." } + }); + + var exists = _discordNotification.GetByWebhookAndSource(userId, request.DiscordId, request.SourceId); + if (exists.Id != Guid.Empty) return DiscordNotificationDto.Convert(exists); + + var discord = _discord.GetById(request.DiscordId); + if (discord.Id == Guid.Empty) return new BadRequestObjectResult(new DiscordNotificationResult + { + IsSuccessful = false, + ErrorMessage = new List { "Unable to find the requested DiscordId in the database." } + }); + + var source = _sources.GetById(request.SourceId); + if (source.Id == Guid.Empty) return new BadRequestObjectResult(new DiscordNotificationResult + { + IsSuccessful = false, + ErrorMessage = new List { "Unable to find the requested SourceId in the database." } + }); + + var item = _discordNotification.New(new DiscordNotificationEntity + { + Id = Guid.NewGuid(), + SourceId = request.SourceId, + DiscordWebHookId = request.DiscordId, + CodeAllowCommits = request.AllowCommits, + CodeAllowReleases = request.AllowReleases, + UserId = userId + }); + + return new OkObjectResult(new DiscordNotificationResult + { + IsSuccessful = true, + Items = new List { DiscordNotificationDto.Convert(item) } + }); + } +} \ No newline at end of file diff --git a/Newsbot.Collector.Database/Repositories/DiscordNotificationTable.cs b/Newsbot.Collector.Database/Repositories/DiscordNotificationTable.cs index 9132d8b..4b884a1 100644 --- a/Newsbot.Collector.Database/Repositories/DiscordNotificationTable.cs +++ b/Newsbot.Collector.Database/Repositories/DiscordNotificationTable.cs @@ -1,10 +1,5 @@ -using System.Data; -using Dapper; -using Microsoft.Extensions.Configuration; using Newsbot.Collector.Domain.Entities; using Newsbot.Collector.Domain.Interfaces; -using Newsbot.Collector.Domain.Models; -using Npgsql; namespace Newsbot.Collector.Database.Repositories; @@ -27,66 +22,76 @@ public class DiscordNotificationTable : IDiscordNotificationRepository public DiscordNotificationEntity New(DiscordNotificationEntity model) { model.Id = new Guid(); - //using var context = new DatabaseContext(_connectionString); + _context.DiscordNotification.Add(model); _context.SaveChanges(); + return model; } - public List List(int page = 0, int count = 25) + public List List(string userId, int page = 0, int count = 25) { - //using var context = new DatabaseContext(_connectionString); - return _context.DiscordNotification.Skip(page * count).Take(count).ToList(); + return _context.DiscordNotification + .Where(x => x.UserId != null && x.UserId.Equals(userId)) + .Skip(page * count) + .Take(count) + .ToList(); } + public List ListBySourceId(string userId, Guid id, int page = 0, int count = 25) + { + return _context.DiscordNotification + .Where(f => f.SourceId.Equals(id)) + .Where(f => f.UserId != null && f.UserId.Equals(userId)) + .Skip(page * count) + .ToList(); + } + public List ListBySourceId(Guid id, int page = 0, int count = 25) { - //using var context = new DatabaseContext(_connectionString); - return _context.DiscordNotification.Where(f => f.SourceId.Equals(id)) + return _context.DiscordNotification + .Where(f => f.SourceId.Equals(id)) .Skip(page * count) .ToList(); } - public List ListByWebhook(Guid id, int page = 0, int count = 25) + public List ListByWebhook(string userId, Guid id, int page = 0, int count = 25) { - //using var context = new DatabaseContext(_connectionString); - return _context.DiscordNotification.Where(f => f.DiscordWebHookId.Equals(id)).Skip(page * count).ToList(); + return _context.DiscordNotification + .Where(f => f.DiscordWebHookId.Equals(id)) + .Where(f => f.UserId != null && f.UserId.Equals(userId)) + .Skip(page * count) + .ToList(); } - public DiscordNotificationEntity GetById(Guid id) + public DiscordNotificationEntity GetById(string userId, Guid id) { - //using var context = new DatabaseContext(_connectionString); var res = _context.DiscordNotification + .Where(f => f.UserId != null && f.UserId.Equals(userId)) .FirstOrDefault(f => f.Id.Equals(id)); return res ??= new DiscordNotificationEntity(); } - public DiscordNotificationEntity GetByWebhookAndSource(Guid webhookId, Guid sourceId) + public DiscordNotificationEntity GetByWebhookAndSource(string userId, Guid webhookId, Guid sourceId) { - //using var context = new DatabaseContext(_connectionString); var res = _context.DiscordNotification + .Where(f => f.UserId != null && f.UserId.Equals(userId)) .Where(f => f.DiscordWebHookId.Equals(webhookId)) .FirstOrDefault(f => f.SourceId.Equals(sourceId)); return res ??= new DiscordNotificationEntity(); } - public void Delete(Guid id) + public int Delete(string userId, Guid id) { - //using var context = new DatabaseContext(_connectionString); - var res = _context.DiscordNotification.FirstOrDefault(f => f.Id.Equals(id)); + var res = _context.DiscordNotification + .Where(f => f.UserId != null && f.UserId.Equals(userId)) + .FirstOrDefault(f => f.Id.Equals(id)); if (res is null) { - return; + return -1; } _context.DiscordNotification.Remove(res); - _context.SaveChanges(); + return _context.SaveChanges(); } - - //private IDbConnection OpenConnection(string connectionString) - //{ - // var conn = new NpgsqlConnection(_connectionString); - // conn.Open(); - // return conn; - //} } \ No newline at end of file diff --git a/Newsbot.Collector.Domain/Entities/DiscordWebhookEntity.cs b/Newsbot.Collector.Domain/Entities/DiscordWebhookEntity.cs index 7e7ec40..38758e2 100644 --- a/Newsbot.Collector.Domain/Entities/DiscordWebhookEntity.cs +++ b/Newsbot.Collector.Domain/Entities/DiscordWebhookEntity.cs @@ -1,3 +1,6 @@ +using System.ComponentModel.DataAnnotations.Schema; +using Microsoft.AspNetCore.Identity; + namespace Newsbot.Collector.Domain.Entities; public class DiscordWebhookEntity @@ -7,4 +10,8 @@ public class DiscordWebhookEntity public string Server { get; set; } = ""; public string Channel { get; set; } = ""; public bool Enabled { get; set; } + + public string? UserId { get; set; } + [ForeignKey(nameof(UserId))] + public IdentityUser? User { get; set; } } \ No newline at end of file diff --git a/Newsbot.Collector.Domain/Interfaces/IDiscordNotificationRepository.cs b/Newsbot.Collector.Domain/Interfaces/IDiscordNotificationRepository.cs index c71e137..f2dbf61 100644 --- a/Newsbot.Collector.Domain/Interfaces/IDiscordNotificationRepository.cs +++ b/Newsbot.Collector.Domain/Interfaces/IDiscordNotificationRepository.cs @@ -7,12 +7,22 @@ public interface IDiscordNotificationRepository { DiscordNotificationEntity New(DiscordNotificationEntity model); - List List(int page = 0, int count = 25); + List List(string userId, int page = 0, int count = 25); + List ListBySourceId(string userId, Guid id, int page = 0, int count = 25); + + /// + /// This will collect all the records based on the SourceId. + /// Background jobs can use this but user facing calls need to define UserId + /// + /// + /// + /// + /// List ListBySourceId(Guid id, int page = 0, int count = 25); - List ListByWebhook(Guid id, int page = 0, int count = 25); + List ListByWebhook(string userId, Guid id, int page = 0, int count = 25); - DiscordNotificationEntity GetById(Guid id); - DiscordNotificationEntity GetByWebhookAndSource(Guid webhookId, Guid sourceId); + DiscordNotificationEntity GetById(string userId, Guid id); + DiscordNotificationEntity GetByWebhookAndSource(string userId, Guid webhookId, Guid sourceId); - void Delete(Guid id); + int Delete(string userId, Guid id); } \ No newline at end of file diff --git a/Newsbot.Collector.Domain/Requests/NewDiscordNotificationRequest.cs b/Newsbot.Collector.Domain/Requests/NewDiscordNotificationRequest.cs new file mode 100644 index 0000000..0ba7f4d --- /dev/null +++ b/Newsbot.Collector.Domain/Requests/NewDiscordNotificationRequest.cs @@ -0,0 +1,10 @@ +namespace Newsbot.Collector.Domain.Requests; + +public class NewDiscordNotificationRequest +{ + public Guid SourceId { get; set; } + public Guid DiscordId { get; set; } + + public bool AllowReleases { get; set; } = false; + public bool AllowCommits { get; set; } = false; +} \ No newline at end of file diff --git a/Newsbot.Collector.Domain/Results/DiscordNotificationDetailsResult.cs b/Newsbot.Collector.Domain/Results/DiscordNotificationDetailsResult.cs new file mode 100644 index 0000000..0d8d40f --- /dev/null +++ b/Newsbot.Collector.Domain/Results/DiscordNotificationDetailsResult.cs @@ -0,0 +1,8 @@ +using Newsbot.Collector.Domain.Dto; + +namespace Newsbot.Collector.Domain.Results; + +public class DiscordNotificationDetailsResult : BaseResult +{ + public DiscordNotificationDetailsDto? Item { get; set; } +} \ No newline at end of file diff --git a/Newsbot.Collector.Domain/Results/DiscordNotificationResult.cs b/Newsbot.Collector.Domain/Results/DiscordNotificationResult.cs new file mode 100644 index 0000000..9cb27da --- /dev/null +++ b/Newsbot.Collector.Domain/Results/DiscordNotificationResult.cs @@ -0,0 +1,8 @@ +using Newsbot.Collector.Domain.Dto; + +namespace Newsbot.Collector.Domain.Results; + +public class DiscordNotificationResult : BaseResult +{ + public IEnumerable? Items { get; set; } +} \ No newline at end of file From 463cb893e8a6005a39d8214a5674294ae8afc703 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sun, 23 Jul 2023 22:57:54 -0700 Subject: [PATCH 11/13] Logging update --- .../Controllers/v1/IdentityController.cs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Newsbot.Collector.Api/Controllers/v1/IdentityController.cs b/Newsbot.Collector.Api/Controllers/v1/IdentityController.cs index ddfd6e7..b260be5 100644 --- a/Newsbot.Collector.Api/Controllers/v1/IdentityController.cs +++ b/Newsbot.Collector.Api/Controllers/v1/IdentityController.cs @@ -1,10 +1,10 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Newsbot.Collector.Api.Domain; -using Newsbot.Collector.Api.Domain.Requests; -using Newsbot.Collector.Api.Domain.Response; -using Newsbot.Collector.Api.Domain.Results; -using Newsbot.Collector.Api.Services; +using Newsbot.Collector.Domain.Requests; +using Newsbot.Collector.Domain.Response; +using Newsbot.Collector.Domain.Results; +using Newsbot.Collector.Services; namespace Newsbot.Collector.Api.Controllers.v1; @@ -12,11 +12,13 @@ namespace Newsbot.Collector.Api.Controllers.v1; [Route("/api/v1/account")] public class IdentityController : ControllerBase { - private IIdentityService _identityService; + private readonly ILogger _logger; + private readonly IIdentityService _identityService; - public IdentityController(IIdentityService identityService) + public IdentityController(IIdentityService identityService, ILogger logger) { _identityService = identityService; + _logger = logger; } [HttpPost("register")] @@ -81,6 +83,7 @@ public class IdentityController : ControllerBase } catch (Exception ex) { + _logger.LogWarning(ex, "Failed to add role to user"); return new BadRequestResult(); } } From c92d13797f68d4abcddfd54bcc7147b369ef3572 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sun, 23 Jul 2023 22:58:25 -0700 Subject: [PATCH 12/13] minor adjustments --- .../Repositories/DiscordWebhooksTable.cs | 11 +++++++++++ .../Repositories/UserSourceSubscriptionTable.cs | 3 ++- .../Interfaces/IDiscordWebHooksRepository.cs | 1 + 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Newsbot.Collector.Database/Repositories/DiscordWebhooksTable.cs b/Newsbot.Collector.Database/Repositories/DiscordWebhooksTable.cs index c667d23..74b53aa 100644 --- a/Newsbot.Collector.Database/Repositories/DiscordWebhooksTable.cs +++ b/Newsbot.Collector.Database/Repositories/DiscordWebhooksTable.cs @@ -1,5 +1,6 @@ using System.Data; using Dapper; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Newsbot.Collector.Domain.Interfaces; using Newsbot.Collector.Domain.Entities; @@ -60,6 +61,16 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository return res; } + public List ListByUserId(string userId, int page) + { + var query = _context.DiscordWebhooks + .Where(p => p.UserId != null && p.UserId.Equals(userId)) + .Skip(page * 25) + .Take(25) + .ToList(); + return query; + } + public List ListByServer(string server, int limit = 25) { //using var context = new DatabaseContext(_connectionString); diff --git a/Newsbot.Collector.Database/Repositories/UserSourceSubscriptionTable.cs b/Newsbot.Collector.Database/Repositories/UserSourceSubscriptionTable.cs index fcfb314..2feb020 100644 --- a/Newsbot.Collector.Database/Repositories/UserSourceSubscriptionTable.cs +++ b/Newsbot.Collector.Database/Repositories/UserSourceSubscriptionTable.cs @@ -19,7 +19,8 @@ public class UserSourceSubscriptionTable : IUserSourceSubscription public List ListUserSubscriptions(Guid userId) { - var results =_context.UserSourceSubscription.Where(i => i.UserId.Equals(userId)).ToList(); + var results =_context.UserSourceSubscription + .Where(i => i.UserId != null && i.UserId.Equals(userId)).ToList(); return results; } } diff --git a/Newsbot.Collector.Domain/Interfaces/IDiscordWebHooksRepository.cs b/Newsbot.Collector.Domain/Interfaces/IDiscordWebHooksRepository.cs index 2192371..6d32c9d 100644 --- a/Newsbot.Collector.Domain/Interfaces/IDiscordWebHooksRepository.cs +++ b/Newsbot.Collector.Domain/Interfaces/IDiscordWebHooksRepository.cs @@ -10,6 +10,7 @@ public interface IDiscordWebHooksRepository DiscordWebhookEntity GetByUrl(string url); List List(int page, int count = 25); + List ListByUserId(string userId, int page); List ListByServer(string server, int limit); List ListByServerAndChannel(string server, string channel, int limit); From 82dea60126e6355bb32bfccbcb6bef572e2e64c3 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sat, 29 Jul 2023 09:44:15 -0700 Subject: [PATCH 13/13] Updated DiscordWebHooks to handle userId --- .../v1/DiscordNotificationController.cs | 4 +- .../v1/DiscordWebHooksController.cs | 40 +++++++++++++------ .../Repositories/DiscordWebhooksTable.cs | 25 ++++++++---- .../Interfaces/IDiscordWebHooksRepository.cs | 8 ++-- .../Requests/NewDiscordWebhookRequest.cs | 8 ++++ .../Results/DiscordWebhookResult.cs | 8 ++++ 6 files changed, 69 insertions(+), 24 deletions(-) create mode 100644 Newsbot.Collector.Domain/Requests/NewDiscordWebhookRequest.cs create mode 100644 Newsbot.Collector.Domain/Results/DiscordWebhookResult.cs diff --git a/Newsbot.Collector.Api/Controllers/v1/DiscordNotificationController.cs b/Newsbot.Collector.Api/Controllers/v1/DiscordNotificationController.cs index c66951d..2d169ff 100644 --- a/Newsbot.Collector.Api/Controllers/v1/DiscordNotificationController.cs +++ b/Newsbot.Collector.Api/Controllers/v1/DiscordNotificationController.cs @@ -91,7 +91,7 @@ public class DiscordNotificationController : ControllerBase } var sub = _discordNotification.GetById(userId, id); - var webhook = _discord.GetById(sub.DiscordWebHookId); + var webhook = _discord.GetById(userId, sub.DiscordWebHookId); var source = _sources.GetById(sub.SourceId); return new OkObjectResult(new DiscordNotificationDetailsResult @@ -206,7 +206,7 @@ public class DiscordNotificationController : ControllerBase var exists = _discordNotification.GetByWebhookAndSource(userId, request.DiscordId, request.SourceId); if (exists.Id != Guid.Empty) return DiscordNotificationDto.Convert(exists); - var discord = _discord.GetById(request.DiscordId); + var discord = _discord.GetById(userId, request.DiscordId); if (discord.Id == Guid.Empty) return new BadRequestObjectResult(new DiscordNotificationResult { IsSuccessful = false, diff --git a/Newsbot.Collector.Api/Controllers/v1/DiscordWebHooksController.cs b/Newsbot.Collector.Api/Controllers/v1/DiscordWebHooksController.cs index a0d745d..28a1ebc 100644 --- a/Newsbot.Collector.Api/Controllers/v1/DiscordWebHooksController.cs +++ b/Newsbot.Collector.Api/Controllers/v1/DiscordWebHooksController.cs @@ -7,6 +7,8 @@ using Newsbot.Collector.Domain.Dto; using Newsbot.Collector.Domain.Entities; using Newsbot.Collector.Domain.Interfaces; using Newsbot.Collector.Domain.Models; +using Newsbot.Collector.Domain.Requests; +using Newsbot.Collector.Domain.Results; namespace Newsbot.Collector.Api.Controllers.v1; @@ -45,30 +47,44 @@ public class DiscordWebHookController : ControllerBase } [HttpPost(Name = "New")] - public DiscordWebHookDto New(string url, string server, string channel) + public ActionResult New([FromBody] NewDiscordWebhookRequest request) { - var exists = _webhooks.GetByUrl(url); + var userId = HttpContext.GetUserId(); + + var exists = _webhooks.GetByUrl(request.Url ?? ""); if (exists.Id != Guid.Empty) { - return DiscordWebHookDto.Convert(exists); + return new BadRequestObjectResult(new DiscordWebhookResult + { + IsSuccessful = true, + Items = new List { DiscordWebHookDto.Convert(exists) } + }); } var res = _webhooks.New(new DiscordWebhookEntity { - Url = url, - Server = server, - Channel = channel, + UserId = userId, + Url = request.Url ?? "", + Server = request.Server ?? "", + Channel = request.Channel ?? "", Enabled = true, }); - return DiscordWebHookDto.Convert(res); + return new OkObjectResult(new DiscordWebhookResult + { + IsSuccessful = true, + Items = new List + { + DiscordWebHookDto.Convert(res) + } + }); } [HttpGet("by/serverAndChannel")] public IEnumerable GetByServerAndChannel(string server, string channel) { var items = new List(); - var res = _webhooks.ListByServerAndChannel(server, channel, 25); + var res = _webhooks.ListByServerAndChannel(HttpContext.GetUserId(), server, channel, 25); foreach (var item in res) { @@ -78,21 +94,21 @@ public class DiscordWebHookController : ControllerBase } [HttpGet("{id}")] - public DiscordWebHookDto GetById(Guid id) + public DiscordWebHookDto GetById(string userId, Guid id) { - var res = _webhooks.GetById(id); + var res = _webhooks.GetById(userId, id); return DiscordWebHookDto.Convert(res); } [HttpPost("{id}/disable")] public void DisableById(Guid id) { - _webhooks.Disable(id); + _webhooks.Disable(HttpContext.GetUserId(), id); } [HttpPost("{id}/enable")] public void EnableById(Guid id) { - _webhooks.Enable(id); + _webhooks.Enable(HttpContext.GetUserId(), id); } } \ No newline at end of file diff --git a/Newsbot.Collector.Database/Repositories/DiscordWebhooksTable.cs b/Newsbot.Collector.Database/Repositories/DiscordWebhooksTable.cs index 74b53aa..cf38100 100644 --- a/Newsbot.Collector.Database/Repositories/DiscordWebhooksTable.cs +++ b/Newsbot.Collector.Database/Repositories/DiscordWebhooksTable.cs @@ -34,10 +34,20 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository return model; } - public DiscordWebhookEntity GetById(Guid id) + public DiscordWebhookEntity GetById(string userId, Guid id) { //using var context = new DatabaseContext(_connectionString); - var res = _context.DiscordWebhooks.FirstOrDefault(d => d.Id.Equals(id)); + var res = _context.DiscordWebhooks + .Where(i => i.UserId != null && i.UserId.Equals(userId)) + .FirstOrDefault(d => d.Id.Equals(id)); + res ??= new DiscordWebhookEntity(); + return res; + } + + public DiscordWebhookEntity GetById(Guid id) + { + var res = _context.DiscordWebhooks + .FirstOrDefault(d => d.Id.Equals(id)); res ??= new DiscordWebhookEntity(); return res; } @@ -82,10 +92,11 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository return res; } - public List ListByServerAndChannel(string server, string channel, int limit = 25) + public List ListByServerAndChannel(string userId, string server, string channel, int limit = 25) { //using var context = new DatabaseContext(_connectionString); var res = _context.DiscordWebhooks + .Where(i => i.UserId != null && i.UserId.Equals(userId)) .Where(s => s.Server.Equals(server)) .Where(c => c.Channel.Equals(channel)) .Take(limit) @@ -94,9 +105,9 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository return res; } - public int Disable(Guid id) + public int Disable(string userId, Guid id) { - var res = GetById(id); + var res = GetById(userId, id); //using var context = new DatabaseContext(_connectionString); res.Enabled = true; @@ -114,9 +125,9 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository } } - public int Enable(Guid id) + public int Enable(string userId, Guid id) { - var res = GetById(id); + var res = GetById(userId, id); //using var context = new DatabaseContext(_connectionString); res.Enabled = false; diff --git a/Newsbot.Collector.Domain/Interfaces/IDiscordWebHooksRepository.cs b/Newsbot.Collector.Domain/Interfaces/IDiscordWebHooksRepository.cs index 6d32c9d..39789fc 100644 --- a/Newsbot.Collector.Domain/Interfaces/IDiscordWebHooksRepository.cs +++ b/Newsbot.Collector.Domain/Interfaces/IDiscordWebHooksRepository.cs @@ -6,14 +6,16 @@ public interface IDiscordWebHooksRepository { DiscordWebhookEntity New(DiscordWebhookEntity model); + DiscordWebhookEntity GetById(string userId, Guid id); DiscordWebhookEntity GetById(Guid id); + DiscordWebhookEntity GetByUrl(string url); List List(int page, int count = 25); List ListByUserId(string userId, int page); List ListByServer(string server, int limit); - List ListByServerAndChannel(string server, string channel, int limit); + List ListByServerAndChannel(string userId, string server, string channel, int limit); - int Disable(Guid id); - int Enable(Guid id); + int Disable(string userId, Guid id); + int Enable(string userId, Guid id); } \ No newline at end of file diff --git a/Newsbot.Collector.Domain/Requests/NewDiscordWebhookRequest.cs b/Newsbot.Collector.Domain/Requests/NewDiscordWebhookRequest.cs new file mode 100644 index 0000000..37a71c1 --- /dev/null +++ b/Newsbot.Collector.Domain/Requests/NewDiscordWebhookRequest.cs @@ -0,0 +1,8 @@ +namespace Newsbot.Collector.Domain.Requests; + +public class NewDiscordWebhookRequest +{ + public string? Url { get; set; } + public string? Server { get; set; } + public string? Channel { get; set; } +} \ No newline at end of file diff --git a/Newsbot.Collector.Domain/Results/DiscordWebhookResult.cs b/Newsbot.Collector.Domain/Results/DiscordWebhookResult.cs new file mode 100644 index 0000000..7173168 --- /dev/null +++ b/Newsbot.Collector.Domain/Results/DiscordWebhookResult.cs @@ -0,0 +1,8 @@ +using Newsbot.Collector.Domain.Dto; + +namespace Newsbot.Collector.Domain.Results; + +public class DiscordWebhookResult : BaseResult +{ + public List? Items { get; set; } +} \ No newline at end of file