英文:
Azure functions with Cosmos DB with wild search
问题
我想要一个“模糊”搜索,我可以获得名字或姓氏。所以可以发送名字为""和姓氏为"Smith",这样我就可以得到所有姓"Smith"的人。
我觉得这部分是正确的,但在标题中使用SQL查询有点奇怪。有更好的方法吗?
[FunctionName("People_Get")]
public IActionResult GetPeople(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "Review/{firstName}/{surname}")]
PeopleRequest request,
HttpRequest req,
[CosmosDB("CosmosDB", "PeopleContainer", Connection = "CosmosDbConnectionString",
SqlQuery = @"SELECT c.id,
c.FirstName,
c.Surname
FROM c.FirstName
WHERE c.FirstName == firstName OR c.FirstName == surname")]
IEnumerable<Models.People> peopleReviews,
ILogger log)
{
return new OkObjectResult(peopleReviews);
}
英文:
I want a 'wild' search where I get FirstName OR Surname. so it would be possible to send a FirstName of "" and Surname of "Smith" so I get everyone with the surname "Smith".
I feel this is partly correct, but having a sql query in the header has a slightly off smell. Is there a better way to do this?
[FunctionName($"People_Get")]
public IActionResult GetPeople(
[HttpTrigger( AuthorizationLevel.Anonymous, "get", Route = "Review/{firstName}/{surname}")]
PeopleRequest request,
HttpRequest req,
[CosmosDB( "CosmosDB", "PeopleContainer", Connection = "CosmosDbConnectionString",
SqlQuery = @"SELECT c.id ,
c.FirstName ,
c.Surname
FROM c.FirstName
WHERE c.FirstName == firstName OR c.FirstName == surname")]
IEnumerable<Models.People> peopleReviews,
ILogger log)
{
return new OkObjectResult(peopleReviews);
}
答案1
得分: 1
以下是已翻译的部分:
Azure functions with Cosmos DB with wild search
The code is correct, but there are a few changes to be done.
-
The SqlQuery property in the CosmosDB attribute should use string interpolation to include the values of the firstName and surname parameters in the query, rather than hardcoding them.
-
Use the 'LIKE' operator in the WHERE clause instead of '==' to match partial strings.
-
And also, you can add a condition to check if either firstName or surname is null or empty, and return an error response in that case.
The below is the updated code:
public static class Function1
{
[FunctionName("People_Get")]
public IActionResult GetPeople([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "Review/{firstName}/{surname}")]
PeopleRequest request, HttpRequest req, [CosmosDB("CosmosDB", "PeopleContainer", Connection = "CosmosDbConnectionString")]
IEnumerable<Models.People> peopleReviews, ILogger log)
{
if (string.IsNullOrEmpty(request.firstName) && string.IsNullOrEmpty(request.surname))
{
return new BadRequestObjectResult("Please provide either a first name or a surname.");
}
var query = from p in peopleReviews
where (string.IsNullOrEmpty(request.firstName) || p.FirstName.Contains(request.firstName))
&& (string.IsNullOrEmpty(request.surname) || p.Surname.Contains(request.surname))
select p;
return new OkObjectResult(query);
}
}
英文:
>Azure functions with Cosmos DB with wild search
The code is correct, but there are a few changes to be done.
-
The SqlQuery property in the CosmosDB attribute should use string interpolation to include the values of the firstName and surname parameters in the query, rather than hardcoding them.
-
Use the 'LIKE' operator in the WHERE clause instead of '==' to match partial strings.
-
And also, you can add a condition to check if either firstName or surname is null or empty, and return an error response in that case.
The below is the updated code:
public static class Function1
{
[FunctionName("People_Get")]
public IActionResult GetPeople([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "Review/{firstName}/{surname}")]
PeopleRequest request, HttpRequest req, [CosmosDB("CosmosDB", "PeopleContainer", Connection = "CosmosDbConnectionString")]
IEnumerable<Models.People> peopleReviews, ILogger log)
{
if (string.IsNullOrEmpty(request.firstName) && string.IsNullOrEmpty(request.surname))
{
return new BadRequestObjectResult("Please provide either a first name or a surname.");
}
var query = from p in peopleReviews
where (string.IsNullOrEmpty(request.firstName) || p.FirstName.Contains(request.firstName))
&& (string.IsNullOrEmpty(request.surname) || p.Surname.Contains(request.surname))
select p;
return new OkObjectResult(query);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论