示例#1
0
        public async Task <IActionResult> Post([FromForm] IFormFile file, [FromRoute] string key,
                                               [FromRoute] string inputType)
        {
            async Task <Either <Error, ImmutableList <object> > > Parse <T>(string k, IFormFile f) where T : IStandardModel
            {
                return(await(await _parserFactory.GetParserAsync <T>(k))
                       .Bind(p => p.Parse(LoadFileContent(f)))
                       .AsTask()
                       .MapT(p => p.Select(e => e as object).ToImmutableList()));
            }

            return(await(ShouldNotNull(file), ShouldNotNullOrEmpty(key), ShouldNotNullOrEmpty(inputType))
                   .Apply((f, k, t) => (f, k, t))
                   .MatchAsync(async d =>
            {
                if (!Enum.TryParse(d.t, true, out InputType inputTypeEnum) ||
                    inputTypeEnum == InputType.NotSupported)
                {
                    return Left <Error, string>(
                        $"Not supported input type: {inputTypeEnum.ToString()}");
                }

                var parsedResult = inputTypeEnum switch
                {
                    InputType.VendorPayment => await Parse <VendorPayment>(d.k, d.f),
                    InputType.PosPay => await Parse <PosPay>(d.k, d.f),
                    InputType.VendorMaster => await Parse <VendorMaster>(d.k, d.f),
                    InputType.PackagingDocument => await Parse <PackagingDocument>(d.k, d.f),
                    _ => Left <Error, ImmutableList <object> >(
                        $"Not supported input type: {inputTypeEnum.ToString()}"),
                };

                return await parsedResult
                .MatchAsync(async pd => (await _exportService.ExportAsync(d.t, pd)).Map(Encoding.UTF8.GetString)
                            , Left <Error, string>);
            }, errors => Left <Error, string>(errors.Join()))
                   .ToActionResultAsync());
        }