Example #1
0
        public CircleGetCandidatesResponseModel GetCandidates(string id, [FromQuery] int page, [FromQuery] int size)
        {
            var command = new CircleGetCandidatesCommand(id, page, size);
            var result  = circleQueryService.GetCandidates(command);

            var users = result.Users.Select(x => new UserResponseModel(x)).ToList();

            return(new CircleGetCandidatesResponseModel(users));
        }
        public CircleGetCandidatesResult GetCandidates(CircleGetCandidatesCommand command)
        {
            var circleId = new CircleId(command.CircleId);
            var circle   = circleRepository.Find(circleId);

            var members    = circle.Members.Concat(new[] { circle.Owner }).ToHashSet();
            var candidates = userRepository.FindAll()
                             .Where(x => !members.Contains(x.Id))
                             .Skip((command.Page - 1) * command.Size)
                             .Take(command.Size);

            var candidatesData = candidates.Select(x => new UserData(x)).ToList();

            return(new CircleGetCandidatesResult(candidatesData));
        }
        public CircleGetCandidatesResult GetCandidates(CircleGetCandidatesCommand command)
        {
            var circle = context.Circles
                         .Include(x => x.CircleMembers)
                         .ThenInclude(cm => cm.Circle)
                         .FirstOrDefault(c => c.Id == command.CircleId);

            if (circle == null)
            {
                throw new CircleNotFoundException(new CircleId(command.CircleId));
            }

            var members = circle.CircleMembers
                          .Select(m => m.UserId);

            if (circle.OwnerId != null)
            {
                members = members.Concat(new List <string> {
                    circle.OwnerId
                });
            }

            var exceptUsers = members.ToHashSet();

            var page = command.Page;
            var size = command.Size;

            var chunk = context.Users
                        .Where(u => !exceptUsers.Contains(u.Id))
                        .Skip((page - 1) * size)
                        .Take(size)
                        .ToList();

            var candidates = chunk.Select(x => new UserData(x.Id, x.Name)).ToList();

            return(new CircleGetCandidatesResult(candidates));
        }
        public CircleGetCandidatesResult GetCandidates(CircleGetCandidatesCommand command)
        {
            var circleId = new CircleId(command.CircleId);
            var circle   = circleRepository.Find(circleId);

            if (circle == null)
            {
                throw new CircleNotFoundException(circleId);
            }

            using (var sqlCommand = provider.Connection.CreateCommand())
            {
                var parameterNames = circle.Members.Select((_, i) => "@member" + i).ToList();

                var selectQuery = new StringBuilder(@" SELECT * FROM users");

                var appendWhere = false;
                if (circle.ExistsOwner())
                {
                    selectQuery.Append(" WHERE id <> @ownerId");
                    appendWhere = true;
                }

                if (parameterNames.Any())
                {
                    var inParameters = string.Join(",", parameterNames);
                    var inPhrase     = new StringBuilder();
                    if (appendWhere)
                    {
                        inPhrase.Append(" AND");
                    }
                    inPhrase.Append($" id NOT IN ({inParameters})");

                    if (!appendWhere)
                    {
                        selectQuery.Append(" WHERE");
                        appendWhere = true;
                    }

                    selectQuery.Append(inPhrase);
                }
                selectQuery.Append(@"
 ORDER BY id
   OFFSET @skip ROWS
   FETCH NEXT @size ROWS ONLY
");

                sqlCommand.CommandText = selectQuery.ToString();

                if (circle.ExistsOwner())
                {
                    sqlCommand.Parameters.Add(new SqlParameter("@ownerId", circle.Owner.Value));
                }

                foreach (var tpl in circle.Members.Zip(parameterNames, (memberId, parameter) => new { memberId, parameter }))
                {
                    sqlCommand.Parameters.Add(new SqlParameter(tpl.parameter, tpl.memberId.Value));
                }

                var page = command.Page;
                var size = command.Size;
                sqlCommand.Parameters.Add(new SqlParameter("@skip", (page - 1) * size));
                sqlCommand.Parameters.Add(new SqlParameter("@size", size));

                using (var reader = sqlCommand.ExecuteReader())
                {
                    var members = new List <UserData>();
                    while (reader.Read())
                    {
                        var id   = (string)reader["id"];
                        var name = (string)reader["name"];
                        var data = new UserData(id, name);

                        members.Add(data);
                    }

                    return(new CircleGetCandidatesResult(members));
                }
            }
        }