Exemple #1
0
        //[ValidateAntiForgeryToken]
        public async Task <IActionResult> Post()
        {
            try
            {
                // Validate user name and authentication status
                var userId = User?.Identity?.Name;
                if (string.IsNullOrEmpty(userId))
                {
                    Log.Debug("Import: No value found for user principal name");
                    return(Unauthorized());
                }

                if (User?.Identity?.IsAuthenticated != true)
                {
                    Log.Debug("Import: User identity is not authenticated");
                    return(Unauthorized());
                }

                // Retrieve settings
                var userSettings = await _userStore.GetUserSettingsAsync(userId);

                if (userSettings == null)
                {
                    Log.Debug("Import: User settings not found.");
                    return(Unauthorized());
                }

                var parserResult = await _parser.ParseImportFormAsync(Request, userId,
                                                                      async (formData, formCollection) =>
                {
                    var formValueProvider = new FormValueProvider(
                        BindingSource.Form,
                        formCollection,
                        CultureInfo.CurrentCulture);
                    return(await TryUpdateModelAsync(formData, "", formValueProvider));
                });

                if (!parserResult.IsValid)
                {
                    if (!ModelState.IsValid)
                    {
                        return(BadRequest(ModelState));
                    }
                    return(BadRequest(parserResult.ValidationErrors));
                }

                var jobRequest = parserResult.ImportJobRequest;

                // Create a settings record for the repository if one does not already exist
                try
                {
                    await _importService.CheckRepoSettingsAsync(User, jobRequest.OwnerId, jobRequest.RepositoryId);
                }
                catch (Exception)
                {
                    Log.Error(
                        $"api/data: unable to retrieve repoSettings for the supplied owner '{jobRequest.OwnerId}' and repo '{jobRequest.RepositoryId}'");
                    return(BadRequest(
                               "Repository does not exist or you do not have the required authorization to publish to it."));
                }

                // create a settings record for the owner if one does not already exist
                try
                {
                    await _importService.CheckOwnerSettingsAsync(User, jobRequest.OwnerId);
                }
                catch (Exception)
                {
                    Log.Error(
                        $"api/data: unable to retrieve ownerSettings for the supplied owner '{jobRequest.OwnerId}'");
                    return(BadRequest(
                               "The user or organization does not exist or you do not have the required authorization to publish to it."));
                }

                var job = await _jobStore.SubmitImportJobAsync(jobRequest);

                Log.Information("api/data(POST): Conversion job started.");

                var queuedJobIds = new List <string> {
                    job.JobId
                };

                if (parserResult.SchemaImportJobRequest != null)
                {
                    try
                    {
                        var schemaJob = await _jobStore.SubmitSchemaImportJobAsync(parserResult.SchemaImportJobRequest);

                        Log.Information("api/data(POST): Schema creation job started.");
                        queuedJobIds.Add(schemaJob.JobId);
                    }
                    catch (Exception)
                    {
                        Log.Error("api/data(POST): Unexpected error staring schema creation job.");
                    }
                }

                return(Ok(new DataControllerResult {
                    StatusCode = 200, Message = "API called successfully", Metadata = parserResult.Metadata, JobIds = queuedJobIds
                }));
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, $"Fatal error in api/data '{ex.Message}'");
                return(StatusCode(StatusCodes.Status500InternalServerError, ex));
            }
        }