113 lines
3.2 KiB
C#
113 lines
3.2 KiB
C#
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;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Add Country
|
|
/// </summary>
|
|
/// <param name="country"></param>
|
|
/// <returns>The newly created country</returns>
|
|
/// /// <summary>
|
|
/// Creates a Country.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Sample request:
|
|
///
|
|
/// POST /Countries
|
|
/// {
|
|
/// "code": 1,
|
|
/// "stateName": "United States of America",
|
|
/// "tld": "us"
|
|
/// }
|
|
///
|
|
/// </remarks>
|
|
/// <response code="201">Returns the newly created country</response>
|
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
|
[HttpPost]
|
|
public ActionResult AddCountry(Country country)
|
|
{
|
|
_dbContext.Countries.Add(country);
|
|
_dbContext.SaveChanges();
|
|
return CreatedAtRoute(nameof(GetCountryById), new {countryId = country.Code}, country);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates a Country
|
|
/// </summary>
|
|
/// <param name="countryToUpdate">Payload with country data to update. Note that the Code is the primary key and can't be changed.</param>
|
|
/// <returns></returns>
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[HttpPut("countries/{country.Code}")]
|
|
public ActionResult UpdateCountry(Country countryToUpdate)
|
|
{
|
|
|
|
var country = _dbContext.Countries.FirstOrDefault(x => x.Code == countryToUpdate.Code);
|
|
|
|
if (country != null)
|
|
{
|
|
var updatedCountry = new Country(country.Code, countryToUpdate.StateName, countryToUpdate.TLD);
|
|
_dbContext.Countries.Update(updatedCountry);
|
|
return Ok(updatedCountry);
|
|
}
|
|
|
|
return new NotFoundResult();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Get countries
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[HttpGet("countries")]
|
|
public ActionResult GetCountries()
|
|
{
|
|
var array = _dbContext.Countries;
|
|
|
|
if (array != null)
|
|
{
|
|
return Ok(array.ToArray());
|
|
}
|
|
|
|
return new NotFoundResult();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get Country by Id
|
|
/// </summary>
|
|
/// <param name="countryId"></param>
|
|
/// <returns></returns>
|
|
[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();
|
|
}
|
|
|
|
|
|
} |