My team lead has asked me to write the code in a different way. The current approach in which the code is written is the only way I know as of now.
May be I dont have his level of expertise.
var param = "page";
var param1 = "size";
Here, page is for pagination and size is the number of records per page.
Is there any other way to use page and size parameter and put it differently in If else condition ??
I have removed the unnecessary code:
if (req.Query.ContainsKey(param) && !req.Query.ContainsKey(param1))
{
if (int.TryParse(req.Query[param], out var number))
{
if (number > 0 )
{
//var result = await _services.EtasjeServiceAsync(number, 1000, req);
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))
{
if(size > 0)
{
//var result = await _services.EtasjeServiceAsync(1, size, req);
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.EtasjeServiceAsync(number, size, req);
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))
{
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);
}
Thank you in advance.