The code is not efficient, let's say the url matched in the first regex case but I'm still evaluating second regex for the second case while knowing it's mutually exclusive and only one of them will be true. The efficiency will further lower if I add more urls to match against.
Also is there a way without introducing another boolean variable to eloquently handle these cases?
public void service(ServiceRequest serviceRequest, ServiceProxy serviceProxy) {
HttpRequest request = serviceRequest.HttpRequest;
HttpResponse response = serviceRequest.HttpResponse;
Match match;
bool found = false;
match = Regex.Match(request.Path.Value, @"/articles/(\d+)/thumbnail"); // case "/articles/:id/thumbnail":
if(match.Success) {
found = match.Success;
if(request.Method.Equals(HttpMethod.Get.ToString())) {
serviceRequest.Params.Add("id", match.Groups[1].Value);
serviceRequest.ResultData = serviceProxy.GetArticleThumbnail(serviceRequest);
} else {
throw new HttpException(HttpStatusCode.MethodNotAllowed, "Method Not Allowed");
}
}
match = Regex.Match(request.Path.Value, @"/leads/(\d+)/collections/(.*)"); // case "/leads/:id/collections/:entityName": // share
if(match.Success) {
found = match.Success;
if(request.Method.Equals(HttpMethod.Put.ToString())) {
serviceRequest.Params.Add("id", match.Groups[1].Value);
serviceRequest.Params.Add("entityName", match.Groups[2].Value);
serviceRequest.ResultData = serviceProxy.ShareCollection(serviceRequest);
} else {
throw new HttpException(HttpStatusCode.MethodNotAllowed, "Method Not Allowed");
}
}
if(!found) throw new HttpException(HttpStatusCode.NotFound, "Not Found"); // default case
}