Files
ShopSolution-backend/Webshop.Api/Controllers/Admin/AdminShippingMethodsController.cs
2025-08-01 13:42:11 +02:00

63 lines
2.3 KiB
C#

// src/Webshop.Api/Controllers/Admin/AdminShippingMethodsController.cs
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Webshop.Application.DTOs.Shipping;
using Webshop.Application.Services.Admin;
namespace Webshop.Api.Controllers.Admin
{
[ApiController]
[Route("api/v1/admin/[controller]")]
[Authorize(Roles = "Admin")]
public class AdminShippingMethodsController : ControllerBase
{
private readonly IAdminShippingMethodService _shippingMethodService;
public AdminShippingMethodsController(IAdminShippingMethodService shippingMethodService)
{
_shippingMethodService = shippingMethodService;
}
[HttpPost]
public async Task<ActionResult<ShippingMethodDto>> CreateShippingMethod([FromBody] ShippingMethodDto shippingMethodDto)
{
var createdMethod = await _shippingMethodService.CreateAsync(shippingMethodDto);
return CreatedAtAction(nameof(GetShippingMethodById), new { id = createdMethod.Id }, createdMethod);
}
[HttpGet("{id}")]
public async Task<ActionResult<ShippingMethodDto>> GetShippingMethodById(Guid id)
{
var method = await _shippingMethodService.GetByIdAsync(id);
if (method == null) return NotFound();
return Ok(method);
}
[HttpGet]
public async Task<ActionResult<IEnumerable<ShippingMethodDto>>> GetAllShippingMethods()
{
var methods = await _shippingMethodService.GetAllAsync();
return Ok(methods);
}
[HttpPut("{id}")]
public async Task<IActionResult> UpdateShippingMethod(Guid id, [FromBody] ShippingMethodDto shippingMethodDto)
{
if (id != shippingMethodDto.Id) return BadRequest();
var success = await _shippingMethodService.UpdateAsync(shippingMethodDto);
if (!success) return NotFound();
return NoContent();
}
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteShippingMethod(Guid id)
{
var success = await _shippingMethodService.DeleteAsync(id);
if (!success) return NotFound();
return NoContent();
}
}
}