I am a bit of a web API beginner so here goes. I have defined my CustomersController:
public class CustomersController : ApiController
{
public Customer Get(int id)
{
var customer = CustomerDataSource.customerData.Where(c => c.id == id).First();
return customer;
}
public List<Customer> Get(string search)
{
var list = CustomerDataSource.customerData;
return list;
}
}
The CustomerDataSource.customerData returns a list of objects :
public static List<Customer> customerData
{
get
{
Customer customer1 = new Customer() {id=1, name = "Bert", address = "London"};
customer1.nicknames= new string[]{"jack","bart"};
Customer customer2 = new Customer() { id=2,name = "Jon", address = "New York" };
customer2.nicknames= new string[]{"ed","fred"};
List<Customer> listCustomers = new List<Customer>();
listCustomers.Add(customer1);
listCustomers.Add(customer2);
return listCustomers;
}
}
public class Customer
{
public int id { get; set; }
public string name { get; set; }
public string address { get; set; }
public string[] nicknames { get; set; }
}
I have defined a Get method which returns all the instances of Customer fine. I can also add an extra search argument if needed in case I would like to search in my datasource. Suppose I would like to do another task like sorting for example. I tried:
public List<Customer> Get(string sort)
{
var list = CustomerDataSource.customerData;
return list;
}
But this wont compile:
'CustomersController' already defines a member called 'Get' with the same parameter
The question is how to refactor my controller so I can pass in a searchargument and a sortargument while still maintaining the web API interface?