/// <summary> /// Generates a string key for the match list to use in Redis based on the profileId /// and the given criteria. /// </summary> public string FormatKey(int profileId, MatchCriteriaModel criteria) { var key = profileId.ToString() + ":"; key += criteria.Gender + ":"; key += criteria.LocationId1.ToString(); return key; }
/// <summary> /// API: returns a list of matches for the given page and criteria. Called by /// AJAX and returns JSON. Looks in the cache first and if not there call /// the match server to calculate matches and save in cache. /// </summary> public async Task<ActionResult> Get(MatchCriteriaModel criteria, int page = 1) { var me = GetMyProfileId(); var matches = await GetMatchesAsync(me, criteria, page); //Serialize matches to JSON var json = JsonConvert.SerializeObject(matches); return Content(json, "application/json"); }
/// <summary> /// Gets the matches for a give page. Looks in the cache first and if not there calculates /// matches and saves them there. If the forceRecalculation flag is true skip looking in the cache. /// </summary> private async Task<IList<MatchModel>> GetMatchesAsync(int profileId, MatchCriteriaModel criteria, int page, bool forceRecalc = false) { //check if matches in cache var matches = _matchCache.Get(profileId, criteria, page); if (matches == null || forceRecalc) { //cache miss await _webClient.CalculateAndSaveMatchesAsync(criteria); Session[OkbConstants.FORCE_RECALCULATE_MATCHES] = false; } //get matches from cache - 2nd try matches = _matchCache.Get(profileId, criteria, page); return matches; }
/// <summary> /// Peforms a match search for a user given their search preferences and returns a list /// of sorted matches. /// </summary> public List<MatchModel> Search(int profileId, MatchCriteriaModel criteria) { var db = new OkbDbContext(); var matches = new List<MatchModel>(); var query = BuildSearchQuery(db, criteria); var myAnswers = _matchCalc.GetAnswerDict(profileId); foreach (var p in query) { var matchResult = _matchCalc.CalculateMatchPercent(p.Id, myAnswers); matches.Add(new MatchModel { MatchPercent = matchResult.MatchPercent, FriendPercent = matchResult.FriendPercent, EnemyPercent = matchResult.EnemeyPercent, UserId = p.UserId, Nickname = p.Nickname, ProfileId = p.Id, Photo = p.GetFirstHeadshot(), Age = p.GetAge(), Gender = p.Gender, Location = _locRepo.GetLocationString(p.LocationId1, p.LocationId2) }); } //For now just sort the list by match % matches.Sort(delegate (MatchModel m1, MatchModel m2) { return m2.MatchPercent.CompareTo(m1.MatchPercent); }); return matches; }
/// <summary> /// Get the search preferences for the given. For now just keep it simple and use the /// "LookingForGender" field in the user's profile. /// </summary> public MatchCriteriaModel GetMatchCriteria(int profileId) { var db = new OkbDbContext(); var profile = db.Profiles.Find(profileId); var criteria = new MatchCriteriaModel { Gender = profile.LookingForGender, LocationId1 = 0 //we don't care about location now, show everyone }; return criteria; }
private IQueryable<Profile> BuildSearchQuery(OkbDbContext db, MatchCriteriaModel criteria) { var query = from p in db.Profiles.AsNoTracking() where p.Deleted == false select p; if (!(criteria.Gender == OkbConstants.UNKNOWN_GENDER)) { query = query.Where(p => p.Gender == criteria.Gender); } if (criteria.LocationId1 != 0) { query = query.Where(p => p.LocationId1 == criteria.LocationId1); } //Limit maximum match results returned query = query.Take(OkbConstants.MAX_MATCH_RESULTS); return query; }