Esempio n. 1
0
 public async Task <IActionResult> MatchUser(Guid id, [FromBody] MatchParametersModel model)
 {
     return(Ok(await _userService.MatchUser(id, model)));
 }
        public async Task <MatchModel[]> MatchUser(Guid id, MatchParametersModel model)
        {
            try
            {
                var matchedUsers = new List <MatchModel>();
                var user         = await GetUser(id);

                var userTastes = await GetUserTastes(id);

                var topUserTastes = userTastes.OrderBy(t => t.Position).Take(3).ToArray();
                var users         = await GetAllUsers();

                foreach (var item in users)
                {
                    if (item.Id.Equals(id))
                    {
                        continue;
                    }
                    if (model != null)
                    {
                        if (item.Id.Equals(id) ||
                            model.Gender != 0 && model.Gender != item.Gender ||
                            model.Radius > 0 && ComputeDistance(user, item) > model.Radius)
                        {
                            continue;
                        }
                    }

                    var matchScore = 0.0;
                    var tastes     = await GetUserTastes(item.Id);

                    var topTastes = tastes.OrderBy(t => t.Position).Take(3).ToArray();
                    if (!topUserTastes.Select(t => t.FamilyId).Any(x => topTastes.Select(t => t.FamilyId).Any(y => y == x)))
                    {
                        continue;
                    }
                    foreach (var taste in topUserTastes)
                    {
                        var matchedTaste = topTastes.SingleOrDefault(t => t.FamilyId.Equals(taste.FamilyId));
                        if (matchedTaste == null)
                        {
                            continue;
                        }
                        matchScore += ComputeMatchScore(matchedTaste.Position, taste.Position);
                    }
                    if (matchScore > 0)
                    {
                        matchedUsers.Add(new MatchModel
                        {
                            User       = item,
                            MatchScore = matchScore
                        });
                    }
                }
                return(matchedUsers.OrderByDescending(m => m.MatchScore).ToArray());
            }
            catch (HttpStatusCodeException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new HttpStatusCodeException(StatusCodes.Status500InternalServerError, e.Message);
            }
        }