Пример #1
0
        public IActionResult Convert <T>(UseCaseResponse <T> response, bool noContentIfSuccess = false)
        {
            if (response == null)
            {
                return(BuildError(new[] { new ErrorMessage("000", "ActionResultConverter Error") }, UseCaseResponseKind.InternalServerError));
            }

            if (response.ErrorMessage is null)
            {
                if (noContentIfSuccess)
                {
                    return(new NoContentResult());
                }
                else
                {
                    return(BuildSuccessResult(response.Result, response.Status));
                }
            }
            else if (response.Result != null)
            {
                return(BuildError(response.Result, response.Status));
            }
            else
            {
                var hasErrors   = response.Errors == null || !response.Errors.Any();
                var errorResult = hasErrors
                    ? new[] { new ErrorMessage("000", response.ErrorMessage ?? "Unknown error") }
                    : response.Errors;

                return(BuildError(errorResult, response.Status));
            }
        }
Пример #2
0
        public async Task <UseCaseResponse <Asset> > Execute(CreateAssetRequest request)
        {
            var response = new UseCaseResponse <Asset>();

            try
            {
                assetValidator.ValidateAndThrow(request);
                var asset = request.ToAsset();

                var createdAsset = await assetRepository.Create(asset);

                sendMessage(asset);


                return(response.SetCreated(createdAsset));
            }
            catch (ValidationException ex)
            {
                return(response.SetBadRequest("Validation exception", ex.ToErrorMessage().ToArray()));
            }
            catch (Exception e)
            {
                logger.LogError(e.Message, e);
                return(response.SetInternalServerError("Unexpected error: " + e.Message));
            }
        }
Пример #3
0
        public async Task <UseCaseResponse <IEnumerable <Asset> > > Execute()
        {
            var response = new UseCaseResponse <IEnumerable <Asset> >();

            try
            {
                var assets = await assetRepository.List();

                return(response.SetResult(assets));
            }
            catch (Exception e)
            {
                logger.LogError(e.Message, e);
                return(response.SetInternalServerError("Unexpected error: " + e.Message));
            }
        }
Пример #4
0
        public async Task <UseCaseResponse <bool> > Execute(Guid id)
        {
            var response = new UseCaseResponse <bool>();

            try
            {
                await assetRepository.Delete(id);

                return(response.SetResult(true));
            }
            catch (Exception e)
            {
                logger.LogError(e.Message, e);
                return(response.SetInternalServerError("Unexpected error: " + e.Message));
            }
        }
Пример #5
0
        /// <summary>
        /// Handles the result of the use case and prepares the presentation
        /// </summary>
        /// <param name="response">Result of the use case</param>
        public void Handle(UseCaseResponse <TResult> response)
        {
            if (response is null)
            {
                throw new System.ArgumentNullException(nameof(response));
            }

            JsonSerializerOptions serializerOptions = new JsonSerializerOptions
            {
                WriteIndented        = true,
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            };

            ContentResult.StatusCode = response.Success ? (int)(HttpStatusCode.OK) : (int)(HttpStatusCode.BadRequest);
            ContentResult.Content    = JsonSerializer.Serialize(new { response.Result, response.Success, response.Message, response.Errors }, serializerOptions);
        }
        /// <summary>
        /// Handles the result of the use case and prepares the presentation
        /// </summary>
        /// <param name="response">Result of the use case</param>
        public void Handle(UseCaseResponse <TokenDto> response)
        {
            if (response is null)
            {
                throw new System.ArgumentNullException(nameof(response));
            }

            JsonSerializerOptions serializerOptions = new JsonSerializerOptions
            {
                WriteIndented        = true,
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            };

            ContentResult.StatusCode = response.Success ? (int)(HttpStatusCode.OK) : (int)(HttpStatusCode.BadRequest);
            ContentResult.Content    = response.Success ? JsonSerializer.Serialize(new Token(response.Result.Type, response.Result.AccessToken, response.Result.RefreshToken), serializerOptions) : JsonSerializer.Serialize(response, serializerOptions);
        }
Пример #7
0
        /// <summary>
        /// Handles the result of the use case and prepares the presentation
        /// </summary>
        /// <param name="response">Result of the use case</param>
        public void Handle(UseCaseResponse <TResult> response)
        {
            if (response is null)
            {
                throw new ArgumentNullException(nameof(response));
            }

            if (response.Success)
            {
                if (response.Result == null)
                {
                    ContentResult.StatusCode = (int)HttpStatusCode.NoContent;
                    ContentResult.Content    = null;
                }
                else
                {
                    ContentResult.StatusCode = (int)HttpStatusCode.OK;

                    JsonSerializerOptions serializerOptions = new JsonSerializerOptions
                    {
                        WriteIndented        = true,
                        PropertyNamingPolicy = JsonNamingPolicy.CamelCase
                    };
                    serializerOptions.Converters.Add(new JsonStringEnumConverter());

                    TModel model = _mapper.Map <TModel>(response.Result);

                    ContentResult.Content = JsonSerializer.Serialize(model, model.GetType(), serializerOptions);
                }
            }
            else
            {
                ContentResult.StatusCode = (int)HttpStatusCode.InternalServerError;
                JsonSerializerOptions serializerOptions = new JsonSerializerOptions
                {
                    WriteIndented        = true,
                    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
                };
                ContentResult.Content = JsonSerializer.Serialize(new { response.Success, response.Message, response.Errors }, serializerOptions);
            }
        }
        public async Task <ReadReportResponse> Execute(int request)
        {
            var specification = new IdSpecification(request);

            var reportQuery = _reportRepository.Get(specification);

            var projection = reportQuery.Select(ReportsSelectors.ReportSelector);

            var reportDto = await _reportRepository.SingleOrDefaultAsync(projection);

            if (reportDto == null)
            {
                return(new ReadReportResponse
                {
                    Report = new Either <Dtos.ReportDto, UseCaseResponse>(UseCaseResponse.Null())
                });
            }

            return(new ReadReportResponse
            {
                Report = new Either <Dtos.ReportDto, UseCaseResponse>(reportDto)
            });
        }
Пример #9
0
 public static IActionResult Converter <T>(UseCaseResponse <T> response)
 => (response.Status) switch
 {