Example #1
0
        public async Task <IList <QuestionInfo> > GetQuestions(DateTime dateToDisplay)
        {
            if (!CrossConnectivity.Current.IsConnected)
            {
                throw new NoInternetException();
            }

            // calculate the from date as 7 days back if none passed in
            var fromDate = (int)(dateToDisplay.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
            var toDate   = (int)(dateToDisplay.AddDays(1).ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;

            var urlToCall = string.Format(QUESTION_URL, fromDate, toDate);

            var decompressedContent = await this.CallAndDecompress(urlToCall);

            var deserializedContent = JsonConvert.DeserializeObject <QuestionResponse> (decompressedContent);

            var questions = new List <QuestionInfo> ();

            foreach (var item in deserializedContent.items)
            {
                var qi = new QuestionInfo {
                    QuestionID       = item.question_id,
                    Title            = item.title,
                    InsertDate       = DateTime.Now,
                    UnixCreationDate = item.creation_date,
                    IsAnswered       = item.is_answered,
                    LoadedFromWeb    = true
                };

                questions.Add(qi);
            }

            return(questions);
        }
Example #2
0
        public async Task SaveQuestion(QuestionInfo question)
        {
            int questionId = question.QuestionID;

            var dbRecord = await Table <QuestionInfo> ()
                           .Where(qi => qi.QuestionID == questionId)
                           .FirstOrDefaultAsync().ConfigureAwait(false);

            if (dbRecord == null)
            {
                question.InsertDate = DateTime.Now;
                await InsertAsync(question).ConfigureAwait(false);
            }
            else
            {
                question.InsertDate = dbRecord.InsertDate;
                await UpdateAsync(question).ConfigureAwait(false);
            }
        }