firebase database in c# web api code example
Example 1: CRUD configuration MVC with Firebase
public async Task<ActionResult> About()
{
var userId = "12345";
var currentLoginTime = DateTime.UtcNow.ToString("MM/dd/yyyy HH:mm:ss");
var currentUserLogin = new LoginData() { TimestampUtc = currentLoginTime };
var firebaseClient = new FirebaseClient("yourFirebaseProjectUrl");
var result = await firebaseClient
.Child("Users/" + userId + "/Logins")
.PostAsync(currentUserLogin);
var dbLogins = await firebaseClient
.Child("Users")
.Child(userId)
.Child("Logins")
.OnceAsync<LoginData>();
var timestampList = new List<DateTime>();
foreach (var login in dbLogins)
{
timestampList.Add(Convert.ToDateTime(login.Object.TimestampUtc).ToLocalTime());
}
ViewBag.CurrentUser = userId;
ViewBag.Logins = timestampList.OrderByDescending(x => x);
return View();
}
Example 2: CRUD configuration MVC with Firebase
@{
ViewBag.Title = "About";
}
<h2>@ViewBag.Title</h2>
<h3>Login History</h3>
<p>Current user: @ViewBag.CurrentUser</p>
<ul>
@foreach(var timestamp in ViewBag.Logins)
{
<li>Login at @timestamp</li>
}
</ul>