Exemplo n.º 1
0
        public ShortUrlListResult FindByUser(string userName, int start, int max)
        {
            ShortUrlListResult result = Validation.Validate <ShortUrlListResult>(() => string.IsNullOrWhiteSpace(userName), "userName", TextMessages.UserNameCannotBeBlank)
                                        .And(() => start < 0, "start", TextMessages.StartIndexCannotBeNegative)
                                        .And(() => max <= 0, "max", TextMessages.MaximumNumberOfUrlsMustBePositive)
                                        .Result();

            if (result.RuleViolations.IsEmpty())
            {
                User user;

                // Ensure user is not locked out.
                result = ValidateUserName <ShortUrlListResult>(userName, out user);

                if (result.RuleViolations.IsEmpty())
                {
                    PagedResult <ShortUrl> pagedResult = shortUrlRepository.FindByUserId(user.Id, start, max);

                    // Convert it to DTOs
                    IEnumerable <ShortUrlDTO> dtos = pagedResult.Result
                                                     .SelectMany(shortUrl => shortUrl.Aliases.Where(alias => (alias.User != null) && alias.User.Id == user.Id))
                                                     .Distinct()
                                                     .Select(CreateShortUrlDTO);

                    result = new ShortUrlListResult(new PagedResult <ShortUrlDTO>(dtos, pagedResult.Total));
                }
            }

            return(result);
        }