Fix get/set pair issues

- add mitmproxy endpoints
- fix indentation
- pass UserId and VikingId rather than session
This commit is contained in:
Spirtix 2023-06-19 20:33:49 +02:00
parent 2a4edfffef
commit e30798270e
3 changed files with 15 additions and 23 deletions

View File

@ -3,7 +3,7 @@ import mitmproxy.http
def routable(path): def routable(path):
methods = ['GetRules', 'LoginParent', 'RegisterParent', 'GetSubscriptionInfo', 'GetUserInfoByApiToken', 'IsValidApiToken_V2', 'ValidateName', 'GetDefaultNameSuggestion', 'RegisterChild', 'GetProfileByUserId', 'LoginChild', 'GetUserProfileByUserID', 'GetKeyValuePair', 'SetKeyValuePair'] methods = ['GetRules', 'LoginParent', 'RegisterParent', 'GetSubscriptionInfo', 'GetUserInfoByApiToken', 'IsValidApiToken_V2', 'ValidateName', 'GetDefaultNameSuggestion', 'RegisterChild', 'GetProfileByUserId', 'LoginChild', 'GetUserProfileByUserID', 'GetKeyValuePair', 'SetKeyValuePair', 'GetKeyValuePairByUserID', 'SetKeyValuePairByUserID']
for method in methods: for method in methods:
if method in path: if method in path:
return True return True

View File

@ -65,7 +65,7 @@ public class ContentController : Controller {
return null; return null;
// Get the pair // Get the pair
Model.PairData? pair = keyValueService.GetPairData(session, pairId); Model.PairData? pair = keyValueService.GetPairData(session.UserId, session.VikingId, pairId);
return keyValueService.ModelToSchema(pair); return keyValueService.ModelToSchema(pair);
} }
@ -80,8 +80,8 @@ public class ContentController : Controller {
Session? session = ctx.Sessions.FirstOrDefault(s => s.ApiToken == apiToken); Session? session = ctx.Sessions.FirstOrDefault(s => s.ApiToken == apiToken);
if (session is null) if (session is null)
return Ok(false); return Ok(false);
bool result = keyValueService.SetPairData(session, pairId, schemaData); bool result = keyValueService.SetPairData(session.UserId, session.VikingId, pairId, schemaData);
return Ok(result); return Ok(result);
} }
@ -94,11 +94,7 @@ public class ContentController : Controller {
if (session is null) if (session is null)
return null; return null;
ctx.Entry(session).State = EntityState.Detached; // Don't update the entity Model.PairData? pair = keyValueService.GetPairData(userId, null, pairId);
session.VikingId = null;
session.UserId = userId;
Model.PairData? pair = keyValueService.GetPairData(session, pairId);
return keyValueService.ModelToSchema(pair); return keyValueService.ModelToSchema(pair);
} }
@ -113,11 +109,7 @@ public class ContentController : Controller {
if (session is null || string.IsNullOrEmpty(userId)) if (session is null || string.IsNullOrEmpty(userId))
return Ok(false); return Ok(false);
ctx.Entry(session).State = EntityState.Detached; // Don't update the entity bool result = keyValueService.SetPairData(userId, null, pairId, schemaData);
session.VikingId = null;
session.UserId = userId;
bool result = keyValueService.SetPairData(session, pairId, schemaData);
return Ok(result); return Ok(result);
} }

View File

@ -10,27 +10,27 @@ public class KeyValueService {
this.ctx = ctx; this.ctx = ctx;
} }
public Model.PairData? GetPairData(Session session, int pairId) { public Model.PairData? GetPairData(string? UserId, string? VikingId, int pairId) {
Model.PairData? pair = null; Model.PairData? pair = null;
if (session.VikingId != null) if (VikingId != null)
pair = ctx.PairData.FirstOrDefault(e => e.PairId == pairId && e.VikingId == session.VikingId); pair = ctx.PairData.FirstOrDefault(e => e.PairId == pairId && e.VikingId == VikingId);
else if (session.UserId != null) else if (UserId != null)
pair = ctx.PairData.FirstOrDefault(e => e.PairId == pairId && e.UserId == session.UserId); pair = ctx.PairData.FirstOrDefault(e => e.PairId == pairId && e.UserId == UserId);
return pair; return pair;
} }
public bool SetPairData(Session session, int pairId, Schema.PairData schemaData) { public bool SetPairData(string? UserId, string? VikingId, int pairId, Schema.PairData schemaData) {
// Get the pair // Get the pair
Model.PairData? pair = GetPairData(session, pairId); Model.PairData? pair = GetPairData(UserId, VikingId, pairId);
// Create the pair if it doesn't exist // Create the pair if it doesn't exist
bool exists = true; bool exists = true;
if (pair is null) { if (pair is null) {
pair = new Model.PairData { pair = new Model.PairData {
PairId = pairId, PairId = pairId,
UserId = session.UserId, UserId = UserId,
VikingId = session.VikingId, VikingId = VikingId,
Pairs = new List<Model.Pair>() Pairs = new List<Model.Pair>()
}; };
exists = false; exists = false;