115 lines
4.3 KiB
C#
115 lines
4.3 KiB
C#
using System.ComponentModel.DataAnnotations;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using SWAD.API.Consts;
|
|
using SWAD.API.Consts.Enums;
|
|
using SWAD.API.Controllers.DTOs;
|
|
using SWAD.API.Services.Links;
|
|
|
|
namespace SWAD.API.Controllers;
|
|
|
|
/// <summary>
|
|
/// Controller for get links from music providers
|
|
/// </summary>
|
|
[Tags($"{nameof(LinkController)}: Controller for get links from music providers")]
|
|
[Route("[controller]")]
|
|
[ApiController]
|
|
public class LinkController(LinksService linksService) : SwadController
|
|
{
|
|
/// <summary>
|
|
/// Get link from other link
|
|
/// </summary>
|
|
/// <param Name="track">Track query such as Name, artist, Albums</param>
|
|
/// <returns>Link for track</returns>
|
|
[HttpPost("FromLink")]
|
|
[ProducesResponseType(typeof(LinkResultDto), 200)]
|
|
[ProducesResponseType(typeof(ProblemDetails), 400)]
|
|
[ProducesResponseType(typeof(ProblemDetails), 404)]
|
|
[ProducesResponseType(typeof(ProblemDetails), 502)]
|
|
public async Task<IActionResult> GetLink(TrackLinkDto track)
|
|
{
|
|
var serviceResult = await linksService.MapLinks(track);
|
|
|
|
switch (serviceResult.result)
|
|
{
|
|
case ServiceResult.Success:
|
|
return Ok(new LinkResultDto(serviceResult.link));
|
|
|
|
case ServiceResult.Failure:
|
|
return BadRequest($"{track.Link} is not recognized. Try other link");
|
|
|
|
case ServiceResult.NoResponse:
|
|
return BadGateway("Bad gateway with one of services");
|
|
|
|
case ServiceResult.BadRequest:
|
|
return BadRequest($"Unknown service {track.Service} or parameters not valid");
|
|
|
|
case ServiceResult.NotFound:
|
|
return NotFound(ErrorResources.NotFound);
|
|
|
|
case ServiceResult.TooManyRequests:
|
|
return TooManyRequests("Yandex is shit, try again later.");
|
|
|
|
default:
|
|
throw new ApplicationException("Unknown response from service");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get link from search query such as artist and song Name
|
|
/// </summary>
|
|
/// <param Name="track">Track query such as Name, artist, Albums</param>
|
|
/// <returns>Link for track</returns>
|
|
[HttpPost("FromQuery")]
|
|
[ProducesResponseType(typeof(LinkResultDto), 200)]
|
|
[ProducesResponseType(typeof(ProblemDetails), 400)]
|
|
[ProducesResponseType(typeof(ProblemDetails), 404)]
|
|
[ProducesResponseType(typeof(ProblemDetails), 502)]
|
|
public async Task<IActionResult> GetLink(TrackDto track)
|
|
{
|
|
var serviceResult = await linksService.GetLinkByQuery(track);
|
|
|
|
switch (serviceResult.result)
|
|
{
|
|
case ServiceResult.Success:
|
|
return Ok(new LinkResultDto(serviceResult.link));
|
|
|
|
case ServiceResult.Failure:
|
|
return NotFound($"Track {track.Name} - {track.Artist} not found!");
|
|
|
|
case ServiceResult.NoResponse:
|
|
return BadGateway($"Bad gateway with service {track.Service}");
|
|
|
|
case ServiceResult.BadRequest:
|
|
return BadRequest($"Unknown service {track.Service} or parameters not valid");
|
|
|
|
case ServiceResult.NotFound:
|
|
return BadRequest(ErrorResources.NotFound);
|
|
default:
|
|
throw new ApplicationException($"Unknown response from service {linksService}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get service what uses that link
|
|
/// </summary>
|
|
/// <param name="link"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="ApplicationException"></exception>
|
|
[HttpGet("GetService")]
|
|
[ProducesResponseType(typeof(ServiceDto), 200)]
|
|
[ProducesResponseType(typeof(ProblemDetails), 400)]
|
|
public async Task<IActionResult> GetServiceOfLink([Required] string link)
|
|
{
|
|
var serviceResult = await linksService.GetServiceByLink(link);
|
|
switch (serviceResult.result)
|
|
{
|
|
case ServiceResult.Success:
|
|
return Ok(new ServiceDto(serviceResult.service!.Value,
|
|
serviceResult.service.Value.ToString()));
|
|
case ServiceResult.Failure:
|
|
return BadRequest($"{link} is not recognized. Try other link");
|
|
default:
|
|
throw new ApplicationException("WTF are you doing?");
|
|
}
|
|
}
|
|
} |