Exemplo n.º 1
0
        public async Task <ActionResult> UrlNullResolve(int?id, AuthorResolvesViewModels.ResolvedNullUrlModel resolveModel)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            if (ModelState.IsValid)
            {
                do
                {
                    var currentAuthorResolve = await _db.AuthorsResolves.FindAsync(id);

                    if (resolveModel.SelectedAuthorId.HasValue)
                    {
                        var authorUrlName = Author.GetUrlName(resolveModel.ResolvedUrl);
                        await ApplayAuthorResolve(currentAuthorResolve, resolveModel.SelectedAuthorId.Value, resolveModel.ResolvedUrl, authorUrlName);
                    }
                    else
                    {
                        try
                        {
                            var    authorUrlName = Author.GetUrlName(resolveModel.ResolvedUrl);
                            Author author        = null;

                            var updateService = new UpdateService(AppConstants.ServerPaths.MediaDirectory, AppConstants.ServerPaths.ArchiveDirectory);
                            using (var downloadManger = new HttpDownloadManager(100, 100, 3, new InCloakWebAnonymizer()))
                            {
                                var callResult =
                                    await
                                    updateService.CreateAuthorAsync(currentAuthorResolve.TrainingProviderId, authorUrlName,
                                                                    downloadManger);

                                if (callResult.Succeeded)
                                {
                                    author = callResult.Value;
                                }
                                else
                                {
                                    ModelState.AddModelError("", string.Join(";\r\n", callResult.Errors));
                                    break;
                                }
                            }

                            await ApplayAuthorResolve(currentAuthorResolve, author.Id, resolveModel.ResolvedUrl, authorUrlName);
                        }
                        // ReSharper disable once CatchAllClause
                        catch (Exception ex)
                        {
                            ModelState.AddModelError("", ex.ToString());
                            break;
                        }
                    }

                    return(RedirectToAction("Index"));

                    // need for flow control
#pragma warning disable 162
                } while (false);
#pragma warning restore 162
            }

            var viewModel = await GetUrlNullResolveViewModel(id.Value);

            viewModel.SelectedAuthorId = resolveModel.SelectedAuthorId;
            viewModel.ResolvedUrl      = resolveModel.ResolvedUrl;

            return(View(viewModel));
        }