Refactor switch statement to switch expression

Replaced the `switch` statement with a `switch` expression to handle HTTP methods (`GET`, `POST`, `PUT`, `DELETE`). This refactoring reduces boilerplate code and improves readability by directly assigning the HTTP request result to the `response` variable, eliminating the need for multiple `using` and `break` statements.
This commit is contained in:
Developer 02 2025-11-26 17:34:42 +01:00
parent a854720c75
commit c16d58788a

View File

@ -25,31 +25,14 @@ public class InvokeRecActionCommandHandler(ISender sender) : IRequestHandler<Inv
{ {
using var http = new HttpClient(); using var http = new HttpClient();
switch (action.RestType?.ToUpper().Trim()) var response = action.RestType?.ToUpper().Trim() switch
{ {
case "GET": "GET" => await http.GetAsync(action.EndpointUri, cancel),
{ "POST" => await http.PostAsync(action.EndpointUri, null, cancel),
using var response = await http.GetAsync(action.EndpointUri, cancel); "PUT" => await http.PutAsync(action.EndpointUri, null, cancel),
break; "DELETE" => await http.DeleteAsync(action.EndpointUri, cancel),
} _ => throw new BadRequestException($"The REST type {action.RestType} is not supported.")
case "POST": };
{
using var response = await http.PostAsync(action.EndpointUri, null, cancel);
break;
}
case "PUT":
{
using var response = await http.PutAsync(action.EndpointUri, null, cancel);
break;
}
case "DELETE":
{
using var response = await http.DeleteAsync(action.EndpointUri, cancel);
break;
}
default:
throw new BadRequestException($"The REST type {action.RestType} is not supported.");
}
} }
} }
} }