initial commit

This commit is contained in:
2026-03-20 23:52:10 +01:00
parent 05bea695bd
commit ce04cd8d77
38 changed files with 3006 additions and 52 deletions

View 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));
}
}
}