-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControllerExtensions.cs
More file actions
157 lines (144 loc) · 6.94 KB
/
Copy pathControllerExtensions.cs
File metadata and controls
157 lines (144 loc) · 6.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using Application.Interfaces.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
namespace Api;
public static class ControllerExtensions
{
extension(WebApplication app)
{
public void AddApiController()
{
app.MapGet("/api/statistics-all",
async (IStatisticsService statisticsService) =>
await statisticsService.GetAllSongsAsync())
.WithName("GetStatisticsAll");
app.MapGet("/api/users",
async (IUserService userService) => await userService.GetAllUsersAsync())
.WithName("GetAllUsers");
app.MapGet("/api/radio-sources",
async (IRadioSourceService radioSourceService, CancellationToken cancellationToken) =>
await radioSourceService.GetAllRadioSourcesAsync(cancellationToken))
.RequireAuthorization()
.WithName("GetAllRadioSources");
app.MapPut("/api/radio-sources/{id:guid}",
async (IRadioSourceService radioSourceService, Guid id, [FromBody] UpdateRadioSourceRequest request,
CancellationToken cancellationToken) =>
{
try
{
await radioSourceService.UpdateRadioSourceUrlAsync(id, request.Name, request.NewSourceUrl,
request.IsActive, cancellationToken);
return Results.NoContent();
}
catch (KeyNotFoundException)
{
return Results.NotFound();
}
catch (ArgumentException ex)
{
return Results.BadRequest(new { error = ex.Message });
}
})
.RequireAuthorization()
.WithName("UpdateRadioSourceUrl");
app.MapGet("/api/radio-sources/{id:guid}",
async (IRadioSourceService radioSourceService, Guid id, CancellationToken cancellationToken) =>
{
try
{
var radioSource = await radioSourceService.GetRadioSourceByIdAsync(id, cancellationToken);
return Results.Ok(radioSource);
}
catch (KeyNotFoundException)
{
return Results.NotFound();
}
})
.RequireAuthorization()
.WithName("GetRadioSourceById");
app.MapPost("/api/radio-sources/add",
async (IRadioSourceService radioSourceService, [FromBody] AddRadioSourceRequest request,
CancellationToken cancellationToken) =>
{
try
{
var id = await radioSourceService.AddRadioSourceAsync(request.Name, request.SourceUrl,
cancellationToken);
var result = await radioSourceService.GetRadioSourceByIdAsync(id, cancellationToken);
return Results.Created($"/api/radio-sources/{id}", result);
}
catch (InvalidOperationException ex)
{
return Results.BadRequest(new { error = ex.Message });
}
catch (Exception)
{
return Results.Problem("An unexpected error occurred.");
}
})
.RequireAuthorization()
.WithName("AddRadioSource");
app.MapDelete("/api/radio-sources/{id:guid}",
async (IRadioSourceService radioSourceService, Guid id, CancellationToken cancellationToken) =>
{
try
{
await radioSourceService.DeleteRadioSourceAsync(id, cancellationToken);
return Results.NoContent();
}
catch (KeyNotFoundException)
{
return Results.NotFound();
}
})
.RequireAuthorization()
.WithName("DeleteRadioSource");
app.MapPost("/api/login",
async (IUserService userService, IConfiguration configuration, IJwtTokenGenerator tokenGenerator,
[FromBody] LoginRequest request) =>
{
try
{
var user = await userService.GetUserByUsernameAsync(request.Username);
// Note: In a real application, you would hash the password and compare it securely.
var password = configuration.GetValue<string>("JwtSettings:InternalPassword");
// Fail closed if the internal password is not configured or the request omits one.
if (string.IsNullOrEmpty(password) || user == null || password != request.Password)
{
throw new UnauthorizedAccessException("Invalid username or password.");
}
var token = tokenGenerator.GenerateToken(request);
return Results.Ok(new { token });
}
catch (UnauthorizedAccessException)
{
return Results.Unauthorized();
}
catch (Exception)
{
return Results.Problem("An unexpected error occurred.");
}
})
.AllowAnonymous()
.WithName("Login");
app.MapGet("/api/auth/validate-token", (HttpContext context) =>
{
// Check if user is authenticated (JWT middleware already validated the token)
if (context.User.Identity?.IsAuthenticated == true)
{
return Results.Ok(new
{
valid = true,
username = context.User.Identity.Name,
expires = context.User.FindFirst("exp")?.Value
});
}
return Results.Unauthorized();
})
.RequireAuthorization()
.WithName("ValidateToken");
}
}
}