Skip to content

Commit

Permalink
Merge branch 'release/universe-1.2.0' into prod
Browse files Browse the repository at this point in the history
  • Loading branch information
kuromukira committed Aug 12, 2022
2 parents be8ee21 + 6e660a6 commit 1edbd99
Show file tree
Hide file tree
Showing 20 changed files with 662 additions and 125 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ _ = services.AddScoped<IGalaxy<MyModel>, MyRepository>(service => new(

#### Example Query:
```csharp
MyModel myModel = await IGalaxy<MyModel>.Get(new QueryParameter("LastName", "last name value", DbType.String));
MyModel myModel = await IGalaxy<MyModel>.Get(new Catalyst("LastName", "last name value", DbType.String));
```
13 changes: 13 additions & 0 deletions code/DarkMatter/DarkMatter.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Universe\UniverseQuery.csproj" />
</ItemGroup>
</Project>
75 changes: 75 additions & 0 deletions code/DarkMatter/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Microsoft.Azure.Cosmos;
using Universe;
using Universe.Interfaces;
using Universe.Options;
using Universe.Options.Query;
using Universe.Response;

string CosmosDbUri = "<FROM AZURE>";
string CosmosDbPrimaryKey = "<FROM AZURE>";

// Imagine this part here as your dependency injection
CosmosClient cosmosClient = new(
CosmosDbUri,
CosmosDbPrimaryKey,
clientOptions: new()
{
Serializer = new UniverseSerializer()
}
);

IGalaxy<MyObject> galaxy = new MyRepo(
db: cosmosClient.GetDatabase("<DATABASE NAME>"),
container: "<CONTAINER NAME>",
partitionKey: "/<PARTITION KEY>"
);
// end of dependency injection

// actual use of UniverseQuery
(Gravity g, IList<MyObject> T) = await galaxy.Paged(
page: new Q.Page(50),
catalysts: new List<Catalyst>
{
new(nameof(MyObject.Links), "<VALUE TO QUERY>", Operator: Q.Operator.In),
new(nameof(MyObject.Code), "<VALUE TO QUERY>", Where: Q.Where.Or)
},
columns: new List<string>
{
nameof(MyObject.id),
nameof(MyObject.Code),
nameof(MyObject.Name),
nameof(MyObject.Description)
},
sorting: new List<Sorting.Option>
{
new(nameof(MyObject.Name), Sorting.Direction.DESC)
}
);

Console.WriteLine($"RU Spent: {g.RU}");
Console.ReadLine();

// Object definitions
class MyObject : ICosmicEntity
{
// Universe Generated
public string id { get; set; }
public DateTime AddedOn { get; set; }
public DateTime? ModifiedOn { get; set; }
public string PartitionKey => Code;

public string Code { get; set; }

public string Name { get; set; }

public string Description { get; set; }

public string[] Links { get; set; }
}

class MyRepo : Galaxy<MyObject>
{
public MyRepo(Database db, string container, string partitionKey) : base(db, container, partitionKey)
{
}
}
52 changes: 0 additions & 52 deletions code/Options/Query/Operators.cs

This file was deleted.

3 changes: 0 additions & 3 deletions code/Responses/Response.cs

This file was deleted.

File renamed without changes.
83 changes: 42 additions & 41 deletions code/Galaxy.cs → code/Universe/Galaxy.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Net;
using UniverseQuery.Responses;
using Universe.Response;

namespace Universe;

Expand All @@ -13,46 +13,47 @@ public abstract class Galaxy<T> : IDisposable, IGalaxy<T> where T : ICosmicEntit
protected Galaxy(Database db, string container, string partitionKey)
=> Container = db.CreateContainerIfNotExistsAsync(container, partitionKey).GetAwaiter().GetResult();

private static QueryDefinition CreateQuery(IList<QueryParameter> parameters, IList<string> columns = null)
private static QueryDefinition CreateQuery(IList<Catalyst> catalysts, IList<string> columns = null, IList<Sorting.Option> sorting = null)
{
StringBuilder columnBuilder = new StringBuilder();
// Column Builder
string columnsBuilder = "*";
if (columns is not null && columns.Any())
columnBuilder.Append(string.Join(", ", columns));
else columnBuilder.Append("*");
columnsBuilder = string.Join(", ", columns.Select(c => $"c.{c}").ToList());

StringBuilder queryBuilder = new($"SELECT {columnBuilder} FROM c");
if (parameters.Any())
// Where Clause Builder
StringBuilder queryBuilder = new($"SELECT {columnsBuilder} FROM c");
if (catalysts.Any())
{
queryBuilder.Append($" WHERE c.{parameters[0].Column} {parameters[0].Operator} @{parameters[0].Column}");
foreach (QueryParameter parameter in parameters.Where(p => p.Column != parameters[0].Column).ToList())
{
switch (parameter.Operator)
{
case Query.Operator.In:
queryBuilder.Append($" {parameter.Where} ARRAY_CONTAINS({$"c.{parameter.Column}"}, {$"@{parameter.Column}"})");
break;

case Query.Operator.Notin:
queryBuilder.Append($" {parameter.Where} NOT ARRAY_CONTAINS({$"c.{parameter.Column}"}, {$"@{parameter.Column}"})");
break;

default:
queryBuilder.Append($" {parameter.Where} {$"c.{parameter.Column}"} {parameter.Operator} {$"@{parameter.Column}"}");
break;
queryBuilder.Append($" WHERE {WhereClauseBuilder(catalysts[0])}");
foreach (Catalyst catalyst in catalysts.Where(p => p.Column != catalysts[0].Column).ToList())
queryBuilder.Append($" {catalyst.Where.Value()} {WhereClauseBuilder(catalyst)}");
}

}
}
// Sorting Builder
if (sorting is not null && sorting.Any())
{
queryBuilder.Append($" ORDER BY c.{sorting[0].Column} {sorting[0].Direction.Value()}");
foreach(Sorting.Option sort in sorting.Where(s => s.Column != sorting[0].Column).ToList())
queryBuilder.Append($", c.{sort.Column} {sort.Direction.Value()}");
}

// Parameters Builder
QueryDefinition query = new(queryBuilder.ToString());
if (!parameters.Any()) return query;
if (!catalysts.Any()) return query;
{
query = query.WithParameter($"@{parameters[0].Column}", parameters[0].Value);
foreach (QueryParameter parameter in parameters.Where(p => p.Column != parameters[0].Column).ToList())
query = query.WithParameter($"@{parameter.Column}", parameter.Value);
query = query.WithParameter($"@{catalysts[0].Column}", catalysts[0].Value);
foreach (Catalyst catalyst in catalysts.Where(p => p.Column != catalysts[0].Column).ToList())
query = query.WithParameter($"@{catalyst.Column}", catalyst.Value);
}

return query;

static string WhereClauseBuilder(Catalyst catalyst) => catalyst.Operator switch
{
Q.Operator.In => $"ARRAY_CONTAINS(c.{catalyst.Column}, @{catalyst.Column})",
Q.Operator.NotIn => $"NOT ARRAY_CONTAINS(c.{catalyst.Column}, @{catalyst.Column})",
_ => $"c.{catalyst.Column} {catalyst.Operator.Value()} @{catalyst.Column}",
};
}

async Task<(Gravity, string)> IGalaxy<T>.Create(T model)
Expand All @@ -65,9 +66,9 @@ private static QueryDefinition CreateQuery(IList<QueryParameter> parameters, ILi
return (new(response.RequestCharge, null), model.id);
}

async Task<Gravity> IGalaxy<T>.Create(IList<QueryParameter> parameters, T model)
async Task<Gravity> IGalaxy<T>.Create(IList<Catalyst> catalysts, T model)
{
QueryDefinition query = CreateQuery(parameters: parameters);
QueryDefinition query = CreateQuery(catalysts: catalysts);

using FeedIterator<T> queryResponse = Container.GetItemQueryIterator<T>(query);
if (queryResponse.HasMoreResults)
Expand Down Expand Up @@ -149,11 +150,11 @@ async Task<Gravity> IGalaxy<T>.Remove(string id, string partitionKey)
else return new(new(0, null), default);
}

async Task<(Gravity, T)> IGalaxy<T>.Get(QueryParameter parameter, IList<string> columns)
async Task<(Gravity, T)> IGalaxy<T>.Get(Catalyst catalyst, IList<string> columns)
{
try
{
QueryDefinition query = CreateQuery(parameters: new[] { parameter }, columns: columns);
QueryDefinition query = CreateQuery(catalysts: new[] { catalyst }, columns: columns);
return await GetOneFromQuery(query);
}
catch (CosmosException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
Expand All @@ -166,11 +167,11 @@ async Task<Gravity> IGalaxy<T>.Remove(string id, string partitionKey)
}
}

async Task<(Gravity, T)> IGalaxy<T>.Get(IList<QueryParameter> parameters, IList<string> columns)
async Task<(Gravity, T)> IGalaxy<T>.Get(IList<Catalyst> catalysts, IList<string> columns)
{
try
{
QueryDefinition query = CreateQuery(parameters: parameters, columns: columns);
QueryDefinition query = CreateQuery(catalysts: catalysts, columns: columns);
return await GetOneFromQuery(query);
}
catch (CosmosException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
Expand Down Expand Up @@ -198,11 +199,11 @@ async Task<Gravity> IGalaxy<T>.Remove(string id, string partitionKey)
return (new(requestCharge, null), collection);
}

async Task<(Gravity, IList<T>)> IGalaxy<T>.List(QueryParameter parameter, IList<string> columns)
async Task<(Gravity, IList<T>)> IGalaxy<T>.List(Catalyst catalyst, IList<string> columns, IList<Sorting.Option> sorting)
{
try
{
QueryDefinition query = CreateQuery(columns: columns, parameters: new[] { parameter });
QueryDefinition query = CreateQuery(catalysts: new[] { catalyst }, columns: columns);
return await GetListFromQuery(query);
}
catch (CosmosException ex) when (ex.StatusCode != HttpStatusCode.NotFound)
Expand All @@ -211,11 +212,11 @@ async Task<Gravity> IGalaxy<T>.Remove(string id, string partitionKey)
}
}

async Task<(Gravity, IList<T>)> IGalaxy<T>.List(IList<QueryParameter> parameters, IList<string> columns)
async Task<(Gravity, IList<T>)> IGalaxy<T>.List(IList<Catalyst> catalysts, IList<string> columns, IList<Sorting.Option> sorting)
{
try
{
QueryDefinition query = CreateQuery(columns: columns, parameters: parameters);
QueryDefinition query = CreateQuery(catalysts: catalysts, columns: columns);
return await GetListFromQuery(query);
}
catch (CosmosException ex) when (ex.StatusCode != HttpStatusCode.NotFound)
Expand All @@ -224,11 +225,11 @@ async Task<Gravity> IGalaxy<T>.Remove(string id, string partitionKey)
}
}

async Task<(Gravity, IList<T>)> IGalaxy<T>.Paged(Query.Page page, IList<QueryParameter> parameters, IList<string> columns)
async Task<(Gravity, IList<T>)> IGalaxy<T>.Paged(Q.Page page, IList<Catalyst> catalysts, IList<string> columns, IList<Sorting.Option> sorting)
{
try
{
QueryDefinition query = CreateQuery(columns: columns, parameters: parameters);
QueryDefinition query = CreateQuery(catalysts: catalysts, columns: columns);

double requestUnit = 0;
string continuationToken = string.Empty;
Expand Down
File renamed without changes.
20 changes: 10 additions & 10 deletions code/Interfaces/IGalaxy.cs → code/Universe/Interfaces/IGalaxy.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using UniverseQuery.Responses;
using Universe.Response;

namespace Universe.Interfaces;

Expand All @@ -8,17 +8,17 @@ public interface IGalaxy<T> where T : ICosmicEntity
/// <summary>
/// Create a new model in the database
/// </summary>
Task<(Gravity, string)> Create(T model);
Task<(Gravity g, string t)> Create(T model);

/// <summary>
/// Create a new model in the database
/// </summary>
Task<Gravity> Create(IList<QueryParameter> parameters, T model);
Task<Gravity> Create(IList<Catalyst> catalysts, T model);

/// <summary>
/// Modify a model in the database
/// </summary>
Task<(Gravity, T)> Modify(T model);
Task<(Gravity g, T T)> Modify(T model);

/// <summary>
/// Remove one model from the database
Expand All @@ -28,30 +28,30 @@ public interface IGalaxy<T> where T : ICosmicEntity
/// <summary>
/// Get one model from the database
/// </summary>
Task<(Gravity, T)> Get(string id, string partitionKey);
Task<(Gravity g, T T)> Get(string id, string partitionKey);

/// <summary>
/// Get one model from the database
/// </summary>
Task<(Gravity, T)> Get(QueryParameter parameter, IList<string> columns = null);
Task<(Gravity g, T T)> Get(Catalyst catalyst, IList<string> columns = null);

/// <summary>
/// Get one model from the database
/// </summary>
Task<(Gravity, T)> Get(IList<QueryParameter> parameters, IList<string> columns = null);
Task<(Gravity g, T T)> Get(IList<Catalyst> catalysts, IList<string> columns = null);

/// <summary>
/// Get a paginated list from the database
/// </summary>
Task<(Gravity, IList<T>)> List(QueryParameter parameter, IList<string> columns = null);
Task<(Gravity g, IList<T> T)> List(Catalyst catalyst, IList<string> columns = null, IList<Sorting.Option> sorting = null);

/// <summary>
/// Get a paginated list from the database
/// </summary>
Task<(Gravity, IList<T>)> List(IList<QueryParameter> parameters, IList<string> columns = null);
Task<(Gravity g, IList<T> T)> List(IList<Catalyst> catalysts, IList<string> columns = null, IList<Sorting.Option> sorting = null);

/// <summary>
/// Get a paginated list from the database
/// </summary>
Task<(Gravity, IList<T>)> Paged(Query.Page page, IList<QueryParameter> parameters, IList<string> columns = null);
Task<(Gravity g, IList<T> T)> Paged(Q.Page page, IList<Catalyst> catalysts, IList<string> columns = null, IList<Sorting.Option> sorting = null);
}
File renamed without changes.
Loading

0 comments on commit 1edbd99

Please sign in to comment.