Files
mixtape/zero.Web/Controllers/CountriesController.cs
T
2020-11-19 00:14:52 +01:00

59 lines
1.8 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Raven.Client.Documents.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
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
{
ICountriesCollection Service;
public CountriesController(ICountriesCollection service)
{
Service = service;
}
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);
}
}