Add more data types
This commit is contained in:
parent
f7c15f371d
commit
5c5e9834f2
|
@ -14,16 +14,23 @@ namespace BlueWest.WebApi.EF
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Companies.
|
/// Companies.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[EFAddMethods(typeof(CompanyCreate))]
|
||||||
|
[EFUpdateMethods(typeof(CompanyUpdate))]
|
||||||
|
|
||||||
public DbSet<Company> Companies { get; set; }
|
public DbSet<Company> Companies { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Industries.
|
/// Industries.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[EFAddMethods(typeof(IndustryCreate))]
|
||||||
|
[EFUpdateMethods(typeof(IndustryUpdate))]
|
||||||
public DbSet<Industry> Industries { get; set; }
|
public DbSet<Industry> Industries { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Products.
|
/// Products.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[EFAddMethods(typeof(ProductCreate))]
|
||||||
|
[EFUpdateMethods(typeof(IndustryUpdate))]
|
||||||
public DbSet<Product> Products { get; set; }
|
public DbSet<Product> Products { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,19 @@ namespace BlueWest.WebApi.EF
|
||||||
public static class CountryDbExtensions
|
public static class CountryDbExtensions
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Add a new country
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dbContext"></param>
|
||||||
|
/// <param name="countryCreate"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static (bool, CountryUnique) AddCountry(this CountryDbContext dbContext, CountryCreate countryCreate)
|
||||||
|
{
|
||||||
|
Country newCountry = new Country(countryCreate);
|
||||||
|
dbContext.Countries.Add(newCountry);
|
||||||
|
bool success = dbContext.SaveChanges() >= 0;
|
||||||
|
return (success, new CountryUnique(newCountry));
|
||||||
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Updates a country data.
|
/// Updates a country data.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -41,7 +54,10 @@ namespace BlueWest.WebApi.EF
|
||||||
/// <param name="countryId"></param>
|
/// <param name="countryId"></param>
|
||||||
/// <param name="currencyCreate"></param>
|
/// <param name="currencyCreate"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static (bool, string, Country) AddCurrency( this CountryDbContext dbContext, int countryId, CurrencyCreate currencyCreate)
|
public static (bool, string, Country) AddCurrency(
|
||||||
|
this CountryDbContext dbContext,
|
||||||
|
int countryId,
|
||||||
|
CurrencyCreate currencyCreate)
|
||||||
{
|
{
|
||||||
var country = dbContext.Countries.FirstOrDefault(d => d.Id == countryId);
|
var country = dbContext.Countries.FirstOrDefault(d => d.Id == countryId);
|
||||||
|
|
||||||
|
@ -55,6 +71,7 @@ namespace BlueWest.WebApi.EF
|
||||||
|
|
||||||
return !success ? (false, "Error saving the changes in the database.", null) : (true, string.Empty, country);
|
return !success ? (false, "Error saving the changes in the database.", null) : (true, string.Empty, country);
|
||||||
}
|
}
|
||||||
|
// country add (add currency) currency create, duplicatioon Validations
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Add Currency with optional duplication checks
|
/// Add Currency with optional duplication checks
|
||||||
|
@ -64,24 +81,21 @@ namespace BlueWest.WebApi.EF
|
||||||
/// <param name="currencyCreate">Data to create currency</param>
|
/// <param name="currencyCreate">Data to create currency</param>
|
||||||
/// <param name="duplicationValidations">List of expressions</param>
|
/// <param name="duplicationValidations">List of expressions</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static (bool, string, CountryUnique) AddCurrency(
|
public static (bool, string, CurrencyUnique) AddCurrency(
|
||||||
this CountryDbContext dbContext,
|
this CountryDbContext dbContext,
|
||||||
int countryId, CurrencyCreate currencyCreate,
|
int countryId, CurrencyCreate currencyCreate,
|
||||||
Expression<Func<Currency,bool>>[] duplicationValidations)
|
Expression<Func<Currency,bool>>[] duplicationValidations)
|
||||||
{
|
{
|
||||||
|
|
||||||
var country = dbContext
|
var countryQuery = from aCountry in dbContext.Countries
|
||||||
.Countries
|
where aCountry.Id == countryId
|
||||||
.Where(data => data.Id == countryId)
|
let currencies = aCountry.Currencies
|
||||||
.Include(o => o.Currencies)
|
select aCountry;
|
||||||
.FirstOrDefault();
|
|
||||||
|
|
||||||
|
var country = countryQuery.FirstOrDefault();
|
||||||
|
|
||||||
// Check if currency exists
|
|
||||||
if (country == null) return (false, $"{nameof(country)} Not found.", null);
|
if (country == null) return (false, $"{nameof(country)} Not found.", null);
|
||||||
|
|
||||||
// Check if there's currency with the same code
|
|
||||||
|
|
||||||
foreach (var duplicationValidation in duplicationValidations)
|
foreach (var duplicationValidation in duplicationValidations)
|
||||||
{
|
{
|
||||||
var currencyToGet = dbContext.Currencies.FirstOrDefault(duplicationValidation);
|
var currencyToGet = dbContext.Currencies.FirstOrDefault(duplicationValidation);
|
||||||
|
@ -91,11 +105,10 @@ namespace BlueWest.WebApi.EF
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates new currency
|
|
||||||
var newCurrency = new Currency(currencyCreate);
|
var newCurrency = new Currency(currencyCreate);
|
||||||
country.Currencies.Add(newCurrency);
|
country.Currencies.Add(newCurrency);
|
||||||
var success = dbContext.SaveChanges() >= 0;
|
var success = dbContext.SaveChanges() >= 0;
|
||||||
return !success ? (false, "Error saving the changes in the database.", null) : (true, string.Empty, new CountryUnique(country));
|
return !success ? (false, "Error saving the changes in the database.", null) : (true, string.Empty, new CurrencyUnique(newCurrency));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ namespace BlueWest.WebApi.EF
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Currency table data extensions
|
/// Currency table data extensions
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class CurrencyExtensions
|
public static partial class CurrencyExtensions
|
||||||
{
|
{
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -124,11 +124,9 @@ namespace BlueWest.WebApi.Controllers
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public ActionResult AddCountry(CountryCreate countryToCreate)
|
public ActionResult AddCountry(CountryCreate countryToCreate)
|
||||||
{
|
{
|
||||||
Country newCountry = new Country(countryToCreate);
|
var (success, country) = _dbContext.AddCountry(countryToCreate);
|
||||||
_dbContext.Countries.Add(newCountry);
|
|
||||||
bool success = _dbContext.SaveChanges() >= 0;
|
|
||||||
if (!success) return new BadRequestResult();
|
if (!success) return new BadRequestResult();
|
||||||
return CreatedAtRoute(nameof(GetCountryById), new {countryId = newCountry.Id}, new CountryUnique(newCountry));
|
return CreatedAtRoute(nameof(GetCountryById), new {countryId = country.Id}, country);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -110,9 +110,10 @@ namespace BlueWest.WebApi.Controllers
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a currency by the currency number (id)
|
/// Gets a specific country id in a country
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="currencyId">The id of the currency to get</param>
|
/// <param name="currencyId">The id of the currency</param>
|
||||||
|
/// <param name="countryId">The id of the country</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
|
|
@ -3,12 +3,9 @@ using System;
|
||||||
namespace BlueWest.WebApi
|
namespace BlueWest.WebApi
|
||||||
{
|
{
|
||||||
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
|
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
|
||||||
public class RepoGeneratorAttribute : Attribute
|
public class EFAddMethods : Attribute
|
||||||
{
|
{
|
||||||
public RepoGeneratorAttribute()
|
public EFAddMethods(Type createDto = null) { }
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace BlueWest.WebApi
|
||||||
|
{
|
||||||
|
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
|
||||||
|
public class EFUpdateMethods : Attribute
|
||||||
|
{
|
||||||
|
public EFUpdateMethods(Type updateDto = null) { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ using MapTo;
|
||||||
|
|
||||||
namespace BlueWest.Data
|
namespace BlueWest.Data
|
||||||
{
|
{
|
||||||
[MapFrom(typeof(CompanyCreate))]
|
[MapFrom(new [] {typeof(CompanyCreate), typeof(CompanyUpdate)})]
|
||||||
public partial class Company
|
public partial class Company
|
||||||
{
|
{
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
|
|
|
@ -1,8 +0,0 @@
|
||||||
namespace BlueWest.Data
|
|
||||||
{
|
|
||||||
public partial class CompanySeller
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
using System;
|
||||||
|
using MapTo;
|
||||||
|
|
||||||
|
namespace BlueWest.Data
|
||||||
|
{
|
||||||
|
[MapFrom(typeof(Company))]
|
||||||
|
public partial class CompanyUpdate
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
public string Address { get; set; }
|
||||||
|
|
||||||
|
public Country CurrentCountry { get; set; }
|
||||||
|
|
||||||
|
public Country OriginCountry { get; set; }
|
||||||
|
|
||||||
|
public DateTime FoundingDate { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,10 +1,14 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using MapTo;
|
||||||
|
|
||||||
namespace BlueWest.Data
|
namespace BlueWest.Data
|
||||||
{
|
{
|
||||||
|
[MapFrom(new []{typeof(ProductUpdate), typeof(ProductCreate)})]
|
||||||
public partial class Product
|
public partial class Product
|
||||||
{
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
|
public string Size { get; set; }
|
||||||
public Industry Industry { get; set; }
|
public Industry Industry { get; set; }
|
||||||
public List<Company> Seller { get; set; }
|
public List<Company> Seller { get; set; }
|
||||||
}
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
using MapTo;
|
||||||
|
|
||||||
|
namespace BlueWest.Data
|
||||||
|
{
|
||||||
|
[MapFrom(typeof(Product))]
|
||||||
|
public partial class ProductCreate
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
public string Size { get; set; }
|
||||||
|
public Industry Industry { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
using MapTo;
|
||||||
|
|
||||||
|
namespace BlueWest.Data
|
||||||
|
{
|
||||||
|
[MapFrom(typeof(Product))]
|
||||||
|
public partial class ProductUpdate
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
public string Size { get; set; }
|
||||||
|
public Industry Industry { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,9 +1,15 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using MapTo;
|
||||||
|
|
||||||
namespace BlueWest.Data
|
namespace BlueWest.Data
|
||||||
{
|
{
|
||||||
|
[MapFrom(new []{typeof(IndustryCreate), typeof(IndustryUpdate)})]
|
||||||
public partial class Industry
|
public partial class Industry
|
||||||
{
|
{
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
public string IndustryType { get; set; }
|
public string IndustryName { get; set; }
|
||||||
|
public Industry IndustryParent { get; set; }
|
||||||
|
public List<Industry> IndustryChilds { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
using MapTo;
|
||||||
|
|
||||||
|
namespace BlueWest.Data
|
||||||
|
{
|
||||||
|
[MapFrom(typeof(Industry))]
|
||||||
|
public partial class IndustryCreate
|
||||||
|
{
|
||||||
|
public string IndustryName { get; set; }
|
||||||
|
public Industry IndustryParent { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
using MapTo;
|
||||||
|
|
||||||
|
namespace BlueWest.Data
|
||||||
|
{
|
||||||
|
[MapFrom(typeof(Industry))]
|
||||||
|
public partial class IndustryUpdate
|
||||||
|
{
|
||||||
|
public string IndustryName { get; set; }
|
||||||
|
public Industry IndustryParent { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||||
|
<s:Boolean x:Key="/Default/CodeEditing/SuppressNullableWarningFix/Enabled/@EntryValue">False</s:Boolean></wpf:ResourceDictionary>
|
Loading…
Reference in New Issue