using System.Linq; using BlueWest.Data; using BlueWest.WebApi.MySQL; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace BlueWest.WebApi.Controllers; [ApiController] [Route("[controller]")] public class CountryController : ControllerBase { private readonly CountriesDbContext _dbContext; public CountryController(CountriesDbContext dbContext) { _dbContext = dbContext; } [ProducesResponseType(StatusCodes.Status201Created)] [HttpPost] public ActionResult AddCountry(Country country) { _dbContext.Countries.Add(country); _dbContext.SaveChanges(); return CreatedAtRoute(nameof(GetCountryById), new {countryId = country.Code}, country); } [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [HttpPut("countries/{country.Code}")] public ActionResult UpdateCountry(Country country) { var array = _dbContext.Countries.FirstOrDefault(x => x.Code == country.Code); if (array != null) { return Ok(array); } return new NotFoundResult(); } [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] [HttpGet("countries/{countryId}", Name = nameof(GetCountryById))] public ActionResult GetCountryById(int countryId) { var array = _dbContext.Countries.FirstOrDefault(x => x.Code == countryId); if (array != null) { return Ok(array); } return new NotFoundResult(); } }