Update controllers

This commit is contained in:
CodeLiturgy 2022-08-22 03:57:06 +01:00
parent a501bb9e33
commit e6754b0018
4 changed files with 113 additions and 41 deletions

View File

@ -12,30 +12,6 @@ namespace BlueWest.WebApi.EF
/// </summary>
public static class CountryDbExtensions
{
static CountryDbExtensions()
{
}
/// <summary>
/// Returns Not found projection
/// </summary>
/// <returns></returns>
public static (bool, Country) NotFound() => (false, null);
/// <summary>
/// Returns Error Message projection
/// </summary>
/// <param name="message"></param>
/// <returns>(false, message, null)</returns>
public static (bool, string, Country) ErrorMessage(string message) => (false, message, null);
/// <summary>
/// Returns Success intent projection
/// </summary>
/// <param name="success">Success message</param>
/// <param name="country">Entity object</param>
/// <returns>(bool success, Country country)</returns>
public static (bool, string, Country) Success(bool success, Country country) => (success, "1", null);
/// <summary>
/// Updates a country data.
@ -50,7 +26,7 @@ namespace BlueWest.WebApi.EF
int countryId)
{
var country = dbContext.Countries.FirstOrDefault(x => x.Id == countryId);
if (country == null) return NotFound();
if (country == null) return Projection.NotFound;
country.Update(countryUpdate);
dbContext.Countries.Update(country);
var result = dbContext.SaveChanges() >= 0;
@ -76,7 +52,7 @@ namespace BlueWest.WebApi.EF
var newCurrency = new Currency(currencyCreate);
country.Currencies.Add(newCurrency);
var success = dbContext.SaveChanges() >= 0;
return Success(success, country);
return Projection.Success(success, country);
}
/// <summary>
@ -108,7 +84,7 @@ namespace BlueWest.WebApi.EF
foreach (var duplicationValidation in duplicationValidations)
{
var currencyToGet = dbContext.Currencies.FirstOrDefault(duplicationValidation);
if (currencyToGet != null) return ErrorMessage($"Duplication Validation failed: {nameof(duplicationValidation.Body.ToString)}");
if (currencyToGet != null) return Projection.ErrorMessage($"Duplication Validation failed: {nameof(duplicationValidation.Body.ToString)}");
}
@ -116,10 +92,11 @@ namespace BlueWest.WebApi.EF
var newCurrency = new Currency(currencyCreate);
country.Currencies.Add(newCurrency);
var success = dbContext.SaveChanges() >= 0;
return Success(success, country);
return Projection.Success(success, country);
}
}
}

View File

@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using BlueWest.Data;
using Microsoft.EntityFrameworkCore;
@ -49,6 +51,37 @@ namespace BlueWest.WebApi.EF
return (resultOperation, currency);
}
public static (bool, string, CountryUnique) AddCountry(
this CountryDbContext dbContext,
int currencyId, CountryCreate countryCreate,
Expression<Func<Country,bool>>[] duplicationValidations)
{
var currency = dbContext
.Currencies
.Where(data => data.Id == currencyId)
.Include(o => o.Countries)
.FirstOrDefault();
// Check if currency exists
if (currency == null) return (false, $"{nameof(Currency)}: {currencyId} - Not found.", null);
// Check if there's currency with the same code
foreach (var duplicationValidation in duplicationValidations)
{
var currencyToGet = dbContext.Countries.FirstOrDefault(duplicationValidation);
if (currencyToGet != null) return Projection.ErrorMessage<CountryUnique>($"Duplication Validation failed: {duplicationValidation.Body}");
}
// Creates new currency
var newCountry = new Country(countryCreate);
currency.Countries.Add(newCountry);
var success = dbContext.SaveChanges() >= 0;
return Projection.Success(success, new CountryUnique(newCountry));
}
}
}

View File

@ -0,0 +1,26 @@
using BlueWest.Data;
namespace BlueWest.WebApi.EF
{
public static class Projection
{
public static readonly (bool, Country) NotFound = (false, null);
/// <summary>
/// Returns Error Message projection
/// </summary>
/// <param name="message"></param>
/// <returns>(false, message, null)</returns>
public static (bool, string, T) ErrorMessage<T>(string message) where T : class => (false, message, null);
/// <summary>
/// Returns Success intent projection
/// </summary>
/// <param name="success">Success message</param>
/// <param name="dataObject">Entity Object</param>
/// <returns>(bool success, Country country)</returns>
public static (bool, string, T) Success<T>(bool success, T dataObject) where T : class => (success, "1", dataObject);
}
}

View File

@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Linq.Expressions;
using BlueWest.Data;
using BlueWest.WebApi.EF;
using Microsoft.AspNetCore.Http;
@ -42,6 +44,31 @@ namespace BlueWest.WebApi.Controllers
return Ok(currencies);
}
/// <summary>
/// Gets a currency by the currency number (id)
/// </summary>
/// <param name="currencyId">The id of the currency to get</param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet("{currencyId}", Name = nameof(GetCurrencyById))]
public ActionResult GetCurrencyById(int currencyId)
{
var currency = _dbContext
.Currencies
.Where(x => x.Id == currencyId)
.Select(x => new CurrencyUnique(x))
.FirstOrDefault();
if (currency != null)
{
return Ok(currency);
}
return new NotFoundResult();
}
/// <summary>
/// Add Currency to the table of currencies
/// </summary>
@ -83,25 +110,34 @@ namespace BlueWest.WebApi.Controllers
return new NotFoundResult();
}
/// <summary>
/// Gets a currency by the currency number (id)
/// Add Currency to the table of currencies
/// </summary>
/// <param name="currencyId">The id of the currency to get</param>
/// <param name="currencyToCreate">Currency data to create</param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet("{currencyId}", Name = nameof(GetCurrencyById))]
public ActionResult GetCurrencyById(int currencyId)
[ProducesResponseType(StatusCodes.Status201Created)]
[HttpPost]
public ActionResult AddCountry(int currencyId, CountryCreate countryToCreate)
{
var currency = _dbContext.Currencies.FirstOrDefault(x => x.Id == currencyId);
if (currency != null)
var (result, message, country) = _dbContext.AddCountry(currencyId, countryToCreate,
new Expression<Func<Country, bool>>[]
{
return Ok(new CurrencyUnique(currency));
x => x.Code == countryToCreate.Code,
y => y.StateName == countryToCreate.StateName,
z => z.Alpha2Code == countryToCreate.Alpha2Code
});
if (!result)
{
return new ConflictObjectResult(message);
}
return new NotFoundResult();
return Ok(country);
}
}
}