I am writing a RESTful service with C # / wcf and you need to put filters in GET. How and how many records to return, maybe if I want to filter something, etc. Consider this code:
[WebGet(UriTemplate = "/devices/{DeviceId}/positions")]
public List<GPSPosition> GetDevicePositions(string deviceId)
{
using (var context = new MobileModelContext(ContextManager.AccountGKey.Value))
{
var d = context.Devices.Where(aa => aa.DeviceId == deviceId).FirstOrDefault();
if (d == null)
{
outgoingResponse.StatusCode = HttpStatusCode.NotFound;
outgoingResponse.StatusDescription = "Device not found";
return null;
}
var query = from p in context.Positions
where p.DeviceKey.Equals(d.DeviceKey)
select new GPSPosition
{
PositionGKey = p.PositionGKey,
Latitude = p.Latitude,
Longitude = p.Longitude,
Speed = p.Speed,
Accuracy = p.Accuracy,
Altitude = p.Altitude,
GPSTime = p.GPSTime,
DeviceTime = p.DeviceTime
};
return query.ToList();
}
}
[WebGet(UriTemplate = "/devices/{DeviceId}/positions?RecordCount={RecordCount}")]
public List<GPSPosition> GetDevicePositions2(string deviceId, int recordCount)
{
using (var context = new MobileModelContext(ContextManager.AccountGKey.Value))
{
var d = context.Devices.Where(aa => aa.DeviceId == deviceId).FirstOrDefault();
if (d == null)
{
outgoingResponse.StatusCode = HttpStatusCode.NotFound;
outgoingResponse.StatusDescription = "Device not found";
return null;
}
var query = from p in context.Positions
where p.DeviceKey.Equals(d.DeviceKey)
select new GPSPosition
{
PositionGKey = p.PositionGKey,
Latitude = p.Latitude,
Longitude = p.Longitude,
Speed = p.Speed,
Accuracy = p.Accuracy,
Altitude = p.Altitude,
GPSTime = p.GPSTime,
DeviceTime = p.DeviceTime
};
return query.Take(recordCount).ToList();
}
}
Lots of repetitions. I can move the code to another function, but still I have 2 templates, I have 2 functions. Is there a way to make 1 template for / provisions / that will cover all possible "?" scripts?
katit source
share