using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization; using DomainResults.Mvc; using WeatherForecast.Services; using WeatherForecast.Policies; using Core.Enumerations; using WeatherForecast.Models.Category.Requests; using WeatherForecast.Models.CategoryItem.Requests; using DomainObjects.Documents.Users; namespace WeatherForecast.Controllers; /// /// /// [ApiController] [Route("api/[controller]")] public class CategoryItemController : ControllerBase { private readonly IAuthorizationService _authorizationService; private readonly ICategoryItemService _categoryItemService; /// /// /// /// /// public CategoryItemController( IAuthorizationService authorizationService, ICategoryItemService categoryItemService ) { _authorizationService = authorizationService; _categoryItemService = categoryItemService; } #region Authless methods /// /// /// /// /// [HttpGet("{siteId}")] public IActionResult GetSlug([FromRoute] Guid siteId, [FromQuery] GetCategoryItemSlugRequestModel requestData) { var result = _categoryItemService.GetSlug(siteId, requestData); return result.ToActionResult(); } #endregion /// /// Can Admin, Editor, Shop manager /// /// /// /// [HttpPost("{siteId}")] public async Task Post([FromRoute] Guid siteId, [FromBody] PostCategoryItemRequestModel requestData) { if ((await _authorizationService.AuthorizeAsync(User, null, new CategoryAuthorizationRequirement { Action = CrudActions.Create, Roles = new List { new CategoryRole { Role = Roles.Admin }, new CategoryRole { Role = Roles.Editor }, new CategoryRole { Role = Roles.ShopManager } } })).Succeeded) { var result = _categoryItemService.Post(siteId, requestData); return result.ToActionResult(); } else { return Unauthorized(); } } /// /// Returns full object /// Can Admin, Editor, Author, Contributor, Shop manager /// /// /// /// [HttpGet("{siteId}/{categoryId}")] public async Task Get([FromRoute] Guid siteId, [FromRoute] Guid categoryId) { if ((await _authorizationService.AuthorizeAsync(User, null, new CategoryAuthorizationRequirement { Action = CrudActions.Read, Roles = new List { new CategoryRole { Role = Roles.Admin }, new CategoryRole { Role = Roles.Editor }, new CategoryRole { Role = Roles.Author }, new CategoryRole { Role = Roles.Contributor }, new CategoryRole { Role = Roles.ShopManager }, } })).Succeeded) { var result = _categoryItemService.Get(siteId, categoryId); return result.ToActionResult(); } else { return Unauthorized(); } } /// /// Can Admin, Editor, Shop manager /// /// /// /// /// [HttpPut("{siteId}/{categoryId}")] public async Task Update([FromRoute] Guid siteId, [FromRoute] Guid categoryId, [FromBody] PutCategoryItemRequestModel requestData) { if ((await _authorizationService.AuthorizeAsync(User, null, new CategoryAuthorizationRequirement { Action = CrudActions.Update, Roles = new List { new CategoryRole { Role = Roles.Admin }, new CategoryRole { Role = Roles.Editor }, new CategoryRole { Role = Roles.ShopManager } } })).Succeeded) { var result = _categoryItemService.Update(siteId, categoryId, requestData); return result.ToActionResult(); } return Unauthorized(); } /// /// Can Admin only /// /// /// /// [HttpDelete("{siteId}/{categoryId}")] public async Task Delete([FromRoute] Guid siteId, [FromRoute] Guid categoryId) { if ((await _authorizationService.AuthorizeAsync(User, null, new CategoryAuthorizationRequirement { Action = CrudActions.Delete, Roles = new List { new CategoryRole { Role = Roles.Admin } } })).Succeeded) { var result = _categoryItemService.Delete(siteId, categoryId); return result.ToActionResult(); } return Unauthorized(); } }