From c9c01135570ccbcfc5c26e3d5e9ffa322a6be489 Mon Sep 17 00:00:00 2001 From: Tobias Klika Date: Sun, 24 May 2020 18:32:47 +0200 Subject: [PATCH] ILanguage --- zero.Core/Api/CountriesApi.cs | 46 +----- zero.Core/Api/LanguagesApi.cs | 84 +++------- zero.Core/Entities/Country.cs | 4 +- zero.Core/Entities/Language.cs | 21 ++- zero.Core/ServiceCollectionExtensions.cs | 4 +- zero.Web.UI/App/pages/settings/language.vue | 2 +- zero.Web/Controllers/LanguagesController.cs | 37 ++--- zero.Web/Controllers/TestController.cs | 162 ------------------- zero.Web/Controllers/UsersController.cs | 6 +- zero.Web/Defaults/DefaultBackofficePlugin.cs | 1 + zero.Web/ZeroBuilder.cs | 1 - 11 files changed, 64 insertions(+), 304 deletions(-) delete mode 100644 zero.Web/Controllers/TestController.cs diff --git a/zero.Core/Api/CountriesApi.cs b/zero.Core/Api/CountriesApi.cs index a84d4da6..eb3b8de0 100644 --- a/zero.Core/Api/CountriesApi.cs +++ b/zero.Core/Api/CountriesApi.cs @@ -62,56 +62,14 @@ namespace zero.Core.Api /// public async Task> Save(T model) { - ValidationResult validation = await Validator.ValidateAsync(model); - - if (!validation.IsValid) - { - return EntityResult.Fail(validation); - } - - if (model.Id.IsNullOrEmpty()) - { - model.CreatedDate = DateTimeOffset.Now; - } - - model.Alias = Alias.Generate(model.Name); - - using (IAsyncDocumentSession session = Raven.OpenAsyncSession()) - { - await session.StoreAsync(model); - - string id = session.Advanced.GetDocumentId(model); - - await session.SaveChangesAsync(); - - if (model.Id.IsNullOrEmpty()) - { - model.Id = id; - } - } - - return EntityResult.Success(model); + return await Save(model, Validator); } /// public async Task> Delete(string id) { - using (IAsyncDocumentSession session = Raven.OpenAsyncSession()) - { - T country = await session.LoadAsync(id); - - if (country == null) - { - return EntityResult.Fail("@errors.ondelete.idnotfound"); - } - - session.Delete(country); - - await session.SaveChangesAsync(); - } - - return EntityResult.Success(); + return await DeleteById(id); } } diff --git a/zero.Core/Api/LanguagesApi.cs b/zero.Core/Api/LanguagesApi.cs index dfce8567..e8c04bc6 100644 --- a/zero.Core/Api/LanguagesApi.cs +++ b/zero.Core/Api/LanguagesApi.cs @@ -1,4 +1,4 @@ -using FluentValidation.Results; +using FluentValidation; using Raven.Client.Documents; using Raven.Client.Documents.Linq; using Raven.Client.Documents.Session; @@ -9,31 +9,33 @@ using System.Linq; using System.Threading.Tasks; using zero.Core.Entities; using zero.Core.Extensions; -using zero.Core.Validation; namespace zero.Core.Api { - public class LanguagesApi : BackofficeApi, ILanguagesApi + public class LanguagesApi : BackofficeApi, ILanguagesApi where T : ILanguage { - public LanguagesApi(IBackofficeStore store) : base(store) { } + IValidator Validator; - /// - public async Task GetById(string id) + public LanguagesApi(IBackofficeStore store, IValidator validator) : base(store) { - using (IAsyncDocumentSession session = Raven.OpenAsyncSession()) - { - return await session.LoadAsync(id); - } + Validator = validator; } /// - public async Task> GetAll() + public async Task GetById(string id) + { + return await GetById(id); + } + + + /// + public async Task> GetAll() { using (IAsyncDocumentSession session = Raven.OpenAsyncSession()) { - return await session.Query() + return await session.Query() .OrderByDescending(x => x.CreatedDate) .ToListAsync(); } @@ -58,78 +60,44 @@ namespace zero.Core.Api /// - public async Task> GetByQuery(ListQuery query) + public async Task> GetByQuery(ListQuery query) { query.SearchFor(entity => entity.Name); using (IAsyncDocumentSession session = Raven.OpenAsyncSession()) { - return await session.Query() + return await session.Query() .ToQueriedListAsync(query); } } /// - public async Task> Save(Language model) + public async Task> Save(T model) { - ValidationResult validation = await new LanguageValidator().ValidateAsync(model); - - if (!validation.IsValid) - { - return EntityResult.Fail(validation); - } - - if (model.Id.IsNullOrEmpty()) - { - model.CreatedDate = DateTimeOffset.Now; - } - - model.Alias = Alias.Generate(model.Name); - - using (IAsyncDocumentSession session = Raven.OpenAsyncSession()) - { - await session.StoreAsync(model); - await session.SaveChangesAsync(); - } - - return EntityResult.Success(model); + return await Save(model, Validator); } /// - public async Task> Delete(string id) + public async Task> Delete(string id) { - using (IAsyncDocumentSession session = Raven.OpenAsyncSession()) - { - Language entity = await session.LoadAsync(id); - - if (entity == null) - { - return EntityResult.Fail("@errors.ondelete.idnotfound"); - } - - session.Delete(entity); - - await session.SaveChangesAsync(); - } - - return EntityResult.Success(); + return await DeleteById(id); } } - public interface ILanguagesApi + public interface ILanguagesApi where T : ILanguage { /// /// Get language by Id /// - Task GetById(string id); + Task GetById(string id); /// /// Get all available languages /// - Task> GetAll(); + Task> GetAll(); /// /// Get all available cultures @@ -139,16 +107,16 @@ namespace zero.Core.Api /// /// Get all available languages (with query) /// - Task> GetByQuery(ListQuery query); + Task> GetByQuery(ListQuery query); /// /// Creates or updates a language /// - Task> Save(Language model); + Task> Save(T model); /// /// Deletes a language by Id /// - Task> Delete(string id); + Task> Delete(string id); } } diff --git a/zero.Core/Entities/Country.cs b/zero.Core/Entities/Country.cs index f63e434b..e0974a77 100644 --- a/zero.Core/Entities/Country.cs +++ b/zero.Core/Entities/Country.cs @@ -18,11 +18,11 @@ /// /// Preferred countries are displayed on top in lists /// - public bool IsPreferred { get; set; } + bool IsPreferred { get; set; } /// /// Country code (ISO 3166-1) /// - public string Code { get; set; } + string Code { get; set; } } } diff --git a/zero.Core/Entities/Language.cs b/zero.Core/Entities/Language.cs index b68e209e..f679262d 100644 --- a/zero.Core/Entities/Language.cs +++ b/zero.Core/Entities/Language.cs @@ -1,20 +1,33 @@ namespace zero.Core.Entities { - public class Language : ZeroEntity, IZeroDbConventions + public class Language : ZeroEntity, ILanguage + { + /// + public string Code { get; set; } + + /// + public bool IsDefault { get; set; } + + /// + public string InheritedLanguageId { get; set; } + } + + + public interface ILanguage : IZeroEntity, IZeroDbConventions { /// /// Language code (ISO 3166-1) /// - public string Code { get; set; } + string Code { get; set; } /// /// Whether this is the default language /// - public bool IsDefault { get; set; } + bool IsDefault { get; set; } /// /// If this language is inherited it gets all missing properties from its parent /// - public string InheritedLanguageId { get; set; } + string InheritedLanguageId { get; set; } } } \ No newline at end of file diff --git a/zero.Core/ServiceCollectionExtensions.cs b/zero.Core/ServiceCollectionExtensions.cs index 431a8efb..caa8cee1 100644 --- a/zero.Core/ServiceCollectionExtensions.cs +++ b/zero.Core/ServiceCollectionExtensions.cs @@ -1,6 +1,5 @@ using FluentValidation; using Microsoft.Extensions.DependencyInjection; -using Scrutor; using System; using System.Linq; using System.Reflection; @@ -19,6 +18,9 @@ namespace zero.Core.Entities services.AddTransient(); services.AddTransient(typeof(ICountriesApi<>), typeof(CountriesApi<>)); + + services.AddTransient(); + services.AddTransient(typeof(ILanguagesApi<>), typeof(LanguagesApi<>)); } diff --git a/zero.Web.UI/App/pages/settings/language.vue b/zero.Web.UI/App/pages/settings/language.vue index fe792864..64bdeac8 100644 --- a/zero.Web.UI/App/pages/settings/language.vue +++ b/zero.Web.UI/App/pages/settings/language.vue @@ -80,7 +80,7 @@ form.load(LanguagesApi.getById(this.id)).then(response => { this.disabled = !response.canEdit; - this.model = response; + this.model = response.entity; }); LanguagesApi.getAllCultures().then(res => diff --git a/zero.Web/Controllers/LanguagesController.cs b/zero.Web/Controllers/LanguagesController.cs index 705faa91..384dbf9d 100644 --- a/zero.Web/Controllers/LanguagesController.cs +++ b/zero.Web/Controllers/LanguagesController.cs @@ -8,12 +8,12 @@ using zero.Web.Models; namespace zero.Web.Controllers { [ZeroAuthorize(Permissions.Settings.Languages, PermissionsValue.Read)] - public class LanguagesController : BackofficeController + public class LanguagesController : BackofficeController where T : ILanguage, new() { - ILanguagesApi Api; + ILanguagesApi Api; - public LanguagesController(ILanguagesApi api) + public LanguagesController(ILanguagesApi api) { Api = api; } @@ -22,57 +22,38 @@ namespace zero.Web.Controllers /// /// Get translation by id /// - public IActionResult GetEmpty() - { - return Json(new LanguageEditModel()); - } + public IActionResult GetEmpty() => Json(new T()); /// /// Get translation by id /// - public async Task GetById([FromQuery] string id) - { - return await As(await Api.GetById(id)); - } + public async Task GetById([FromQuery] string id) => JsonEdit(await Api.GetById(id)); /// /// Get all translations /// - public async Task GetAll([FromQuery] ListQuery query) - { - return await As(await Api.GetByQuery(query)); - } + public async Task GetAll([FromQuery] ListQuery query) => Json(await Api.GetByQuery(query)); /// /// Returns all cultures available for creating languages. /// - public IActionResult GetAllCultures() - { - return Json(Api.GetAllCultures()); - } + public IActionResult GetAllCultures() => Json(Api.GetAllCultures()); /// /// Save translation /// [ZeroAuthorize(Permissions.Settings.Languages, PermissionsValue.Write)] - public async Task Save([FromBody] LanguageEditModel model) - { - Language entity = await Mapper.Map(model, await Api.GetById(model.Id)); - return await As(await Api.Save(entity)); - } + public async Task Save([FromBody] T model) => Json(await Api.Save(model)); /// /// Deletes a translation /// [ZeroAuthorize(Permissions.Settings.Languages, PermissionsValue.Write)] - public async Task Delete([FromQuery] string id) - { - return await As(await Api.Delete(id)); - } + public async Task Delete([FromQuery] string id) => Json(await Api.Delete(id)); } } diff --git a/zero.Web/Controllers/TestController.cs b/zero.Web/Controllers/TestController.cs deleted file mode 100644 index 85837ada..00000000 --- a/zero.Web/Controllers/TestController.cs +++ /dev/null @@ -1,162 +0,0 @@ -using Microsoft.AspNetCore.Identity; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Options; -using System; -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; -using zero.Core.Options; -using zero.Core.Plugins; -using zero.Core.Renderer; - -namespace zero.Web.Controllers -{ - //[ZeroAuthorize("tobi")] - public class TestController : BackofficeController - { - IAuthenticationApi Api; - ISpacesApi SpacesApi; - ILanguagesApi LanguagesApi; - SignInManager SignInManager; - - - public TestController(IAuthenticationApi api, ISpacesApi spacesApi, ILanguagesApi languagesApi, SignInManager signInManager) - { - Api = api; - SpacesApi = spacesApi; - LanguagesApi = languagesApi; - SignInManager = signInManager; - } - - - [HttpGet] - [ZeroAuthorize(false)] - public IActionResult GetPlugins([FromServices] IEnumerable plugins) - { - return Json(plugins); - } - - - //[HttpGet] - //[ZeroAuthorize(false)] - //public IActionResult RenderConfig() - //{ - // SocialContentRenderer renderer = new SocialContentRenderer(); - - // return Json(renderer.Build()); - //} - - - //[HttpGet] - //[ZeroAuthorize(false)] - //public async Task SaveSpaceContent() - //{ - // TeamMember model = new TeamMember() - // { - // IsActive = true, - // Email = "tobi@test.com", - // Name = "Tobi", - // Position = "Chef", - // VideoUri = "https://swcs.pro" - // }; - - // model.Addresses.Add(new TeamMemberAddress() - // { - // City = "Braunau", - // Street = "My street", - // No = "23" - // }); - - // return Json(await SpacesApi.Save("team", model)); - //} - - - [HttpGet] - [ZeroAuthorize(false)] - public async Task AddLanguage() - { - return Json(await LanguagesApi.Save(new Language() - { - - IsActive = true, - Name = "English", - Code = "en-US", - IsDefault = true - })); - } - - - //[HttpGet] - //[ZeroAuthorize(false)] - //public async Task GetSpaceList() - //{ - // return Json(await SpacesApi.GetList("team")); - //} - - - [HttpGet] - [ZeroAuthorize(false)] - public IActionResult GetRenderer([FromQuery] string alias) - { - Space space = SpacesApi.GetByAlias(alias); - - AbstractGenericRenderer renderer = Options.Renderers.GetAllItems().FirstOrDefault(x => x.TargetType == space.Type); - - if (renderer == null) - { - return NotFound(); - } - - return Json(renderer.Build()); - } - - - [HttpPost] - public async Task Login([FromQuery] string username, [FromQuery] string password) - { - Microsoft.AspNetCore.Identity.SignInResult result = await SignInManager.PasswordSignInAsync(username, password, false, true); - - return Json(new - { - username, - password, - result - }); - } - - - [ZeroAuthorize] - public async Task GetUser() - { - User user = await SignInManager.UserManager.GetUserAsync(HttpContext.User); - - return Json(new - { - user - }); - } - - - [ZeroAuthorize] - public IActionResult GetUserClaims() - { - return Json(HttpContext.User.Claims.Select(claim => new { claim.Type, claim.Value }).ToArray()); - } - - - [HttpPost] - public async Task Logout() - { - await SignInManager.SignOutAsync(); - - return Json(new - { - success = true - }); - } - } -} diff --git a/zero.Web/Controllers/UsersController.cs b/zero.Web/Controllers/UsersController.cs index a8e2a64b..cc910b98 100644 --- a/zero.Web/Controllers/UsersController.cs +++ b/zero.Web/Controllers/UsersController.cs @@ -8,16 +8,16 @@ using zero.Web.Models; namespace zero.Web.Controllers { [ZeroAuthorize(Permissions.Settings.Users, PermissionsValue.Read)] - public class UsersController : BackofficeController + public class UsersController : BackofficeController where TLanguage : ILanguage { IUserApi Api; IAuthenticationApi AuthenticationApi; IUserRolesApi RolesApi; - ILanguagesApi LanguagesApi; + ILanguagesApi LanguagesApi; IPermissionsApi PermissionsApi; - public UsersController(IUserApi api, IAuthenticationApi authenticationApi, IUserRolesApi rolesApi, ILanguagesApi languagesApi, IPermissionsApi permissionsApi) + public UsersController(IUserApi api, IAuthenticationApi authenticationApi, IUserRolesApi rolesApi, ILanguagesApi languagesApi, IPermissionsApi permissionsApi) { Api = api; AuthenticationApi = authenticationApi; diff --git a/zero.Web/Defaults/DefaultBackofficePlugin.cs b/zero.Web/Defaults/DefaultBackofficePlugin.cs index 0ca99e25..f611d9ff 100644 --- a/zero.Web/Defaults/DefaultBackofficePlugin.cs +++ b/zero.Web/Defaults/DefaultBackofficePlugin.cs @@ -14,6 +14,7 @@ namespace zero.Web.Defaults { EntityMap.Use(); EntityMap.Use(); + EntityMap.Use(); } public void Configure(IZeroPluginOptions plugin, IZeroOptions zero) diff --git a/zero.Web/ZeroBuilder.cs b/zero.Web/ZeroBuilder.cs index ce9d3153..277b7d53 100644 --- a/zero.Web/ZeroBuilder.cs +++ b/zero.Web/ZeroBuilder.cs @@ -105,7 +105,6 @@ namespace zero.Web Services.AddTransient(); Services.AddTransient(); Services.AddTransient(); - Services.AddTransient(); Services.AddTransient(); Services.AddTransient(); Services.AddTransient();