using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization; using DomainResults.Mvc; using WeatherForecast.Services; using WeatherForecast.Models.Requests; using WeatherForecast.Policies; using Core.Enumerations; using DataProviders.Collections; using DomainObjects.Documents; using DomainResults.Common; using ExtensionMethods; namespace WeatherForecast.Controllers; /// /// /// [ApiController] [Route("api/[controller]")] public class ShopItemController : ControllerBase { private readonly IAuthorizationService _authorizationService; private readonly IShopCatalogDataProvider _shopCatalogDataProvider; private readonly IShopItemService _shopItemService; /// /// /// /// /// /// public ShopItemController( IAuthorizationService authorizationService, IShopCatalogDataProvider shopCatalogDataProvider, IShopItemService shopItemService ) { _authorizationService = authorizationService; _shopCatalogDataProvider = shopCatalogDataProvider; _shopItemService = shopItemService; } #region Authless methods /// /// /// /// /// /// [HttpGet("{siteId}")] public IActionResult GetSlug([FromRoute] Guid siteId, [FromQuery] string slug) { var result = _shopItemService.GetSlug(siteId, slug); return result.ToActionResult(); } #endregion /// /// /// /// /// /// /// [HttpPost("{siteId}/{sku}")] public async Task Post([FromRoute] Guid siteId, [FromRoute] string sku, [FromBody] ShopItemRequestModel requestData) { var shopItem = requestData.ToDomainObject(); shopItem.SiteId = siteId; shopItem.Sku = sku; var userId = User?.Identity?.Name?.ToNullableGuid(); if (userId == null) return IDomainResult.Failed().ToActionResult(); shopItem.Author = userId.Value; if ((await _authorizationService.AuthorizeAsync(User, new List { shopItem }, new ShopAuthorizationRequirement { Action = CrudActions.Create })).Succeeded) { var result = _shopItemService.Post(shopItem); return result.ToActionResult(); } return Unauthorized(); } /// /// Returns full object /// /// /// /// [HttpGet("{siteId}/{sku}")] public async Task Get([FromRoute] Guid siteId, [FromRoute] string sku) { var (shopItem, getShopItemResult) = _shopCatalogDataProvider.Get(siteId, sku); if (!getShopItemResult.IsSuccess || shopItem == null) return getShopItemResult.ToActionResult(); if ((await _authorizationService.AuthorizeAsync(User, new List { shopItem }, new ShopAuthorizationRequirement { Action = CrudActions.Read })).Succeeded) { var result = _shopItemService.Get(shopItem); return result.ToActionResult(); } return Unauthorized(); } /// /// /// /// /// /// /// [HttpPut("{siteId}/{sku}")] public async Task Update([FromRoute] Guid siteId, [FromRoute] string sku, [FromBody] ShopItemRequestModel requestData) { var (shopItem, getShopItemResult) = _shopCatalogDataProvider.Get(siteId, sku); if (!getShopItemResult.IsSuccess || shopItem == null) return getShopItemResult.ToActionResult(); if ((await _authorizationService.AuthorizeAsync(User, new List { shopItem }, new ShopAuthorizationRequirement { Action = CrudActions.Update })).Succeeded) { var result = _shopItemService.Update(siteId, sku, requestData); return result.ToActionResult(); } return Unauthorized(); } /// /// /// /// /// /// [HttpDelete("{siteId}/{sku}")] public async Task Delete([FromRoute] Guid siteId, [FromRoute] string sku) { var (shopItem, getShopItemResult) = _shopCatalogDataProvider.Get(siteId, sku); if (!getShopItemResult.IsSuccess || shopItem == null) return getShopItemResult.ToActionResult(); if ((await _authorizationService.AuthorizeAsync(User, new List { shopItem }, new ShopAuthorizationRequirement { Action = CrudActions.Delete })).Succeeded) { var result = _shopCatalogDataProvider.Delete(shopItem.Id); return result.ToActionResult(); } return Unauthorized(); } }