using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization; using DomainResults.Mvc; using WeatherForecast.Services; using WeatherForecast.Models.Requests; using WeatherForecast.Policies; using Core.Enumerations; namespace WeatherForecast.Controllers; /// /// /// [ApiController] [Route("api/[controller]")] public class CategoryItemController : ControllerBase { private readonly IAuthorizationService _authorizationService; private readonly ICategoryItemService _categoryItemService; private readonly WebapiControllers _webapiController = WebapiControllers.CategoryItem; /// /// /// /// /// public CategoryItemController( IAuthorizationService authorizationService, ICategoryItemService categoryItemService ) { _authorizationService = authorizationService; _categoryItemService = categoryItemService; } /// /// /// /// /// /// [HttpPost("{siteId}")] public async Task Post([FromRoute] Guid siteId, [FromBody] CategoryItemRequestModel requestData) { if ((await _authorizationService.AuthorizeAsync(User, null, new CrudActionRequirement(_webapiController, CrudActions.Create))).Succeeded) { var result = _categoryItemService.Post(siteId, requestData); return result.ToActionResult(); } else { return Unauthorized(); } } /// /// Returns full object /// /// /// /// [HttpGet("{siteId}/{categoryId}")] public async Task Get([FromRoute] Guid siteId, [FromRoute] Guid categoryId) { if ((await _authorizationService.AuthorizeAsync(User, null, new CrudActionRequirement(_webapiController, CrudActions.Read))).Succeeded) { var result = _categoryItemService.Get(siteId, categoryId); return result.ToActionResult(); } else { return Unauthorized(); } } /// /// /// /// /// [HttpGet("{siteId}")] public IActionResult GetSlug([FromRoute] Guid siteId, [FromQuery] string slug) { var result = _categoryItemService.GetSlug(siteId, slug); return result.ToActionResult(); } /// /// /// /// /// /// /// [HttpPut("{siteId}/{categoryId}")] public async Task Update([FromRoute] Guid siteId, [FromRoute] Guid categoryId, [FromBody] CategoryItemRequestModel requestData) { if ((await _authorizationService.AuthorizeAsync(User, null, new CrudActionRequirement(_webapiController, CrudActions.Update))).Succeeded) { var result = _categoryItemService.Update(siteId, categoryId, requestData); return result.ToActionResult(); } else { return Unauthorized(); } } /// /// /// /// /// /// [HttpDelete("{siteId}/{categoryId}")] public async Task Delete([FromRoute] Guid siteId, [FromRoute] Guid categoryId) { if ((await _authorizationService.AuthorizeAsync(User, null, new CrudActionRequirement(_webapiController, CrudActions.Delete))).Succeeded) { var result = _categoryItemService.Delete(siteId, categoryId); return result.ToActionResult(); } else { return Unauthorized(); } } }