CodeLiturgy.Dashboard/BlueWest.Api/Controllers/CountryController.cs

201 lines
6.1 KiB
C#
Raw Normal View History

using System;
2022-08-18 18:46:20 +03:00
using System.Collections.Generic;
2022-08-13 06:35:36 +03:00
using System.Linq;
using System.Linq.Expressions;
2022-08-13 06:35:36 +03:00
using BlueWest.Data;
2022-08-22 00:14:50 +03:00
using BlueWest.WebApi.EF;
2022-09-10 07:12:03 +03:00
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
2022-08-13 06:35:36 +03:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
2022-08-17 23:21:00 +03:00
namespace BlueWest.WebApi.Controllers
{
2022-08-17 23:17:37 +03:00
/// <summary>
2022-08-22 02:51:45 +03:00
/// Controller responsible to get countryId data
2022-08-17 23:17:37 +03:00
/// </summary>
2022-08-19 06:18:50 +03:00
[ApiController]
[Route("[controller]")]
2022-09-10 07:12:03 +03:00
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[Authorize(AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme)]
2022-08-22 02:51:45 +03:00
public class CountryController : ControllerBase
2022-08-13 06:35:36 +03:00
{
2022-09-05 05:58:37 +03:00
#region Initialization
2022-08-19 06:22:55 +03:00
private readonly CountryDbContext _dbContext;
2022-08-13 06:35:36 +03:00
2022-08-19 06:18:50 +03:00
/// <summary>
2022-08-22 02:51:45 +03:00
/// Controller responsible for handling countryId data in the Country table
2022-08-19 06:18:50 +03:00
/// </summary>
/// <param name="dbContext"></param>
2022-08-19 06:22:55 +03:00
public CountryController(CountryDbContext dbContext)
2022-08-19 06:18:50 +03:00
{
_dbContext = dbContext;
}
2022-08-13 06:35:36 +03:00
2022-09-05 05:58:37 +03:00
#endregion
2022-09-08 06:15:44 +03:00
#region GetCountries
2022-08-19 06:18:50 +03:00
/// <summary>
/// Get countries
/// </summary>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet]
2022-09-06 00:42:45 +03:00
public ActionResult GetCountries(
int skip = 0,
int take = 50,
int orderDir = 1)
2022-08-13 06:35:36 +03:00
{
2022-09-08 06:15:44 +03:00
2022-09-09 06:26:16 +03:00
var (success, countries) = _dbContext.GetCountries(skip, take, orderDir);
2022-09-06 00:42:45 +03:00
if (!success) return new NotFoundResult();
return Ok(countries);
2022-08-19 06:18:50 +03:00
2022-08-13 06:35:36 +03:00
}
2022-09-08 06:15:44 +03:00
#endregion
#region GetCountryById
2022-08-19 06:18:50 +03:00
/// <summary>
/// Get Country by Id
/// </summary>
2022-08-22 02:51:45 +03:00
/// <param name="countryId">ISO 3166-1 countryId numeric code</param>
2022-08-19 06:18:50 +03:00
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet("{countryId}", Name = nameof(GetCountryById))]
public ActionResult GetCountryById(int countryId)
{
2022-09-05 05:58:37 +03:00
var (success, country) = _dbContext.GetOneCountryById(countryId);
2022-09-05 05:58:37 +03:00
if (success)
2022-08-19 06:18:50 +03:00
{
return Ok(country);
2022-08-19 06:18:50 +03:00
}
return new NotFoundResult();
}
2022-09-08 06:15:44 +03:00
#endregion
2022-09-09 06:26:16 +03:00
#region GetCountryCurrencies
2022-08-19 06:18:50 +03:00
/// <summary>
2022-08-22 02:51:45 +03:00
/// Get currencies of a countryId
2022-08-19 06:18:50 +03:00
/// </summary>
/// <param name="countryId"></param>
2022-09-09 06:26:16 +03:00
/// <param name="skip">How many records to skip</param>
/// <param name="take">How many records to take</param>
/// <param name="orderDir">The order direction</param>
2022-08-19 06:18:50 +03:00
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet("{countryId}/currencies")]
2022-09-09 06:26:16 +03:00
public ActionResult GetCountryCurrencies(int countryId, int skip = 0, int take = 50, int orderDir = 1)
2022-08-19 06:18:50 +03:00
{
2022-09-09 06:26:16 +03:00
var (success, result) = _dbContext.GetCountryCurrencies(countryId, skip, take, orderDir);
2022-08-19 06:18:50 +03:00
2022-09-09 06:26:16 +03:00
if(success)
2022-09-08 06:15:44 +03:00
{
2022-09-09 06:26:16 +03:00
return Ok(result);
2022-09-08 06:15:44 +03:00
}
2022-08-13 06:35:36 +03:00
2022-09-09 06:26:16 +03:00
return new NotFoundResult();
2022-08-19 06:18:50 +03:00
}
2022-09-09 06:26:16 +03:00
#endregion
2022-09-08 06:15:44 +03:00
#region AddCountry
/// <summary>
/// Add Country
/// </summary>
/// <param name="countryToCreate">The countryId data to create</param>
/// <returns>The newly created countryId</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 countryId</response>
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status406NotAcceptable)]
[HttpPost]
public ActionResult AddCountry(CountryCreate countryToCreate)
{
2022-08-23 19:48:16 +03:00
var (success, country) = _dbContext.AddCountry(countryToCreate);
if (!success) return new BadRequestResult();
2022-08-23 19:48:16 +03:00
return CreatedAtRoute(nameof(GetCountryById), new {countryId = country.Id}, country);
}
2022-09-08 06:15:44 +03:00
#endregion
#region UpdateCountry
/// <summary>
/// Updates a Country
/// </summary>
/// <param name="countryCode">The countryId ISO 3166 code</param>
/// <param name="countryToUpdate">Country payload data</param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpPut("{countryCode}")]
public ActionResult UpdateCountry(int countryCode, CountryUpdate countryToUpdate)
{
var (success, country) = _dbContext.UpdateCountry(countryToUpdate, countryCode);
if (success)
{
2022-08-29 03:40:57 +03:00
return Ok(country);
}
return new NotFoundResult();
}
2022-09-08 06:15:44 +03:00
#endregion
#region AddCurrencyToCountry
/// <summary>
/// Adds a currency to Country
/// </summary>
/// <param name="countryId"></param>
/// <param name="currencyCreate"></param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
[HttpPost("{countryId}/currencies")]
2022-09-08 06:15:44 +03:00
public ActionResult AddCurrencyToCountry(int countryId, CurrencyCreate currencyCreate)
{
2022-09-09 06:26:16 +03:00
var (success, message, newCurrency) = _dbContext.AddCurrencyToCountry(countryId, currencyCreate);
if(success)
{
2022-09-09 06:26:16 +03:00
return Ok(newCurrency);
}
2022-09-09 06:26:16 +03:00
return new NotFoundObjectResult(message);
}
2022-09-08 06:15:44 +03:00
#endregion
2022-08-19 06:18:50 +03:00
}
}