Skip to content
This repository has been archived by the owner on May 16, 2020. It is now read-only.

Commit

Permalink
override entity name with jsonobjectattribute id
Browse files Browse the repository at this point in the history
  • Loading branch information
agrothe committed Apr 16, 2017
1 parent 0c461cd commit 0d8b93f
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ namespace BorderEast.ArangoDB.Client
/// Entry point for interacting with ArangoDB. You only need one instance of this class,
/// even if working with multiple databases, hence the Singleton pattern.
/// </summary>
public class ArangoClient
{
public class ArangoClient : IArangoClient {
private readonly string DEFAULT = Res.Msg.Default;
private static ArangoClient _client = new ArangoClient();
internal readonly IDictionary<string, ClientSettings> databases = new SortedDictionary<string, ClientSettings>();
internal readonly IDictionary<string, ConnectionPool<IConnection>> pools = new SortedDictionary<string, ConnectionPool<IConnection>>();

private ArangoClient() { }

/// <summary>
/// Get the client instance
/// </summary>
/// <returns></returns>
public static ArangoClient Client() {
return _client;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<PackageId>BorderEast.ArangoDB.Client</PackageId>
<PackageVersion>0.1.0</PackageVersion>
<PackageVersion>0.1.01</PackageVersion>
<Authors>Border East</Authors>
<Description>DotNet Core ArangoDB Client</Description>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageReleaseNotes>alpha release</PackageReleaseNotes>
<Copyright>Copyright 2017 (c) Border East. All rights reserved.</Copyright>
<PackageTags>arangodb database-client netcore</PackageTags>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>0.1.01</Version>
<PackageLicenseUrl>https://github.com/bordereast/arangodb-net-core/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/bordereast/arangodb-net-core</PackageProjectUrl>
<PackageIconUrl>https://avatars1.githubusercontent.com/u/27213082?v=3&amp;s=200</PackageIconUrl>
<RepositoryUrl>https://github.com/bordereast/arangodb-net-core</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<AssemblyVersion>1.0.0.1</AssemblyVersion>
<FileVersion>1.0.0.1</FileVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public async Task<List<T>> GetByExampleAsync<T>(dynamic parameters) {
/// <returns>Single entity</returns>
public async Task<T> GetByKeyAsync<T>(string key) {
Type type = typeof(T);
var typeName = GetTypeName(type);

ForeignKey fk = HasForeignKey(type);

Expand All @@ -99,7 +100,7 @@ public async Task<T> GetByKeyAsync<T>(string key) {
Payload payload = new Payload() {
Content = string.Empty,
Method = HttpMethod.Get,
Path = string.Format("_api/document/{0}/{1}", type.Name, key)
Path = string.Format("_api/document/{0}/{1}", typeName, key)
};

var result = await GetResultAsync(payload);
Expand All @@ -118,18 +119,18 @@ public async Task<T> GetByKeyAsync<T>(string key) {
/// <typeparam name="T">Entity type</typeparam>
/// <returns>List of entities</returns>
public async Task<List<T>> GetAllAsync<T>() {
Type t = typeof(T);
var typeName = GetTypeName(typeof(T));

ForeignKey fk = HasForeignKey(t);
ForeignKey fk = HasForeignKey(typeof(T));

if (fk.IsForeignKey) {
var q = BuildFKQuery(fk, t);
var q = BuildFKQuery(fk, typeof(T));

return await Query<T>(q).ToListAsync();
}

return await Query<T>("for x in @@col return x",
new Dictionary<string, object>{{ "@col", typeof(T).Name }}).ToListAsync();
new Dictionary<string, object>{{ "@col", typeName }}).ToListAsync();
}

private AQLQuery BuildFKQuery(ForeignKey fk, Type baseType, dynamic parameters = null) {
Expand All @@ -142,7 +143,7 @@ private AQLQuery BuildFKQuery(ForeignKey fk, Type baseType, dynamic parameters =
}


sb.Append("for x1 in " + baseType.Name);
sb.Append("for x1 in " + GetTypeName(baseType));
// parms.Add("@col", baseType.Name);

if (fk.IsForeignKey) {
Expand Down Expand Up @@ -220,7 +221,7 @@ private ForeignKey HasForeignKey(Type t) {
/// <returns>List of entity keys</returns>
public async Task<List<string>> GetAllKeysAsync<T>() {
return await Query<string>("for x in @@col return x._key",
new Dictionary<string, object> { { "@col", typeof(T).Name } }).ToListAsync();
new Dictionary<string, object> { { "@col", GetTypeName(typeof(T)) } }).ToListAsync();
}

/// <summary>
Expand All @@ -231,15 +232,15 @@ public async Task<List<string>> GetAllKeysAsync<T>() {
/// <param name="item">Entity with all or partial properties</param>
/// <returns>UpdatedDocument with complete new Entity</returns>
public async Task<UpdatedDocument<T>> UpdateAsync<T>(string key, T item) {
Type type = typeof(T);
var typeName = GetTypeName(typeof(T));

HttpMethod method = new HttpMethod("PATCH");

Payload payload = new Payload()
{
Content = JsonConvert.SerializeObject(item),
Method = method,
Path = string.Format("_api/document/{0}/{1}?mergeObjects=false&returnNew=true", type.Name, key)
Path = string.Format("_api/document/{0}/{1}?mergeObjects=false&returnNew=true", typeName, key)
};

var result = await GetResultAsync(payload);
Expand All @@ -255,13 +256,13 @@ public async Task<UpdatedDocument<T>> UpdateAsync<T>(string key, T item) {
/// <param name="key">Entity key</param>
/// <returns>True for success, false on error</returns>
public async Task<bool> DeleteAsync<T>(string key) {
Type type = typeof(T);
var typeName = GetTypeName(typeof(T));

Payload payload = new Payload()
{
Content = string.Empty,
Method = HttpMethod.Delete,
Path = string.Format("_api/document/{0}/{1}?silent=true", type.Name, key)
Path = string.Format("_api/document/{0}/{1}?silent=true", typeName, key)
};

var result = await GetResultAsync(payload);
Expand All @@ -283,13 +284,13 @@ public async Task<bool> DeleteAsync<T>(string key) {
/// <param name="item">Entity to insert</param>
/// <returns>UpdatedDocument with new entity</returns>
public async Task<UpdatedDocument<T>> InsertAsync<T>(T item) {
Type type = typeof(T);
var typeName = GetTypeName(typeof(T));

Payload payload = new Payload()
{
Content = JsonConvert.SerializeObject(item),
Method = HttpMethod.Post,
Path = string.Format("_api/document/{0}/?returnNew=true", type.Name)
Path = string.Format("_api/document/{0}/?returnNew=true", typeName)
};

var result = await GetResultAsync(payload);
Expand All @@ -298,6 +299,13 @@ public async Task<UpdatedDocument<T>> InsertAsync<T>(T item) {
return json;
}

private string GetTypeName(Type t) {
if (t.GetTypeInfo().GetCustomAttribute(typeof(JsonObjectAttribute)) is JsonObjectAttribute attr) {
return attr.Id ?? t.Name;
}
return t.Name;
}

internal async Task<Result> GetResultAsync(Payload payload) {

// Get connection just before we use it
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using BorderEast.ArangoDB.Client.Database;

namespace BorderEast.ArangoDB.Client {

/// <summary>
/// Entry point for interacting with ArangoDB. You only need one instance of this class,
/// even if working with multiple databases, hence the Singleton pattern.
/// </summary>
public interface IArangoClient {

/// <summary>
/// Convience method to get the default database.
/// </summary>
/// <returns></returns>
ArangoDatabase DB();

/// <summary>
/// Get database by name
/// </summary>
/// <param name="database"></param>
/// <returns></returns>
ArangoDatabase DB(string database);

/// <summary>
/// Initialize a new database, does not actually create the db in Arango
/// </summary>
/// <param name="databaseSettings"></param>
/// <returns></returns>
ArangoDatabase InitDB(ClientSettings databaseSettings);

/// <summary>
/// Setup your default database
/// </summary>
/// <param name="defaultConnection"></param>
void SetDefaultDatabase(ClientSettings defaultConnection);
}
}
35 changes: 18 additions & 17 deletions src/BorderEast.ArangoDB.Client/ConsoleTestApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,28 @@ static void Main(string[] args)

var client = ArangoClient.Client();

Role r = new Role()
{
Name = "sysadmin",
Permission = "RW"
};
Role r2 = new Role()
{
Name = "author",
Permission = "RW"
};
//Role r = new Role()
//{
// Name = "sysadmin",
// Permission = "RW"
//};
//Role r2 = new Role()
//{
// Name = "author",
// Permission = "RW"
//};

var uR1 = client.DB().InsertAsync<Role>(r).Result;
var uR2 = client.DB().InsertAsync<Role>(r2).Result;
//var uR1 = client.DB().InsertAsync<Role>(r).Result;
//var uR2 = client.DB().InsertAsync<Role>(r2).Result;




var juser = new User()
{
Username = "andrew",
Password = "passcode",
Roles = new List<Role>() { uR1.New, uR2.New}
Password = "passcode"//,
//Roles = new List<Role>() { uR1.New, uR2.New}
};


Expand All @@ -58,10 +58,11 @@ static void Main(string[] args)

string json = JsonConvert.SerializeObject(juser);

var u1 = client.DB().InsertAsync<Role>(r2).Result;
var users = client.DB().GetAllAsync<User>();
var s = json;
//var u1 = client.DB().InsertAsync<Role>(r2).Result;
//var users = client.DB().GetAllAsync<User>();

var u = client.DB().GetByKeyAsync<User>("");
//var u = client.DB().GetByKeyAsync<User>("");

//var token = JsonConvert.SerializeObject(juser, Formatting.Indented, new ArangoJsonConverter(typeof(User)));

Expand Down
1 change: 1 addition & 0 deletions src/BorderEast.ArangoDB.Client/ConsoleTestApp/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace ConsoleTestApp
{
[JsonObject(Title = "title", Id = "id", Description = "desc")]
[Collection(HasForeignKey = true)]
public class User : ArangoBaseEntity
{
Expand Down

0 comments on commit 0d8b93f

Please sign in to comment.