Working on a web api restful service.
I have a table called tests. The primary key is auto-increment. Each row contains a username and a test question. This DB is not really optimised, but it doesn't need to be.
Because the primary key is just an int to keep each row unique, there are many rows with duplicate usernames. This is fine.
I want to be able to return all rows with a matching username.
I want to be able to do it with a get request with the url: www.mywebsite.com/api/tests/{username}
The default controller methods in Visual Studio are only able to search by primary key to return one unique result.
This is something like what I want, but it doesn't work. 500 error. I'm not knowledgeable on debugging either, so point me in the right direction there if possible so I can provide more info.
// GET: api/Tests/robert
[ResponseType(typeof(Test))]
public async Task<IHttpActionResult> GetTest(string id)
{
//This is the default approach that just searches by primary key
//Test test = await db.Tests.FindAsync(id);
/*This is roughly what I want. Compiles, but doesn't work.*/
var query = "SELECT* WHERE id=" + id;
Test test = db.Tests.SqlQuery(query, id);
if (test == null)
{
return NotFound();
}
return Ok(test);
}
Let me know where I screwed up. I've been blundering over this for hours. I don't claim to be particularly good at any of this.