Skip to content

Commit e6b9871

Browse files
authored
4 pages within the "Work with Realm" section (#545)
* lots of pages within Work with Realm * review feedback * final review feedback
1 parent 6cd85ce commit e6b9871

30 files changed

+549
-119
lines changed

examples/dotnet/Examples/CustomUserDataExamples.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public async Task Reads()
5151
await user.RefreshCustomDataAsync();
5252

5353
// Tip: define a class that represents the custom data:
54-
var cud = BsonSerializer.Deserialize<CustomUserData>(user.CustomData);
54+
var cud = user.GetCustomData<CustomUserData>();
5555

5656
Console.WriteLine($"User is cool: {cud.IsCool}");
5757
Console.WriteLine($"User's favorite color is {cud.FavoriteColor}");
@@ -69,7 +69,7 @@ public async Task Updates()
6969
new BsonDocument("$set", new BsonDocument("IsCool", false)));
7070

7171
await user.RefreshCustomDataAsync();
72-
var cud = BsonSerializer.Deserialize<CustomUserData>(user.CustomData);
72+
var cud = user.GetCustomData<CustomUserData>();
7373

7474
Console.WriteLine($"User is cool: {cud.IsCool}");
7575
Console.WriteLine($"User's favorite color is {cud.FavoriteColor}");

examples/dotnet/Examples/Examples.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void OpensLocalRealm()
7878
try
7979
{
8080
Directory.Delete(pathToDb, true);
81-
} catch (Exception e)
81+
} catch (Exception)
8282
{
8383

8484
}

examples/dotnet/Examples/Examples.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
1212
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
1313
<PackageReference Include="MongoDB.Bson" Version="2.11.2" />
14-
<PackageReference Include="Realm.Fody" Version="10.0.0-alpha.48">
14+
<PackageReference Include="Realm.Fody" Version="10.0.0-beta.1">
1515
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1616
<PrivateAssets>all</PrivateAssets>
1717
</PackageReference>
18-
<PackageReference Include="Realm" Version="10.0.0-alpha.48" />
18+
<PackageReference Include="Realm" Version="10.0.0-beta.1" />
1919
</ItemGroup>
2020
<ItemGroup>
2121
<None Remove="Readme.md" />

examples/dotnet/Examples/MongoDBExamples.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using Realms;
88
using Realms.Sync;
99

10-
1110
namespace UnitTests
1211
{
1312
public class MongoDBExamples
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using MongoDB.Bson;
5+
using NUnit.Framework;
6+
using Realms;
7+
using Realms.Sync;
8+
using TaskStatus = dotnet.TaskStatus;
9+
using Task = dotnet.Task;
10+
11+
namespace UnitTests
12+
{
13+
public class WorkWithRealm
14+
{
15+
App app;
16+
ObjectId testTaskId;
17+
Realms.Sync.User user;
18+
SyncConfiguration config;
19+
string myRealmAppId = "tuts-tijya";
20+
21+
[Test]
22+
public async System.Threading.Tasks.Task LotsaStuff()
23+
{
24+
string userEmail = "";
25+
26+
// :code-block-start: initialize-realm
27+
// :hide-start:
28+
/*:replace-with:
29+
var myRealmAppId = "<your_app_id>";
30+
var app = App.Create(myRealmAppId);
31+
:code-block-end:*/
32+
// :code-block-start: appConfig
33+
var appConfig = new AppConfiguration(myRealmAppId)
34+
{
35+
LogLevel = LogLevel.Debug,
36+
DefaultRequestTimeout = TimeSpan.FromMilliseconds(1500)
37+
};
38+
39+
app = App.Create(appConfig);
40+
//:code-block-end:
41+
// :code-block-start: register-user
42+
await app.EmailPasswordAuth.RegisterUserAsync(userEmail, "sekrit");
43+
//:code-block-end:
44+
// :code-block-start: confirm-user
45+
await app.EmailPasswordAuth.ConfirmUserAsync("<token>", "<token-id>");
46+
//:code-block-end:
47+
// :code-block-start: reset-user-1
48+
await app.EmailPasswordAuth.SendResetPasswordEmailAsync(userEmail);
49+
//:code-block-end:
50+
string myNewPassword = "";
51+
// :code-block-start: reset-user-2
52+
await app.EmailPasswordAuth.ResetPasswordAsync(
53+
myNewPassword, "<token>", "<token-id>");
54+
//:code-block-end:
55+
// :code-block-start: reset-user-3
56+
await app.EmailPasswordAuth.CallResetPasswordFunctionAsync(
57+
userEmail, myNewPassword,
58+
"<security-question-1-answer>",
59+
"<security-question-2-answer>");
60+
//:code-block-end:
61+
62+
}
63+
64+
[Test]
65+
public async System.Threading.Tasks.Task APIKeys()
66+
{
67+
{
68+
//:code-block-start:apikey-create
69+
var newKey = await user.ApiKeys.CreateAsync("someKeyName");
70+
Console.WriteLine($"I created a key named {newKey.Name}. " +
71+
$"Is it enabled? {newKey.IsEnabled}");
72+
//:code-block-end:
73+
}
74+
{
75+
//:code-block-start:apikey-fetch
76+
var key = await user.ApiKeys.FetchAsync(ObjectId.Parse("00112233445566778899aabb"));
77+
Console.WriteLine($"I fetched the key named {key.Name}. " +
78+
$"Is it enabled? {key.IsEnabled}");
79+
//:code-block-end:
80+
}
81+
{
82+
//:code-block-start:apikey-fetch-all
83+
var allKeys = await user.ApiKeys.FetchAllAsync();
84+
foreach (var key in allKeys)
85+
{
86+
Console.WriteLine($"I fetched the key named {key.Name}. " +
87+
$"Is it enabled? {key.IsEnabled}");
88+
}
89+
//:code-block-end:
90+
}
91+
{
92+
//:code-block-start:apikey-enable-disable
93+
var key = await user.ApiKeys.FetchAsync(ObjectId.Parse("00112233445566778899aabb"));
94+
if (!key.IsEnabled)
95+
{
96+
// enable the key
97+
await user.ApiKeys.EnableAsync(key.Id);
98+
}
99+
else
100+
{
101+
// disable the key
102+
await user.ApiKeys.DisableAsync(key.Id);
103+
}
104+
//:code-block-end:
105+
}
106+
{
107+
//:code-block-start:apikey-delete
108+
await user.ApiKeys.DeleteAsync(ObjectId.Parse("00112233445566778899aabb"));
109+
//:code-block-end:
110+
}
111+
}
112+
[Test]
113+
public async System.Threading.Tasks.Task MultiUser()
114+
{
115+
myRealmAppId = "tuts-tijya";
116+
var app = App.Create(myRealmAppId);
117+
118+
{
119+
foreach (var user in app.AllUsers)
120+
{
121+
await user.LogOutAsync();
122+
}
123+
Assert.AreEqual(0, app.AllUsers.Count());
124+
//:code-block-start:multi-add
125+
var aimee = await app.LogInAsync(Credentials.EmailPassword(
126+
"[email protected]", "sekrit"));
127+
Assert.IsTrue(aimee.Id == app.CurrentUser.Id, "aimee is current user");
128+
129+
var elvis = await app.LogInAsync(Credentials.EmailPassword(
130+
"[email protected]", "sekrit2"));
131+
Assert.IsTrue(elvis.Id == app.CurrentUser.Id, "elvis is current user");
132+
//:code-block-end:
133+
134+
//:code-block-start:multi-list
135+
foreach (var user in app.AllUsers)
136+
{
137+
Console.WriteLine($"User {user.Id} is logged on via {user.Provider}");
138+
}
139+
Assert.AreEqual(2, app.AllUsers.Count());
140+
//:code-block-end:
141+
//:code-block-start:multi-switch
142+
app.SwitchUser(aimee);
143+
Assert.IsTrue(aimee.Id == app.CurrentUser.Id, "aimee is current user");
144+
//:code-block-end:
145+
146+
//:code-block-start:multi-remove
147+
await app.RemoveUserAsync(elvis);
148+
var noMoreElvis = app.AllUsers.FirstOrDefault(u => u.Id == elvis.Id);
149+
Assert.IsNull(noMoreElvis);
150+
Console.WriteLine("Elvis has left the application.");
151+
//:code-block-end:
152+
}
153+
154+
return;
155+
}
156+
}
157+
}

source/dotnet/access-custom-user-data.txt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,19 @@ following class to map the properties:
6464

6565
Read Custom User Data
6666
---------------------
67-
The custom user data is stored in the
68-
:dotnet-sdk:`CustomData <reference/Realms.Sync.User.html#Realms_Sync_User_CustomData>`
69-
property on the ``User`` object of a logged in user:
67+
You retrieve custom user data by calling the
68+
:dotnet-sdk:`GetCustomData() <reference/Realms.Sync.User.html#Realms_Sync_User_GetCustomData>`
69+
method on the ``User`` object of a logged in user:
7070

7171
.. literalinclude:: /examples/generated/code/start/CustomUserDataExamples.codeblock.read.cs
7272
:language: csharp
7373

7474
.. admonition:: Custom Data May Be Stale
7575
:class: warning
7676

77-
{+backend+} does not dynamically update the value of
78-
:dotnet-sdk:`CustomData <reference/Realms.Sync.User.html#Realms_Sync_User_CustomData>`
79-
immediately when underlying data changes. Instead, {+backend+}
80-
fetches the most recent version of custom user data whenever a user
77+
{+backend+} does not dynamically update the value of the
78+
``CustomData`` immediately when underlying data changes. Instead, {+backend+}
79+
fetches the most recent version of custom user data whenever a user
8180
refreshes their :ref:`access token <user-sessions>` or when you explicitly
8281
call the
8382
:dotnet-sdk:`RefreshCustomDataAsync() <reference/Realms.Sync.User.html#Realms_Sync_User_RefreshCustomDataAsync>`

source/dotnet/authenticate.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ Authenticate a User
1414

1515
Overview
1616
--------
17-
1817
{+service+} provides an API for authenticating users using any enabled
19-
authentication provider. Instantiate a ``Credentials`` object and pass
20-
it to the ``app.LogInAsync()`` method to authenticate and obtain a ``User``
18+
authentication provider. Instantiate a
19+
:dotnet-sdk:`Credentials <reference/Realms.Sync.Credentials.html>`
20+
object and pass
21+
it to the
22+
:dotnet-sdk:`LogInAsync() <reference/Realms.Sync.App.html#Realms_Sync_App_LogInAsync_Realms_Sync_Credentials_>`
23+
method to authenticate and obtain a :dotnet-sdk:`User <reference/Realms.Sync.User.html>`
2124
instance. The ``Credentials`` class exposes factory methods that correspond to
2225
each of the authentication providers:
2326

source/dotnet/create-manage-api-keys.txt

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,53 +12,41 @@ Create & Manage API Keys
1212
:depth: 2
1313
:class: singlecol
1414

15-
Overview
16-
--------
17-
1815
.. _dotnet-api-key-create-user-key:
1916

2017
Create a User API Key
2118
---------------------
2219

23-
.. tabs-realm-languages::
24-
25-
.. tab::
26-
:tabid: c-sharp
27-
28-
.. code-block:: csharp
20+
.. literalinclude:: /examples/generated/code/start/WorkWithRealm.codeblock.apikey-create.cs
21+
:language: csharp
2922

3023
.. _dotnet-api-key-look-up-user-key:
3124

3225
Look up a User API Key
3326
----------------------
27+
To get a single key:
28+
29+
.. literalinclude:: /examples/generated/code/start/WorkWithRealm.codeblock.apikey-fetch.cs
30+
:language: csharp
3431

35-
.. tabs-realm-languages::
36-
37-
.. tab::
38-
:tabid: c-sharp
32+
To get all keys:
33+
34+
.. literalinclude:: /examples/generated/code/start/WorkWithRealm.codeblock.apikey-fetch-all.cs
35+
:language: csharp
3936

40-
.. code-block:: csharp
4137

4238
.. _dotnet-api-key-enable-disable:
4339

4440
Enable or Disable an API Key
4541
----------------------------
4642

47-
.. tabs-realm-languages::
48-
49-
.. tab::
50-
:tabid: c-sharp
51-
52-
.. code-block:: csharp
43+
.. literalinclude:: /examples/generated/code/start/WorkWithRealm.codeblock.apikey-enable-disable.cs
44+
:language: csharp
5345

5446
.. _dotnet-api-key-delete:
5547

5648
Delete an API Key
5749
-----------------
5850

59-
.. tabs-realm-languages::
60-
61-
.. tab::
62-
:tabid: c-sharp
63-
64-
.. code-block:: csharp
51+
.. literalinclude:: /examples/generated/code/start/WorkWithRealm.codeblock.apikey-delete.cs
52+
:language: csharp

source/dotnet/init-realmclient.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
.. _dotnet-init-appclient:
2+
3+
===============================
4+
Initialize the Realm App Client
5+
===============================
6+
7+
.. default-domain:: mongodb
8+
9+
Overview
10+
--------
11+
The {+app+} client is the interface to the {+backend+}
12+
backend. It provides access to the :ref:`authentication functionality
13+
<dotnet-authenticate>`, :ref:`functions <dotnet-call-a-function>`, and
14+
:ref:`sync management <dotnet-sync-data>`.
15+
16+
.. _dotnet-access-the-app-client:
17+
18+
Access the App Client
19+
---------------------
20+
Pass the {+app+} ID for your {+app+}, which you can
21+
:ref:`find in the Realm UI <find-your-app-id>`.
22+
23+
.. literalinclude:: /examples/generated/code/start/WorkWithRealm.codeblock.initialize-realm.cs
24+
:language: csharp
25+
26+
.. _dotnet-app-client-configuration:
27+
28+
Configuration
29+
-------------
30+
For granular control of your app connection, such as custom timeouts for connections
31+
and the log level, you can pass an
32+
:dotnet-sdk:`AppConfiguration <reference/Realms.Sync.AppConfiguration.html>`
33+
object to the
34+
:dotnet-sdk:`App.Create() <reference/Realms.Sync.App.html#Realms_Sync_App_Create_Realms_Sync_AppConfiguration_>`
35+
method. The following example sets the LogLevel and the request timeout:
36+
37+
.. literalinclude:: /examples/generated/code/start/WorkWithRealm.codeblock.appConfig.cs
38+
:language: csharp
39+
40+
.. note::
41+
42+
For most use cases, you only need your application's App ID to connect
43+
to {+service+}. The other settings demonstrated here are optional.

0 commit comments

Comments
 (0)