I am wanting to return an https error status code with a custom message in a class. However, whenever I try to do so it says it is unable to convert from IResult to my model class employee. Prior to creating a class that handles the logic for sql I had it as the following in the program.cs file which worked as expected when I entered a value that did not exist in the db.
app.MapGet("/employee/{id}", (ApplicationDbContext db, int id) =>
{
var employee = db.Employee.Find(id);
if (employee == null)
return Results.NotFound("Employee not found");
return Results.Ok(employee);
});
Now creating a separate class to have all the logic in one file I have the following in the program.cs file and the logic in the separate file.
app.MapGet("/employee/{id}", (IDataRepository dr, int id) =>
{
return dr.GetEmployee(id);
});
now in the separate file:
public Employee GetEmployee(int id)
{
var employee = _db.Employee.Find(id);
if (employee == null)
{
return Results.NotFound("Employee not found!");
}
return employee;
}
It suggested a cast to my model class, employee. When I enter an id knowing that it does not exist in the db it gives me a casting error saying cannot convert the two. Any suggestions would be greatly appreciated.