using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using BlueWest.Data; using BlueWest.WebApi.EF; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace BlueWest.WebApi.Controllers { /// /// Controller responsible to get countryId data /// [ApiController] [Route("[controller]")] [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] [Authorize(AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme)] [Authorize(Roles = "Administrator")] public class CountryController : ControllerBase { #region Initialization private readonly CountryDbContext _dbContext; /// /// Controller responsible for handling countryId data in the Country table /// /// public CountryController(CountryDbContext dbContext) { _dbContext = dbContext; } #endregion #region GetCountries /// /// Get countries /// /// [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] [HttpGet] public ActionResult GetCountries( int skip = 0, int take = 50, int orderDir = 1) { var (success, countries) = _dbContext.GetCountries( skip, take, orderDir); if (!success) return new NotFoundResult(); return Ok(countries); } #endregion #region GetCountryById /// /// Get Country by Id /// /// ISO 3166-1 countryId numeric code /// [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] [HttpGet("{countryId}", Name = nameof(GetCountryById))] public ActionResult GetCountryById(int countryId) { var (success, country) = _dbContext.GetOneCountryById(countryId); if (success) { return Ok(country); } return new NotFoundResult(); } #endregion #region GetCountryCurrencies /// /// Get currencies of a countryId /// /// /// How many records to skip /// How many records to take /// The order direction /// [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] [HttpGet("{countryId}/currencies")] public ActionResult GetCountryCurrencies(int countryId, int skip = 0, int take = 50, int orderDir = 1) { var (success, result) = _dbContext.GetCountryCurrencies(countryId, skip, take, orderDir); if(success) { return Ok(result); } return new NotFoundResult(); } #endregion #region AddCountry /// /// Add Country /// /// The countryId data to create /// The newly created countryId /// /// /// Creates a Country. /// /// /// Sample request: /// /// POST /Countries /// { /// "code": 1, /// "stateName": "United States of America", /// "tld": "us" /// } /// /// /// Returns the newly created countryId [ProducesResponseType(StatusCodes.Status201Created)] [ProducesResponseType(StatusCodes.Status406NotAcceptable)] [HttpPost] public ActionResult AddCountry(CountryCreate countryToCreate) { var (success, country) = _dbContext.AddCountry(countryToCreate); if (!success) return new BadRequestResult(); return CreatedAtRoute(nameof(GetCountryById), new {countryId = country.Id}, country); } #endregion #region UpdateCountry /// /// Updates a Country /// /// The countryId ISO 3166 code /// Country payload data /// [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [HttpPut("{countryCode}")] public ActionResult UpdateCountry(int countryCode, CountryUpdate countryToUpdate) { var (success, country) = _dbContext.UpdateCountry(countryToUpdate, countryCode); if (success) { return Ok(country); } return new NotFoundResult(); } #endregion #region AddCurrencyToCountry /// /// Adds a currency to Country /// /// /// /// [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status409Conflict)] [HttpPost("{countryId}/currencies")] public ActionResult AddCurrencyToCountry(int countryId, CurrencyCreate currencyCreate) { var (success, message, newCurrency) = _dbContext.AddCurrencyToCountry(countryId, currencyCreate); if(success) { return Ok(newCurrency); } return new NotFoundObjectResult(message); } #endregion } }