@@ -104,26 +104,26 @@ public ActionResult<ApiResponse<string>> HealthCheck()
104
104
/// <param name="pageSize">Page size</param>
105
105
/// <param name="cancellationToken">Cancellation token</param>
106
106
/// <returns>Paginated users</returns>
107
- async Task < ApiResult < CollectionModel < User > > > IUserApiV1 . GetUsersAsync ( int page , int pageSize , CancellationToken cancellationToken )
107
+ Task < ApiResult < CollectionModel < User > > > IUserApiV1 . GetUsersAsync ( int page , int pageSize , CancellationToken cancellationToken )
108
108
{
109
109
// Bearer token validation
110
110
if ( ! Request . Headers . ContainsKey ( "Authorization" ) )
111
111
{
112
- return new ApiResponse < CollectionModel < User > > (
112
+ return Task . FromResult ( new ApiResponse < CollectionModel < User > > (
113
113
new ApiError ( ApiErrorConstants . ErrorCodes . Unauthorized ,
114
114
ApiErrorConstants . ErrorMessages . AuthorizationHeaderRequired ,
115
115
ApiErrorConstants . ErrorDetails . BearerTokenMissing )
116
- ) . ToApiResult ( System . Net . HttpStatusCode . Unauthorized ) ;
116
+ ) . ToApiResult ( System . Net . HttpStatusCode . Unauthorized ) ) ;
117
117
}
118
118
119
119
var authHeader = Request . Headers [ "Authorization" ] . ToString ( ) ;
120
120
if ( ! authHeader . StartsWith ( "Bearer " ) || authHeader != "Bearer user-test-token" )
121
121
{
122
- return new ApiResponse < CollectionModel < User > > (
122
+ return Task . FromResult ( new ApiResponse < CollectionModel < User > > (
123
123
new ApiError ( ApiErrorConstants . ErrorCodes . Unauthorized ,
124
124
ApiErrorConstants . ErrorMessages . InvalidOrMissingAuthentication ,
125
125
ApiErrorConstants . ErrorDetails . InvalidBearerToken )
126
- ) . ToApiResult ( System . Net . HttpStatusCode . Unauthorized ) ;
126
+ ) . ToApiResult ( System . Net . HttpStatusCode . Unauthorized ) ) ;
127
127
}
128
128
129
129
_logger . LogInformation ( "Getting users page {Page} with size {PageSize}" , page , pageSize ) ;
@@ -141,7 +141,7 @@ async Task<ApiResult<CollectionModel<User>>> IUserApiV1.GetUsersAsync(int page,
141
141
FilteredCount = totalCount // For simplicity, no filtering in this demo
142
142
} ;
143
143
144
- return new ApiResponse < CollectionModel < User > > ( collection ) . ToApiResult ( ) ;
144
+ return Task . FromResult ( new ApiResponse < CollectionModel < User > > ( collection ) . ToApiResult ( ) ) ;
145
145
}
146
146
147
147
/// <summary>
@@ -150,41 +150,41 @@ async Task<ApiResult<CollectionModel<User>>> IUserApiV1.GetUsersAsync(int page,
150
150
/// <param name="userId">User ID</param>
151
151
/// <param name="cancellationToken">Cancellation token</param>
152
152
/// <returns>User</returns>
153
- async Task < ApiResult < User > > IUserApiV1 . GetUserByIdAsync ( int userId , CancellationToken cancellationToken )
153
+ Task < ApiResult < User > > IUserApiV1 . GetUserByIdAsync ( int userId , CancellationToken cancellationToken )
154
154
{
155
155
// Bearer token validation
156
156
if ( ! Request . Headers . ContainsKey ( "Authorization" ) )
157
157
{
158
- return new ApiResponse < User > (
158
+ return Task . FromResult ( new ApiResponse < User > (
159
159
new ApiError ( ApiErrorConstants . ErrorCodes . Unauthorized ,
160
160
ApiErrorConstants . ErrorMessages . AuthorizationHeaderRequired ,
161
161
ApiErrorConstants . ErrorDetails . BearerTokenMissing )
162
- ) . ToApiResult ( System . Net . HttpStatusCode . Unauthorized ) ;
162
+ ) . ToApiResult ( System . Net . HttpStatusCode . Unauthorized ) ) ;
163
163
}
164
164
165
165
var authHeader = Request . Headers [ "Authorization" ] . ToString ( ) ;
166
166
if ( ! authHeader . StartsWith ( "Bearer " ) || authHeader != "Bearer user-test-token" )
167
167
{
168
- return new ApiResponse < User > (
168
+ return Task . FromResult ( new ApiResponse < User > (
169
169
new ApiError ( ApiErrorConstants . ErrorCodes . Unauthorized ,
170
170
ApiErrorConstants . ErrorMessages . InvalidOrMissingAuthentication ,
171
171
ApiErrorConstants . ErrorDetails . InvalidBearerToken )
172
- ) . ToApiResult ( System . Net . HttpStatusCode . Unauthorized ) ;
172
+ ) . ToApiResult ( System . Net . HttpStatusCode . Unauthorized ) ) ;
173
173
}
174
174
175
175
_logger . LogInformation ( "Getting user {UserId}" , userId ) ;
176
176
177
177
var user = _users . FirstOrDefault ( u => u . Id == userId ) ;
178
178
if ( user == null )
179
179
{
180
- return new ApiResponse < User > (
180
+ return Task . FromResult ( new ApiResponse < User > (
181
181
new ApiError ( ApiErrorConstants . ErrorCodes . NotFound ,
182
182
string . Format ( ApiErrorConstants . ErrorMessages . ResourceNotFound , "User" , userId ) ,
183
183
string . Format ( ApiErrorConstants . ErrorDetails . ResourceDoesNotExist , "user" ) )
184
- ) . ToApiResult ( System . Net . HttpStatusCode . NotFound ) ;
184
+ ) . ToApiResult ( System . Net . HttpStatusCode . NotFound ) ) ;
185
185
}
186
186
187
- return new ApiResponse < User > ( user ) . ToApiResult ( ) ;
187
+ return Task . FromResult ( new ApiResponse < User > ( user ) . ToApiResult ( ) ) ;
188
188
}
189
189
190
190
/// <summary>
@@ -193,38 +193,38 @@ async Task<ApiResult<User>> IUserApiV1.GetUserByIdAsync(int userId, Cancellation
193
193
/// <param name="user">User to create</param>
194
194
/// <param name="cancellationToken">Cancellation token</param>
195
195
/// <returns>Created user</returns>
196
- async Task < ApiResult < User > > IUserApiV1 . CreateUserAsync ( User user , CancellationToken cancellationToken )
196
+ Task < ApiResult < User > > IUserApiV1 . CreateUserAsync ( User user , CancellationToken cancellationToken )
197
197
{
198
198
// Bearer token validation
199
199
if ( ! Request . Headers . ContainsKey ( "Authorization" ) )
200
200
{
201
- return new ApiResponse < User > (
201
+ return Task . FromResult ( new ApiResponse < User > (
202
202
new ApiError ( ApiErrorConstants . ErrorCodes . Unauthorized ,
203
203
ApiErrorConstants . ErrorMessages . AuthorizationHeaderRequired ,
204
204
ApiErrorConstants . ErrorDetails . BearerTokenMissing )
205
- ) . ToApiResult ( System . Net . HttpStatusCode . Unauthorized ) ;
205
+ ) . ToApiResult ( System . Net . HttpStatusCode . Unauthorized ) ) ;
206
206
}
207
207
208
208
var authHeader = Request . Headers [ "Authorization" ] . ToString ( ) ;
209
209
if ( ! authHeader . StartsWith ( "Bearer " ) || authHeader != "Bearer user-test-token" )
210
210
{
211
- return new ApiResponse < User > (
211
+ return Task . FromResult ( new ApiResponse < User > (
212
212
new ApiError ( ApiErrorConstants . ErrorCodes . Unauthorized ,
213
213
ApiErrorConstants . ErrorMessages . InvalidOrMissingAuthentication ,
214
214
ApiErrorConstants . ErrorDetails . InvalidBearerToken )
215
- ) . ToApiResult ( System . Net . HttpStatusCode . Unauthorized ) ;
215
+ ) . ToApiResult ( System . Net . HttpStatusCode . Unauthorized ) ) ;
216
216
}
217
217
218
218
_logger . LogInformation ( "Creating user {Username}" , user . Username ) ;
219
219
220
220
// Check if username already exists
221
221
if ( _users . Any ( u => u . Username == user . Username ) )
222
222
{
223
- return new ApiResponse < User > (
223
+ return Task . FromResult ( new ApiResponse < User > (
224
224
new ApiError ( ApiErrorConstants . ErrorCodes . ResourceExists ,
225
225
string . Format ( ApiErrorConstants . ErrorMessages . ResourceAlreadyExists , "User" , "username" , user . Username ) ,
226
226
string . Format ( ApiErrorConstants . ErrorDetails . ResourceAlreadyExistsDetail , "user" , "username" ) )
227
- ) . ToApiResult ( System . Net . HttpStatusCode . BadRequest ) ;
227
+ ) . ToApiResult ( System . Net . HttpStatusCode . BadRequest ) ) ;
228
228
}
229
229
230
230
// Set the ID and creation timestamp
@@ -233,7 +233,7 @@ async Task<ApiResult<User>> IUserApiV1.CreateUserAsync(User user, CancellationTo
233
233
234
234
_users . Add ( user ) ;
235
235
236
- return new ApiResponse < User > ( user ) . ToApiResult ( System . Net . HttpStatusCode . Created ) ;
236
+ return Task . FromResult ( new ApiResponse < User > ( user ) . ToApiResult ( System . Net . HttpStatusCode . Created ) ) ;
237
237
}
238
238
239
239
/// <summary>
@@ -242,42 +242,42 @@ async Task<ApiResult<User>> IUserApiV1.CreateUserAsync(User user, CancellationTo
242
242
/// <param name="userId">User ID to delete</param>
243
243
/// <param name="cancellationToken">Cancellation token</param>
244
244
/// <returns>Deletion result</returns>
245
- async Task < ApiResult < string > > IUserApiV1 . DeleteUserAsync ( int userId , CancellationToken cancellationToken )
245
+ Task < ApiResult < string > > IUserApiV1 . DeleteUserAsync ( int userId , CancellationToken cancellationToken )
246
246
{
247
247
// Bearer token validation
248
248
if ( ! Request . Headers . ContainsKey ( "Authorization" ) )
249
249
{
250
- return new ApiResponse < string > (
250
+ return Task . FromResult ( new ApiResponse < string > (
251
251
new ApiError ( ApiErrorConstants . ErrorCodes . Unauthorized ,
252
252
ApiErrorConstants . ErrorMessages . AuthorizationHeaderRequired ,
253
253
ApiErrorConstants . ErrorDetails . BearerTokenMissing )
254
- ) . ToApiResult ( System . Net . HttpStatusCode . Unauthorized ) ;
254
+ ) . ToApiResult ( System . Net . HttpStatusCode . Unauthorized ) ) ;
255
255
}
256
256
257
257
var authHeader = Request . Headers [ "Authorization" ] . ToString ( ) ;
258
258
if ( ! authHeader . StartsWith ( "Bearer " ) || authHeader != "Bearer user-test-token" )
259
259
{
260
- return new ApiResponse < string > (
260
+ return Task . FromResult ( new ApiResponse < string > (
261
261
new ApiError ( ApiErrorConstants . ErrorCodes . Unauthorized ,
262
262
ApiErrorConstants . ErrorMessages . InvalidOrMissingAuthentication ,
263
263
ApiErrorConstants . ErrorDetails . InvalidBearerToken )
264
- ) . ToApiResult ( System . Net . HttpStatusCode . Unauthorized ) ;
264
+ ) . ToApiResult ( System . Net . HttpStatusCode . Unauthorized ) ) ;
265
265
}
266
266
267
267
_logger . LogInformation ( "Deleting user {UserId}" , userId ) ;
268
268
269
269
var user = _users . FirstOrDefault ( u => u . Id == userId ) ;
270
270
if ( user == null )
271
271
{
272
- return new ApiResponse < string > (
272
+ return Task . FromResult ( new ApiResponse < string > (
273
273
new ApiError ( ApiErrorConstants . ErrorCodes . NotFound ,
274
274
string . Format ( ApiErrorConstants . ErrorMessages . ResourceNotFound , "User" , userId ) ,
275
275
string . Format ( ApiErrorConstants . ErrorDetails . ResourceDoesNotExist , "user" ) )
276
- ) . ToApiResult ( System . Net . HttpStatusCode . NotFound ) ;
276
+ ) . ToApiResult ( System . Net . HttpStatusCode . NotFound ) ) ;
277
277
}
278
278
279
279
_users . Remove ( user ) ;
280
280
281
- return new ApiResponse < string > ( $ "User { userId } deleted successfully") . ToApiResult ( ) ;
281
+ return Task . FromResult ( new ApiResponse < string > ( $ "User { userId } deleted successfully") . ToApiResult ( ) ) ;
282
282
}
283
283
}
0 commit comments