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; /// /// /// [AllowAnonymous] [Route("api/[controller]")] public class ShopCartItemController : ControllerBase { private readonly IAuthorizationService _authorizationService; private readonly IShopCartItemService _shopCartItemService; private readonly WebapiControllers _webapiController = WebapiControllers.ShopCartItem; /// /// /// /// public ShopCartItemController( IAuthorizationService authorizationService, IShopCartItemService shopCartItemService ) { _authorizationService = authorizationService; _shopCartItemService = shopCartItemService; } /// /// /// /// /// /// /// /// [HttpPost("{siteId}/{userId}/{sku}")] public async Task Post([FromRoute] Guid siteId, [FromRoute] Guid userId, [FromRoute] string sku, [FromBody] ShopCartItemRequestModel requestData) { if ((await _authorizationService.AuthorizeAsync(User, null, new CrudActionRequirement(_webapiController, CrudActions.Create))).Succeeded) { var result = _shopCartItemService.Post(siteId, userId, sku, requestData); return result.ToActionResult(); } return Unauthorized(); } /// /// /// /// [HttpGet("{siteId}/{userId}/{sku}")] public async Task Get([FromRoute] Guid siteId, [FromRoute] Guid userId, [FromRoute] string sku, [FromQuery] string? locale) { if ((await _authorizationService.AuthorizeAsync(User, null, new CrudActionRequirement(_webapiController, CrudActions.Read))).Succeeded) { var result = _shopCartItemService.Get(siteId, userId, sku, locale); return result.ToActionResult(); } return Unauthorized(); } /// /// /// /// /// /// /// /// [HttpPut("{siteId}/{userId}/{sku}")] public async Task Update([FromRoute] Guid siteId, [FromRoute] Guid userId, [FromRoute] string sku, [FromBody] ShopCartItemRequestModel requestData) { if ((await _authorizationService.AuthorizeAsync(User, null, new CrudActionRequirement(_webapiController, CrudActions.Update))).Succeeded) { var result = _shopCartItemService.Update(siteId, userId, sku, requestData); return result.ToActionResult(); } return Unauthorized(); } /// /// /// /// /// /// /// [HttpDelete("{siteId}/{userId}/{sku}")] public async Task Delete([FromRoute] Guid siteId, [FromRoute] Guid userId, [FromRoute] string sku) { if ((await _authorizationService.AuthorizeAsync(User, null, new CrudActionRequirement(_webapiController, CrudActions.Delete))).Succeeded) { var result = _shopCartItemService.Delete(siteId, userId, sku); return result.ToActionResult(); } return Unauthorized(); } }