Add more data types

This commit is contained in:
CodeLiturgy 2022-08-23 17:48:16 +01:00
parent f7c15f371d
commit 5c5e9834f2
19 changed files with 139 additions and 38 deletions

View File

@ -14,16 +14,23 @@ namespace BlueWest.WebApi.EF
/// <summary>
/// Companies.
/// </summary>
[EFAddMethods(typeof(CompanyCreate))]
[EFUpdateMethods(typeof(CompanyUpdate))]
public DbSet<Company> Companies { get; set; }
/// <summary>
/// Industries.
/// </summary>
[EFAddMethods(typeof(IndustryCreate))]
[EFUpdateMethods(typeof(IndustryUpdate))]
public DbSet<Industry> Industries { get; set; }
/// <summary>
/// Products.
/// </summary>
[EFAddMethods(typeof(ProductCreate))]
[EFUpdateMethods(typeof(IndustryUpdate))]
public DbSet<Product> Products { get; set; }

View File

@ -13,6 +13,19 @@ namespace BlueWest.WebApi.EF
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>
/// Updates a country data.
/// </summary>
@ -41,7 +54,10 @@ namespace BlueWest.WebApi.EF
/// <param name="countryId"></param>
/// <param name="currencyCreate"></param>
/// <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);
@ -55,7 +71,8 @@ namespace BlueWest.WebApi.EF
return !success ? (false, "Error saving the changes in the database.", null) : (true, string.Empty, country);
}
// country add (add currency) currency create, duplicatioon Validations
/// <summary>
/// Add Currency with optional duplication checks
/// </summary>
@ -64,24 +81,21 @@ namespace BlueWest.WebApi.EF
/// <param name="currencyCreate">Data to create currency</param>
/// <param name="duplicationValidations">List of expressions</param>
/// <returns></returns>
public static (bool, string, CountryUnique) AddCurrency(
public static (bool, string, CurrencyUnique) AddCurrency(
this CountryDbContext dbContext,
int countryId, CurrencyCreate currencyCreate,
Expression<Func<Currency,bool>>[] duplicationValidations)
{
var country = dbContext
.Countries
.Where(data => data.Id == countryId)
.Include(o => o.Currencies)
.FirstOrDefault();
var countryQuery = from aCountry in dbContext.Countries
where aCountry.Id == countryId
let currencies = aCountry.Currencies
select aCountry;
var country = countryQuery.FirstOrDefault();
// Check if currency exists
if (country == null) return (false, $"{nameof(country)} Not found.", null);
// Check if there's currency with the same code
foreach (var duplicationValidation in duplicationValidations)
{
var currencyToGet = dbContext.Currencies.FirstOrDefault(duplicationValidation);
@ -90,12 +104,11 @@ namespace BlueWest.WebApi.EF
return (false, $"Duplication Validation failed: {nameof(duplicationValidation.Body.ToString)}", null);
}
}
// Creates new currency
var newCurrency = new Currency(currencyCreate);
country.Currencies.Add(newCurrency);
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));
}
}
}

View File

@ -11,7 +11,7 @@ namespace BlueWest.WebApi.EF
/// <summary>
/// Currency table data extensions
/// </summary>
public static class CurrencyExtensions
public static partial class CurrencyExtensions
{
/// <summary>

View File

@ -124,11 +124,9 @@ namespace BlueWest.WebApi.Controllers
[HttpPost]
public ActionResult AddCountry(CountryCreate countryToCreate)
{
Country newCountry = new Country(countryToCreate);
_dbContext.Countries.Add(newCountry);
bool success = _dbContext.SaveChanges() >= 0;
var (success, country) = _dbContext.AddCountry(countryToCreate);
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>

View File

@ -108,11 +108,12 @@ namespace BlueWest.WebApi.Controllers
return new NotFoundResult();
}
/// <summary>
/// Gets a currency by the currency number (id)
/// Gets a specific country id in a country
/// </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>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]

View File

@ -3,12 +3,9 @@ using System;
namespace BlueWest.WebApi
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class RepoGeneratorAttribute : Attribute
public class EFAddMethods : Attribute
{
public RepoGeneratorAttribute()
{
}
public EFAddMethods(Type createDto = null) { }
}
}

View File

@ -0,0 +1,11 @@
using System;
namespace BlueWest.WebApi
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class EFUpdateMethods : Attribute
{
public EFUpdateMethods(Type updateDto = null) { }
}
}

View File

@ -4,7 +4,7 @@ using MapTo;
namespace BlueWest.Data
{
[MapFrom(typeof(CompanyCreate))]
[MapFrom(new [] {typeof(CompanyCreate), typeof(CompanyUpdate)})]
public partial class Company
{
public int Id { get; set; }

View File

@ -1,8 +0,0 @@
namespace BlueWest.Data
{
public partial class CompanySeller
{
}
}

View File

@ -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; }
}
}

View File

@ -1,10 +1,14 @@
using System.Collections.Generic;
using MapTo;
namespace BlueWest.Data
{
[MapFrom(new []{typeof(ProductUpdate), typeof(ProductCreate)})]
public partial class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Size { get; set; }
public Industry Industry { get; set; }
public List<Company> Seller { get; set; }
}

View File

@ -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; }
}
}

View File

@ -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; }
}
}

View File

@ -1,9 +1,15 @@
using System.Collections.Generic;
using MapTo;
namespace BlueWest.Data
{
[MapFrom(new []{typeof(IndustryCreate), typeof(IndustryUpdate)})]
public partial class Industry
{
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; }
}
}

View File

@ -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; }
}
}

View File

@ -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; }
}
}

2
BlueWest.sln.DotSettings Normal file
View File

@ -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>