Storing Filter Data

0
121

The Tailspin Surveys service stores filter data in Windows Azure table storage. It uses one table to store the tenant filter data, and one table to store the device filter data. The following code example shows how the TenantFilterStore class saves tenant filter data in the Windows Azure table storage.

C#
public class TenantFilterStore : ITenantFilterStore
{
private readonly IAzureTable<TenantFilterRow>
tenantFilterTable;

public void SetTenants(string username,
IEnumerable<string> tenants)
{
var rowsToDelete =
(from r in this.tenantFilterTable.Query
where r.RowKey == username
select r).ToList();
this.tenantFilterTable.Delete(rowsToDelete);
var rowsToAdd = tenants.Select(t => new TenantFilterRow
{ PartitionKey = t, RowKey = username });
this.tenantFilterTable.Add(rowsToAdd);
}

}

The following code example shows how the UserDeviceStore class saves device data.

C#
public class UserDeviceStore : IUserDeviceStore
{
private readonly IAzureTable<UserDeviceRow> userDeviceTable;

public void SetUserDevice(string username, Uri deviceUri)
{
this.RemoveUserDevice(deviceUri);
var encodedUri = Convert.ToBase64String(
Encoding.UTF8.GetBytes(deviceUri.ToString()));
this.userDeviceTable.Add(
new UserDeviceRow
{
PartitionKey = username,
RowKey = encodedUri
});
}
public void RemoveUserDevice(Uri deviceUri)
{
var encodedUri = Convert.ToBase64String(
Encoding.UTF8.GetBytes(deviceUri.ToString()));
var row =
(from r in this.userDeviceTable.Query
where r.RowKey == encodedUri
select r).SingleOrDefault();
if (row != null)
{
this.userDeviceTable.Delete(row);
}
}

}

The following code example shows how the SetFilters web method in the RegistrationService class stores the list of preferred tenants sent from the mobile client.

C#
public void SetFilters(SurveyFiltersDto surveyFiltersDto)
{
var username = Thread.CurrentPrincipal.Identity.Name;
this.tenantFilterStore.SetTenants(
username, surveyFiltersDto.Tenants.Select(t => t.Key));
}

 

Note: There is also a GetFilters web method to return the current list of preferred tenants to the client. The phone does not save the filter information locally; instead, it retrieves it from the web service using the GetFilters method when it needs it.