47 lines
1.7 KiB
C#
47 lines
1.7 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using UniVerse.Application.DTOs.Courses;
|
|
using UniVerse.Application.Interfaces;
|
|
|
|
namespace UniVerse.Api.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/v1/courses")]
|
|
[Authorize]
|
|
public class CoursesController : ControllerBase
|
|
{
|
|
private readonly ICourseService _courses;
|
|
public CoursesController(ICourseService courses) => _courses = courses;
|
|
|
|
[HttpGet]
|
|
public async Task<ActionResult> GetAll([FromQuery] CourseFilterRequest filter) =>
|
|
Ok(await _courses.GetAllAsync(filter));
|
|
|
|
[HttpGet("{id:int}")]
|
|
public async Task<ActionResult<CourseDto>> Get(int id) => Ok(await _courses.GetByIdAsync(id));
|
|
|
|
[Authorize(Roles = "Admin")]
|
|
[HttpPost]
|
|
public async Task<ActionResult<CourseDto>> Create([FromBody] CreateCourseRequest req) =>
|
|
CreatedAtAction(nameof(Get), new { id = 0 }, await _courses.CreateAsync(req));
|
|
|
|
[Authorize(Roles = "Admin")]
|
|
[HttpPut("{id:int}")]
|
|
public async Task<ActionResult<CourseDto>> Update(int id, [FromBody] UpdateCourseRequest req) =>
|
|
Ok(await _courses.UpdateAsync(id, req));
|
|
|
|
[Authorize(Roles = "Admin")]
|
|
[HttpDelete("{id:int}")]
|
|
public async Task<IActionResult> Delete(int id) { await _courses.DeleteAsync(id); return NoContent(); }
|
|
|
|
[Authorize(Roles = "Admin")]
|
|
[HttpPost("{id:int}/tags")]
|
|
public async Task<IActionResult> AddTag(int id, [FromBody] int tagId)
|
|
{ await _courses.AddTagAsync(id, tagId); return NoContent(); }
|
|
|
|
[Authorize(Roles = "Admin")]
|
|
[HttpDelete("{id:int}/tags/{tagId:int}")]
|
|
public async Task<IActionResult> RemoveTag(int id, int tagId)
|
|
{ await _courses.RemoveTagAsync(id, tagId); return NoContent(); }
|
|
}
|