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>> 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>(cityEntities)); } [HttpGet("{id}")] public async Task GetCity(int id, bool includePointsOfInterest = false) { var city = await _cityInfoRepository.GetCityAsync(id, includePointsOfInterest); if (city == null) { return NotFound(); } if (includePointsOfInterest) { return Ok(_mapper.Map(city)); } return Ok(_mapper.Map(city)); } } }