-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Major refactoring work across multiple services and controllers. Adde…
…d middleware for sql user data validation
- Loading branch information
Showing
11 changed files
with
217 additions
and
170 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
handlenett-backend/web-api/HandlenettAPI/Configurations/SlackSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace HandlenettAPI.Configurations | ||
{ | ||
public class SlackSettings | ||
{ | ||
[Required] | ||
public required string SlackBotUserOAuthToken { get; set; } | ||
[Required] | ||
public required string ContainerNameUserImages { get; set; } | ||
[Required] | ||
public required string BlobStorageUri { get; set; } | ||
public string BlobStoragePathIncludingContainer => BlobStorageUri + ContainerNameUserImages + "/"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 0 additions & 4 deletions
4
handlenett-backend/web-api/HandlenettAPI/Controllers/ItemController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 11 additions & 31 deletions
42
handlenett-backend/web-api/HandlenettAPI/Controllers/UserController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,34 @@ | ||
using Azure.Identity; | ||
using HandlenettAPI.DTO; | ||
using HandlenettAPI.Models; | ||
using HandlenettAPI.DTO; | ||
using HandlenettAPI.Services; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Graph; | ||
|
||
namespace HandlenettAPI.Controllers | ||
{ | ||
[Authorize] | ||
[ApiController] | ||
[Route("[controller]")] | ||
public class UserController : BaseController | ||
public class UserController : ControllerBase | ||
{ | ||
private readonly UserService _userService; | ||
private readonly UserInitializationService _userInitializationService; | ||
|
||
public UserController(ILogger<UserController> logger, GraphServiceClient graphServiceClient, IConfiguration config, SlackService slackService) | ||
: base(logger, graphServiceClient, config, slackService) | ||
public UserController(UserService userService, UserInitializationService userInitializationService) | ||
{ | ||
_userService = userService; | ||
_userInitializationService = userInitializationService; | ||
} | ||
|
||
[HttpGet(Name = "GetUsers")] | ||
public async Task<List<UserDTO>> Get() | ||
public async Task<ActionResult<List<UserDTO>>> Get() | ||
{ | ||
try | ||
{ | ||
await InitializeAsync(); | ||
var dbService = new UserService(_config); | ||
return dbService.GetUsers(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Failed to get users"); | ||
throw new Exception("Failed to get users"); | ||
} | ||
return Ok(_userService.GetUsers()); | ||
} | ||
|
||
[HttpGet("{id}", Name = "GetUser")] | ||
public async Task<UserDTO> Get(Guid id) | ||
public async Task<ActionResult<UserDTO>> Get(Guid id) | ||
{ | ||
try | ||
{ | ||
await InitializeAsync(); | ||
var dbService = new UserService(_config); | ||
return dbService.GetUser(id); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Failed to get user"); | ||
throw new Exception("Failed to get user"); | ||
} | ||
return Ok(_userService.GetUser(id)); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
handlenett-backend/web-api/HandlenettAPI/Middleware/UserInitializationMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using HandlenettAPI.Services; | ||
|
||
namespace HandlenettAPI.Middleware | ||
{ | ||
public class UserInitializationMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
|
||
public UserInitializationMiddleware(RequestDelegate next) | ||
{ | ||
_next = next; | ||
} | ||
|
||
public async Task InvokeAsync(HttpContext context, IServiceProvider serviceProvider) | ||
{ | ||
// Create a scope for resolving scoped services | ||
using (var scope = serviceProvider.CreateScope()) | ||
{ | ||
// Resolve the scoped UserInitializationService | ||
var userInitializationService = scope.ServiceProvider.GetRequiredService<UserInitializationService>(); | ||
|
||
// Ensure the user exists using the service | ||
await userInitializationService.EnsureUserExistsAsync(); | ||
} | ||
|
||
// Call the next middleware in the pipeline | ||
await _next(context); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.