public CircleGetCandidatesOutputData Handle(CircleGetCandidatesInputData inputData)
        {
            var circle = context.Circles
                .Include(x => x.CircleMembers)
                .ThenInclude(cm => cm.Circle)
                .FirstOrDefault(c => c.Id == inputData.CircleId);

            if (circle == null)
            {
                throw new CircleNotFoundException(new CircleId(inputData.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 = inputData.Page;
            var size = inputData.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 CircleGetCandidatesOutputData(candidates);
        }
Beispiel #2
0
        public CircleGetCandidatesResponseModel GetCandidates(string id, [FromQuery] int page, [FromQuery] int size)
        {
            var command = new CircleGetCandidatesInputData(id, page, size);
            var result  = bus.Handle(command);

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

            return(new CircleGetCandidatesResponseModel(users));
        }
Beispiel #3
0
        public CircleGetCandidatesOutputData Handle(CircleGetCandidatesInputData inputData)
        {
            var circleId = new CircleId(inputData.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((inputData.Page - 1) * inputData.Size)
                             .Take(inputData.Size);

            var candidatesData = candidates.Select(toUserData).ToList();

            return(new CircleGetCandidatesOutputData(candidatesData));
        }
        public CircleGetCandidatesOutputData Handle(CircleGetCandidatesInputData inputData)
        {
            var circleId = new CircleId(inputData.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 = inputData.Page;
                var size = inputData.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 CircleGetCandidatesOutputData(members));
                }
            }
        }