Users' information about the current session is kept in the WebApiContext.
We extend the previous example to store the user height and weight in the user session.
Basic modifications to store data
The TestPageModel contains the height and the weight, but that is a display page. The actual user's height and weight belong to the user and therefore are separate from the data we render in the page.
We add a UserInfo class to keep the user's data
namespace Litium.AddOns.HeadlessApi.Models.UserSessions
{
public class UserInfo
{
public int Height { get; set; }
public decimal Weight { get; set; }
}
}
and add this class into UserSession, so it can be maintained across multiple requests.
using Litium.Foundation.Modules.ECommerce.Carriers;
using Litium.AddOns.HeadlessApi.Models.UserSessions.Builders;
using System;
using System.Collections.Generic;
namespace Litium.AddOns.HeadlessApi.Models.UserSessions
{
/// <summary>
/// Info about current user. Use <see cref="UserSessionBuilder"/> to build.
/// </summary>
public class UserSession
{
public KeyValuePair<string, Guid> Currency { get; set; }
public KeyValuePair<string, Guid> Culture { get; set; }
public string Id { get; set; }
public OrderCarrier OrderCarrier { get; set; }
public UserInfo UserInfo { get; set; }
}
}
The user session builder should ideally use a UserInfo builder to build the user info. To keep the code simple, we build the user info also inside the userSession builder, we modify its \src\Litium.AddOns.HeadlessApi\Models\UserSessions\Builders\UserSessionBuilder.cs Build () method.
public virtual UserSession Build(string id = null)
{
if (string.IsNullOrEmpty(id))
id = Guid.NewGuid().ToString();
var orderCarrier = _orderFactory.Create(null, ModuleECommerce.Instance.AdminToken);
orderCarrier.WebSiteID = Guid.Empty;
var websiteSettings = _webSiteSettingsBuilder.Build();
orderCarrier.CurrencyID = websiteSettings.CurrencyCodes.FirstOrDefault().Value;
var result = new UserSession()
{
Id = id,
OrderCarrier = orderCarrier,
Culture = websiteSettings.DefaultLanguage,
Currency = websiteSettings.DefaultCurrency,
UserInfo = new UserInfo()
};
return result;
}
POST information to user session
To save the information to the user session, we add a new endpoint, that can capture the user session. We pass in the user info as a parameter also.
When the Route("{sessionId}" attribute is set, it auotmcatically finds and populates the WebApiContext parameter in the SetUserInfo() method below. This parameter binding is done in the WebApiContextModelBinder class (src\Litium.AddOns.HeadlessApi\Models\WebApiContextModelBinder.cs.)
We also modify the Get method, so that it will return the TestPageModel based on the current user session.
using Litium.AddOns.HeadlessApi.Models;
using Litium.AddOns.HeadlessApi.Models.Site;
using Litium.AddOns.HeadlessApi.Models.Site.Builders;
using Litium.AddOns.HeadlessApi.Models.UserSessions;
using System.Web.Http;
using System.Web.Http.Description;
namespace Litium.AddOns.HeadlessApi.Controllers.Site
{
[RoutePrefix(Routes.RootUri + "/TestPage")]
public class TestPageController:HeadlessApiControllerBase
{
private readonly TestPageModelBuilder _testPageModelBuilder;
public TestPageController(TestPageModelBuilder testPageModelBuilder)
{
_testPageModelBuilder = testPageModelBuilder;
}
[HttpGet]
[Route("{sessionId}", Name = "TestPage")]
[ResponseType(typeof(TestPageModel))]
public IHttpActionResult Get(WebApiContext webApiContext)
{
var model = _testPageModelBuilder.Build(webApiContext.UserSession.UserInfo);
if (model == null)
{
return NotFound();
}
return Ok(model);
}
[HttpPost]
[Route("{sessionId}", Name = "SetUserInfo")]
[ResponseType(typeof(UserInfo))]
public IHttpActionResult SetUserInfo(WebApiContext webApiContext, [FromBody] UserInfo userInfo)
{
webApiContext.UserSession.UserInfo = userInfo;
return Ok(webApiContext.UserSession.UserInfo);
}
}
}
The test page model builder should be changed, so that the model building accepts a userInfo, and builds the model from it.
using Litium.AddOns.HeadlessApi.Models.UserSessions;
using Litium.AddOns.HeadlessApi.Services.TestServices;
using Litium.Runtime.DependencyInjection;
namespace Litium.AddOns.HeadlessApi.Models.Site.Builders
{
[Service(Lifetime = DependencyLifetime.Singleton, ServiceType = typeof(TestPageModelBuilder))]
public class TestPageModelBuilder
{
private const int initialHeight = 181;
private const decimal initialWeight = 50;
private readonly BodyMassIndexService _bodyMassIndexService;
public TestPageModelBuilder(BodyMassIndexService bodyMassIndexService)
{
_bodyMassIndexService = bodyMassIndexService;
}
public TestPageModel Build(UserInfo userInfo)
{
return new TestPageModel()
{
Weight = userInfo.Weight,
Height = userInfo.Height,
BodyMassIndex = _bodyMassIndexService.Calculate(userInfo.Height, userInfo.Weight)
};
}
}
}
Then we send a POST request, with a body containing UserInfo json, and a Content-Type header with applicaiton/json as content type.

Now a Get request to the TestPage endpoint with same session would give our results back, together with bodymass index.
