async Task ExecuteGetSyncCodeAsync() { if (!CrossConnectivity.Current.IsConnected) { MessagingUtils.SendOfflineMessage(); return; } if (IsBusy) { return; } IsBusy = true; SyncCodeError = false; try { Logger.Track(EvolveLoggerKeys.GetSyncCode); var mobileClient = DataStore.Azure.StoreManager.MobileService; if (mobileClient == null) { return; } var code = new MobileToWebSync(); var result = await mobileClient.InvokeApiAsync <MobileToWebSync, MobileToWebSync>("MobileToWebSync", code, HttpMethod.Post, null); if (result == null) { SyncCodeError = true; return; } SyncCode = result.TempCode; ResultVisible = true; } catch (Exception ex) { SyncCodeError = true; Logger.Report(ex); } finally { IsBusy = false; } }
public async Task <IHttpActionResult> PostMobileToWebSync(MobileToWebSync item) { var userId = AuthenticationHelper.GetAuthenticatedUserId(RequestContext); MobileToWebSync result; using (var context = new XamarinEvolveContext()) { // First look if there's an existing code for this user result = await context.MobileToWebSyncs.Where(i => i.UserId == userId).FirstOrDefaultAsync(); if (result != null) { // If it's expired, remove it so we can create a new one if (result.Expires < DateTime.UtcNow) { context.MobileToWebSyncs.Remove(result); result = null; } } if (result == null) { // Make a new server generated entity var newItem = new MobileToWebSync { UserId = userId, TempCode = _codeGenerator.Generate(FixedLength: 5), Expires = DateTime.UtcNow.AddHours(1) }; result = context.MobileToWebSyncs.Add(newItem); } await context.SaveChangesAsync(); } return(Ok(result)); }