initial commit
This commit is contained in:
63
CityInfo.API/Controllers/CitiesController.cs
Normal file
63
CityInfo.API/Controllers/CitiesController.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using AutoMapper;
|
||||
using CityInfo.API.Models;
|
||||
using CityInfo.API.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace CityInfo.API.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
[Route("api/cities")]
|
||||
public class CitiesController: ControllerBase
|
||||
{
|
||||
private readonly ICityInfoRepository _cityInfoRepository;
|
||||
private readonly IMapper _mapper;
|
||||
const int maximumPageSize = 20;
|
||||
|
||||
public CitiesController(
|
||||
ICityInfoRepository cityInfoRepository,
|
||||
IMapper mapper)
|
||||
{
|
||||
_cityInfoRepository = cityInfoRepository ?? throw new ArgumentNullException(nameof(cityInfoRepository));
|
||||
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
|
||||
}
|
||||
[HttpGet()]
|
||||
public async Task<ActionResult<IEnumerable<CityWithoutPointsOfInterestDto>>> GetCities(
|
||||
string? name, string? searchQuery, int pageNumber = 1, int pageSize = 10)
|
||||
{
|
||||
if(pageSize > maximumPageSize)
|
||||
{
|
||||
pageSize = maximumPageSize;
|
||||
}
|
||||
|
||||
var (cityEntities, paginationMetaData) = await _cityInfoRepository
|
||||
.GetCitiesAsync(name, searchQuery,pageNumber, pageSize);
|
||||
|
||||
Response.Headers.Append("X-Pagination", JsonSerializer.Serialize(paginationMetaData));
|
||||
|
||||
return Ok(_mapper.Map<IEnumerable<CityWithoutPointsOfInterestDto>>(cityEntities));
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public async Task<IActionResult> GetCity(int id, bool includePointsOfInterest = false)
|
||||
{
|
||||
var city = await _cityInfoRepository.GetCityAsync(id, includePointsOfInterest);
|
||||
|
||||
if (city == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
if (includePointsOfInterest)
|
||||
{
|
||||
return Ok(_mapper.Map<CityDto>(city));
|
||||
}
|
||||
|
||||
return Ok(_mapper.Map<CityWithoutPointsOfInterestDto>(city));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user