From 6eb81a5eebc457a2a75d0b703dc4ee02fb502c8d Mon Sep 17 00:00:00 2001 From: Tobias Klika Date: Tue, 27 Oct 2020 15:47:23 +0100 Subject: [PATCH] try rewirte of all backoffice controllers to ApiControllers --- zero.Core/Api/ApplicationContext.cs | 12 +++-- .../BackofficeJsonSerlializerSettings.cs | 12 ++--- zero.Core/Routing/AbtractRouteProvider.cs | 2 +- .../Routing/Page/PageEndpointResolver.cs | 2 +- zero.Core/zero.Core.csproj | 4 +- zero.Web.UI/App/resources/applications.js | 10 +++- .../Controllers/ApplicationsController.cs | 50 ++++++------------- .../Controllers/AuthenticationController.cs | 48 ++++-------------- zero.Web/Controllers/BackofficeController.cs | 28 +++++------ zero.Web/Controllers/CountriesController.cs | 24 ++++----- zero.Web/Controllers/IndexController.cs | 32 ------------ zero.Web/Controllers/LanguagesController.cs | 49 +++++------------- zero.Web/Controllers/MediaController.cs | 20 ++++---- zero.Web/Controllers/MediaFolderController.cs | 15 +++--- zero.Web/Controllers/ModulesController.cs | 11 ++-- zero.Web/Controllers/PageTreeController.cs | 6 +-- zero.Web/Controllers/PagesController.cs | 33 +++++++----- zero.Web/Controllers/PreviewController.cs | 11 ++-- zero.Web/Controllers/RecycleBinController.cs | 17 ++++--- zero.Web/Controllers/SectionsController.cs | 8 ++- zero.Web/Controllers/SettingsController.cs | 9 ++-- zero.Web/Controllers/SpacesController.cs | 28 +++-------- .../Controllers/TranslationsController.cs | 31 +++--------- zero.Web/Controllers/UserRolesController.cs | 12 +++-- zero.Web/Controllers/UsersController.cs | 32 ++++++------ zero.Web/Controllers/UtilsController.cs | 5 +- .../Controllers/ZeroBackofficeController.cs | 35 +++++++++++++ ...ZeroBackofficeControllerModelConvention.cs | 33 ++++++++++++ zero.Web/Controllers/ZeroController.cs | 4 -- ...ontroller.cs => ZeroFrontendController.cs} | 4 +- ...upController.cs => ZeroSetupController.cs} | 10 ++-- zero.Web/Controllers/ZeroVueController.cs | 4 +- zero.Web/Views/{Shared => Zero}/Index.cshtml | 0 zero.Web/Views/{Shared => Zero}/Setup.cshtml | 0 zero.Web/ZeroApplicationBuilderExtensions.cs | 30 +++++------ zero.Web/ZeroBuilder.cs | 30 ++++++++--- zero.Web/zero.Web.csproj | 8 +-- 37 files changed, 315 insertions(+), 354 deletions(-) delete mode 100644 zero.Web/Controllers/IndexController.cs create mode 100644 zero.Web/Controllers/ZeroBackofficeController.cs create mode 100644 zero.Web/Controllers/ZeroBackofficeControllerModelConvention.cs rename zero.Web/Controllers/{DefaultRouteController.cs => ZeroFrontendController.cs} (77%) rename zero.Web/Controllers/{SetupController.cs => ZeroSetupController.cs} (79%) rename zero.Web/Views/{Shared => Zero}/Index.cshtml (100%) rename zero.Web/Views/{Shared => Zero}/Setup.cshtml (100%) diff --git a/zero.Core/Api/ApplicationContext.cs b/zero.Core/Api/ApplicationContext.cs index 1b0b99d1..e8ccb2e6 100644 --- a/zero.Core/Api/ApplicationContext.cs +++ b/zero.Core/Api/ApplicationContext.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Extensions; using Microsoft.AspNetCore.Identity; +using Microsoft.Extensions.Logging; using Raven.Client.Documents; using Raven.Client.Documents.Session; using System; @@ -29,15 +30,18 @@ namespace zero.Core.Api protected UserManager UserManager { get; private set; } + protected ILogger Logger { get; private set; } + static IList Apps { get; set; } - public ApplicationContext(IDocumentStore raven, IZeroOptions options, UserManager userManager) + public ApplicationContext(IDocumentStore raven, IZeroOptions options, UserManager userManager, ILogger logger) { Raven = raven; Options = options; UserManager = userManager; + Logger = logger; } @@ -62,7 +66,9 @@ namespace zero.Core.Api if (app == null) { - throw new NullReferenceException($"Could not resolve application for host {context.Request.Host}"); + Logger.LogWarning($"Could not resolve application for host $host", context.Request.Host); + IList apps = await GetApplications(); + app = apps.FirstOrDefault(); } App = app; @@ -171,7 +177,7 @@ namespace zero.Core.Api using IAsyncDocumentSession session = Raven.OpenAsyncSession(); IApplication app = await session.LoadAsync(appId); - return new ApplicationContext(Raven, Options, UserManager) + return new ApplicationContext(Raven, Options, UserManager, Logger) { App = app, AppId = appId diff --git a/zero.Core/Options/BackofficeJsonSerlializerSettings.cs b/zero.Core/Options/BackofficeJsonSerlializerSettings.cs index 12d8fca3..9fb28d33 100644 --- a/zero.Core/Options/BackofficeJsonSerlializerSettings.cs +++ b/zero.Core/Options/BackofficeJsonSerlializerSettings.cs @@ -7,19 +7,17 @@ namespace zero.Core.Options { public class BackofficeJsonSerlializerSettings : JsonSerializerSettings { - public BackofficeJsonSerlializerSettings() : this(false) { } - - public BackofficeJsonSerlializerSettings(bool typed) + public BackofficeJsonSerlializerSettings() { - Setup(this, typed); + Setup(this); } - public static void Setup(JsonSerializerSettings settings, bool typed = false) + public static void Setup(JsonSerializerSettings settings) { settings.Converters.Add(new StringEnumConverter(new CamelCaseNamingStrategy())); - settings.Converters.Add(new RefJsonConverter()); - settings.ContractResolver = new ZeroJsonContractResolver(); + //settings.Converters.Add(new RefJsonConverter()); + //settings.ContractResolver = new ZeroJsonContractResolver(); settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; settings.TypeNameHandling = TypeNameHandling.Objects; //settings.SerializationBinder = new ZeroInterfaceBinder(); diff --git a/zero.Core/Routing/AbtractRouteProvider.cs b/zero.Core/Routing/AbtractRouteProvider.cs index 1ac08194..3231b66d 100644 --- a/zero.Core/Routing/AbtractRouteProvider.cs +++ b/zero.Core/Routing/AbtractRouteProvider.cs @@ -25,7 +25,7 @@ namespace zero.Core.Routing AffectedTypes = new Type[1] { typeof(T) }; DefaultEndpoint = new RouteProviderEndpoint() { - Controller = "DefaultRoute", + Controller = "ZeroFrontend", Action = "Index" }; } diff --git a/zero.Core/Routing/Page/PageEndpointResolver.cs b/zero.Core/Routing/Page/PageEndpointResolver.cs index 162d6f33..a4ba0d98 100644 --- a/zero.Core/Routing/Page/PageEndpointResolver.cs +++ b/zero.Core/Routing/Page/PageEndpointResolver.cs @@ -9,7 +9,7 @@ { DefaultEndpoint = new RouteProviderEndpoint() { - Controller = "DefaultRoute", + Controller = "ZeroFrontend", Action = "Index" }; } diff --git a/zero.Core/zero.Core.csproj b/zero.Core/zero.Core.csproj index 7e271945..cb75fc87 100644 --- a/zero.Core/zero.Core.csproj +++ b/zero.Core/zero.Core.csproj @@ -20,8 +20,8 @@ - - + + diff --git a/zero.Web.UI/App/resources/applications.js b/zero.Web.UI/App/resources/applications.js index da8787d8..53fea40f 100644 --- a/zero.Web.UI/App/resources/applications.js +++ b/zero.Web.UI/App/resources/applications.js @@ -17,9 +17,15 @@ export default { }, // get all applications - getAll(query) + getAll() { - return Axios.get(base + 'getAll', { params: query }).then(res => Promise.resolve(res.data)); + return Axios.get(base + 'getAll').then(res => Promise.resolve(res.data)); + }, + + // get applications by query + getByQuery(query) + { + return Axios.get(base + 'getByQuery', { params: query }).then(res => Promise.resolve(res.data)); }, // get all application features diff --git a/zero.Web/Controllers/ApplicationsController.cs b/zero.Web/Controllers/ApplicationsController.cs index e25bee81..8da6454d 100644 --- a/zero.Web/Controllers/ApplicationsController.cs +++ b/zero.Web/Controllers/ApplicationsController.cs @@ -1,9 +1,11 @@ using Microsoft.AspNetCore.Mvc; +using System.Collections.Generic; using System.Threading.Tasks; using zero.Core.Api; using zero.Core.Entities; using zero.Core.Extensions; using zero.Core.Identity; +using zero.Web.Models; namespace zero.Web.Controllers { @@ -11,58 +13,34 @@ namespace zero.Web.Controllers public class ApplicationsController : BackofficeController { IApplicationsApi Api; - IApplication Blueprint; - public ApplicationsController(IApplicationsApi api, IApplication blueprint) + public ApplicationsController(IApplicationsApi api) { Api = api; - Blueprint = blueprint; } - - /// - /// Get translation by id - /// - public IActionResult GetEmpty() => Edit(Blueprint.Clone()); + public EditModel GetEmpty([FromServices] IApplication blueprint) => Edit(blueprint); - /// - /// Get translation by id - /// - public async Task GetById([FromQuery] string id) => Edit(await Api.GetById(id)); + public async Task> GetById([FromQuery] string id) => Edit(await Api.GetById(id)); - /// - /// Get all translations - /// - public async Task GetAll([FromQuery] ListQuery query = null) - { - if (query == null) - { - return Json(await Api.GetAll()); - } - - return Json(await Api.GetByQuery(query)); - } + public async Task> GetAll() => await Api.GetAll(); - /// - /// Get all available features to select - /// - public IActionResult GetAllFeatures() => Json(Options.Features.GetAllItems()); + public async Task> GetByQuery([FromQuery] ListQuery query) => await Api.GetByQuery(query); - /// - /// Save translation - /// + public IReadOnlyCollection GetAllFeatures() => Options.Features.GetAllItems(); + + + [HttpPost] [ZeroAuthorize(Permissions.Settings.Applications, PermissionsValue.Update)] - public async Task Save([FromBody] IApplication model) => Json(await Api.Save(model)); + public async Task> Save([FromBody] IApplication model) => await Api.Save(model); - /// - /// Deletes a translation - /// + [HttpDelete] [ZeroAuthorize(Permissions.Settings.Applications, PermissionsValue.Update)] - public async Task Delete([FromQuery] string id) => Json(await Api.Delete(id)); + public async Task> Delete([FromQuery] string id) => await Api.Delete(id); } } diff --git a/zero.Web/Controllers/AuthenticationController.cs b/zero.Web/Controllers/AuthenticationController.cs index f62ab895..b248d4b6 100644 --- a/zero.Web/Controllers/AuthenticationController.cs +++ b/zero.Web/Controllers/AuthenticationController.cs @@ -21,59 +21,29 @@ namespace zero.Web.Controllers } - /// - /// Returns the current user - /// - public async Task GetUser() - { - return Edit(await Api.GetUser()); - } + public async Task> GetUser() => Edit(await Api.GetUser()); + + + public EntityResult IsLoggedIn() => EntityResult.Maybe(Api.IsLoggedIn()); - /// - /// If a user is logged in - /// - public IActionResult IsLoggedIn() - { - return Json(EntityResult.Maybe(Api.IsLoggedIn())); - } - - - /// - /// Tries a login for a user with username/password - /// [HttpPost] - public async Task LoginUser([FromBody] LoginModel model) - { - EntityResult result = await Api.Login(model.Email, model.Password, model.IsPersistent); - return Json(result); - } + public async Task LoginUser([FromBody] LoginModel model) => await Api.Login(model.Email, model.Password, model.IsPersistent); - /// - /// Logout for the current user - /// [HttpPost, ZeroAuthorize] - public async Task LogoutUser() + public async Task LogoutUser() { await Api.Logout(); - return Json(EntityResult.Success()); + return EntityResult.Success(); } - /// - /// Try to switch selected application for user - /// [HttpPost, ZeroAuthorize] - public async Task SwitchApp(string appId) + public async Task SwitchApp(string appId) { User user = await Api.GetUser(); - bool isSuccess = await AppContext.TrySwitchForUser(user, appId); - - return Json(new EntityResult() - { - IsSuccess = isSuccess - }); + return EntityResult.Maybe(await AppContext.TrySwitchForUser(user, appId)); } } } diff --git a/zero.Web/Controllers/BackofficeController.cs b/zero.Web/Controllers/BackofficeController.cs index d0891b70..bccbe568 100644 --- a/zero.Web/Controllers/BackofficeController.cs +++ b/zero.Web/Controllers/BackofficeController.cs @@ -16,7 +16,9 @@ namespace zero.Web.Controllers { [ZeroAuthorize] [ServiceFilter(typeof(ModelStateValidationFilterAttribute))] - public abstract class BackofficeController : Controller + [ApiController] + [Route("getsreplaced/[controller]/[action]")] + public abstract class BackofficeController : ControllerBase { IZeroOptions _options; IToken _token; @@ -28,16 +30,10 @@ namespace zero.Web.Controllers static Type AppAwareShareableType = typeof(IAppAwareShareableEntity); - /// - /// Creates a Microsoft.AspNetCore.Mvc.JsonResult object that serializes the specified data object to JSON. - /// - public JsonResult Json(object data, bool typed) => Json(data); - - /// /// Creates an edit model with appropriate options and permissions /// - public IActionResult Edit(T data, bool typed = true, Action> transform = null) where T : IZeroIdEntity + public EditModel Edit(T data, bool typed = true, Action> transform = null) where T : IZeroIdEntity { Type type = typeof(T); bool canBeShared = AppAwareShareableType.IsAssignableFrom(type); @@ -46,7 +42,7 @@ namespace zero.Web.Controllers if (data == null) { - return NotFound(); + return null; } EditModel model = new EditModel(); @@ -61,14 +57,14 @@ namespace zero.Web.Controllers transform?.Invoke(model); - return Json(model, typed); + return model; } /// /// Creates an edit model with appropriate options and permissions /// - public IActionResult Edit(TWrapper data, bool typed = true, Action> transform = null) + public TWrapper Edit(TWrapper data, bool typed = true, Action> transform = null) where T : IZeroIdEntity where TWrapper : EditModel, new() { @@ -87,11 +83,11 @@ namespace zero.Web.Controllers transform?.Invoke(data); - return Json(data, typed); + return data; } - public IActionResult JsonPreviews(Dictionary items, Func transform) + public IList Previews(Dictionary items, Func transform) { IList previews = new List(); @@ -116,12 +112,12 @@ namespace zero.Web.Controllers } } - return Json(previews); + return previews; } - public async Task JsonPreviews(Dictionary items, Func> transform) + public async Task> Previews(Dictionary items, Func> transform) { IList previews = new List(); @@ -146,7 +142,7 @@ namespace zero.Web.Controllers } } - return Json(previews); + return previews; } diff --git a/zero.Web/Controllers/CountriesController.cs b/zero.Web/Controllers/CountriesController.cs index a0241c3d..c7acf2ed 100644 --- a/zero.Web/Controllers/CountriesController.cs +++ b/zero.Web/Controllers/CountriesController.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Mvc; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -7,6 +8,7 @@ using zero.Core.Api; using zero.Core.Entities; using zero.Core.Extensions; using zero.Core.Identity; +using zero.Web.Models; namespace zero.Web.Controllers { @@ -14,35 +16,33 @@ namespace zero.Web.Controllers public class CountriesController : BackofficeController { ICountriesApi Api; - ICountry Blueprint; - public CountriesController(ICountriesApi api, ICountry blueprint) + public CountriesController(ICountriesApi api) { Api = api; - Blueprint = blueprint; } - public async Task GetById([FromQuery] string id) => Edit(await Api.GetById(id)); + public async Task> GetById([FromQuery] string id) => Edit(await Api.GetById(id)); - public IActionResult GetEmpty() => Edit(Blueprint.Clone()); + public EditModel GetEmpty([FromServices] ICountry blueprint) => Edit(blueprint); - public async Task GetAll([FromQuery] ListQuery query) => Json(await Api.GetByQuery("languages.1-A", query)); // TODO correct language + public async Task> GetAll([FromQuery] ListQuery query) => await Api.GetByQuery("languages.1-A", query); // TODO correct language - public async Task GetForPicker() => Json((await Api.GetAll("languages.1-A")).Select(x => new SelectModel() + public async Task> GetForPicker() => (await Api.GetAll("languages.1-A")).Select(x => new SelectModel() { Id = x.Id, Name = x.Name, IsActive = x.IsActive - })); + }); - public async Task GetPreviews(List ids) + public async Task> GetPreviews(List ids) { - return JsonPreviews(await Api.GetByIds(ids.ToArray()), item => new PreviewModel() + return Previews(await Api.GetByIds(ids.ToArray()), item => new PreviewModel() { Id = item.Id, Icon = "flag flag-" + item.Code.ToLowerInvariant(), @@ -52,10 +52,10 @@ namespace zero.Web.Controllers [ZeroAuthorize(Permissions.Settings.Countries, PermissionsValue.Update)] - public async Task Save([FromBody] ICountry model) => Json(await Api.Save(model)); + public async Task> Save([FromBody] ICountry model) => await Api.Save(model); [ZeroAuthorize(Permissions.Settings.Countries, PermissionsValue.Update)] - public async Task Delete([FromQuery] string id) => Json(await Api.Delete(id)); + public async Task> Delete([FromQuery] string id) => await Api.Delete(id); } } diff --git a/zero.Web/Controllers/IndexController.cs b/zero.Web/Controllers/IndexController.cs deleted file mode 100644 index 46ff4db7..00000000 --- a/zero.Web/Controllers/IndexController.cs +++ /dev/null @@ -1,32 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using zero.Core.Extensions; -using zero.Core.Identity; -using zero.Web.Models; - -namespace zero.Web.Controllers -{ - [ZeroAuthorize(false)] - public class IndexController : BackofficeController - { - private IZeroVue ZeroVue { get; set; } - - public IndexController(IZeroVue zeroVue) - { - ZeroVue = zeroVue; - } - - - public IActionResult Index() - { - if (Options.ZeroVersion.IsNullOrEmpty()) - { - return RedirectToAction("Index", "Setup"); - } - - return View(new ZeroBackofficeModel() - { - Vue = ZeroVue - }); - } - } -} diff --git a/zero.Web/Controllers/LanguagesController.cs b/zero.Web/Controllers/LanguagesController.cs index 87800db2..4dfcc682 100644 --- a/zero.Web/Controllers/LanguagesController.cs +++ b/zero.Web/Controllers/LanguagesController.cs @@ -4,8 +4,8 @@ using System.Linq; using System.Threading.Tasks; using zero.Core.Api; using zero.Core.Entities; -using zero.Core.Extensions; using zero.Core.Identity; +using zero.Web.Models; namespace zero.Web.Controllers { @@ -13,56 +13,39 @@ namespace zero.Web.Controllers public class LanguagesController : BackofficeController { ILanguagesApi Api; - ILanguage Blueprint; - public LanguagesController(ILanguagesApi api, ILanguage blueprint) + public LanguagesController(ILanguagesApi api) { Api = api; - Blueprint = blueprint; } - /// - /// Get empty language model - /// - public IActionResult GetEmpty() => Edit(Blueprint.Clone()); + public EditModel GetEmpty([FromServices] ILanguage blueprint) => Edit(blueprint); - /// - /// Get language by id - /// - public async Task GetById([FromQuery] string id) => Edit(await Api.GetById(id)); + public async Task> GetById([FromQuery] string id) => Edit(await Api.GetById(id)); - /// - /// Get all languages - /// - public async Task GetAll([FromQuery] ListQuery query) => Json(await Api.GetByQuery(query)); + public async Task> GetAll([FromQuery] ListQuery query) => await Api.GetByQuery(query); - /// - /// Returns all cultures available for creating languages. - /// - public IActionResult GetAllCultures() => Json(Api.GetAllCultures()); + public IList GetAllCultures() => Api.GetAllCultures(); - /// - /// Returns all available backoffice cultures. - /// - public IActionResult GetSupportedCultures() => Json(Api.GetAllCultures(Options.SupportedLanguages)); + public IList GetSupportedCultures() => Api.GetAllCultures(Options.SupportedLanguages); - public async Task GetForPicker() => Json((await Api.GetAll()).Select(x => new SelectModel() + public async Task> GetForPicker() => (await Api.GetAll()).Select(x => new SelectModel() { Id = x.Id, Name = x.Name, IsActive = x.IsActive - })); + }); - public async Task GetPreviews(List ids) + public async Task> GetPreviews(List ids) { - return JsonPreviews(await Api.GetByIds(ids.ToArray()), item => new PreviewModel() + return Previews(await Api.GetByIds(ids.ToArray()), item => new PreviewModel() { Id = item.Id, Icon = "fth-globe", @@ -71,17 +54,11 @@ namespace zero.Web.Controllers } - /// - /// Save language - /// [ZeroAuthorize(Permissions.Settings.Languages, PermissionsValue.Update)] - public async Task Save([FromBody] ILanguage model) => Json(await Api.Save(model)); + public async Task> Save([FromBody] ILanguage model) => await Api.Save(model); - /// - /// Deletes a language - /// [ZeroAuthorize(Permissions.Settings.Languages, PermissionsValue.Update)] - public async Task Delete([FromQuery] string id) => Json(await Api.Delete(id)); + public async Task> Delete([FromQuery] string id) => await Api.Delete(id); } } diff --git a/zero.Web/Controllers/MediaController.cs b/zero.Web/Controllers/MediaController.cs index fcb9e250..2e0b4ab9 100644 --- a/zero.Web/Controllers/MediaController.cs +++ b/zero.Web/Controllers/MediaController.cs @@ -31,32 +31,32 @@ namespace zero.Web.Controllers } - public async Task GetById([FromQuery] string id) => Edit(await Api.GetById(id)); + public async Task> GetById([FromQuery] string id) => Edit(await Api.GetById(id)); - public async Task GetByIds([FromQuery] string[] ids) => Json(await Api.GetById(ids)); + public async Task> GetByIds([FromQuery] string[] ids) => await Api.GetById(ids); - public async Task GetListByQuery([FromQuery] MediaListItemQuery query) => Json(await Api.GetListByQuery(query)); + public async Task> GetListByQuery([FromQuery] MediaListItemQuery query) => await Api.GetListByQuery(query); - public async Task Save([FromBody] IMedia model) => Json(await Api.Save(model)); + public async Task> Save([FromBody] IMedia model) => await Api.Save(model); - public async Task Upload(IFormFile file, string folderId) => Json(await Api.Save(await Api.Upload(file, folderId))); + public async Task> Upload(IFormFile file, string folderId) => await Api.Save(await Api.Upload(file, folderId)); - public async Task UploadTemporary(IFormFile file, string folderId) => Json(await Api.Upload(file, folderId)); + public async Task UploadTemporary(IFormFile file, string folderId) => await Api.Upload(file, folderId); - public async Task Delete([FromQuery] string id) => Json(await Api.Delete(id)); + public async Task> Delete([FromQuery] string id) => await Api.Delete(id); [HttpPost] - public async Task Move([FromBody] ActionCopyModel model) => Json(await Api.Move(model.Id, model.DestinationId)); + public async Task> Move([FromBody] ActionCopyModel model) => await Api.Move(model.Id, model.DestinationId); - public async Task GetAll([FromQuery] MediaListQuery query) + public async Task GetAll([FromQuery] MediaListQuery query) { ListResult items = (await Api.GetByQuery(query)).MapTo(x => new MediaListModel() { @@ -77,7 +77,7 @@ namespace zero.Web.Controllers hierarchy = await MediaFolderApi.GetHierarchy(query.FolderId); } - return Json(new MediaListResultModel(items, null, folder, hierarchy)); + return new MediaListResultModel(items, null, folder, hierarchy); } diff --git a/zero.Web/Controllers/MediaFolderController.cs b/zero.Web/Controllers/MediaFolderController.cs index f735f414..c451156e 100644 --- a/zero.Web/Controllers/MediaFolderController.cs +++ b/zero.Web/Controllers/MediaFolderController.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Mvc; +using System.Collections.Generic; using System.Threading.Tasks; using zero.Core.Api; using zero.Core.Entities; @@ -18,25 +19,25 @@ namespace zero.Web.Controllers } - public async Task GetById([FromQuery] string id) => Edit(await Api.GetById(id)); + public async Task> GetById([FromQuery] string id) => Edit(await Api.GetById(id)); - public IActionResult GetEmpty([FromServices] IMediaFolder blueprint) => Edit(blueprint); + public EditModel GetEmpty([FromServices] IMediaFolder blueprint) => Edit(blueprint); - public async Task GetHierarchy([FromQuery] string id) => Json(await Api.GetHierarchy(id)); + public async Task> GetHierarchy([FromQuery] string id) => await Api.GetHierarchy(id); - public async Task GetAllAsTree([FromQuery] string parent = null, [FromQuery] string active = null) => Json(await Api.GetAllAsTree(parent, active)); + public async Task> GetAllAsTree([FromQuery] string parent = null, [FromQuery] string active = null) => await Api.GetAllAsTree(parent, active); [HttpPost] - public async Task Move([FromBody] ActionCopyModel model) => Json(await Api.Move(model.Id, model.DestinationId)); + public async Task> Move([FromBody] ActionCopyModel model) => await Api.Move(model.Id, model.DestinationId); - public async Task Save([FromBody] IMediaFolder model) => Json(await Api.Save(model)); + public async Task> Save([FromBody] IMediaFolder model) => await Api.Save(model); - public async Task Delete([FromQuery] string id) => Json(await Api.Delete(id)); + public async Task> Delete([FromQuery] string id) => await Api.Delete(id); } } diff --git a/zero.Web/Controllers/ModulesController.cs b/zero.Web/Controllers/ModulesController.cs index f3ee8fe7..a83f0370 100644 --- a/zero.Web/Controllers/ModulesController.cs +++ b/zero.Web/Controllers/ModulesController.cs @@ -1,9 +1,10 @@ using Microsoft.AspNetCore.Mvc; using System; -using System.Linq; +using System.Collections.Generic; using zero.Core.Api; using zero.Core.Entities; using zero.Core.Utils; +using zero.Web.Models; namespace zero.Web.Controllers { @@ -17,11 +18,13 @@ namespace zero.Web.Controllers } - public IActionResult GetModuleTypes([FromQuery] string[] tags = default) => Json(Api.GetModuleTypes(tags)); + public IList GetModuleTypes([FromQuery] string[] tags = default) => Api.GetModuleTypes(tags); - public IActionResult GetModuleType([FromQuery] string alias) => Json(Api.GetModuleType(alias)); - public IActionResult GetEmpty(string alias) + public ModuleType GetModuleType([FromQuery] string alias) => Api.GetModuleType(alias); + + + public EditModel GetEmpty(string alias) { ModuleType moduleType = Api.GetModuleType(alias); IModule module = Activator.CreateInstance(moduleType.ContentType) as IModule; diff --git a/zero.Web/Controllers/PageTreeController.cs b/zero.Web/Controllers/PageTreeController.cs index e37d7bb8..ac818b4b 100644 --- a/zero.Web/Controllers/PageTreeController.cs +++ b/zero.Web/Controllers/PageTreeController.cs @@ -1,4 +1,4 @@ -using Microsoft.AspNetCore.Mvc; +using System.Collections.Generic; using System.Threading.Tasks; using zero.Core.Api; using zero.Core.Entities; @@ -15,9 +15,9 @@ namespace zero.Web.Controllers } - public async Task GetChildren(string parent = null, string active = null) + public async Task> GetChildren(string parent = null, string active = null) { - return Json(await Api.GetChildren(parent, active)); + return await Api.GetChildren(parent, active); } } } diff --git a/zero.Web/Controllers/PagesController.cs b/zero.Web/Controllers/PagesController.cs index 1e7a3707..d4ff446c 100644 --- a/zero.Web/Controllers/PagesController.cs +++ b/zero.Web/Controllers/PagesController.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Mvc; using System; +using System.Collections.Generic; using System.Threading.Tasks; using zero.Core.Api; using zero.Core.Entities; @@ -20,18 +21,18 @@ namespace zero.Web.Controllers } - public async Task GetAllowedPageTypes([FromQuery] string parent = null) => Json(await Api.GetAllowedPageTypes(parent)); + public async Task> GetAllowedPageTypes([FromQuery] string parent = null) => await Api.GetAllowedPageTypes(parent); - public IActionResult GetPageType([FromQuery] string alias) => Json(Api.GetPageType(alias)); + public PageType GetPageType([FromQuery] string alias) => Api.GetPageType(alias); - public async Task GetById([FromQuery] string id) + public async Task> GetById([FromQuery] string id) { IPage entity = await Api.GetById(id); if (entity == null) { - return NotFound(); + return null; } return Edit>(new PageEditModel() @@ -42,7 +43,8 @@ namespace zero.Web.Controllers }); } - public IActionResult GetEmpty(string type, string parent = null) + + public EditModel GetEmpty(string type, string parent = null) { PageType pageType = Api.GetPageType(type); IPage model = Activator.CreateInstance(pageType.ContentType) as IPage; @@ -53,22 +55,29 @@ namespace zero.Web.Controllers return Edit(model); } - public async Task GetRevisions([FromQuery] string id, [FromQuery] int page = 1) => Json(await RevisionsApi.GetPaged(id, page)); - public async Task Save([FromBody] IPage model) => Json(await Api.Save(model)); + public async Task> GetRevisions([FromQuery] string id, [FromQuery] int page = 1) => await RevisionsApi.GetPaged(id, page); + + + public async Task> Save([FromBody] IPage model) => await Api.Save(model); + [HttpPost] - public async Task SaveSorting([FromBody] string[] ids) => Json(await Api.SaveSorting(ids)); + public async Task>> SaveSorting([FromBody] string[] ids) => await Api.SaveSorting(ids); + [HttpPost] - public async Task Move([FromBody] ActionCopyModel model) => Json(await Api.Move(model.Id, model.DestinationId)); + public async Task> Move([FromBody] ActionCopyModel model) => await Api.Move(model.Id, model.DestinationId); + [HttpPost] - public async Task Copy([FromBody] ActionCopyModel model) => Json(await Api.Copy(model.Id, model.DestinationId, model.IncludeDescendants)); + public async Task> Copy([FromBody] ActionCopyModel model) => await Api.Copy(model.Id, model.DestinationId, model.IncludeDescendants); + [HttpPost] - public async Task Restore([FromBody] ActionCopyModel model) => Json(await Api.Restore(model.Id, model.IncludeDescendants)); + public async Task> Restore([FromBody] ActionCopyModel model) => await Api.Restore(model.Id, model.IncludeDescendants); - public async Task Delete([FromQuery] string id) => Json(await Api.Delete(id, true)); + + public async Task> Delete([FromQuery] string id) => await Api.Delete(id, true); } } diff --git a/zero.Web/Controllers/PreviewController.cs b/zero.Web/Controllers/PreviewController.cs index e6702b6b..fd098bee 100644 --- a/zero.Web/Controllers/PreviewController.cs +++ b/zero.Web/Controllers/PreviewController.cs @@ -2,6 +2,7 @@ using System.Threading.Tasks; using zero.Core.Api; using zero.Core.Entities; +using zero.Web.Models; namespace zero.Web.Controllers { @@ -15,20 +16,20 @@ namespace zero.Web.Controllers } - public async Task GetById([FromQuery] string id) => Edit(await Api.GetById(id)); + public async Task> GetById([FromQuery] string id) => Edit(await Api.GetById(id)); - public async Task Add([FromBody] IZeroEntity model) + public async Task> Add([FromBody] IZeroEntity model) { EntityResult preview = await Api.Add(model); - return Json(EntityResult.From(preview, preview.Model?.Id)); + return EntityResult.From(preview, preview.Model?.Id); } - public async Task Update([FromBody] string id, [FromBody] IZeroEntity model) + public async Task> Update([FromQuery] string id, [FromBody] IZeroEntity model) { EntityResult preview = await Api.Update(id, model); - return Json(EntityResult.From(preview, id)); + return EntityResult.From(preview, id); } } } diff --git a/zero.Web/Controllers/RecycleBinController.cs b/zero.Web/Controllers/RecycleBinController.cs index 517e8348..ee2dda79 100644 --- a/zero.Web/Controllers/RecycleBinController.cs +++ b/zero.Web/Controllers/RecycleBinController.cs @@ -1,11 +1,7 @@ using Microsoft.AspNetCore.Mvc; -using System.Collections.Generic; -using System.Linq; using System.Threading.Tasks; -using zero.Core; using zero.Core.Api; using zero.Core.Entities; -using zero.Core.Identity; namespace zero.Web.Controllers { @@ -18,14 +14,19 @@ namespace zero.Web.Controllers Api = api; } - public async Task GetByQuery([FromQuery] RecycleBinListQuery query) => Json(await Api.GetByQuery(query)); - public async Task GetCountByOperation([FromQuery] string operationId) => Json(await Api.GetCountByOperation(operationId)); + public async Task> GetByQuery([FromQuery] RecycleBinListQuery query) => await Api.GetByQuery(query); + + + public async Task GetCountByOperation([FromQuery] string operationId) => await Api.GetCountByOperation(operationId); + [HttpDelete] - public async Task Delete([FromQuery] string id) => Json(await Api.Delete(id)); + public async Task> Delete([FromQuery] string id) => await Api.Delete(id); + [HttpDelete] - public async Task DeleteByGroup([FromQuery] string group) => Json(await Api.DeleteByGroup(group)); + public async Task> DeleteByGroup([FromQuery] string group) => await Api.DeleteByGroup(group); + } } diff --git a/zero.Web/Controllers/SectionsController.cs b/zero.Web/Controllers/SectionsController.cs index cd6a891d..c44fe871 100644 --- a/zero.Web/Controllers/SectionsController.cs +++ b/zero.Web/Controllers/SectionsController.cs @@ -1,5 +1,6 @@ -using Microsoft.AspNetCore.Mvc; +using System.Collections.Generic; using zero.Core.Api; +using zero.Core.Entities; namespace zero.Web.Controllers { @@ -13,9 +14,6 @@ namespace zero.Web.Controllers } - public IActionResult GetAll() - { - return Json(Api.GetAll()); - } + public IReadOnlyCollection GetAll() => Api.GetAll(); } } diff --git a/zero.Web/Controllers/SettingsController.cs b/zero.Web/Controllers/SettingsController.cs index 6de72a5d..670e8e94 100644 --- a/zero.Web/Controllers/SettingsController.cs +++ b/zero.Web/Controllers/SettingsController.cs @@ -1,5 +1,4 @@ -using Microsoft.AspNetCore.Mvc; -using System.Collections.Generic; +using System.Collections.Generic; using System.Threading.Tasks; using zero.Core; using zero.Core.Api; @@ -28,7 +27,7 @@ namespace zero.Web.Controllers /// /// Get all settings areas /// - public async Task GetAreas() + public async Task GetAreas() { bool isSuperUser = AuthApi.IsSuper(); IList permissions = AuthApi.GetPermissions(Permissions.Settings.PREFIX); @@ -75,11 +74,11 @@ namespace zero.Web.Controllers applications = await ApplicationsApi.GetAll(); } - return Json(new + return new { groups, applications - }); + }; } } } \ No newline at end of file diff --git a/zero.Web/Controllers/SpacesController.cs b/zero.Web/Controllers/SpacesController.cs index 7ec7b28d..087d1e21 100644 --- a/zero.Web/Controllers/SpacesController.cs +++ b/zero.Web/Controllers/SpacesController.cs @@ -21,9 +21,6 @@ namespace zero.Web.Controllers } - /// - /// Get space by alias - /// public IActionResult GetByAlias([FromQuery] string alias) { if (!CanReadSpace(alias)) @@ -31,23 +28,13 @@ namespace zero.Web.Controllers return new StatusCodeResult(403); } - return Json(Api.GetByAlias(alias)); + return Ok(Api.GetByAlias(alias)); } - /// - /// Get all spaces - /// - public IActionResult GetAll() - { - IList spaces = Api.GetAll().Where(space => CanReadSpace(space.Alias)).ToList(); - return Json(spaces); - } + public List GetAll() => Api.GetAll().Where(space => CanReadSpace(space.Alias)).ToList(); - /// - /// Get list items in a space - /// public async Task GetList([FromQuery] string alias, [FromQuery] ListQuery query = null) { if (!CanReadSpace(alias)) @@ -55,13 +42,10 @@ namespace zero.Web.Controllers return new StatusCodeResult(403); } - return Json(await Api.GetListByQuery(alias, query)); + return Ok(await Api.GetListByQuery(alias, query)); } - /// - /// Get list items in a space - /// public async Task GetContent([FromQuery] string alias, [FromQuery] string contentId = null) { if (!CanReadSpace(alias)) @@ -88,7 +72,7 @@ namespace zero.Web.Controllers model.SpaceAlias = space.Alias; } - return Edit(model); + return Ok(Edit(model)); } @@ -102,7 +86,7 @@ namespace zero.Web.Controllers return new StatusCodeResult(403); } - return Json(await Api.Save(model)); + return Ok(await Api.Save(model)); } @@ -116,7 +100,7 @@ namespace zero.Web.Controllers return new StatusCodeResult(403); } - return Json(await Api.Delete(id)); + return Ok(await Api.Delete(id)); } diff --git a/zero.Web/Controllers/TranslationsController.cs b/zero.Web/Controllers/TranslationsController.cs index be06922c..9840d5e9 100644 --- a/zero.Web/Controllers/TranslationsController.cs +++ b/zero.Web/Controllers/TranslationsController.cs @@ -2,8 +2,8 @@ using System.Threading.Tasks; using zero.Core.Api; using zero.Core.Entities; -using zero.Core.Extensions; using zero.Core.Identity; +using zero.Web.Models; namespace zero.Web.Controllers { @@ -11,44 +11,27 @@ namespace zero.Web.Controllers public class TranslationsController : BackofficeController { ITranslationsApi Api; - ITranslation Blueprint; - public TranslationsController(ITranslationsApi api, ITranslation blueprint) + public TranslationsController(ITranslationsApi api) { Api = api; - Blueprint = blueprint; } - /// - /// Get translation by id - /// - public IActionResult GetEmpty() => Edit(Blueprint.Clone()); + public EditModel GetEmpty([FromServices] ITranslation blueprint) => Edit(blueprint); - /// - /// Get translation by id - /// - public async Task GetById([FromQuery] string id) => Edit(await Api.GetById(id)); + public async Task> GetById([FromQuery] string id) => Edit(await Api.GetById(id)); - /// - /// Get all translations - /// - public async Task GetAll([FromQuery] ListQuery query) => Json(await Api.GetByQuery(query)); + public async Task> GetAll([FromQuery] ListQuery query) => await Api.GetByQuery(query); - /// - /// Save translation - /// [ZeroAuthorize(Permissions.Settings.Translations, PermissionsValue.Update)] - public async Task Save([FromBody] ITranslation model) => Json(await Api.Save(model)); + public async Task> Save([FromBody] ITranslation model) => await Api.Save(model); - /// - /// Deletes a translation - /// [ZeroAuthorize(Permissions.Settings.Translations, PermissionsValue.Update)] - public async Task Delete([FromQuery] string id) => Json(await Api.Delete(id)); + public async Task> Delete([FromQuery] string id) => await Api.Delete(id); } } diff --git a/zero.Web/Controllers/UserRolesController.cs b/zero.Web/Controllers/UserRolesController.cs index 03a2de4a..069dbf37 100644 --- a/zero.Web/Controllers/UserRolesController.cs +++ b/zero.Web/Controllers/UserRolesController.cs @@ -1,8 +1,10 @@ using Microsoft.AspNetCore.Mvc; +using System.Collections.Generic; using System.Threading.Tasks; using zero.Core.Api; using zero.Core.Entities; using zero.Core.Identity; +using zero.Web.Models; namespace zero.Web.Controllers { @@ -20,20 +22,20 @@ namespace zero.Web.Controllers } - public async Task GetById([FromQuery] string id) => Edit(await Api.GetById(id)); + public async Task> GetById([FromQuery] string id) => Edit(await Api.GetById(id)); - public async Task GetAll() => Json(await Api.GetAll()); + public async Task> GetAll() => await Api.GetAll(); - public IActionResult GetAllPermissions() => Json(PermissionsApi.GetAll()); + public IList GetAllPermissions() => PermissionsApi.GetAll(); [ZeroAuthorize(Permissions.Settings.Users, PermissionsValue.Update)] - public async Task Save([FromBody] IUserRole model) => Json(await Api.Save(model)); + public async Task> Save([FromBody] IUserRole model) => await Api.Save(model); [ZeroAuthorize(Permissions.Settings.Users, PermissionsValue.Update)] - public async Task Delete([FromQuery] string id) => Json(await Api.Delete(id)); + public async Task> Delete([FromQuery] string id) => await Api.Delete(id); } } diff --git a/zero.Web/Controllers/UsersController.cs b/zero.Web/Controllers/UsersController.cs index 47ea0470..a595a73d 100644 --- a/zero.Web/Controllers/UsersController.cs +++ b/zero.Web/Controllers/UsersController.cs @@ -25,26 +25,26 @@ namespace zero.Web.Controllers } - public async Task GetById([FromQuery] string id) => Edit(await Api.GetUserById(id)); + public async Task> GetById([FromQuery] string id) => Edit(await Api.GetUserById(id)); - public async Task GetAll([FromQuery] ListQuery query) => Json(await Api.GetByQuery(query)); + public async Task> GetAll([FromQuery] ListQuery query) => await Api.GetByQuery(query); - public IActionResult GetAllPermissions() => Json(PermissionsApi.GetAll()); + public IList GetAllPermissions() => PermissionsApi.GetAll(); - public async Task GetForPicker() => Json((await Api.GetAll()).Select(x => new SelectModel() + public async Task> GetForPicker() => (await Api.GetAll()).Select(x => new SelectModel() { Id = x.Id, Name = x.Name, IsActive = x.IsActive - })); + }); - public async Task GetPreviews(List ids) + public async Task> GetPreviews(List ids) { - return JsonPreviews(await Api.GetByIds(ids.ToArray()), item => new PreviewModel() + return Previews(await Api.GetByIds(ids.ToArray()), item => new PreviewModel() { Id = item.Id, Icon = item.AvatarId, @@ -54,7 +54,7 @@ namespace zero.Web.Controllers [ZeroAuthorize] - public async Task UpdatePassword([FromBody] UserPasswordEditModel model) + public async Task> UpdatePassword([FromBody] UserPasswordEditModel model) { EntityResult result; @@ -73,36 +73,36 @@ namespace zero.Web.Controllers } } - return Json(result); + return result; } [ZeroAuthorize(Permissions.Settings.Users, PermissionsValue.Update)] - public async Task Disable([FromBody] IUser model) + public async Task> Disable([FromBody] IUser model) { IUser entity = await Api.GetUserById(model.Id); - return Json(await Api.Disable(entity)); + return await Api.Disable(entity); } [ZeroAuthorize(Permissions.Settings.Users, PermissionsValue.Update)] - public async Task Enable([FromBody] IUser model) + public async Task> Enable([FromBody] IUser model) { IUser entity = await Api.GetUserById(model.Id); - return Json(await Api.Enable(entity)); + return await Api.Enable(entity); } [ZeroAuthorize(Permissions.Settings.Users, PermissionsValue.Update)] - public async Task Save([FromBody] IUser model) => Json(await Api.Save(model)); + public async Task> Save([FromBody] IUser model) => await Api.Save(model); [ZeroAuthorize(Permissions.Settings.Users, PermissionsValue.Update)] // TODO do not need settings.users authorization for editing current user profiles - public async Task SaveCurrent([FromBody] IUser model) => Json(await Api.Save(model)); + public async Task> SaveCurrent([FromBody] IUser model) => await Api.Save(model); [ZeroAuthorize(Permissions.Settings.Users, PermissionsValue.Update)] - public async Task Delete([FromQuery] string id) => Json(await Api.Delete(id)); + public async Task> Delete([FromQuery] string id) => await Api.Delete(id); } } diff --git a/zero.Web/Controllers/UtilsController.cs b/zero.Web/Controllers/UtilsController.cs index b1999e3c..d49f2e0a 100644 --- a/zero.Web/Controllers/UtilsController.cs +++ b/zero.Web/Controllers/UtilsController.cs @@ -5,9 +5,6 @@ namespace zero.Web.Controllers { public class UtilsController : BackofficeController { - /// - /// Generate alias from name - /// - public IActionResult GenerateAlias([FromQuery] string name) => Json(Safenames.Alias(name)); + public string GenerateAlias([FromQuery] string name) => Safenames.Alias(name); } } diff --git a/zero.Web/Controllers/ZeroBackofficeController.cs b/zero.Web/Controllers/ZeroBackofficeController.cs new file mode 100644 index 00000000..2b0357cd --- /dev/null +++ b/zero.Web/Controllers/ZeroBackofficeController.cs @@ -0,0 +1,35 @@ +using Microsoft.AspNetCore.Mvc; +using zero.Core.Extensions; +using zero.Core.Identity; +using zero.Core.Options; +using zero.Web.Models; + +namespace zero.Web.Controllers +{ + [ZeroAuthorize(false)] + public class ZeroBackofficeController : Controller + { + IZeroVue ZeroVue { get; set; } + IZeroOptions Options { get; set; } + + public ZeroBackofficeController(IZeroVue zeroVue, IZeroOptions options) + { + ZeroVue = zeroVue; + Options = options; + } + + + public IActionResult Index() + { + if (Options.ZeroVersion.IsNullOrEmpty()) + { + return RedirectToAction("ZeroBackoffice", "Setup"); + } + + return View("Views/Zero/Index.cshtml", new ZeroBackofficeModel() + { + Vue = ZeroVue + }); + } + } +} diff --git a/zero.Web/Controllers/ZeroBackofficeControllerModelConvention.cs b/zero.Web/Controllers/ZeroBackofficeControllerModelConvention.cs new file mode 100644 index 00000000..c84ac3db --- /dev/null +++ b/zero.Web/Controllers/ZeroBackofficeControllerModelConvention.cs @@ -0,0 +1,33 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.ApplicationModels; +using System; + +namespace zero.Web.Controllers +{ + public class ZeroBackofficeControllerModelConvention : IControllerModelConvention + { + readonly AttributeRouteModel RouteModel; + + readonly Type BaseClass = typeof(BackofficeController); + + + public ZeroBackofficeControllerModelConvention(string backofficePath) + { + RouteModel = new AttributeRouteModel(new RouteAttribute(backofficePath + "/api/[controller]/[action]")); + } + + + public void Apply(ControllerModel controller) + { + if (!controller.ControllerType.IsSubclassOf(BaseClass)) + { + return; + } + + foreach (var selector in controller.Selectors) + { + selector.AttributeRouteModel = RouteModel; + } + } + } +} diff --git a/zero.Web/Controllers/ZeroController.cs b/zero.Web/Controllers/ZeroController.cs index cefcef3b..8043b00d 100644 --- a/zero.Web/Controllers/ZeroController.cs +++ b/zero.Web/Controllers/ZeroController.cs @@ -1,10 +1,6 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using zero.Core.Entities; using zero.Core.Routing; diff --git a/zero.Web/Controllers/DefaultRouteController.cs b/zero.Web/Controllers/ZeroFrontendController.cs similarity index 77% rename from zero.Web/Controllers/DefaultRouteController.cs rename to zero.Web/Controllers/ZeroFrontendController.cs index 22943d87..9df072fd 100644 --- a/zero.Web/Controllers/DefaultRouteController.cs +++ b/zero.Web/Controllers/ZeroFrontendController.cs @@ -6,9 +6,9 @@ using zero.Core.Routing; namespace zero.Web.Controllers { - public class DefaultRouteController : ZeroController + public class ZeroFrontendController : ZeroController { - public DefaultRouteController() + public ZeroFrontendController() { } diff --git a/zero.Web/Controllers/SetupController.cs b/zero.Web/Controllers/ZeroSetupController.cs similarity index 79% rename from zero.Web/Controllers/SetupController.cs rename to zero.Web/Controllers/ZeroSetupController.cs index 8d242c6a..c8e7ece8 100644 --- a/zero.Web/Controllers/SetupController.cs +++ b/zero.Web/Controllers/ZeroSetupController.cs @@ -8,21 +8,23 @@ using zero.Core.Entities; using zero.Core.Entities.Setup; using zero.Core.Extensions; using zero.Core.Identity; -using zero.Web.Controllers; +using zero.Core.Options; namespace zero.Web.Setup { [ZeroAuthorize(false)] - public class SetupController : BackofficeController + public class ZeroSetupController : Controller { ISetupApi Api; IWebHostEnvironment Env; + IZeroOptions Options; - public SetupController(ISetupApi api, IWebHostEnvironment env) + public ZeroSetupController(ISetupApi api, IWebHostEnvironment env, IZeroOptions options) { Api = api; Env = env; + Options = options; } @@ -33,7 +35,7 @@ namespace zero.Web.Setup return Redirect(Options.BackofficePath); } - return View("/Views/Setup.cshtml"); + return View("/Views/Zero/Setup.cshtml"); } diff --git a/zero.Web/Controllers/ZeroVueController.cs b/zero.Web/Controllers/ZeroVueController.cs index 159179d2..834936a1 100644 --- a/zero.Web/Controllers/ZeroVueController.cs +++ b/zero.Web/Controllers/ZeroVueController.cs @@ -19,12 +19,12 @@ namespace zero.Web.Controllers [HttpGet] - public async Task Config() + public async Task Config() { var settings = new JsonSerializerSettings(); settings.Converters.Add(new StringEnumConverter(new CamelCaseNamingStrategy())); - return Json(await ZeroVue.Config()); //, settings); + return await ZeroVue.Config(); //, settings); } } } diff --git a/zero.Web/Views/Shared/Index.cshtml b/zero.Web/Views/Zero/Index.cshtml similarity index 100% rename from zero.Web/Views/Shared/Index.cshtml rename to zero.Web/Views/Zero/Index.cshtml diff --git a/zero.Web/Views/Shared/Setup.cshtml b/zero.Web/Views/Zero/Setup.cshtml similarity index 100% rename from zero.Web/Views/Shared/Setup.cshtml rename to zero.Web/Views/Zero/Setup.cshtml diff --git a/zero.Web/ZeroApplicationBuilderExtensions.cs b/zero.Web/ZeroApplicationBuilderExtensions.cs index a7de3545..77518173 100644 --- a/zero.Web/ZeroApplicationBuilderExtensions.cs +++ b/zero.Web/ZeroApplicationBuilderExtensions.cs @@ -40,21 +40,21 @@ namespace zero.Web builder.UseEndpoints(endpoints => { // setup route - endpoints.MapControllerRoute( - name: "setup", - pattern: path + "/setup", - defaults: new - { - controller = "Setup", - action = "Index" - } - ); + //endpoints.MapControllerRoute( + // name: "setup", + // pattern: path + "/setup", + // defaults: new + // { + // controller = "ZeroSetup", + // action = "Index" + // } + //); - // routes for API - endpoints.MapControllerRoute( - name: "api", - pattern: path + "/api/{controller}/{action}/{id?}" - ); + //// routes for API + //endpoints.MapControllerRoute( + // name: "api", + // pattern: path + "/api/{controller}/{action}/{id?}" + //); if (devPath != null) { @@ -62,7 +62,7 @@ namespace zero.Web } // fallbacks for SPA - endpoints.MapFallbackToController(path + "/{**path}", "Index", "Index"); + endpoints.MapFallbackToController(path + "/{**path}", "Index", "ZeroBackoffice"); }); }); diff --git a/zero.Web/ZeroBuilder.cs b/zero.Web/ZeroBuilder.cs index 0a7509a7..d4c14129 100644 --- a/zero.Web/ZeroBuilder.cs +++ b/zero.Web/ZeroBuilder.cs @@ -1,8 +1,10 @@ using FluentValidation; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Options; using Newtonsoft.Json; using Newtonsoft.Json.Converters; @@ -24,6 +26,7 @@ using zero.Core.Options; using zero.Core.Plugins; using zero.Core.Utils; using zero.Core.Validation; +using zero.Web.Controllers; using zero.Web.Defaults; using zero.Web.Filters; @@ -45,10 +48,7 @@ namespace zero.Web public ZeroBuilder(IServiceCollection services, IConfiguration configuration, Action setupAction) { Services = services; - Mvc = services.AddMvc(opts => - { - //opts.ModelBinderProviders.Insert(0, new ZeroEntityInterfaceBinderProvider()); - }); + Mvc = services.AddMvc(); Configuration = configuration; // create startup options @@ -80,16 +80,18 @@ namespace zero.Web // configure MVC + Services.TryAddEnumerable(ServiceDescriptor.Transient, ZeroBuilderMvcOptions>()); + Mvc.AddNewtonsoftJson(x => { // TODO this shall only be configurated for backoffice controllers - BackofficeJsonSerlializerSettings.Setup(x.SerializerSettings, true); + BackofficeJsonSerlializerSettings.Setup(x.SerializerSettings); }); if (Environment.GetEnvironmentVariable("DOTNET_WATCH") == "1") { Mvc.AddRazorRuntimeCompilation(); - } + } // configure Raven + Identity ConfigureDatabase(); @@ -254,5 +256,21 @@ namespace zero.Web throw new Exception($"Plugin \"{nameof(T)}\" needs an additional parameterless constructor as ConfigureServices() is called before the DI container is built"); } } + + + class ZeroBuilderMvcOptions : IConfigureOptions + { + IZeroOptions Options { get; set; } + + public ZeroBuilderMvcOptions(IZeroOptions options) + { + Options = options; + } + + public void Configure(MvcOptions options) + { + options.Conventions.Add(new ZeroBackofficeControllerModelConvention(Options.BackofficePath)); + } + } } } diff --git a/zero.Web/zero.Web.csproj b/zero.Web/zero.Web.csproj index 347b9bb8..1bc358d3 100644 --- a/zero.Web/zero.Web.csproj +++ b/zero.Web/zero.Web.csproj @@ -14,12 +14,12 @@ - - + + - + - +