Hi,
Could someone please help me to refactor the "if else code" below in a more simplified way:
public async Task<IActionResult> GetEtasjeAsync(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "etasjer")]
HttpRequest req, ILogger log)
{
try
{
// Check if there is an access token to authorize the call to this endpoint
var accessToken = _azureAdJwtBearerValidation.GetAccessToken(req);
if (accessToken == null) return new UnauthorizedResult();
// validate that the access token is valid by checking that the signature is valid
var validateAccessToken = await _azureAdJwtBearerValidation.ValidateAccessTokenAsync(accessToken, log);
if (validateAccessToken == null) return new UnauthorizedResult();
var param = "page";
var param1 = "size";
if (req.Query.ContainsKey(param) && !req.Query.ContainsKey(param1))
{
if (int.TryParse(req.Query[param], out var number) && !int.TryParse(req.Query[param1], out var size))
{
if (number > 0 )
{
var result = await _services.EtasjeQueryServiceAsync(req, number, 1000);
if (result == null) return new NotFoundResult();
if (result.Content.Count == 0) return new NoContentResult();
return new OkObjectResult(result);
}
return new BadRequestResult();
}
return new BadRequestResult();
}
else if(req.Query.ContainsKey(param1) && !req.Query.ContainsKey(param))
{
if(int.TryParse(req.Query[param1], out var size) && !int.TryParse(req.Query[param], out var number))
{
if(size > 0)
{
var result = await _services.EtasjeQueryServiceAsync(req, 1, size);
if (result == null) return new NotFoundResult();
if (result.Content.Count == 0) return new NoContentResult();
return new OkObjectResult(result);
}
return new BadRequestResult();
}
return new BadRequestResult();
}
else if(req.Query.ContainsKey(param) && req.Query.ContainsKey(param1))
{
if(int.TryParse(req.Query[param], out var number) && int.TryParse(req.Query[param1], out var size))
{
if(number > 0 && size > 0)
{
var result = await _services.EtasjeQueryServiceAsync(req, number, size);
if (result == null) return new NotFoundResult();
if (result.Content.Count == 0) return new NoContentResult();
return new OkObjectResult(result);
}
return new BadRequestResult();
}
return new BadRequestResult();
}
else if (!req.Query.ContainsKey(param) && !req.Query.ContainsKey(param1))
{
if (!int.TryParse(req.Query[param], out var number) && !int.TryParse(req.Query[param1], out var size))
{
var result = await _services.EtasjeQueryServiceAsync(req, 1, 1000);
if (result == null) return new NotFoundResult();
if (result.Content.Count == 0) return new NoContentResult();
return new OkObjectResult(result);
}
return new BadRequestResult();
}
var response = await _services.EtasjeServiceAsync(1, 1000, req);
if (response == null) return new NotFoundResult();
if (response.Content.Count == 0) return new NoContentResult();
return new OkObjectResult(response);
}
catch (Exception ex)
{
log.LogError(ex, ex.Message);
throw;
}
}