Esempio n. 1
0
        public async Task <ServiceResult <GetWidgetResponseDto> > GetWidgetAsync(GetWidgetRequestDto request, CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (string.IsNullOrEmpty(request.Id))
            {
                return(ServiceResult.Failure(ExampleApiErrors.CreateInvalidRequestMissingWidgetId()));
            }

            var widget = await m_repository.TryGetWidgetAsync(request.Id, cancellationToken).ConfigureAwait(false);

            if (widget == null)
            {
                return(ServiceResult.Failure(ExampleApiErrors.CreateNotFoundWidget(request.Id)));
            }

            string eTag = CreateWidgetETag(widget);

            if (request.IfNoneMatch == eTag)
            {
                return(ServiceResult.Success(new GetWidgetResponseDto {
                    NotModified = true, ETag = eTag
                }));
            }

            return(ServiceResult.Success(new GetWidgetResponseDto {
                Widget = widget, ETag = eTag
            }));
        }
Esempio n. 2
0
        public async Task <ServiceResult <EditWidgetResponseDto> > EditWidgetAsync(EditWidgetRequestDto request, CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (string.IsNullOrEmpty(request.Id))
            {
                return(ServiceResult.Failure(ExampleApiErrors.CreateInvalidRequestMissingWidgetId()));
            }

            return(ServiceResult.Success(await m_repository.EditWidgetAsync(request.Id, request.Ops, cancellationToken).ConfigureAwait(false)));
        }
Esempio n. 3
0
        public async Task <ServiceResult <DeleteWidgetResponseDto> > DeleteWidgetAsync(DeleteWidgetRequestDto request, CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (string.IsNullOrEmpty(request.Id))
            {
                return(ServiceResult.Failure(ExampleApiErrors.CreateInvalidRequestMissingWidgetId()));
            }

            bool wasDeleted = await m_repository.TryDeleteWidgetAsync(request.Id, cancellationToken).ConfigureAwait(false);

            if (!wasDeleted)
            {
                return(ServiceResult.Failure(ExampleApiErrors.CreateNotFoundWidget(request.Id)));
            }

            return(ServiceResult.Success(new DeleteWidgetResponseDto()));
        }
Esempio n. 4
0
        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()
            }));
        }