superninja
superninja

Reputation: 3411

CreateDocumentQuery() with property other than id

I am trying to query document using property other than id in DocumentDB. I found a solution here, but I am not sure what should the URL be when I am doing the query. For example, in this senario:

var families = from f in client.CreateDocumentQuery<Family>(colSelfLink)
       where f.Address.City != "NY"
       select f;

If it is getFamilyById, it might be http://localhost:50912/api/family/xxxxxx where the xxxxx = family id

If if is getFamilyByCity, it cannot use this format anymore: http://localhost:50912/api/family/xxxxxx where the xxxxx = city name. Because the API would be confused about whether you are choosing a family id or city name. So I think we should use the URL like http://localhost:50912/api/family/byCity/xxxx where xxxx = city name.

But I was wondering how can we achieve this?

Here's my sample code:

namespace Family.Controllers
{
    [Produces("application/json")]
    [Route("api/Family")]
    public class FamilyController : Controller
    {
        [HttpGet]
        public async Task<IEnumerable<Family>> GetAllAsync()
        {
            var families= await FamilyProfile.DocumentDBRepository<Family>.GetIndividualsAsync(t => t.PrimaryKey != null);
            return families;
        }

        [HttpGet("{id}")]
        public async Task<IActionResult> GetByIdAsync(string id)
        {
            var family= await DocumentDBRepository<Individual>.GetFamilyAsync(id);
            if (family== null)
            {
                return NotFound();
            }
            return new ObjectResult(family);
        }

        [HttpGet("{FamilyID}")]
        [Route("/ByCity")]
        public async Task<IActionResult> GetByCityAsync(string city)
        {
            var family= await DocumentDBRepository<Family>.GetFamilyAsyncByFamilyID(city); 

            if (family== null)
            {
                return NotFound();
            }
            return new ObjectResult(family);
        }
             }
    } 

It returns 404 when I ran http://localhost:50912/api/family/xxxxxx and the break point is not hitting GetByCityAsync(string city). Any suggestions in what I should look into? Thanks!

Upvotes: 0

Views: 193

Answers (1)

Joey Cai
Joey Cai

Reputation: 20127

So I think we should use the URL like http://localhost:50912/api/family/byCity/xxxx where xxxx = city name.

According to your description and code, I suggest that you could modify the Route about GetByCityAsync.

Change

[Route("/ByCity")]

To

[Route("ByCity/{city}")]

Upvotes: 1

Related Questions