2023-09-15 08:42:46 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-09-19 07:48:59 +00:00
|
|
|
using Microsoft.OpenApi.Writers;
|
2023-09-15 08:42:46 +00:00
|
|
|
|
2023-09-19 11:45:34 +00:00
|
|
|
namespace backend;
|
|
|
|
using static backend.QueryParameterValidators;
|
2023-09-15 08:42:46 +00:00
|
|
|
|
|
|
|
[ApiController]
|
|
|
|
[Route("[controller]")]
|
|
|
|
public class MovieController: ControllerBase
|
|
|
|
{
|
2023-09-19 07:48:59 +00:00
|
|
|
private readonly MovieDbContext _context;
|
2023-09-15 08:42:46 +00:00
|
|
|
private readonly ILogger<MovieController> _logger;
|
2023-09-19 11:45:34 +00:00
|
|
|
private static readonly string[] sortTypes = new[]
|
|
|
|
{
|
|
|
|
"titleasc", "titledesc", "yearasc", "yeardesc"
|
|
|
|
};
|
2023-09-15 08:42:46 +00:00
|
|
|
|
2023-09-19 07:48:59 +00:00
|
|
|
public MovieController(ILogger<MovieController> logger, MovieDbContext context)
|
2023-09-15 08:42:46 +00:00
|
|
|
{
|
|
|
|
_logger = logger;
|
2023-09-19 07:48:59 +00:00
|
|
|
_context = context;
|
2023-09-15 08:42:46 +00:00
|
|
|
}
|
|
|
|
|
2023-09-19 08:14:35 +00:00
|
|
|
[HttpGet(Name = "GetMovies")]
|
2023-09-19 09:58:43 +00:00
|
|
|
public ActionResult<IEnumerable<MovieDB>> Get(
|
2023-09-19 11:45:34 +00:00
|
|
|
[FromQuery] string s,
|
2023-09-19 09:58:43 +00:00
|
|
|
[FromQuery] string? type,
|
|
|
|
[FromQuery] string? y,
|
2023-09-19 11:45:34 +00:00
|
|
|
[FromQuery] string? sort,
|
2023-09-19 09:58:43 +00:00
|
|
|
[FromQuery] int pageNumber = 1,
|
|
|
|
[FromQuery] int pageSize = 5
|
|
|
|
) {
|
2023-09-19 08:14:35 +00:00
|
|
|
try
|
|
|
|
{
|
2023-09-19 11:57:44 +00:00
|
|
|
if (!IsValidS(s)) return StatusCode(400, "Bad Request: Invalid title");
|
|
|
|
var movies = _context.Movies.Where(m => m.Title.ToLower().Contains(s.ToLower()));
|
2023-09-19 09:58:43 +00:00
|
|
|
|
|
|
|
if (type != null) {
|
2023-09-19 11:45:34 +00:00
|
|
|
if (!IsValidType(type)) return StatusCode(400, "Bad Request: Invalid type");
|
2023-09-19 09:58:43 +00:00
|
|
|
movies = movies.Where(m => m.Type == type);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (y != null) {
|
2023-09-19 11:45:34 +00:00
|
|
|
if (!IsValidYear(y)) return StatusCode(400, "Bad Request: Invalid year");
|
2023-09-19 09:58:43 +00:00
|
|
|
movies = movies.Where(m => m.Year == y);
|
|
|
|
}
|
|
|
|
|
2023-09-19 11:45:34 +00:00
|
|
|
if (sort != null) {
|
|
|
|
if (!IsValidSort(sort)) return StatusCode(400, "Bad Request: Invalid sort-type");
|
2023-09-19 11:53:05 +00:00
|
|
|
if (sort == "titleasc") movies = movies.OrderBy(m => m.Title);
|
|
|
|
if (sort == "titledesc") movies = movies.OrderByDescending(m => m.Title);
|
|
|
|
if (sort == "yearasc") movies = movies.OrderBy(m => m.Year);
|
|
|
|
if (sort == "yeardesc") movies = movies.OrderByDescending(m => m.Year);
|
|
|
|
}
|
|
|
|
|
2023-09-19 11:45:34 +00:00
|
|
|
|
|
|
|
if (!IsValidPageNumber(pageNumber) || !IsValidPageSize(pageSize))
|
|
|
|
return StatusCode(400, "Bad Request: Invalid page-size or page-number");
|
|
|
|
|
2023-09-19 09:58:43 +00:00
|
|
|
var totalMovies = movies.Count();
|
2023-09-19 09:14:59 +00:00
|
|
|
var totalPages = Math.Ceiling((double)totalMovies / pageSize);
|
2023-09-19 11:45:34 +00:00
|
|
|
|
2023-09-19 09:14:59 +00:00
|
|
|
IEnumerable<MovieDB> resultSkip = movies.Skip(pageSize * (pageNumber - 1));
|
|
|
|
IEnumerable<MovieDB> resultTake = resultSkip.Take(pageSize);
|
|
|
|
|
2023-09-19 09:21:13 +00:00
|
|
|
if (resultTake.ToList().Count == 0) {
|
|
|
|
return StatusCode(204, "No Content");
|
|
|
|
}
|
|
|
|
|
2023-09-19 09:14:59 +00:00
|
|
|
return Ok(resultTake);
|
2023-09-19 08:14:35 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.LogError(ex, "Error fetching movies");
|
|
|
|
return StatusCode(500, "Internal server error");
|
|
|
|
}
|
2023-09-15 08:42:46 +00:00
|
|
|
}
|
2023-09-19 08:14:35 +00:00
|
|
|
|
|
|
|
// [HttpPost(Name = "PostMovie")]
|
|
|
|
// public ActionResult<> Post([FromQuery] string movieTitle) {
|
|
|
|
// try
|
|
|
|
// {
|
|
|
|
|
|
|
|
// }
|
|
|
|
// catch (Exception ex)
|
|
|
|
// {
|
|
|
|
|
|
|
|
// }
|
|
|
|
// }
|
2023-09-15 08:42:46 +00:00
|
|
|
}
|