130 lines
5.1 KiB
C#
130 lines
5.1 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using T120B165_ImgBoard.Dtos;
|
|
using T120B165_ImgBoard.Dtos.Tag;
|
|
using T120B165_ImgBoard.Models;
|
|
using T120B165_ImgBoard.Services;
|
|
using T120B165_ImgBoard.Utils;
|
|
|
|
namespace T120B165_ImgBoard.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/tags")]
|
|
public class TagController(ITagService tagService) : ControllerBase
|
|
{
|
|
/// <summary>
|
|
/// Creates a new tag.
|
|
/// </summary>
|
|
/// <param name="dto">New tag data.</param>
|
|
/// <response code="201">Returns the newly created tag</response>
|
|
/// <response code="400">If request is malformed</response>
|
|
/// <response code="401">If authentication is missing</response>
|
|
/// <response code="403">If authorization is missing</response>
|
|
/// <response code="409">If tag already exists with such name</response>
|
|
[HttpPost]
|
|
[Authorize(Roles = UserRoles.Admin)]
|
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
|
[ProducesResponseType(StatusCodes.Status409Conflict)]
|
|
public async Task<ActionResult<Tag>> Create(CreateTagDto dto)
|
|
{
|
|
// Check if tag exists, if it does, throw a conflict
|
|
var existingTag = await tagService.GetByName(dto.Name);
|
|
if (existingTag != null)
|
|
{
|
|
return Problem(
|
|
detail: "Tag with such name already exists.",
|
|
statusCode: StatusCodes.Status409Conflict
|
|
);
|
|
}
|
|
|
|
var createdTag = await tagService.Create(dto.Type, dto.Name);
|
|
return CreatedAtAction(nameof(Get), new { name = createdTag.Name }, createdTag);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get a paginated list of tags.
|
|
/// </summary>
|
|
/// <param name="pageNumber">The page number</param>
|
|
/// <response code="200">Returns paginated list</response>
|
|
/// <response code="400">If request is malformed</response>
|
|
[HttpGet]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
public async Task<ActionResult<PagedList<Tag>>> GetAll(int pageNumber = 1)
|
|
{
|
|
return Ok(await tagService.GetAll(pageNumber));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get specific tag by name.
|
|
/// </summary>
|
|
/// <param name="name">The tag name</param>
|
|
/// <response code="200">The tag data</response>
|
|
/// <response code="400">If request is malformed</response>
|
|
/// <response code="404">If tag does not exist</response>
|
|
[HttpGet("{name}")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
public async Task<ActionResult<Tag>> Get(string name)
|
|
{
|
|
var tag = await tagService.GetByName(name);
|
|
if (tag == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
return Ok(tag);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delete specified tag.
|
|
/// </summary>
|
|
/// <param name="name">The tag name</param>
|
|
/// <response code="204">Indicates tag deletion success</response>
|
|
/// <response code="400">If request is malformed</response>
|
|
/// <response code="401">If authentication is missing</response>
|
|
/// <response code="403">If authorization is missing</response>
|
|
/// <response code="404">If tag does not exist</response>
|
|
[HttpDelete("{name}")]
|
|
[Authorize(Roles = UserRoles.Admin)]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
public async Task<ActionResult> Delete(string name)
|
|
{
|
|
var deleted = await tagService.DeleteByName(name);
|
|
if (!deleted) return NotFound();
|
|
return NoContent();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update specified tag.
|
|
/// </summary>
|
|
/// <param name="name">The tag name</param>
|
|
/// <param name="dto">The new tag data</param>
|
|
/// <response code="200">Indicates tag update success</response>
|
|
/// <response code="400">If request is malformed</response>
|
|
/// <response code="401">If authentication is missing</response>
|
|
/// <response code="403">If authorization is missing</response>
|
|
/// <response code="404">If tag does not exist</response>
|
|
[HttpPatch("{name}")]
|
|
[Authorize(Roles = UserRoles.Admin)]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
public async Task<ActionResult<Tag>> Update(string name, EditTagDto dto)
|
|
{
|
|
var tag = await tagService.GetByName(name);
|
|
if (tag == null) return NotFound();
|
|
var updatedTag = await tagService.Update(tag, dto.Type);
|
|
return Ok(updatedTag);
|
|
}
|
|
}
|