create collections for translation, langauge, mails
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Raven.Client.Documents.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using zero.Core.Collections;
|
||||
using zero.Core.Entities;
|
||||
using zero.Core.Extensions;
|
||||
using zero.Web.Models;
|
||||
|
||||
|
||||
namespace zero.Web.Controllers
|
||||
{
|
||||
public abstract class BackofficeCollectionController<TEntity, TCollection> : BackofficeController
|
||||
where TEntity : IZeroEntity
|
||||
where TCollection : ICollectionBase<TEntity>
|
||||
{
|
||||
protected TCollection Collection { get; private set; }
|
||||
|
||||
protected Func<IRavenQueryable<TEntity>, IRavenQueryable<TEntity>> DefaultQuery { get; set; }
|
||||
|
||||
protected Action<TEntity, PreviewModel> PreviewTransform { get; set; }
|
||||
|
||||
|
||||
public BackofficeCollectionController(TCollection collection)
|
||||
{
|
||||
Collection = collection;
|
||||
}
|
||||
|
||||
public override void OnScopeChanged(string scope)
|
||||
{
|
||||
Collection.ApplyScope(scope);
|
||||
}
|
||||
|
||||
|
||||
public virtual async Task<EditModel<TEntity>> GetById([FromQuery] string id) => Edit(await Collection.GetById(id));
|
||||
|
||||
|
||||
public virtual EditModel<TEntity> GetEmpty([FromServices] TEntity blueprint) => Edit(blueprint);
|
||||
|
||||
|
||||
public virtual async Task<ListResult<TEntity>> GetByQuery([FromQuery] ListQuery<TEntity> query)
|
||||
{
|
||||
query.SearchSelector = model => model.Name;
|
||||
IRavenQueryable<TEntity> ravenQuery = Collection.Query;
|
||||
if (DefaultQuery != null)
|
||||
{
|
||||
ravenQuery = DefaultQuery(ravenQuery);
|
||||
}
|
||||
|
||||
return await ravenQuery.ToQueriedListAsync(query);
|
||||
}
|
||||
|
||||
|
||||
public virtual async Task<IEnumerable<SelectModel>> GetForPicker() => await SelectList(Collection.Stream());
|
||||
|
||||
|
||||
public virtual async Task<IList<PreviewModel>> GetPreviews([FromQuery] List<string> ids) => Previews(await Collection.GetByIds(ids.ToArray()), PreviewTransform);
|
||||
|
||||
|
||||
public virtual async Task<EntityResult<TEntity>> Save([FromBody] TEntity model) => await Collection.Save(model);
|
||||
|
||||
|
||||
public virtual async Task<EntityResult<TEntity>> Delete([FromQuery] string id) => await Collection.DeleteById(id);
|
||||
}
|
||||
}
|
||||
@@ -1,58 +1,18 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Raven.Client.Documents.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Raven.Client.Documents.Linq;
|
||||
using zero.Core.Collections;
|
||||
using zero.Core.Entities;
|
||||
using zero.Core.Extensions;
|
||||
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 : BackofficeCollectionController<ICountry, ICountriesCollection>
|
||||
{
|
||||
ICountriesCollection Service;
|
||||
|
||||
public CountriesController(ICountriesCollection service)
|
||||
public CountriesController(ICountriesCollection collection) : base(collection)
|
||||
{
|
||||
Service = service;
|
||||
DefaultQuery = q => q.OrderByDescending(x => x.IsPreferred).ThenBy(x => x.Name);
|
||||
PreviewTransform = (item, model) => model.Icon = "flag flag-" + item.Code.ToLowerInvariant();
|
||||
}
|
||||
|
||||
public override void OnScopeChanged(string scope)
|
||||
{
|
||||
Service.ApplyScope(scope);
|
||||
}
|
||||
|
||||
|
||||
public async Task<EditModel<ICountry>> GetById([FromQuery] string id) => Edit(await Service.GetById(id));
|
||||
|
||||
|
||||
public EditModel<ICountry> GetEmpty([FromServices] ICountry blueprint) => Edit(blueprint);
|
||||
|
||||
|
||||
public async Task<ListResult<ICountry>> GetByQuery([FromQuery] ListQuery<ICountry> query)
|
||||
{
|
||||
query.SearchSelector = country => country.Name;
|
||||
return await Service.Query.OrderByDescending(x => x.IsPreferred).ThenBy(x => x.Name).ToQueriedListAsync(query);
|
||||
}
|
||||
|
||||
|
||||
public async Task<IEnumerable<SelectModel>> GetForPicker() => await SelectList(Service.Stream());
|
||||
|
||||
|
||||
public async Task<IList<PreviewModel>> GetPreviews([FromQuery] List<string> ids)
|
||||
{
|
||||
return Previews(await Service.GetByIds(ids.ToArray()), (item, model) => model.Icon = "flag flag-" + item.Code.ToLowerInvariant());
|
||||
}
|
||||
|
||||
|
||||
[ZeroAuthorize(Permissions.Settings.Countries, PermissionsValue.Update)]
|
||||
public async Task<EntityResult<ICountry>> Save([FromBody] ICountry model) => await Service.Save(model);
|
||||
|
||||
|
||||
[ZeroAuthorize(Permissions.Settings.Countries, PermissionsValue.Update)]
|
||||
public async Task<EntityResult<ICountry>> Delete([FromQuery] string id) => await Service.DeleteById(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,64 +1,17 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using zero.Core.Api;
|
||||
using Raven.Client.Documents.Linq;
|
||||
using zero.Core.Collections;
|
||||
using zero.Core.Entities;
|
||||
using zero.Core.Identity;
|
||||
using zero.Web.Models;
|
||||
|
||||
namespace zero.Web.Controllers
|
||||
{
|
||||
[ZeroAuthorize(Permissions.Settings.Languages, PermissionsValue.Read)]
|
||||
public class LanguagesController : BackofficeController
|
||||
public class LanguagesController : BackofficeCollectionController<ILanguage, ILanguagesCollection>
|
||||
{
|
||||
ILanguagesApi Api;
|
||||
|
||||
public LanguagesController(ILanguagesApi api)
|
||||
public LanguagesController(ILanguagesCollection collection) : base(collection)
|
||||
{
|
||||
Api = api;
|
||||
DefaultQuery = q => q.OrderByDescending(x => x.CreatedDate);
|
||||
PreviewTransform = (item, model) => model.Icon = "fth-globe";
|
||||
}
|
||||
|
||||
|
||||
public EditModel<ILanguage> GetEmpty([FromServices] ILanguage blueprint) => Edit(blueprint);
|
||||
|
||||
|
||||
public async Task<EditModel<ILanguage>> GetById([FromQuery] string id) => Edit(await Api.GetById(id));
|
||||
|
||||
|
||||
public async Task<ListResult<ILanguage>> GetAll([FromQuery] ListQuery<ILanguage> query) => await Api.GetByQuery(query);
|
||||
|
||||
|
||||
public IList<Culture> GetAllCultures() => Api.GetAllCultures();
|
||||
|
||||
|
||||
public IList<Culture> GetSupportedCultures() => Api.GetAllCultures(Options.SupportedLanguages);
|
||||
|
||||
|
||||
public async Task<IEnumerable<SelectModel>> GetForPicker() => (await Api.GetAll()).Select(x => new SelectModel()
|
||||
{
|
||||
Id = x.Id,
|
||||
Name = x.Name,
|
||||
IsActive = x.IsActive
|
||||
});
|
||||
|
||||
|
||||
public async Task<IList<PreviewModel>> GetPreviews([FromQuery] List<string> ids)
|
||||
{
|
||||
return Previews(await Api.GetByIds(ids.ToArray()), item => new PreviewModel()
|
||||
{
|
||||
Id = item.Id,
|
||||
Icon = "fth-globe",
|
||||
Name = item.Name
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
[ZeroAuthorize(Permissions.Settings.Languages, PermissionsValue.Update)]
|
||||
public async Task<EntityResult<ILanguage>> Save([FromBody] ILanguage model) => await Api.Save(model);
|
||||
|
||||
|
||||
[ZeroAuthorize(Permissions.Settings.Languages, PermissionsValue.Update)]
|
||||
public async Task<EntityResult<ILanguage>> Delete([FromQuery] string id) => await Api.Delete(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,50 +1,15 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using zero.Core.Api;
|
||||
using zero.Core;
|
||||
using zero.Core.Collections;
|
||||
using zero.Core.Entities;
|
||||
using zero.Web.Controllers;
|
||||
using zero.Web.Models;
|
||||
using zero.Core.Identity;
|
||||
|
||||
namespace zero.Commerce.Backoffice
|
||||
namespace zero.Web.Controllers
|
||||
{
|
||||
public class MailTemplatesController : BackofficeController
|
||||
[ZeroAuthorize(Permissions.Settings.Mails, PermissionsValue.Read)]
|
||||
public class MailTemplatesController : BackofficeCollectionController<IMailTemplate, IMailTemplatesCollection>
|
||||
{
|
||||
IMailTemplatesApi Api;
|
||||
|
||||
public MailTemplatesController(IMailTemplatesApi api)
|
||||
public MailTemplatesController(IMailTemplatesCollection collection) : base(collection)
|
||||
{
|
||||
Api = api;
|
||||
PreviewTransform = (item, model) => model.Icon = "fth-mail";
|
||||
}
|
||||
|
||||
|
||||
public EditModel<IMailTemplate> GetEmpty([FromServices] IMailTemplate blueprint) => Edit(blueprint);
|
||||
|
||||
public async Task<EditModel<IMailTemplate>> GetById([FromQuery] string id) => Edit(await Api.GetById(id));
|
||||
|
||||
public async Task<ListResult<IMailTemplate>> GetAll([FromQuery] ListQuery<IMailTemplate> query) => await Api.GetByQuery(query);
|
||||
|
||||
public async Task<IEnumerable<SelectModel>> GetForPicker() => (await Api.GetAll()).Select(x => new SelectModel()
|
||||
{
|
||||
Id = x.Id,
|
||||
Name = x.Name,
|
||||
IsActive = x.IsActive
|
||||
});
|
||||
|
||||
|
||||
public async Task<IList<PreviewModel>> GetPreviews([FromQuery] List<string> ids) => Previews(await Api.GetByIds(ids.ToArray()), item => new PreviewModel()
|
||||
{
|
||||
Id = item.Id,
|
||||
Icon = "fth-mail",
|
||||
Name = item.Name
|
||||
});
|
||||
|
||||
|
||||
public async Task<EntityResult<IMailTemplate>> Save([FromBody] IMailTemplate model) => await Api.Save(model);
|
||||
|
||||
|
||||
public async Task<EntityResult<IMailTemplate>> Delete([FromQuery] string id) => await Api.Delete(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,37 +1,25 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Raven.Client.Documents.Linq;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using zero.Core.Api;
|
||||
using zero.Core.Collections;
|
||||
using zero.Core.Entities;
|
||||
using zero.Core.Extensions;
|
||||
using zero.Core.Identity;
|
||||
using zero.Web.Models;
|
||||
|
||||
namespace zero.Web.Controllers
|
||||
{
|
||||
[ZeroAuthorize(Permissions.Settings.Translations, PermissionsValue.Read)]
|
||||
public class TranslationsController : BackofficeController
|
||||
public class TranslationsController : BackofficeCollectionController<ITranslation, ITranslationsCollection>
|
||||
{
|
||||
ITranslationsApi Api;
|
||||
|
||||
public TranslationsController(ITranslationsApi api)
|
||||
public TranslationsController(ITranslationsCollection collection) : base(collection)
|
||||
{
|
||||
Api = api;
|
||||
}
|
||||
|
||||
|
||||
public EditModel<ITranslation> GetEmpty([FromServices] ITranslation blueprint) => Edit(blueprint);
|
||||
|
||||
|
||||
public async Task<EditModel<ITranslation>> GetById([FromQuery] string id) => Edit(await Api.GetById(id));
|
||||
|
||||
|
||||
public async Task<ListResult<ITranslation>> GetAll([FromQuery] ListQuery<ITranslation> query) => await Api.GetByQuery(query);
|
||||
|
||||
|
||||
[ZeroAuthorize(Permissions.Settings.Translations, PermissionsValue.Update)]
|
||||
public async Task<EntityResult<ITranslation>> Save([FromBody] ITranslation model) => await Api.Save(model);
|
||||
|
||||
|
||||
[ZeroAuthorize(Permissions.Settings.Translations, PermissionsValue.Update)]
|
||||
public async Task<EntityResult<ITranslation>> Delete([FromQuery] string id) => await Api.Delete(id);
|
||||
public override async Task<ListResult<ITranslation>> GetByQuery([FromQuery] ListQuery<ITranslation> query)
|
||||
{
|
||||
query.SearchFor(entity => entity.Key, entity => entity.Value);
|
||||
return await Collection.Query.OrderByDescending(x => x.CreatedDate).ToQueriedListAsync(query);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user