I use web api 2 to implement the service. Having done some research on best practices, everyone seems to have different opinions on how to do the following. I have got
public HttpResponseMessage Get(string crewId, string shiftDate, int offset = 1, int limit = 10)
This GET method returns a list. There are several ways to get data from this method.
Get crew only Get only with shiftDate or Get with crewId and shiftDateYou (1) Mark crewId and shiftDate as optional?
public HttpResponseMessage Get(string crewId = null, string shiftDate = null, int offset = 1, int limit = 10)
and then there are a bunch of if statements to check what is filled and what is not filled in order to be able to do actions.
if(crewId != null && shiftDate == null){
}else if(crewId == null && shiftDate != null){
}else if(crewId != null && shiftDate != null){
}
This seems crazy to me, especially if you have a lot of options, you will have too many ifs in your code.
Do you (2) have different settings?
public HttpResponseMessage GetByCrewId(string crewId, int offset = 1, int limit = 10)
public HttpResponseMessage GetByShiftDate(string shiftDate, int offset = 1, int limit = 10)
public HttpResponseMessage GetByCrewIdShiftDate(string crewId, string shiftDate, int offset = 1, int limit = 10)
and then you will have a route map to the URI method
... / api / GetByCrewId? CrewId = 1234 ... / api / GetByShiftDate? ShiftDate = 1111-11-11 ... / api / GetByCrewIdShiftDate crewId = 1234 &? ShiftDate = 1111-11-11is option 2 calm?
Or there are better options (3).
Both of the options above will only work to ensure that I am using best practices and following REST standards. It just seems like I'm missing something, and hopefully you can put me in the right direction.