diff --git a/zero.Core/Api/TranslationsApi.cs b/zero.Core/Api/TranslationsApi.cs index 8c6b0151..5bf6137d 100644 --- a/zero.Core/Api/TranslationsApi.cs +++ b/zero.Core/Api/TranslationsApi.cs @@ -1,35 +1,39 @@ -using Raven.Client.Documents; +using FluentValidation; +using Raven.Client.Documents; using Raven.Client.Documents.Linq; using Raven.Client.Documents.Session; using System.Collections.Generic; using System.Threading.Tasks; using zero.Core.Entities; using zero.Core.Extensions; -using zero.Core.Validation; namespace zero.Core.Api { - public class TranslationsApi : AppAwareBackofficeApi, ITranslationsApi + public class TranslationsApi : AppAwareBackofficeApi, ITranslationsApi where T : ITranslation { - public TranslationsApi(IBackofficeStore store) : base(store) + IValidator Validator; + + + public TranslationsApi(IBackofficeStore store, IValidator validator) : base(store) { + Validator = validator; AllowShared = true; } /// - public async Task GetById(string id) + public async Task GetById(string id) { - return await GetById(id); + return await GetById(id); } /// - public async Task> GetAll() + public async Task> GetAll() { using (IAsyncDocumentSession session = Raven.OpenAsyncSession()) { - return await session.Query() + return await session.Query() .OrderByDescending(x => x.CreatedDate) .Scope(Scope) .ToListAsync(); @@ -38,57 +42,57 @@ namespace zero.Core.Api /// - public async Task> GetByQuery(ListQuery query) + public async Task> GetByQuery(ListQuery query) { query.SearchFor(entity => entity.Key, entity => entity.Value); using (IAsyncDocumentSession session = Raven.OpenAsyncSession()) { - return await session.Query().Scope(Scope).ToQueriedListAsync(query); + return await session.Query().Scope(Scope).ToQueriedListAsync(query); } } /// - public async Task> Save(Translation model) + public async Task> Save(T model) { - return await Save(model, new TranslationValidator()); + return await Save(model, Validator); } /// - public async Task> Delete(string id) + public async Task> Delete(string id) { - return await DeleteById(id); + return await DeleteById(id); } } - public interface ITranslationsApi : IAppAwareBackofficeApi + public interface ITranslationsApi where T : ITranslation { /// /// Get translation by Id /// - Task GetById(string id); + Task GetById(string id); /// /// Get all available translations /// - Task> GetAll(); + Task> GetAll(); /// /// Get all available translations (with query) /// - Task> GetByQuery(ListQuery query); + Task> GetByQuery(ListQuery query); /// /// Creates or updates a translation /// - Task> Save(Translation model); + Task> Save(T model); /// /// Deletes a translation by Id /// - Task> Delete(string id); + Task> Delete(string id); } } diff --git a/zero.Core/Entities/Translation.cs b/zero.Core/Entities/Translation.cs index b2ac0b19..318dbaa5 100644 --- a/zero.Core/Entities/Translation.cs +++ b/zero.Core/Entities/Translation.cs @@ -1,6 +1,6 @@ namespace zero.Core.Entities { - public class Translation : ZeroEntity, ILanguageAwareEntity, IAppAwareEntity, IZeroDbConventions + public class Translation : ZeroEntity, ITranslation { /// public string AppId { get; set; } @@ -8,19 +8,13 @@ /// public string LanguageId { get; set; } - /// - /// Key which can be used to query translations - /// + /// public string Key { get; set; } - /// - /// Value of the translation - /// + /// public string Value { get; set; } - /// - /// Display + input type - /// + /// public TranslationDisplay Display { get; set; } } @@ -30,4 +24,23 @@ Text = 0, HTML = 1 } + + + public interface ITranslation : IZeroEntity, ILanguageAwareEntity, IAppAwareEntity, IZeroDbConventions + { + /// + /// Key which can be used to query translations + /// + string Key { get; set; } + + /// + /// Value of the translation + /// + string Value { get; set; } + + /// + /// Display + input type + /// + TranslationDisplay Display { get; set; } + } } \ No newline at end of file diff --git a/zero.Core/ServiceCollectionExtensions.cs b/zero.Core/ServiceCollectionExtensions.cs index caa8cee1..8677e33a 100644 --- a/zero.Core/ServiceCollectionExtensions.cs +++ b/zero.Core/ServiceCollectionExtensions.cs @@ -21,6 +21,9 @@ namespace zero.Core.Entities services.AddTransient(); services.AddTransient(typeof(ILanguagesApi<>), typeof(LanguagesApi<>)); + + services.AddTransient(); + services.AddTransient(typeof(ITranslationsApi<>), typeof(TranslationsApi<>)); } diff --git a/zero.Debug/Controllers/TestController.cs b/zero.Debug/Controllers/TestController.cs index 3bfbec12..09f6faba 100644 --- a/zero.Debug/Controllers/TestController.cs +++ b/zero.Debug/Controllers/TestController.cs @@ -12,17 +12,12 @@ namespace zero.Debug.Controllers { public class TestController : Controller { - IAppScope Api; - ITranslationsApi CurrentApi; - private readonly IActionDescriptorCollectionProvider _actionDescriptorCollectionProvider; - public TestController(IAppScope api, ITranslationsApi currentApi, IActionDescriptorCollectionProvider actionDescriptorCollectionProvider) + public TestController(IActionDescriptorCollectionProvider actionDescriptorCollectionProvider) { _actionDescriptorCollectionProvider = actionDescriptorCollectionProvider; - Api = api; - CurrentApi = currentApi; } @@ -57,23 +52,6 @@ namespace zero.Debug.Controllers } - [HttpGet] - public async Task Index() - { - IList global = await Api.Global.GetAll(); - IList current = await CurrentApi.GetAll(); - IList appTwo = await Api.App("applications.2-A").GetAll(); - IList shared = await Api.Shared.GetAll(); - - return Json(new { - current, - global, - appTwo, - shared - }); - } - - [HttpGet] diff --git a/zero.Web.UI/App/pages/settings/translation.vue b/zero.Web.UI/App/pages/settings/translation.vue index 46dd5eb0..c407185b 100644 --- a/zero.Web.UI/App/pages/settings/translation.vue +++ b/zero.Web.UI/App/pages/settings/translation.vue @@ -51,7 +51,7 @@ form.load(!this.model.id ? TranslationsApi.getEmpty() : TranslationsApi.getById(this.model.id)).then(response => { this.disabled = !response.canEdit; - this.item = response; + this.item = response.entity; this.loading = false; }); }, diff --git a/zero.Web/Controllers/CountriesController.cs b/zero.Web/Controllers/CountriesController.cs index 0035dd5c..c29e76eb 100644 --- a/zero.Web/Controllers/CountriesController.cs +++ b/zero.Web/Controllers/CountriesController.cs @@ -26,7 +26,7 @@ namespace zero.Web.Controllers /// /// Get new country /// - public IActionResult GetEmpty() => Json(new T()); + public IActionResult GetEmpty() => JsonEdit(new T()); /// diff --git a/zero.Web/Controllers/LanguagesController.cs b/zero.Web/Controllers/LanguagesController.cs index 384dbf9d..66b91542 100644 --- a/zero.Web/Controllers/LanguagesController.cs +++ b/zero.Web/Controllers/LanguagesController.cs @@ -3,7 +3,6 @@ using System.Threading.Tasks; using zero.Core.Api; using zero.Core.Entities; using zero.Core.Identity; -using zero.Web.Models; namespace zero.Web.Controllers { @@ -22,7 +21,7 @@ namespace zero.Web.Controllers /// /// Get translation by id /// - public IActionResult GetEmpty() => Json(new T()); + public IActionResult GetEmpty() => JsonEdit(new T()); /// diff --git a/zero.Web/Controllers/TranslationsController.cs b/zero.Web/Controllers/TranslationsController.cs index c330d99d..d606f371 100644 --- a/zero.Web/Controllers/TranslationsController.cs +++ b/zero.Web/Controllers/TranslationsController.cs @@ -8,11 +8,11 @@ using zero.Web.Models; namespace zero.Web.Controllers { [ZeroAuthorize(Permissions.Settings.Translations, PermissionsValue.Read)] - public class TranslationsController : BackofficeController + public class TranslationsController : BackofficeController where T : ITranslation, new() { - ITranslationsApi Api; + ITranslationsApi Api; - public TranslationsController(ITranslationsApi api) + public TranslationsController(ITranslationsApi api) { Api = api; } @@ -21,48 +21,32 @@ namespace zero.Web.Controllers /// /// Get translation by id /// - public IActionResult GetEmpty() - { - return Json(new TranslationEditModel()); - } + public IActionResult GetEmpty() => JsonEdit(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)); /// /// Save translation /// [ZeroAuthorize(Permissions.Settings.Translations, PermissionsValue.Write)] - public async Task Save([FromBody] TranslationEditModel model) - { - Translation 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.Translations, 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/Defaults/DefaultBackofficePlugin.cs b/zero.Web/Defaults/DefaultBackofficePlugin.cs index f611d9ff..fb5cb141 100644 --- a/zero.Web/Defaults/DefaultBackofficePlugin.cs +++ b/zero.Web/Defaults/DefaultBackofficePlugin.cs @@ -15,6 +15,7 @@ namespace zero.Web.Defaults EntityMap.Use(); 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 277b7d53..b5ace5a6 100644 --- a/zero.Web/ZeroBuilder.cs +++ b/zero.Web/ZeroBuilder.cs @@ -104,7 +104,6 @@ namespace zero.Web Services.AddTransient(); Services.AddTransient(); Services.AddTransient(); - Services.AddTransient(); Services.AddTransient(); Services.AddTransient(); Services.AddTransient();