/// <summary>
        /// Gets or creates the categories data source.
        /// </summary>
        /// <returns></returns>
        private static async Task <DataSourceResource> GetOrCreateCategoriesDataSource()
        {
            var categories = await MConnector.Client.DataSources.Get(Constants.DataSources.RecipeCategories);

            if (categories != null)
            {
                return(categories);
            }

            var categoriesResource = new DataSourceResource()
            {
                Name          = Constants.DataSources.RecipeCategories,
                Type          = DataSourceType.Flat,
                IsSystemOwned = false,
                Values        = new List <DataSourceValue>()
                {
                    new DataSourceValue()
                    {
                        Identifier = "Asian",
                        Labels     = new Dictionary <CultureInfo, string>()
                        {
                            { Constants.DefaultCulture, "Asian" }
                        }
                    },
                    new DataSourceValue()
                    {
                        Identifier = "Paleo",
                        Labels     = new Dictionary <CultureInfo, string>()
                        {
                            { Constants.DefaultCulture, "Paleo" }
                        }
                    },
                    new DataSourceValue()
                    {
                        Identifier = "Southern",
                        Labels     = new Dictionary <CultureInfo, string>()
                        {
                            { Constants.DefaultCulture, "Southern" }
                        }
                    }
                }
            };

            // Create and return the categories datasource
            await MConnector.Client.DataSources.Create(categoriesResource);

            return(await MConnector.Client.DataSources.Get(Constants.DataSources.RecipeCategories));

            #endregion
        }
Exemple #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));
        }