Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix paths in Example/Fixture.cs #6

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Neoflix.Challenges/Neoflix.Challenges.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
20 changes: 10 additions & 10 deletions Neoflix/Example/Fixtures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ static Fixtures()
var converter = new RecursiveListDictionaryConverter();
var dictionaryConverter = new RecursiveDictionaryConverter();

Favorites = JsonConvert.DeserializeObject<object[]>(File.ReadAllText("./example/favorites.json"),
Favorites = JsonConvert.DeserializeObject<object[]>(File.ReadAllText("./Example/favorites.json"),
converter).OfType<Dictionary<string, object>>().ToArray();
Genres = JsonConvert.DeserializeObject<object[]>(File.ReadAllText("./example/genre.json"),
Genres = JsonConvert.DeserializeObject<object[]>(File.ReadAllText("./Example/genre.json"),
converter).OfType<Dictionary<string, object>>().ToArray();
Goodfellas = JsonConvert.DeserializeObject<Dictionary<string, object>>(
File.ReadAllText("./example/goodfellas.json"), dictionaryConverter);
File.ReadAllText("./Example/goodfellas.json"), dictionaryConverter);
Pacino = JsonConvert.DeserializeObject<Dictionary<string, object>>(
File.ReadAllText("./example/pacino.json"), dictionaryConverter);
People = JsonConvert.DeserializeObject<object[]>(File.ReadAllText("./example/people.json"),
File.ReadAllText("./Example/pacino.json"), dictionaryConverter);
People = JsonConvert.DeserializeObject<object[]>(File.ReadAllText("./Example/people.json"),
converter).OfType<Dictionary<string, object>>().ToArray();
Popular = JsonConvert.DeserializeObject<object[]>(File.ReadAllText("./example/popular.json"),
Popular = JsonConvert.DeserializeObject<object[]>(File.ReadAllText("./Example/popular.json"),
converter).OfType<Dictionary<string, object>>().ToArray();
Ratings = JsonConvert.DeserializeObject<object[]>(File.ReadAllText("./example/ratings.json"),
Ratings = JsonConvert.DeserializeObject<object[]>(File.ReadAllText("./Example/ratings.json"),
converter).OfType<Dictionary<string, object>>().ToArray();
Roles = JsonConvert.DeserializeObject<object[]>(File.ReadAllText("./example/roles.json"),
Roles = JsonConvert.DeserializeObject<object[]>(File.ReadAllText("./Example/roles.json"),
converter).OfType<Dictionary<string, object>>().ToArray();
Similar = JsonConvert.DeserializeObject<object[]>(File.ReadAllText("./example/similar.json"),
Similar = JsonConvert.DeserializeObject<object[]>(File.ReadAllText("./Example/similar.json"),
converter).OfType<Dictionary<string, object>>().ToArray();
User = JsonConvert.DeserializeObject<Dictionary<string, object>>(File.ReadAllText("./example/user.json"),
User = JsonConvert.DeserializeObject<Dictionary<string, object>>(File.ReadAllText("./Example/user.json"),
dictionaryConverter);
}

Expand Down
6 changes: 3 additions & 3 deletions Neoflix/Neo4j.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public static class Neo4j
/// <param name="password">Password for the user.</param>
/// <returns>A task that represents the asynchronous initialization operation.</returns>
// tag::initDriver[]
public static Task InitDriverAsync(string uri, string username, string password)
public static async Task InitDriverAsync(string uri, string username, string password)
{
// TODO: Create an instance of the driver here
return Task.CompletedTask;
_driver = GraphDatabase.Driver(uri, AuthTokens.Basic(username, password));
await _driver.VerifyConnectivityAsync();
}
// end::initDriver[]

Expand Down
4 changes: 2 additions & 2 deletions Neoflix/Neoflix.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.4" />
<PackageReference Include="Neo4j.Driver" Version="5.6.0" />
<PackageReference Include="Neo4j.Driver" Version="5.9.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

Expand Down
29 changes: 17 additions & 12 deletions Neoflix/Services/AuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,25 @@ public async Task<Dictionary<string, object>> RegisterAsync(string email, string
if (email != "[email protected]")
throw new ValidationException($"An account already exists with the email address", email);
// end::constraintError[]
// TODO: Save user
var exampleUser = new Dictionary<string, object>

await using var session = _driver.AsyncSession();
var user = await session.ExecuteWriteAsync(async tx =>
{
["identity"] = 1,
["properties"] = new Dictionary<string, object>
{
["userId"] = 1,
["email"] = "[email protected]",
["name"] = "Graph Academy"
}
};
var cursor = await tx.RunAsync("""
CREATE (u:User {
userId: randomUuid(),
email: $email,
password: $encrypted,
name: $name
})
RETURN u { .userId, .name, .email } as u
""",
new { email, encrypted, name });
var record = await cursor.SingleAsync();
return record["u"].As<Dictionary<string, object>>();
});

var safeProperties = SafeProperties(exampleUser["properties"] as Dictionary<string, object>);
var safeProperties = SafeProperties(user);
safeProperties.Add("token", JwtHelper.CreateToken(GetUserClaims(safeProperties)));

return safeProperties;
Expand Down
24 changes: 18 additions & 6 deletions Neoflix/Services/MovieService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,24 @@ public MovieService(IDriver driver)
public async Task<Dictionary<string, object>[]> AllAsync(string sort = "title",
Ordering order = Ordering.Asc, int limit = 6, int skip = 0, string userId = null)
{
// TODO: Open an Session
// TODO: Execute a query in a new Read Transaction
// TODO: Get a list of Movies from the Result
// TODO: Close the session

return await Task.FromResult(Fixtures.Popular.Skip(skip).Take(limit).ToArray());
await using var session = _driver.AsyncSession();
var movies = await session.ExecuteReadAsync(async tx =>
{
var cursor = await tx.RunAsync($$"""
MATCH (m:Movie)
WHERE m.{{sort}} IS NOT NULL
RETURN m { .* } AS movie
ORDER BY m.{{sort}} {{order.ToString("G").ToUpper()}}
SKIP $skip
LIMIT $limit
""",
new { skip, limit });
var records = await cursor.ToListAsync();
return records.Select(r => r["movie"].As<Dictionary<string, object>>()).ToArray();
});

await session.CloseAsync();
return movies;
}
// end::all[]

Expand Down