0

So this should be a pretty easy one. In fact I've read lots of Q&A's on much more complicated Restful Design problems out there and I did not find the answer to this simple one... So if I have the following entity:

public class Guest 
{
   public int GuestId { get; set; }
   public int PersonId { get; set; }
   // bunch of other properties
}

What should my GuestController look like if I want to have methods available to retrieve a Guest object via the GuestId or the PersonId?

I know I could accomplish this by it looking something like this..

public class GuestsController
{
    // GET api/guests/1
    public Guest Get(int id)
    {
        // get the Guest by GuestId
    }

    // GET api/guests/GetbyPersonId/2
    public Guest GetbyPersonId(int personId)
    {
        // get the Guest by PersonId
    }
}

... and then using attribute routing or actions or whatever to reach the GetByPersonId method, but that just doesn't seem to comply with the whole Restful design principles I've been reading about. What is suggested in this case?

PressTheAnyKey
  • 133
  • 1
  • 7
  • I might suggest mapping `/people/{personId}/guest` to `GetGuestByPersonId`, assuming that `guest` is a one-to-one relation of `person`. – Dai Jul 30 '14 at 02:50

1 Answers1

0

I Edited my answer, I would suggest you use better logical hierarchy url to map your impelementation. so I would change the url structure to below.

// /api/people/{Id}/
public class PersonRequest
{
   public int PersonId {get; set;}
}

//  /api/people/{Id}/guest
public class GuestRequest : PersonRequest
{

}

after using the better hierachy url structure, I think it much clearer you only provide Get by Person Id implementation and there is no need for Get By Guest Id

public class PersonService
{
  public object Get(PersonRequest request)
  {
    return GuestController.Get(request.PersonId);
  }

  public object Get(GuestRequest request)
  {
    return GuestController.Get(request.PersonId);
  }

}
Turbot
  • 5,095
  • 1
  • 22
  • 30
  • I edited my post as I had a mistake which might have given you the wrong impression, I want both methods to return a Guest object. So basically it will be like search for a Guest by GuestId and another method searching for a Guest by PersonId. While your example works for returning a Guest or Person by their respective primary keys, it does not work for my requirement. Any more thoughts? – PressTheAnyKey Jul 30 '14 at 14:43
  • I updated the answer, is that what you looking for ? – Turbot Aug 01 '14 at 01:23