Skip to content

Handling User Data

Derek Williamson edited this page Sep 25, 2019 · 6 revisions

Because of the nature of the AppState implementation, it is not possible to retrieve the current user's data without a HttpContextAccessor instance (accessible from a View) or a Controller instance.

It is, however, possible to retrieve a specific user's data through a Condition Expression.

Retrieving User Data by Property

The following example retrieves a list of users that match an AWS Condition Expression for John * EXCEPT John Doe and logs a debug message of the number of results found.

var persister = Singleton.Get<DynamoDbPersister>();
var results = persister.Scan<UserObject>(new Expression()
{
    ExpressionStatement = "FirstName = :firstName AND NOT LastName = :lastName",
    ExpressionAttributeValues = new Dictionary<string, DynamoDBEntry>()
    {
        { ":firstName", "John" },
        { ":lastName", "Doe" }
    }
});
Log.Debug($"Found {results.Count} users that matched the expression.");

Unfortunately, the ExpressionAttributeValues field only supports DynamoDBEntry types. The types you want to query on must either have an implicit operator in DynamoDBEntry OR must derive from DynamoDBEntry with the proper conversion methods.

Retrieving User Data by GUID

Instead of scanning the table for the property, you can query the table by key directly:

var persister = Singleton.Get<DynamoDbPersister>();
var result = persister.Get<UserObject>(Guid.Parse("c6a17a6b-0494-453a-8691-90dcc8bcf1bf"));
if (result != null)
    Log.Debug($"Found the specified user!");
else
    Log.Debug("There is no user with the GUID \"c6a17a6b-0494-453a-8691-90dcc8bcf1bf\".");

Retrieving User Data Through a View

In the following example, example data is pulled from the user's AppState through their current session, and their user data is pulled from the database by their GUID:

@{
    var session = Singleton.Get<SessionService>();
    var persister = Singleton.Get<DynamoDbPersister>();

    var exampleString = session.Get("ExampleString", "", HttpContextAccessor);
    var exampleFloat = session.Get("ExampleFloat", 0.0f, HttpContextAccessor);
    var exampleJson = session.GetJson("ExampleJson", HttpContextAccessor);
    var guid = session.Get("UserGuid", "", HttpContextAccessor);

    string userFirstName = "stranger";
    if (guid != "") // If the user is logged in...
    {
        var user = persister.Get<UserObject>(Guid.Parse(guid));
        if (user != null) // If the user's data was found in the table...
            userFirstName = user.FirstName;
        else userFirstName = "my dood";
    }
}

<div>
    <p>Well howdy hey, @userFirstName</p>
    <p>@exampleString</p>
    <p>@exampleFloat</p>
    <p>@exampleJson.ToString()</p>
</div>

The end of the C# block will (1) ensure the user is logged in, (2) retrieve the user from the table, and (3) assign the UserObject's FirstName to userFirstName. If the user was not found in the table, userFirstName will be set to "my dood". If the user is not logged in, userFirstName will be set to "stranger".

Retrieving User Data Through a Controller

This is similar to the example above:

var session = Singleton.Get<SessionService>();
Log.Debug(session.Get("UserEmail", "default_result@email.com", null, this));

If you want to retrieve the user's data from the table, retrieve the UserGuid variable and follow the example here.

Modifying User Data Through a Controller

To modify the user's AppState for their current session, use the Set() method in SessionService with the variable name and value:

var session = Singleton.Get<SessionService>();
session.Set("ExampleVariable", 123, null, this);

To modify the user's UserObject; retrieve it by GUID, modify any property, and use the Save() method in DynamoDbPersister:

var persister = Singleton.Get<DynamoDbPersister>();
var session = Singleton.Get<SessionService>();
var guid = session.Get("UserGuid", "", null, this);

if (guid != "") // If the user is logged in...
{
    var user = persister.Get<UserObject>(Guid.Parse(guid));
    if (user != null) // If the user's data was found in the table...
    {
        user.FirstName = "John"; // Modify some fields and properties.
        persister.Save(user);
    }
}