public async Task <OpResult <Gramercy> > SaveGramercyAsync(Gramercy gramercy, Value gramercyValue = null, bool isUpdate = false) { OpResult <Gramercy> result = new OpResult <Gramercy>(); try { if (isUpdate) { await _faunaClient.Query( Update( gramercyValue.At("ref"), Obj("data", Encoder.Encode(gramercy)) ) ); } else { await _faunaClient.Query( Create( Ref("classes/gramercy"), Obj("data", Encoder.Encode(gramercy)) ) ); } result.Result = gramercy; } catch (Exception ex) { result.FromException(ex); } return(result); }
public async Task <OpResult <Gramercy> > UpdateGrammercy(string grammercyId, int status) { OpResult <Gramercy> result = new OpResult <Gramercy>(); try { FaunaContext faunaContext = new FaunaContext(); OpResult <Value> getGrammercyResult = await faunaContext.GetGramercyByIdAsync(grammercyId); if (getGrammercyResult.Code == Constants.Enums.OperationResultCode.Success && getGrammercyResult.Result != null) { Value gramercyValue = getGrammercyResult.Result; Gramercy gramercy = gramercyValue.ToGramercy(); gramercy.Status = status; OpResult <Gramercy> saveGrammercyResult = await faunaContext.SaveGramercyAsync(gramercy, gramercyValue, true); result.Result = saveGrammercyResult.Result; } else { result.CopyFrom(getGrammercyResult); } } catch (Exception ex) { result.FromException(ex); } return(result); }
public async Task <OpResult <Gramercy> > ProcessTweetAsync(Tweet tweet) { OpResult <Gramercy> result = new OpResult <Gramercy>(); try { if (tweet.UserMentions.Count() > 0) { IEnumerable <string> recipients = tweet.UserMentions.Select(s => s.UserName); FaunaContext faunaContext = new FaunaContext(); foreach (string recipient in recipients) { string recipientId = tweet.UserMentions.First(f => f.UserName == recipient).Id.ToString(); Gramercy gramercy = EntityFactory.CreateGramercy(tweet.Message, recipientId, recipient, tweet.SenderHandle, tweet.MessageId); result = await faunaContext.SaveGramercyAsync(gramercy); } } } catch (Exception ex) { result.FromException(ex); } return(result); }
public async Task <OperationResult> SendAnonymousGramerciesAsync() { OperationResult result = new OperationResult(); try { // Use the FaunaDbContext to get all non-sent gramercies with: // 1. isRelayed = false // 2. senderHandle = null FaunaContext faunaContext = new FaunaContext(); OpResults <Value> unsentGramercyResults = await faunaContext.GetUnsentGramerciesAsync(); if (unsentGramercyResults.Code != Constants.Enums.OperationResultCode.Success) { result.CopyFrom(unsentGramercyResults); return(result); } int unsentGramercies = unsentGramercyResults.Results.Count(); if (unsentGramercies > 0) { TwitterClient twitterClient = new TwitterClient(); // For each gramercy, send a tweet and record isRelayed = true foreach (Value gramercyValue in unsentGramercyResults.Results) { Gramercy gramercy = gramercyValue.ToGramercy(); string message = $"Hey @{gramercy.RecipientHandle}, someone thinks you're #awesum! {gramercy.Message}"; OperationResult sendResult = twitterClient.SendTweet(message); if (sendResult.Code == Constants.Enums.OperationResultCode.Success) { gramercy.IsRelayed = true; await faunaContext.SaveGramercyAsync(gramercy, gramercyValue, true); } } } } catch (Exception ex) { result.FromException(ex); } return(result); }