using Microsoft.AspNetCore.Mvc; namespace zero.Backoffice.Modules.Countries; public class CountriesController : ZeroBackofficeApiController { protected ICountryStore Store { get; set; } protected IPickerProvider Picker { get; set; } public CountriesController(ICountryStore store, IPickerProvider picker) { Store = store; Picker = picker; } [HttpGet("empty")] [ZeroAuthorize(CountryPermissions.Create)] public virtual async Task> Empty() { Country model = await Store.Empty(); if (model == null) { return NotFound(); } return Mapper.Map(model); } [HttpGet("pick")] [ZeroAuthorize(CountryPermissions.Read)] public virtual async Task> Pick(ListQuery query) { query.OrderQuery = q => q.OrderByDescending(x => x.IsPreferred).ThenBy(x => x.Name); return await Picker.PickFrom(query.Page, query.PageSize, query); } [HttpGet("pick")] [ZeroAuthorize(CountryPermissions.Read)] public virtual async Task>> Pick(string[] ids) { return await Picker.GetPreviews(ids); } [HttpGet("{id}")] [ZeroAuthorize(CountryPermissions.Read)] public virtual async Task> Get(string id, string changeVector = null) { Country model = await Store.Load(id, changeVector); if (model == null) { return NotFound(); } return Mapper.Map(model); } [HttpGet] [ZeroAuthorize(CountryPermissions.Read)] public virtual async Task> Get(ListQuery query) { query.OrderQuery = q => q.OrderByDescending(x => x.IsPreferred).ThenBy(x => x.Name); Paged result = await Store.Load(query.Page, query.PageSize, q => q.Filter(query)); return Mapper.Map(result); } [HttpPost] [ZeroAuthorize(CountryPermissions.Create)] public virtual async Task> Create(CountrySave saveModel) { Country model = Mapper.Map(saveModel); Result result = await Store.Create(model); if (result.IsSuccess) { Result mappedResult = Mapper.Map(result); return CreatedAtAction(nameof(CountriesController.Get), new { id = model.Id }, mappedResult); } return result.WithoutModel(); } [HttpPut("{id}")] [ZeroAuthorize(CountryPermissions.Update)] public virtual async Task> Update(string id, CountrySave updateModel) { if (id != updateModel.Id) { return BadRequest(BackofficeConstants.HttpErrors.NoIdMatchOnUpdate); } Country model = Mapper.Map(updateModel); model.Id = id; Result result = await Store.Update(model); return result.WithoutModel(); } [HttpDelete("{id}")] [ZeroAuthorize(CountryPermissions.Delete)] public virtual async Task> Delete(string id) { Country model = await Store.Load(id); if (model == null) { return NotFound(); } Result result = await Store.Delete(model); return result.WithoutModel(); } }