From 4b5597b3fe6e02f1655e6a731e83bdcdf1017d63 Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Mon, 14 Nov 2022 13:56:56 +0700 Subject: refactor: Api files always returns Response --- .../src/Data/Enums/PasswordResetRequestStatus.cs | 6 +++ .../CreateResetRequestRoute.cs | 32 +++--------- .../IsResetRequestValidRoute.cs | 6 --- .../src/Utilities/SwaggerGenOptionsExtensions.cs | 58 +++++++++++----------- 4 files changed, 43 insertions(+), 59 deletions(-) create mode 100644 code/api/src/Data/Enums/PasswordResetRequestStatus.cs (limited to 'code/api') diff --git a/code/api/src/Data/Enums/PasswordResetRequestStatus.cs b/code/api/src/Data/Enums/PasswordResetRequestStatus.cs new file mode 100644 index 0000000..5629e6f --- /dev/null +++ b/code/api/src/Data/Enums/PasswordResetRequestStatus.cs @@ -0,0 +1,6 @@ +namespace IOL.GreatOffice.Api.Data.Enums; + +public enum PasswordResetRequestStatus +{ + +} \ No newline at end of file diff --git a/code/api/src/Endpoints/Internal/PasswordResetRequests/CreateResetRequestRoute.cs b/code/api/src/Endpoints/Internal/PasswordResetRequests/CreateResetRequestRoute.cs index 49df35b..edf825e 100644 --- a/code/api/src/Endpoints/Internal/PasswordResetRequests/CreateResetRequestRoute.cs +++ b/code/api/src/Endpoints/Internal/PasswordResetRequests/CreateResetRequestRoute.cs @@ -1,6 +1,6 @@ namespace IOL.GreatOffice.Api.Endpoints.Internal.PasswordResetRequests; -public class CreateResetRequestRoute : RouteBaseAsync.WithRequest.WithActionResult +public class CreateResetRequestRoute : RouteBaseAsync.WithRequest.WithActionResult { private readonly ILogger _logger; private readonly PasswordResetService _passwordResetService; @@ -12,34 +12,18 @@ public class CreateResetRequestRoute : RouteBaseAsync.WithRequest HandleAsync(Payload request, CancellationToken cancellationToken = default) { - if (!request.Username.IsValidEmailAddress()) { - _logger.LogInformation("Username is invalid, not doing request for password change"); - return KnownProblem("Invalid email address", request.Username + " looks like an invalid email address"); - } - + public override async Task HandleAsync([FromQuery(Name = "for_user")] string username, CancellationToken cancellationToken = default) { var tz = GetRequestTimeZone(_logger); _logger.LogInformation("Creating forgot password request with local date time: " + tz.LocalDateTime.ToString("u")); - try { - var user = _database.Users.SingleOrDefault(c => c.Username.Equals(request.Username)); - if (user != default) { - await _passwordResetService.AddRequestAsync(user, tz.TimeZoneInfo, cancellationToken); - return Ok(); - } + var user = _database.Users.FirstOrDefault(c => c.Username.Equals(username)); + // Don't inform the caller that the user does not exist. + if (user == default) return Ok(); + + await _passwordResetService.AddRequestAsync(user, tz.TimeZoneInfo, cancellationToken); - _logger.LogInformation("User was not found, not doing request for password change"); - return Ok(); - } catch (Exception e) { - _logger.LogError(e, "_/password-reset-request/create threw an exception"); - return Ok(); - } + return Ok(); } } \ No newline at end of file diff --git a/code/api/src/Endpoints/Internal/PasswordResetRequests/IsResetRequestValidRoute.cs b/code/api/src/Endpoints/Internal/PasswordResetRequests/IsResetRequestValidRoute.cs index 687cef6..1ad0f47 100644 --- a/code/api/src/Endpoints/Internal/PasswordResetRequests/IsResetRequestValidRoute.cs +++ b/code/api/src/Endpoints/Internal/PasswordResetRequests/IsResetRequestValidRoute.cs @@ -8,12 +8,6 @@ public class IsResetRequestValidRoute : RouteBaseAsync.WithRequest.WithAct _passwordResetService = passwordResetService; } - /// - /// Check if a given password reset request is still valid. - /// - /// - /// - /// [AllowAnonymous] [HttpGet("~/_/password-reset-request/is-valid")] public override async Task HandleAsync(Guid id, CancellationToken cancellationToken = default) { diff --git a/code/api/src/Utilities/SwaggerGenOptionsExtensions.cs b/code/api/src/Utilities/SwaggerGenOptionsExtensions.cs index 9b70194..a3d9036 100644 --- a/code/api/src/Utilities/SwaggerGenOptionsExtensions.cs +++ b/code/api/src/Utilities/SwaggerGenOptionsExtensions.cs @@ -8,36 +8,36 @@ namespace IOL.GreatOffice.Api.Utilities; public static class SwaggerGenOptionsExtensions { - /// - /// Updates Swagger document to support ApiEndpoints.

- /// For controllers inherited from :
- /// - Replaces action Tag with [namespace]
- ///
- public static void UseApiEndpoints(this SwaggerGenOptions options) { - options.TagActionsBy(EndpointNamespaceOrDefault); - } + /// + /// Updates Swagger document to support ApiEndpoints.

+ /// For controllers inherited from :
+ /// - Replaces action Tag with [namespace]
+ ///
+ public static void UseApiEndpoints(this SwaggerGenOptions options) { + options.TagActionsBy(EndpointNamespaceOrDefault); + } - private static IList EndpointNamespaceOrDefault(ApiDescription api) { - if (api.ActionDescriptor is not ControllerActionDescriptor actionDescriptor) { - throw new InvalidOperationException($"Unable to determine tag for endpoint: {api.ActionDescriptor.DisplayName}"); - } + private static IList EndpointNamespaceOrDefault(ApiDescription api) { + if (api.ActionDescriptor is not ControllerActionDescriptor actionDescriptor) { + throw new InvalidOperationException($"Unable to determine tag for endpoint: {api.ActionDescriptor.DisplayName}"); + } - if (actionDescriptor.ControllerTypeInfo.GetBaseTypesAndThis().Any(t => t == typeof(EndpointBase))) { - return new[] { - actionDescriptor.ControllerTypeInfo.Namespace?.Split('.').Last() - }; - } + if (actionDescriptor.ControllerTypeInfo.GetBaseTypesAndThis().Any(t => t == typeof(EndpointBase))) { + return new[] { + actionDescriptor.ControllerTypeInfo.Namespace?.Split('.').Last() + }; + } - return new[] { - actionDescriptor.ControllerName - }; - } + return new[] { + actionDescriptor.ControllerName + }; + } - public static IEnumerable GetBaseTypesAndThis(this Type type) { - var current = type; - while (current != null) { - yield return current; - current = current.BaseType; - } - } -} + private static IEnumerable GetBaseTypesAndThis(this Type type) { + var current = type; + while (current != null) { + yield return current; + current = current.BaseType; + } + } +} \ No newline at end of file -- cgit v1.3