This commit is contained in:
2020-05-24 18:23:56 +02:00
parent 96eb4a0d09
commit 8caab8b473
8 changed files with 65 additions and 63 deletions
+31 -25
View File
@@ -1,4 +1,5 @@
using FluentValidation.Results;
using FluentValidation;
using FluentValidation.Results;
using Raven.Client.Documents;
using Raven.Client.Documents.Linq;
using Raven.Client.Documents.Session;
@@ -7,28 +8,33 @@ 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 CountriesApi : BackofficeApi, ICountriesApi
public class CountriesApi<T> : BackofficeApi, ICountriesApi<T> where T : ICountry
{
public CountriesApi(IBackofficeStore store) : base(store) { }
IValidator<T> Validator;
/// <inheritdoc />
public async Task<Country> GetById(string id)
public CountriesApi(IBackofficeStore store, IValidator<T> validator) : base(store)
{
return await GetById<Country>(id);
Validator = validator;
}
/// <inheritdoc />
public async Task<IList<Country>> GetAll(string languageId)
public async Task<T> GetById(string id)
{
return await GetById<T>(id);
}
/// <inheritdoc />
public async Task<IList<T>> GetAll(string languageId)
{
using (IAsyncDocumentSession session = Raven.OpenAsyncSession())
{
return await session.Query<Country>()
return await session.Query<T>()
.Where(x => x.LanguageId == languageId)
.OrderByDescending(x => x.IsPreferred)
.ThenBy(x => x.Name)
@@ -38,13 +44,13 @@ namespace zero.Core.Api
/// <inheritdoc />
public async Task<ListResult<Country>> GetByQuery(string languageId, ListQuery<Country> query)
public async Task<ListResult<T>> GetByQuery(string languageId, ListQuery<T> query)
{
query.SearchSelector = country => country.Name;
using (IAsyncDocumentSession session = Raven.OpenAsyncSession())
{
return await session.Query<Country>()
return await session.Query<T>()
.Where(x => x.LanguageId == languageId)
.OrderByDescending(x => x.IsPreferred)
.ThenBy(x => x.Name)
@@ -54,13 +60,13 @@ namespace zero.Core.Api
/// <inheritdoc />
public async Task<EntityResult<Country>> Save(Country model)
public async Task<EntityResult<T>> Save(T model)
{
ValidationResult validation = await new CountryValidator().ValidateAsync(model);
ValidationResult validation = await Validator.ValidateAsync(model);
if (!validation.IsValid)
{
return EntityResult<Country>.Fail(validation);
return EntityResult<T>.Fail(validation);
}
if (model.Id.IsNullOrEmpty())
@@ -84,20 +90,20 @@ namespace zero.Core.Api
}
}
return EntityResult<Country>.Success(model);
return EntityResult<T>.Success(model);
}
/// <inheritdoc />
public async Task<EntityResult<Country>> Delete(string id)
public async Task<EntityResult<T>> Delete(string id)
{
using (IAsyncDocumentSession session = Raven.OpenAsyncSession())
{
Country country = await session.LoadAsync<Country>(id);
T country = await session.LoadAsync<T>(id);
if (country == null)
{
return EntityResult<Country>.Fail("@errors.ondelete.idnotfound");
return EntityResult<T>.Fail("@errors.ondelete.idnotfound");
}
session.Delete(country);
@@ -105,36 +111,36 @@ namespace zero.Core.Api
await session.SaveChangesAsync();
}
return EntityResult<Country>.Success();
return EntityResult<T>.Success();
}
}
public interface ICountriesApi
public interface ICountriesApi<T> where T : ICountry
{
/// <summary>
/// Get country by Id
/// </summary>
Task<Country> GetById(string id);
Task<T> GetById(string id);
/// <summary>
/// Get all available countries
/// </summary>
Task<IList<Country>> GetAll(string languageId);
Task<IList<T>> GetAll(string languageId);
/// <summary>
/// Get all available countries (with query)
/// </summary>
Task<ListResult<Country>> GetByQuery(string languageId, ListQuery<Country> query);
Task<ListResult<T>> GetByQuery(string languageId, ListQuery<T> query);
/// <summary>
/// Creates or updates a country
/// </summary>
Task<EntityResult<Country>> Save(Country model);
Task<EntityResult<T>> Save(T model);
/// <summary>
/// Deletes a country by Id
/// </summary>
Task<EntityResult<Country>> Delete(string id);
Task<EntityResult<T>> Delete(string id);
}
}
+2 -2
View File
@@ -209,9 +209,9 @@ namespace zero.Core.Api
{
CreatedDate = DateTimeOffset.Now,
IsActive = true,
Alias = Alias.Generate(country.Key),
Alias = Alias.Generate(country.Value),
LanguageId = defaultLanguage.Id,
Code = country.Key,
Code = country.Key.ToLowerInvariant(),
Name = country.Value
}).ToList());
}
+13 -8
View File
@@ -1,12 +1,20 @@
namespace zero.Core.Entities
{
public class Country : ZeroEntity, ILanguageAwareEntity, IZeroDbConventions
public class Country : ZeroEntity, ICountry
{
/// <summary>
/// Language variant
/// </summary>
public class Variant : LanguageVariant { }
/// <inheritdoc />
public bool IsPreferred { get; set; }
/// <inheritdoc />
public string Code { get; set; }
/// <inheritdoc />
public string LanguageId { get; set; }
}
public interface ICountry : IZeroEntity, ILanguageAwareEntity, IZeroDbConventions
{
/// <summary>
/// Preferred countries are displayed on top in lists
/// </summary>
@@ -16,8 +24,5 @@
/// Country code (ISO 3166-1)
/// </summary>
public string Code { get; set; }
/// <inheritdoc />
public string LanguageId { get; set; }
}
}
+3
View File
@@ -16,6 +16,9 @@ namespace zero.Core.Entities
services.AddTransient<IApplication, Application>();
services.AddTransient(typeof(IApplicationsApi<>), typeof(ApplicationsApi<>));
services.AddTransient<ICountry, Country>();
services.AddTransient(typeof(ICountriesApi<>), typeof(CountriesApi<>));
}
+1 -1
View File
@@ -79,7 +79,7 @@
{
form.load(!this.id ? CountriesApi.getEmpty() : CountriesApi.getById(this.id)).then(response =>
{
this.model = response;
this.model = response.entity;
});
},
+8 -25
View File
@@ -3,16 +3,15 @@ using System.Threading.Tasks;
using zero.Core.Api;
using zero.Core.Entities;
using zero.Core.Identity;
using zero.Web.Models;
namespace zero.Web.Controllers
{
[ZeroAuthorize(Permissions.Settings.Countries, PermissionsValue.Read)]
public class CountriesController : BackofficeController
public class CountriesController<T> : BackofficeController where T : ICountry, new()
{
ICountriesApi Api;
ICountriesApi<T> Api;
public CountriesController(ICountriesApi api)
public CountriesController(ICountriesApi<T> api)
{
Api = api;
}
@@ -21,48 +20,32 @@ namespace zero.Web.Controllers
/// <summary>
/// Get country by id
/// </summary>
public async Task<IActionResult> GetById([FromQuery] string id)
{
return await As<Country, CountryEditModel>(await Api.GetById(id));
}
public async Task<IActionResult> GetById([FromQuery] string id) => JsonEdit(await Api.GetById(id));
/// <summary>
/// Get new country
/// </summary>
public IActionResult GetEmpty()
{
return Json(new CountryEditModel());
}
public IActionResult GetEmpty() => Json(new T());
/// <summary>
/// Get all countries
/// </summary>
public async Task<IActionResult> GetAll([FromQuery] ListQuery<Country> query)
{
return await As<Country, CountryListModel>(await Api.GetByQuery("en-US", query));
}
public async Task<IActionResult> GetAll([FromQuery] ListQuery<T> query) => Json(await Api.GetByQuery("languages.1-A", query)); // TODO correct language
/// <summary>
/// Save country
/// </summary>
[ZeroAuthorize(Permissions.Settings.Countries, PermissionsValue.Write)]
public async Task<IActionResult> Save([FromBody] CountryEditModel model)
{
Country country = await Mapper.Map(model, await Api.GetById(model.Id));
return Json(await Api.Save(country));
}
public async Task<IActionResult> Save([FromBody] T model) => Json(await Api.Save(model));
/// <summary>
/// Deletes a country
/// </summary>
[ZeroAuthorize(Permissions.Settings.Countries, PermissionsValue.Write)]
public async Task<IActionResult> Delete([FromQuery] string id)
{
return Json(await Api.Delete(id));
}
public async Task<IActionResult> Delete([FromQuery] string id) => Json(await Api.Delete(id));
}
}
+7 -1
View File
@@ -1,4 +1,6 @@
using Microsoft.Extensions.DependencyInjection;
using zero.Core;
using zero.Core.Entities;
using zero.Core.Options;
using zero.Core.Plugins;
using zero.Web.Mapper;
@@ -8,7 +10,11 @@ namespace zero.Web.Defaults
{
internal class DefaultBackofficePlugin : IZeroPlugin
{
public void ConfigureServices(IServiceCollection services) { }
public void ConfigureServices(IServiceCollection services)
{
EntityMap.Use<IApplication, Application>();
EntityMap.Use<ICountry, Country>();
}
public void Configure(IZeroPluginOptions plugin, IZeroOptions zero)
{
-1
View File
@@ -100,7 +100,6 @@ namespace zero.Web
Services.AddTransient<IPageTreeApi, PageTreeApi>();
Services.AddTransient<ISettingsApi, SettingsApi>();
Services.AddTransient<IAuthenticationApi, AuthenticationApi>();
Services.AddTransient<ICountriesApi, CountriesApi>();
Services.AddTransient<IUserApi, UserApi>();
Services.AddTransient<IUserRolesApi, UserRolesApi>();
Services.AddTransient<IToken, Token>();