Files
mixtape/zero.Api/Modules/Countries/CountriesController.cs
T

146 lines
4.0 KiB
C#
Raw Normal View History

2021-11-23 15:43:21 +01:00
using Microsoft.AspNetCore.Mvc;
2021-11-29 18:25:50 +01:00
using System.Collections;
2021-11-23 15:43:21 +01:00
2021-11-29 18:25:50 +01:00
namespace zero.Api.Modules.Countries;
2021-11-23 15:43:21 +01:00
2021-11-29 18:25:50 +01:00
public class CountriesController : ZeroApiController
2021-11-23 15:43:21 +01:00
{
2021-11-26 15:47:11 +01:00
protected ICountryStore Store { get; set; }
2021-11-23 15:43:21 +01:00
2021-11-27 16:09:02 +01:00
protected IPickerProvider<Country> Picker { get; set; }
public CountriesController(ICountryStore store, IPickerProvider<Country> picker)
2021-11-23 15:43:21 +01:00
{
2021-11-26 15:47:11 +01:00
Store = store;
2021-11-27 16:09:02 +01:00
Picker = picker;
2021-11-23 15:43:21 +01:00
}
[HttpGet("empty")]
2021-11-27 00:59:27 +01:00
[ZeroAuthorize(CountryPermissions.Create)]
2021-11-29 18:25:50 +01:00
public virtual async Task<ActionResult<DisplayModel>> Empty()
2021-11-23 15:43:21 +01:00
{
2021-11-26 15:47:11 +01:00
Country model = await Store.Empty();
if (model == null)
{
return NotFound();
}
return Mapper.Map<Country, CountryDisplay>(model);
2021-11-23 15:43:21 +01:00
}
2021-11-26 15:47:11 +01:00
2021-11-27 16:09:02 +01:00
[HttpGet("pick")]
[ZeroAuthorize(CountryPermissions.Read)]
2021-11-29 18:25:50 +01:00
public virtual async Task<ActionResult<Paged>> Pick([FromQuery] ListQuery<Country> query)
2021-11-27 16:09:02 +01:00
{
query.OrderQuery = q => q.OrderByDescending(x => x.IsPreferred).ThenBy(x => x.Name);
2021-11-29 18:25:50 +01:00
Paged<Country> result = await Store.Load<zero_Backoffice_Countries_Listing>(query.Page, query.PageSize, q => q.Filter(query));
return Mapper.Map<Country, PickerModel>(result);
2021-11-27 16:09:02 +01:00
}
2021-11-29 18:25:50 +01:00
[HttpGet("pickpreview")]
2021-11-27 16:09:02 +01:00
[ZeroAuthorize(CountryPermissions.Read)]
2021-11-29 18:25:50 +01:00
public virtual async Task<ActionResult<IEnumerable>> Pick([FromQuery] string[] ids)
2021-11-27 16:09:02 +01:00
{
2021-11-29 18:25:50 +01:00
Dictionary<string, Country> model = await Store.Load(ids);
return Mapper.Map<Country, PickerPreviewModel>(model);
2021-11-27 16:09:02 +01:00
}
2021-11-23 15:43:21 +01:00
[HttpGet("{id}")]
2021-11-27 00:59:27 +01:00
[ZeroAuthorize(CountryPermissions.Read)]
2021-11-29 18:25:50 +01:00
public virtual async Task<ActionResult<DisplayModel>> Get(string id, string changeVector = null)
2021-11-23 15:43:21 +01:00
{
2021-11-26 15:47:11 +01:00
Country model = await Store.Load(id, changeVector);
if (model == null)
{
return NotFound();
}
return Mapper.Map<Country, CountryDisplay>(model);
2021-11-23 15:43:21 +01:00
}
2021-11-26 15:47:11 +01:00
2021-11-29 00:38:55 +01:00
[HttpGet("")]
2021-11-27 00:59:27 +01:00
[ZeroAuthorize(CountryPermissions.Read)]
2021-11-29 18:25:50 +01:00
public virtual async Task<ActionResult<Paged>> Get([FromQuery] ListQuery<Country> query)
2021-11-23 15:43:21 +01:00
{
2021-11-27 16:09:02 +01:00
query.OrderQuery = q => q.OrderByDescending(x => x.IsPreferred).ThenBy(x => x.Name);
2021-11-26 15:47:11 +01:00
Paged<Country> result = await Store.Load<zero_Backoffice_Countries_Listing>(query.Page, query.PageSize, q => q.Filter(query));
return Mapper.Map<Country, CountryBasic>(result);
2021-11-23 15:43:21 +01:00
}
2021-11-29 00:38:55 +01:00
[HttpPost("")]
2021-11-27 00:59:27 +01:00
[ZeroAuthorize(CountryPermissions.Create)]
2021-11-26 15:47:11 +01:00
public virtual async Task<ActionResult<Result>> Create(CountrySave saveModel)
2021-11-23 15:43:21 +01:00
{
2021-11-26 15:47:11 +01:00
Country model = Mapper.Map<CountrySave, Country>(saveModel);
Result<Country> result = await Store.Create(model);
if (result.IsSuccess)
{
2021-11-26 15:51:14 +01:00
Result<CountryDisplay> mappedResult = Mapper.Map<Country, CountryDisplay>(result);
2021-11-26 15:47:11 +01:00
return CreatedAtAction(nameof(CountriesController.Get), new { id = model.Id }, mappedResult);
}
2021-11-29 18:25:50 +01:00
if (Hints.ResponsePreference == ApiResponsePreference.Minimal)
{
return result.WithoutModel();
}
return Mapper.Map<Country, CountryDisplay>(result);
2021-11-23 15:43:21 +01:00
}
2021-11-26 15:47:11 +01:00
2021-11-23 15:43:21 +01:00
[HttpPut("{id}")]
2021-11-27 00:59:27 +01:00
[ZeroAuthorize(CountryPermissions.Update)]
2021-11-26 15:47:11 +01:00
public virtual async Task<ActionResult<Result>> Update(string id, CountrySave updateModel)
2021-11-23 15:43:21 +01:00
{
2021-11-26 15:47:11 +01:00
if (id != updateModel.Id)
{
return BadRequest(BackofficeConstants.HttpErrors.NoIdMatchOnUpdate);
}
2021-11-29 18:25:50 +01:00
Country model = await Store.Load(id);
if (model == null)
{
return BadRequest(BackofficeConstants.HttpErrors.IdNotFound);
}
Mapper.Map(updateModel, model);
2021-11-26 15:47:11 +01:00
Result<Country> result = await Store.Update(model);
2021-11-29 18:25:50 +01:00
if (Hints.ResponsePreference == ApiResponsePreference.Minimal)
{
return result.WithoutModel();
}
// TODO add Preference-Applied header, see https://github.com/microsoft/api-guidelines/blob/vNext/Guidelines.md#76-standard-response-headers
return Mapper.Map<Country, CountryDisplay>(result);
2021-11-23 15:43:21 +01:00
}
2021-11-26 15:47:11 +01:00
2021-11-23 15:43:21 +01:00
[HttpDelete("{id}")]
2021-11-27 00:59:27 +01:00
[ZeroAuthorize(CountryPermissions.Delete)]
2021-11-26 15:47:11 +01:00
public virtual async Task<ActionResult<Result>> Delete(string id)
2021-11-23 15:43:21 +01:00
{
2021-11-26 15:47:11 +01:00
Country model = await Store.Load(id);
if (model == null)
{
return NotFound();
}
Result<Country> result = await Store.Delete(model);
return result.WithoutModel();
2021-11-23 15:43:21 +01:00
}
}