Skip to content

Commit

Permalink
Merge pull request #178 from onerain88/query-user
Browse files Browse the repository at this point in the history
Query user
  • Loading branch information
onerain88 authored Oct 13, 2021
2 parents 475d44a + 09814ea commit ac2b0d7
Show file tree
Hide file tree
Showing 21 changed files with 245 additions and 34 deletions.
10 changes: 8 additions & 2 deletions Engine/Internal/Controllers/LCClassHookController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.AspNetCore.Cors;
using LeanCloud.Storage.Internal.Object;
using LeanCloud.Storage;
using LeanCloud.Storage.Internal.Codec;

namespace LeanCloud.Engine {
[ApiController]
Expand Down Expand Up @@ -51,12 +52,17 @@ public async Task<object> Hook(string className, string hookName, JsonElement bo

LCObject result = await LCEngine.Invoke(mi, new object[] { obj }) as LCObject;
if (result != null) {
return LCCloud.Encode(result);
Dictionary<string, object> dict = LCEncoder.EncodeLCObject(result, true) as Dictionary<string, object>;
dict.Remove("__type");
dict.Remove("className");
return dict;
}
}
return body;
} catch (LCException e) {
return StatusCode(400, LCEngine.ConvertException(e));
} catch (Exception e) {
return StatusCode(500, e.Message);
return StatusCode(500, LCEngine.ConvertException(e));
}
}

Expand Down
10 changes: 7 additions & 3 deletions Engine/Internal/Controllers/LCFunctionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public object GetFunctions() {
try {
return LCEngine.GetFunctions(Request);
} catch (Exception e) {
return StatusCode(500, e.Message);
return StatusCode(500, LCEngine.ConvertException(e));
}
}

Expand All @@ -44,8 +44,10 @@ public async Task<object> Run(string funcName, JsonElement body) {
}
}
return body;
} catch (LCException e) {
return StatusCode(400, LCEngine.ConvertException(e));
} catch (Exception e) {
return StatusCode(500, e.Message);
return StatusCode(500, LCEngine.ConvertException(e));
}
}

Expand All @@ -68,8 +70,10 @@ public async Task<object> RPC(string funcName, JsonElement body) {
}
}
return body;
} catch (LCException e) {
return StatusCode(400, LCEngine.ConvertException(e));
} catch (Exception e) {
return StatusCode(500, e.Message);
return StatusCode(500, LCEngine.ConvertException(e));
}
}

Expand Down
12 changes: 9 additions & 3 deletions Engine/Internal/Controllers/LCUserHookController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ public async Task<object> HookSMSVerification(JsonElement body) {
return await Invoke(mi, dict);
}
return body;
} catch (LCException e) {
return StatusCode(400, LCEngine.ConvertException(e));
} catch (Exception e) {
return StatusCode(500, e.Message);
return StatusCode(500, LCEngine.ConvertException(e));
}
}

Expand All @@ -50,8 +52,10 @@ public async Task<object> HookEmailVerification(JsonElement body) {
return await Invoke(mi, dict);
}
return body;
} catch (LCException e) {
return StatusCode(400, LCEngine.ConvertException(e));
} catch (Exception e) {
return StatusCode(500, e.Message);
return StatusCode(500, LCEngine.ConvertException(e));
}
}

Expand All @@ -70,8 +74,10 @@ public async Task<object> HookLogin(JsonElement body) {
return await Invoke(mi, dict);
}
return body;
} catch (LCException e) {
return StatusCode(400, LCEngine.ConvertException(e));
} catch (Exception e) {
return StatusCode(500, e.Message);
return StatusCode(500, LCEngine.ConvertException(e));
}
}

Expand Down
32 changes: 16 additions & 16 deletions Engine/Public/LCEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,7 @@ internal static async Task<object> Invoke(MethodInfo mi, object[] parameters) {
}
return mi.Invoke(null, parameters);
} catch (TargetInvocationException e) {
Exception ex = e.InnerException;
if (ex is LCException lcEx) {
throw new Exception(JsonConvert.SerializeObject(new Dictionary<string, object> {
{ "code", lcEx.Code },
{ "message", lcEx.Message }
}));
}
throw ex;
throw e.InnerException;
}
}

Expand All @@ -245,14 +238,7 @@ internal static async Task<object> Invoke(MethodInfo mi, object request) {
}
return mi.Invoke(null, ps);
} catch (TargetInvocationException e) {
Exception ex = e.InnerException;
if (ex is LCException lcEx) {
throw new Exception(JsonConvert.SerializeObject(new Dictionary<string, object> {
{ "code", lcEx.Code },
{ "message", lcEx.Message }
}));
}
throw ex;
throw e.InnerException;
}
}

Expand Down Expand Up @@ -316,5 +302,19 @@ internal static object GetFunctions(HttpRequest request) {
{ "result", functions }
};
}

internal static object ConvertException(Exception e) {
LCLogger.Error(e.ToString());
if (e is LCException lcEx) {
return new Dictionary<string, object> {
{ "code", lcEx.Code },
{ "error", lcEx.Message }
};
}
return new Dictionary<string, object> {
{ "code", 1 },
{ "error", e.Message }
};
}
}
}
2 changes: 1 addition & 1 deletion Sample/LeanEngineApp/.leancloud/current_app_id
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz
ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz
2 changes: 1 addition & 1 deletion Sample/LeanEngineApp/.leancloud/current_group
Original file line number Diff line number Diff line change
@@ -1 +1 @@
web
web
54 changes: 54 additions & 0 deletions Sample/LeanEngineApp/web/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
using System.Linq;
using LeanCloud.Storage;
using LeanCloud.Engine;
using LeanCloud;

namespace web {
public class App {
private const string HOOK_CLASS_NAME = "TestHookClass";

// Function
[LCEngineFunction("ping")]
public static string Ping() {
Expand Down Expand Up @@ -41,5 +44,56 @@ public static async Task<Dictionary<string, LCObject>> GetObjectMap() {
ReadOnlyCollection<LCObject> todos = await query.Find();
return todos.ToDictionary(t => t.ObjectId);
}

[LCEngineFunction("lcexception")]
public static string LCException() {
throw new LCException(123, "Runtime exception");
}

[LCEngineFunction("exception")]
public static string Exception() {
throw new Exception("Hello, exception");
}

// Class Hook
[LCEngineClassHook(HOOK_CLASS_NAME, LCEngineObjectHookType.BeforeSave)]
public static LCObject BeforeSaveClass(LCObject obj) {
if (obj["score"] == null) {
obj["score"] = 60;
}
return obj;
}

[LCEngineClassHook(HOOK_CLASS_NAME, LCEngineObjectHookType.AfterSave)]
public static void AfterSaveClass(LCObject obj) {
LCLogger.Debug($"Saved {obj.ObjectId}");
}

[LCEngineClassHook(HOOK_CLASS_NAME, LCEngineObjectHookType.BeforeUpdate)]
public static LCObject BeforeUpdateClass(LCObject obj) {
ReadOnlyCollection<string> updatedKeys = obj.GetUpdatedKeys();
if (updatedKeys.Contains("score")) {
int score = (int) obj["score"];
if (score > 100) {
throw new Exception($"Error score: {score}");
}
}
return obj;
}

[LCEngineClassHook(HOOK_CLASS_NAME, LCEngineObjectHookType.AfterUpdate)]
public static void AfterUpdateClass(LCObject obj) {
LCLogger.Debug($"Updated {obj.ObjectId}");
}

[LCEngineClassHook(HOOK_CLASS_NAME, LCEngineObjectHookType.BeforeDelete)]
public static void BeforeDeleteClass(LCObject obj) {
throw new Exception($"Cannot delete {obj.ClassName}");
}

[LCEngineClassHook(HOOK_CLASS_NAME, LCEngineObjectHookType.AfterDelete)]
public static void AfterDeleteClass(LCObject obj) {
LCLogger.Debug($"Deleted {obj.ObjectId}");
}
}
}
Binary file modified Sample/LeanEngineApp/web/Libs/Common.dll
Binary file not shown.
Binary file modified Sample/LeanEngineApp/web/Libs/Engine.dll
Binary file not shown.
Binary file modified Sample/LeanEngineApp/web/Libs/LC.Newtonsoft.Json.dll
Binary file not shown.
Binary file modified Sample/LeanEngineApp/web/Libs/Storage.Standard.dll
Binary file not shown.
Binary file modified Sample/LeanEngineApp/web/Libs/Storage.dll
Binary file not shown.
9 changes: 6 additions & 3 deletions Storage/Storage.AOT/Storage.AOT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@
<Compile Include="..\Storage\Public\LCHookObject.cs">
<Link>Storage\Public\LCHookObject.cs</Link>
</Compile>
<Compile Include="..\Storage\Public\LCUser.cs">
<Link>Storage\Public\LCUser.cs</Link>
</Compile>
<Compile Include="..\Storage\Public\LCObject.cs">
<Link>Storage\Public\LCObject.cs</Link>
</Compile>
Expand Down Expand Up @@ -170,5 +167,11 @@
<Compile Include="..\Storage\Public\Friendship\LCFollowersAndFollowees.cs">
<Link>Storage\Public\Friendship\LCFollowersAndFollowees.cs</Link>
</Compile>
<Compile Include="..\Storage\Public\User\LCUser.cs">
<Link>Storage\Public\User\LCUser.cs</Link>
</Compile>
<Compile Include="..\Storage\Public\User\LCUserQueryCondition.cs">
<Link>Storage\Public\User\LCUserQueryCondition.cs</Link>
</Compile>
</ItemGroup>
</Project>
40 changes: 40 additions & 0 deletions Storage/Storage.Test/ClassHookTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using LeanCloud;
using LeanCloud.Storage;

namespace Storage.Test {
public class ClassHookTest : BaseTest {
private const string CLASS_NAME = "TestHookClass";

private LCObject obj;

[Test]
[Order(0)]
public async Task Save() {
obj = new LCObject(CLASS_NAME);
await obj.Save();
}

[Test]
[Order(10)]
public async Task Update() {
obj["score"] = 200;
LCException e = Assert.CatchAsync<LCException>(() => obj.Save());
Assert.AreEqual(e.Code, 142);

obj["score"] = 90;
await obj.Save();
}

[Test]
[Order(20)]
public void Delete() {
LCException e = Assert.CatchAsync<LCException>(() => obj.Delete());
Assert.AreEqual(e.Code, 142);
}
}
}
15 changes: 15 additions & 0 deletions Storage/Storage.Test/CloudTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using LeanCloud;
using LeanCloud.Storage;

namespace Storage.Test {
Expand Down Expand Up @@ -58,5 +59,19 @@ public async Task GetObjectMap() {
Assert.AreEqual(kv.Key, obj.ObjectId);
}
}

[Test]
public void CatchLCException() {
LCException ex = Assert.CatchAsync<LCException>(() => LCCloud.Run("lcexception"));
Assert.AreEqual(ex.Code, 123);
Assert.AreEqual(ex.Message, "Runtime exception");
}

[Test]
public void CatchException() {
LCException ex = Assert.CatchAsync<LCException>(() => LCCloud.Run("exception"));
Assert.AreEqual(ex.Code, 1);
Assert.AreEqual(ex.Message, "Hello, exception");
}
}
}
22 changes: 22 additions & 0 deletions Storage/Storage.Test/UserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Text;
using System.Collections.ObjectModel;
using LeanCloud;
using LeanCloud.Storage;
using LC.Newtonsoft.Json;
Expand Down Expand Up @@ -298,6 +299,27 @@ public async Task AuthData() {
}
}

[Test]
[Order(18)]
public async Task QueryUser() {
LCUser anoymous1 = await LCUser.LoginAnonymously();

string nickname = anoymous1.ObjectId;
anoymous1["nickname"] = nickname;
await anoymous1.Save();

await LCUser.LoginAnonymously();

LCUserQueryCondition condition = new LCUserQueryCondition();
condition.WhereEqualTo("nickname", nickname);
ReadOnlyCollection<LCUser> users = await LCUser.StrictlyFind(condition);

Assert.Greater(users.Count, 0);
foreach (LCUser user in users) {
Assert.AreEqual(user.ObjectId, anoymous1.ObjectId);
}
}

private string GetTestEmail() {
return $"{TestPhone}@leancloud.rocks";
}
Expand Down
9 changes: 5 additions & 4 deletions Storage/Storage/Internal/Query/LCCompositionalCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public class LCCompositionalCondition : ILCQueryCondition {
public const string And = "$and";
public const string Or = "$or";

readonly string composition;
protected string composition;

List<ILCQueryCondition> conditionList;
protected List<ILCQueryCondition> conditionList;

List<string> orderByList;
HashSet<string> includes;
Expand Down Expand Up @@ -217,8 +217,9 @@ public Dictionary<string, object> BuildParams() {
{ "skip", Skip },
{ "limit", Limit }
};
if (conditionList != null && conditionList.Count > 0) {
dict["where"] = JsonConvert.SerializeObject(Encode());
string where = BuildWhere();
if (!string.IsNullOrEmpty(where)) {
dict["where"] = where;
}
string order = BuildOrders();
if (!string.IsNullOrEmpty(order)) {
Expand Down
1 change: 0 additions & 1 deletion Storage/Storage/Public/LCCloud.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using LeanCloud.Common;
using LeanCloud.Storage.Internal.Codec;
using LeanCloud.Storage.Internal.Object;

namespace LeanCloud.Storage {
/// <summary>
Expand Down
Loading

0 comments on commit ac2b0d7

Please sign in to comment.