From 578197cb75f2dfd31c041bea960f3add4d5a714e Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Mon, 13 Mar 2023 21:57:32 -0700 Subject: [PATCH] Added better error checking on the new route requests --- .../Controllers/DiscordWebHooksController.cs | 2 +- .../Controllers/SourcesController.cs | 37 +++++-------------- .../Controllers/SubscriptionsController.cs | 2 +- 3 files changed, 12 insertions(+), 29 deletions(-) diff --git a/Newsbot.Collector.Api/Controllers/DiscordWebHooksController.cs b/Newsbot.Collector.Api/Controllers/DiscordWebHooksController.cs index 5ba1d9c..5706972 100644 --- a/Newsbot.Collector.Api/Controllers/DiscordWebHooksController.cs +++ b/Newsbot.Collector.Api/Controllers/DiscordWebHooksController.cs @@ -40,7 +40,7 @@ public class DiscordWebHookController : ControllerBase { var exists = _webhooks.GetByUrl(url); // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract - if (exists is not null) + if (exists.ID != Guid.Empty) { return DiscordWebHookDto.Convert(exists); } diff --git a/Newsbot.Collector.Api/Controllers/SourcesController.cs b/Newsbot.Collector.Api/Controllers/SourcesController.cs index b09cdc7..15bec9d 100644 --- a/Newsbot.Collector.Api/Controllers/SourcesController.cs +++ b/Newsbot.Collector.Api/Controllers/SourcesController.cs @@ -13,14 +13,15 @@ namespace Newsbot.Collector.Api.Controllers; public class SourcesController : ControllerBase { private readonly ILogger _logger; - private readonly ConnectionStrings _settings; + + //private readonly ConnectionStrings _settings; private readonly ISourcesRepository _sources; public SourcesController(ILogger logger, IOptions settings) { _logger = logger; - _settings = settings.Value; - _sources = new SourcesTable(_settings.Database); + //_settings = settings.Value; + _sources = new SourcesTable(settings.Value.Database); } [HttpGet(Name = "GetSources")] @@ -28,10 +29,7 @@ public class SourcesController : ControllerBase { var res = new List(); var temp = _sources.List(page, 25); - foreach (var item in temp) - { - res.Add(SourceDto.Convert(item)); - } + foreach (var item in temp) res.Add(SourceDto.Convert(item)); return res; } @@ -40,10 +38,7 @@ public class SourcesController : ControllerBase { var res = new List(); var temp = _sources.ListByType(type); - foreach (var item in temp) - { - res.Add(SourceDto.Convert(item)); - } + foreach (var item in temp) res.Add(SourceDto.Convert(item)); return res; } @@ -51,10 +46,7 @@ public class SourcesController : ControllerBase public SourceDto NewReddit(string name, string url) { var res = _sources.GetByNameAndType(name, SourceTypes.Reddit); - if (res.ID != Guid.Empty) - { - return SourceDto.Convert(res); - } + if (res.ID != Guid.Empty) return SourceDto.Convert(res); var item = _sources.New(new SourceModel { @@ -73,10 +65,7 @@ public class SourcesController : ControllerBase public SourceDto NewRss(string name, string url) { var res = _sources.GetByNameAndType(name, SourceTypes.Rss); - if (res.ID != Guid.Empty) - { - return SourceDto.Convert(res); - } + if (res.ID != Guid.Empty) return SourceDto.Convert(res); var m = new SourceModel { @@ -96,10 +85,7 @@ public class SourcesController : ControllerBase public SourceDto NewYoutube(string name, string url) { var res = _sources.GetByNameAndType(name, SourceTypes.YouTube); - if (res.ID != Guid.Empty) - { - return SourceDto.Convert(res); - } + if (res.ID != Guid.Empty) return SourceDto.Convert(res); var item = _sources.New(new SourceModel { @@ -119,10 +105,7 @@ public class SourcesController : ControllerBase public SourceDto NewTwitch(string name) { var res = _sources.GetByNameAndType(name, SourceTypes.Twitch); - if (res.ID != Guid.Empty) - { - return SourceDto.Convert(res); - } + if (res.ID != Guid.Empty) return SourceDto.Convert(res); var item = _sources.New(new SourceModel { diff --git a/Newsbot.Collector.Api/Controllers/SubscriptionsController.cs b/Newsbot.Collector.Api/Controllers/SubscriptionsController.cs index b490e4e..46a969c 100644 --- a/Newsbot.Collector.Api/Controllers/SubscriptionsController.cs +++ b/Newsbot.Collector.Api/Controllers/SubscriptionsController.cs @@ -86,7 +86,7 @@ public class SubscriptionsController : ControllerBase { var exists = _subscription.GetByWebhookAndSource(discordId, sourceId); // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract - if (exists is not null) + if (exists.ID != Guid.Empty) { return SubscriptionDto.Convert(exists); }