Exemplo n.º 1
0
        public async Task <ActionResult> Index(UploadTextModel model)
        {
            var textes = new List <string>();

            if (!string.IsNullOrWhiteSpace(model.Text))
            {
                textes.Add(model.Text);
            }

            if (model.FileInput1 != null)
            {
                using (var reader = new StreamReader(model.FileInput1.InputStream))
                {
                    var file1Text = await reader.ReadToEndAsync();

                    textes.Add(file1Text);
                }
            }

            if (model.FileInput2 != null)
            {
                using (var reader = new StreamReader(model.FileInput2.InputStream))
                {
                    var file2Text = await reader.ReadToEndAsync();

                    textes.Add(file2Text);
                }
            }

            if (!textes.Any())
            {
                return(this.RedirectToAction("Index"));
            }

            using (var proxy = ServiceProxy.For <ICategorizerService>())
            {
                var tasks = textes
                            .Select(text => proxy.Invoker(it => it.UploadFragment(new DtoFragment {
                    Text = text
                })))
                            .ToList();

                var fragmentResults = await Task.WhenAll(tasks);

                var isAllSuccess = fragmentResults.All(it => it != null);
                this.ViewBag.IsUploadSuccess = isAllSuccess;

                this.ViewBag.Message = isAllSuccess
                    ? string.Format(LocalizationStrings.SuccessTextSuccessfullyAdded,
                                    string.Join(", ", fragmentResults.Where(it => it != null).Select(it => it.Category.Name)))
                    : LocalizationStrings.ErrorUploadingTextIsNotCategorized;

                this.ModelState.Clear();

                TempData["Fragments"] = await this.GetLatestFragments();

                return(this.View());
            }
        }
Exemplo n.º 2
0
        private async Task <IEnumerable <DtoFragment> > GetLatestFragments()
        {
            using (var proxy = ServiceProxy.For <ICategorizerService>())
            {
                var fragments = await proxy.Invoker(it => it.GetLatestFragments(5));

                return(fragments);
            }
        }
Exemplo n.º 3
0
        public async Task <ActionResult> Index()
        {
            using (var proxy = ServiceProxy.For <ICategorizerService>())
            {
                var fragments = await proxy.Invoker(it => it.GetFragments());

                return(this.View(fragments));
            }
        }