Example #1
0
        public static Task <ServiceResult <GetWidgetBatchResponseDto> > GetWidgetBatchAsync(this IExampleApi service, IEnumerable <string> ids, CancellationToken cancellationToken = default(CancellationToken))
        {
            var request = new GetWidgetBatchRequestDto {
                Ids = ids?.ToList()
            };

            return(service.GetWidgetBatchAsync(request, cancellationToken));
        }
        public async Task <ServiceResult <GetWidgetBatchResponseDto> > GetWidgetBatchAsync(GetWidgetBatchRequestDto request, CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (request.Ids == null)
            {
                return(ServiceResult.Failure(ServiceErrors.CreateRequestFieldRequired("ids")));
            }
            if (request.Ids.Count == 0)
            {
                return(ServiceResult.Failure(ExampleApiErrors.CreateInvalidRequestMissingWidgetIds()));
            }

            var widgets = await m_repository.GetWidgetBatchAsync(request.Ids, cancellationToken).ConfigureAwait(false);

            return(ServiceResult.Success(new GetWidgetBatchResponseDto
            {
                Results = widgets.Select((x, i) =>
                                         x != null ? ServiceResult.Success(x) :
                                         string.IsNullOrEmpty(request.Ids[i]) ? ServiceResult.Failure(ExampleApiErrors.CreateInvalidRequestMissingWidgetId()) :
                                         ServiceResult.Failure(ExampleApiErrors.CreateNotFoundWidget(request.Ids[i]))).ToList()
            }));
        }