Пример #1
0
        public async Task <IActionResult> GetDataSourceById(string guid)
        {
            if (!Guid.TryParse(guid, out Guid _))
            {
                ProblemDetails problem = new ProblemDetails
                {
                    Title    = "Specified guid is not valid.",
                    Detail   = "The specified guid is not a real or valid guid.",
                    Instance = "64052C41-FB93-4733-918F-056C765044AE"
                };
                return(BadRequest(problem));
            }

            IDataSourceAdaptee dataSource = await dataProviderService.RetrieveDataSourceByGuid(guid);

            if (dataSource == null)
            {
                ProblemDetails problem = new ProblemDetails
                {
                    Title    = "No data source with the specified guid found.",
                    Detail   = "The database does not contain an institution with that guid.",
                    Instance = "3B2C12E3-CDE0-4853-A687-C1024E096479"
                };
                return(NotFound(problem));
            }

            DataSourceResourceResult dataSourceResourceResult =
                mapper.Map <IDataSourceAdaptee, DataSourceResourceResult>(dataSource);

            return(Ok(dataSourceResourceResult));
        }
Пример #2
0
        public async Task <IActionResult> UpdateDataSource(string guid, [FromBody] DataSourceResource dataSourceResource)
        {
            if (!Guid.TryParse(guid, out Guid _))
            {
                ProblemDetails problem = new ProblemDetails
                {
                    Title    = "Specified guid is not valid.",
                    Detail   = "The specified guid is not a real or valid guid.",
                    Instance = "F472CEEC-BBC7-41A7-87C9-24B669DB9D80"
                };
                return(BadRequest(problem));
            }

            DataSource dataSourceModel = await dataSourceModelService.GetDataSourceByGuid(guid);

            if (dataSourceModel == null)
            {
                ProblemDetails problem = new ProblemDetails
                {
                    Title    = "Failed retrieving the data source.",
                    Detail   = "The database does not contain an institution with that guid.",
                    Instance = "031FE0E3-D8CF-4DEC-81D5-E89B33BED8D0"
                };
                return(NotFound(problem));
            }

            DataSource dataSourceWithSpecifiedName =
                await dataSourceModelService.GetDataSourceByName(dataSourceResource.Title);

            if (dataSourceWithSpecifiedName != null &&
                dataSourceWithSpecifiedName.Guid != guid)
            {
                ProblemDetails problem = new ProblemDetails
                {
                    Title  = "Specified name of the data source already exists",
                    Detail =
                        "Another data source already has the specified name, no doubles are allowed",
                    Instance = "804F134C-E679-4AF5-B602-18433F26019A"
                };
                return(BadRequest(problem));
            }

            if (dataSourceResource.WizardPageResources != null &&
                dataSourceResource.WizardPageResources.Any() && !await wizardPageService.ValidateWizardPagesExist(
                    dataSourceResource.WizardPageResources.Select(w => w.WizardPageId)))
            {
                ProblemDetails problem = new ProblemDetails
                {
                    Title    = "Not all specified wizard pages could be found.",
                    Detail   = "One or more specified wizard page ids don't exist",
                    Instance = "EF1490B0-DB22-4A0D-B6D1-D6E89192381E"
                };
                return(NotFound(problem));
            }

            if (dataSourceResource.IconId != 0)
            {
                File file = await fileService.FindAsync(dataSourceResource.IconId);

                if (file != null)
                {
                    dataSourceModel.Icon = file;
                }
                else
                {
                    ProblemDetails problem = new ProblemDetails
                    {
                        Title    = "File was not found.",
                        Detail   = "The specified file was not found while updating project.",
                        Instance = "7A6BF2DE-A0BC-4C84-8CC4-89EC0C706EAB"
                    };
                    return(NotFound(problem));
                }
            }

            int[] wizardPageOrderIndexesAuthFlow = dataSourceResource.WizardPageResources?.Where(p => p.AuthFlow)
                                                   .Select(p => p.OrderIndex)
                                                   .ToArray();
            int[] wizardPageOrderIndexesPublicFlow = dataSourceResource.WizardPageResources?.Where(p => !p.AuthFlow)
                                                     .Select(p => p.OrderIndex)
                                                     .ToArray();

            bool authFlowIsValid = wizardPageOrderIndexesAuthFlow?.Length == 0 ||
                                   indexOrderHelper.ValidateAscendingConsecutiveOrder(wizardPageOrderIndexesAuthFlow,
                                                                                      1);

            bool publicFlowIsValid = wizardPageOrderIndexesPublicFlow?.Length == 0 ||
                                     indexOrderHelper.ValidateAscendingConsecutiveOrder(
                wizardPageOrderIndexesPublicFlow,
                1);

            if (!authFlowIsValid ||
                !publicFlowIsValid)
            {
                ProblemDetails problem = new ProblemDetails
                {
                    Title  = "The order from the wizard page indexes is invalid.",
                    Detail =
                        "The order indexes from the wizard pages should start at 1, be consecutive and have no doubles.",
                    Instance = "A5F70346-8044-42AC-8BFD-76FCD108ABBE"
                };
                return(BadRequest(problem));
            }

            mapper.Map(dataSourceResource, dataSourceModel);

            dataSourceModelService.Update(dataSourceModel);
            dataSourceModelService.Save();

            DataSource updatedDataSourceModel = await dataSourceModelService.GetDataSourceByGuid(guid);

            DataSourceResourceResult model = mapper.Map <DataSource, DataSourceResourceResult>(updatedDataSourceModel);

            return(Ok(model));
        }