2022-08-22 01:32:47 +03:00
|
|
|
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;
|
2022-08-22 01:32:47 +03:00
|
|
|
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-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-08-22 02:51:45 +03:00
|
|
|
public class CountryController : ControllerBase
|
2022-08-13 06:35:36 +03:00
|
|
|
{
|
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-08-22 01:32:47 +03:00
|
|
|
|
2022-08-19 06:18:50 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Get countries
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
|
|
[HttpGet]
|
|
|
|
public ActionResult GetCountries()
|
2022-08-13 06:35:36 +03:00
|
|
|
{
|
2022-08-22 05:13:53 +03:00
|
|
|
var array = _dbContext
|
|
|
|
.Countries
|
|
|
|
.Select(x => new CountryUnique(x))
|
|
|
|
.ToArray();
|
2022-08-19 06:18:50 +03:00
|
|
|
|
2022-08-22 05:13:53 +03:00
|
|
|
return Ok(array);
|
2022-08-19 06:18:50 +03:00
|
|
|
|
2022-08-13 06:35:36 +03:00
|
|
|
}
|
|
|
|
|
2022-08-22 05:13:53 +03:00
|
|
|
|
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-08-22 05:13:53 +03:00
|
|
|
var country = _dbContext.Countries
|
|
|
|
.Where(x => x.Id == countryId)
|
|
|
|
.Select(x => new CountryUnique(x))
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
2022-08-19 06:18:50 +03:00
|
|
|
|
2022-08-22 05:13:53 +03:00
|
|
|
if (country != null)
|
2022-08-19 06:18:50 +03:00
|
|
|
{
|
2022-08-22 05:13:53 +03:00
|
|
|
return Ok(country);
|
2022-08-19 06:18:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return new NotFoundResult();
|
|
|
|
}
|
|
|
|
|
2022-08-22 05:13:53 +03:00
|
|
|
|
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>
|
|
|
|
/// <returns></returns>
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
|
|
[HttpGet("{countryId}/currencies")]
|
|
|
|
public ActionResult GetCountryCurrencies(int countryId)
|
|
|
|
{
|
2022-08-22 02:51:45 +03:00
|
|
|
var countryObj = _dbContext.Countries.FirstOrDefault(d => d.Id == countryId);
|
2022-08-19 06:18:50 +03:00
|
|
|
|
2022-08-22 02:51:45 +03:00
|
|
|
if (countryObj == null) return new NotFoundResult();
|
2022-08-19 06:18:50 +03:00
|
|
|
|
|
|
|
var array = _dbContext
|
|
|
|
.Countries
|
2022-08-22 01:32:47 +03:00
|
|
|
.Where(data => data.Id == countryId)
|
2022-08-19 06:18:50 +03:00
|
|
|
.SelectMany(o => o.Currencies)
|
2022-08-22 05:13:53 +03:00
|
|
|
.Select(x => new CurrencyUnique(x))
|
2022-08-19 06:18:50 +03:00
|
|
|
.ToArray();
|
2022-08-13 06:35:36 +03:00
|
|
|
|
2022-08-19 06:18:50 +03:00
|
|
|
return Ok(array);
|
|
|
|
}
|
2022-08-22 01:32:47 +03:00
|
|
|
|
2022-08-22 05:13:53 +03:00
|
|
|
|
|
|
|
/// <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)
|
|
|
|
{
|
|
|
|
Country newCountry = new Country(countryToCreate);
|
|
|
|
_dbContext.Countries.Add(newCountry);
|
|
|
|
bool success = _dbContext.SaveChanges() >= 0;
|
|
|
|
if (!success) return new BadRequestResult();
|
|
|
|
return CreatedAtRoute(nameof(GetCountryById), new {countryId = newCountry.Id}, new CountryUnique(newCountry));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
{
|
|
|
|
var countryReply = new CountryUnique(country);
|
|
|
|
return Ok(countryReply);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new NotFoundResult();
|
|
|
|
}
|
|
|
|
|
2022-08-22 01:32:47 +03:00
|
|
|
|
|
|
|
/// <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")]
|
|
|
|
public ActionResult AddCurrency(int countryId, CurrencyCreate currencyCreate)
|
|
|
|
{
|
|
|
|
var (result, message, country) = _dbContext.AddCurrency(countryId, currencyCreate,
|
|
|
|
new Expression<Func<Currency, bool>>[]
|
|
|
|
{
|
|
|
|
x => x.Code == currencyCreate.Code,
|
|
|
|
y => y.Num == currencyCreate.Num
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
{
|
|
|
|
return new ConflictObjectResult(message);
|
|
|
|
}
|
|
|
|
|
2022-08-22 05:13:53 +03:00
|
|
|
return Ok(new CountryUnique(country));
|
2022-08-22 01:32:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-19 06:18:50 +03:00
|
|
|
}
|
|
|
|
}
|