Exemplo n.º 1
0
        public object Patch(string id, [FromBody] dynamic model)
        {
            EntryId entryId = new EntryId(id);

            Site site = entryId.SiteId == null ? null : SiteHelper.GetSite(entryId.SiteId.Value);

            if (entryId.SiteId != null && site == null)
            {
                Context.Response.StatusCode = (int)HttpStatusCode.NotFound;
                return(null);
            }

            ModuleHelper.EnsureValidScope(site, entryId.Path);

            // Get the enabled modules
            string        configPath = ManagementUnit.ResolveConfigScope(model);
            List <Module> modules    = ModuleHelper.GetModules(site, entryId.Path, configPath);

            Module module = modules.FirstOrDefault(m => m.Name.Equals(entryId.Name));

            if (module == null)
            {
                return(NotFound());
            }

            ModuleHelper.UpdateModule(module, model, site);

            ManagementUnit.Current.Commit();

            //
            // Create response
            dynamic entry = (dynamic)ModuleHelper.ModuleToJsonModel(module, site, entryId.Path);

            if (entry.id != id)
            {
                return(LocationChanged(ModuleHelper.GetModuleEntryLocation(entry.id), entry));
            }

            return(entry);
        }
Exemplo n.º 2
0
        public object Post([FromBody] dynamic model)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }
            if (model.modules == null || !(model.modules is JObject))
            {
                throw new ApiArgumentException("modules");
            }

            string modulesUuid = DynamicHelper.Value(model.modules.id);

            if (modulesUuid == null)
            {
                throw new ApiArgumentException("modules.id");
            }

            // Get the feature id
            Module    module    = null;
            ModulesId modulesId = new ModulesId(modulesUuid);
            Site      site      = modulesId.SiteId == null ? null : SiteHelper.GetSite(modulesId.SiteId.Value);

            if (modulesId.SiteId != null && site == null)
            {
                Context.Response.StatusCode = (int)HttpStatusCode.NotFound;
                return(null);
            }

            ModuleHelper.EnsureValidScope(site, modulesId.Path);

            string         configPath = ManagementUnit.ResolveConfigScope(model);
            ModulesSection section    = ModuleHelper.GetModulesSection(site, modulesId.Path, configPath);

            // The post could either be creating a Managed module, or adding an existing
            // global module to the modules list.
            // This information is taken from the model's type value
            string type = DynamicHelper.Value(model.type);

            if (string.IsNullOrEmpty(type))
            {
                // The module being added is a global/native module

                string name = DynamicHelper.Value(model.name);

                if (string.IsNullOrEmpty(name))
                {
                    throw new ApiArgumentException("name");
                }

                GlobalModule existingGlobalModule = ModuleHelper.GetGlobalModules().FirstOrDefault(m => m.Name.Equals(name));

                // Adding a global module to the modules list means it must already exist in global modules
                if (existingGlobalModule == null)
                {
                    throw new NotFoundException("name");
                }

                // Add the existing global module
                module = ModuleHelper.AddExistingGlobalModule(existingGlobalModule, section);
                ManagementUnit.Current.Commit();
            }
            else
            {
                // Module being added to enabled modules is a managed module

                // Create module from model
                module = ModuleHelper.CreateManagedModule(model, section);

                // Save it
                ModuleHelper.AddManagedModule(module, section);
                ManagementUnit.Current.Commit();
            }


            //
            // Create response
            dynamic moduleEntry = ModuleHelper.ModuleToJsonModel(module, site, modulesId.Path);

            return(Created(ModuleHelper.GetModuleEntryLocation(moduleEntry.id), moduleEntry));
        }
        private void ConfigureModuleEntries()
        {
            // Top level resource routes for plugin
            Environment.Host.RouteBuilder.MapWebApiRoute(Defines.ModuleEntriesResource.Guid, $"{ Defines.MODULE_ENTRIES_PATH}/{{id?}}", new { controller = "modules" });

            Environment.Hal.ProvideLink(Defines.ModuleEntriesResource.Guid, "self", entry => new { href = ModuleHelper.GetModuleEntryLocation(entry.id) });

            Environment.Hal.ProvideLink(Defines.ModulesResource.Guid, Defines.ModuleEntriesResource.Name, module => new { href = $"/{Defines.MODULE_ENTRIES_PATH}?{Defines.MODULES_IDENTIFIER}={module.id}" });
        }