YTSearch/YTSearch/Controllers/YouTubeController.cs
2025-05-20 21:45:49 -03:00

74 lines
2.3 KiB
C#

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using YTSearch.Contracts;
using YTSearch.Models;
namespace YTSearch.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class YouTubeController : ControllerBase
{
private readonly IYouTubeSearchService _youtubeService;
public YouTubeController(IYouTubeSearchService youtubeService)
{
_youtubeService = youtubeService;
}
/// <summary>
/// Search for YouTube videos by keywords
/// </summary>
/// <param name="keywords">Search keywords</param>
/// <param name="pageToken">Token for pagination (optional)</param>
/// <param name="maxResults">Maximum number of results (default: 20, max: 50)</param>
/// <returns>List of videos with details</returns>
[HttpGet("search")]
public async Task<ActionResult<SearchResponse>> SearchVideos(
[FromQuery] string keywords,
[FromQuery] string pageToken = "",
[FromQuery] int maxResults = 20)
{
if (string.IsNullOrWhiteSpace(keywords))
{
return BadRequest(new { message = "Keywords are required" });
}
if (maxResults > 50)
{
return BadRequest(new { message = "Maximum results cannot exceed 50" });
}
var request = new SearchRequest
{
Keywords = keywords,
PageToken = pageToken,
MaxResults = maxResults
};
var result = await _youtubeService.SearchVideosAsync(request);
return Ok(result);
}
/// <summary>
/// Search for YouTube videos using POST method
/// </summary>
[HttpPost("search")]
public async Task<ActionResult<SearchResponse>> SearchVideosPost([FromBody] SearchRequest request)
{
if (string.IsNullOrWhiteSpace(request.Keywords))
{
return BadRequest(new { message = "Keywords are required" });
}
if (request.MaxResults > 50)
{
return BadRequest(new { message = "Maximum results cannot exceed 50" });
}
var result = await _youtubeService.SearchVideosAsync(request);
return Ok(result);
}
}
}