/// <summary> /// Adds or updates a users answer. Creates new user if they're not in the cache, /// otherwise update answer. returns the number of questions answered. /// </summary> public int AddOrUpdate(Answer updateAnswer) { List<CacheAnswer> listAnswers; // Check for new user if(!_answerCache.TryGetValue(updateAnswer.ProfileId, out listAnswers)) { listAnswers = new List<CacheAnswer>(); _answerCache.Add(updateAnswer.ProfileId, listAnswers); } //Check if updating or adding new answer for(int i=0; i < listAnswers.Count; i++) { var a = listAnswers[i]; if(a.QuestionId==updateAnswer.QuestionId) { a.ChoiceBit = updateAnswer.ChoiceBit(); a.ChoiceAccept = updateAnswer.ChoiceAccept; a.ChoiceWeight = updateAnswer.ChoiceWeight; a.LastAnswered = DateTime.Now; //copy back to list since structs are passed by value listAnswers[i] = a; //found and updated answer, done return listAnswers.Count; } } //Got here: New answer listAnswers.Add(new CacheAnswer { QuestionId = updateAnswer.QuestionId, ChoiceBit = updateAnswer.ChoiceBit(), ChoiceAccept = updateAnswer.ChoiceAccept, ChoiceWeight = updateAnswer.ChoiceWeight, LastAnswered = DateTime.Now }); return listAnswers.Count; }
/// <summary> /// Returns true if the other answer matches my requirements /// </summary> public bool IsMatch(Answer otherAnswer) { return (otherAnswer.ChoiceBit() & ChoiceAccept) != 0; }