112 lines
4.9 KiB
C#
112 lines
4.9 KiB
C#
// src/Webshop.Api/Controllers/Admin/AdminSuppliersController.cs
|
||
using Microsoft.AspNetCore.Authorization;
|
||
using Microsoft.AspNetCore.Http;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Threading.Tasks;
|
||
using Webshop.Application;
|
||
using Webshop.Application.DTOs.Suppliers;
|
||
using Webshop.Application.Services.Admin.Interfaces;
|
||
|
||
namespace Webshop.Api.Controllers.Admin
|
||
{
|
||
[ApiController]
|
||
[Route("api/v1/admin/[controller]")]
|
||
[Authorize(Roles = "Admin")]
|
||
public class AdminSuppliersController : ControllerBase
|
||
{
|
||
private readonly IAdminSupplierService _adminSupplierService;
|
||
|
||
public AdminSuppliersController(IAdminSupplierService adminSupplierService)
|
||
{
|
||
_adminSupplierService = adminSupplierService;
|
||
}
|
||
|
||
[HttpGet]
|
||
[ProducesResponseType(typeof(IEnumerable<SupplierDto>), StatusCodes.Status200OK)]
|
||
public async Task<IActionResult> GetAllSuppliers()
|
||
{
|
||
var result = await _adminSupplierService.GetAllSuppliersAsync();
|
||
return Ok(result.Value);
|
||
}
|
||
|
||
[HttpGet("{id:guid}")]
|
||
[ProducesResponseType(typeof(SupplierDto), StatusCodes.Status200OK)]
|
||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
||
public async Task<IActionResult> GetSupplierById(Guid id)
|
||
{
|
||
var result = await _adminSupplierService.GetSupplierByIdAsync(id);
|
||
|
||
return result.Type switch
|
||
{
|
||
ServiceResultType.Success => Ok(result.Value),
|
||
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
|
||
_ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." })
|
||
};
|
||
}
|
||
|
||
[HttpPost]
|
||
[ProducesResponseType(typeof(SupplierDto), StatusCodes.Status201Created)]
|
||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status409Conflict)]
|
||
public async Task<IActionResult> CreateSupplier([FromBody] SupplierDto supplierDto)
|
||
{
|
||
if (!ModelState.IsValid)
|
||
{
|
||
return BadRequest(ModelState);
|
||
}
|
||
|
||
var result = await _adminSupplierService.CreateSupplierAsync(supplierDto);
|
||
|
||
return result.Type switch
|
||
{
|
||
ServiceResultType.Success => CreatedAtAction(nameof(GetSupplierById), new { id = result.Value!.Id }, result.Value),
|
||
ServiceResultType.Conflict => Conflict(new { Message = result.ErrorMessage }),
|
||
_ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." })
|
||
};
|
||
}
|
||
|
||
[HttpPut("{id:guid}")]
|
||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status409Conflict)]
|
||
public async Task<IActionResult> UpdateSupplier(Guid id, [FromBody] SupplierDto supplierDto)
|
||
{
|
||
if (id != supplierDto.Id)
|
||
{
|
||
return BadRequest(new { Message = "ID in der URL und im Body stimmen nicht <20>berein." });
|
||
}
|
||
if (!ModelState.IsValid)
|
||
{
|
||
return BadRequest(ModelState);
|
||
}
|
||
|
||
var result = await _adminSupplierService.UpdateSupplierAsync(supplierDto);
|
||
|
||
return result.Type switch
|
||
{
|
||
ServiceResultType.Success => NoContent(),
|
||
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
|
||
ServiceResultType.Conflict => Conflict(new { Message = result.ErrorMessage }),
|
||
_ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." })
|
||
};
|
||
}
|
||
|
||
[HttpDelete("{id:guid}")]
|
||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
||
public async Task<IActionResult> DeleteSupplier(Guid id)
|
||
{
|
||
var result = await _adminSupplierService.DeleteSupplierAsync(id);
|
||
|
||
return result.Type switch
|
||
{
|
||
ServiceResultType.Success => NoContent(),
|
||
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
|
||
_ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." })
|
||
};
|
||
}
|
||
}
|
||
} |