From 5c5e9834f2851e0260d15515ea5c3b0347ba47ff Mon Sep 17 00:00:00 2001 From: CodeLiturgy Date: Tue, 23 Aug 2022 17:48:16 +0100 Subject: [PATCH] Add more data types --- BlueWest.Api/Context/CompanyDbContext.cs | 7 +++ .../Context/Extensions/CountryDbExtensions.cs | 43 ++++++++++++------- .../Context/Extensions/CurrencyExtensions.cs | 2 +- BlueWest.Api/Controllers/CountryController.cs | 6 +-- .../Controllers/CurrencyController.cs | 7 +-- ...oGeneratorAttribute.cs => EFAddMethods.cs} | 7 +-- BlueWest.Api/EfUpdateMethods.cs | 11 +++++ BlueWest.Data.Capital/Company/Company.cs | 2 +- .../Company/CompanySeller.cs | 8 ---- .../Company/{ => CompanyType}/CompanyType.cs | 0 .../{ => CompanyType}/CompanyTypeCreate.cs | 0 .../Company/CompanyUpdate.cs | 20 +++++++++ .../Company/{ => Product}/Product.cs | 4 ++ .../Company/Product/ProductCreate.cs | 13 ++++++ .../Company/Product/ProductUpdate.cs | 13 ++++++ BlueWest.Data.Capital/Industry/Industry.cs | 8 +++- .../Industry/IndustryCreate.cs | 12 ++++++ .../Industry/IndustryUpdate.cs | 12 ++++++ BlueWest.sln.DotSettings | 2 + 19 files changed, 139 insertions(+), 38 deletions(-) rename BlueWest.Api/{RepoGeneratorAttribute.cs => EFAddMethods.cs} (51%) create mode 100644 BlueWest.Api/EfUpdateMethods.cs delete mode 100644 BlueWest.Data.Capital/Company/CompanySeller.cs rename BlueWest.Data.Capital/Company/{ => CompanyType}/CompanyType.cs (100%) rename BlueWest.Data.Capital/Company/{ => CompanyType}/CompanyTypeCreate.cs (100%) create mode 100644 BlueWest.Data.Capital/Company/CompanyUpdate.cs rename BlueWest.Data.Capital/Company/{ => Product}/Product.cs (60%) create mode 100644 BlueWest.Data.Capital/Company/Product/ProductCreate.cs create mode 100644 BlueWest.Data.Capital/Company/Product/ProductUpdate.cs create mode 100644 BlueWest.Data.Capital/Industry/IndustryCreate.cs create mode 100644 BlueWest.Data.Capital/Industry/IndustryUpdate.cs create mode 100644 BlueWest.sln.DotSettings diff --git a/BlueWest.Api/Context/CompanyDbContext.cs b/BlueWest.Api/Context/CompanyDbContext.cs index af19183..7a7c03a 100644 --- a/BlueWest.Api/Context/CompanyDbContext.cs +++ b/BlueWest.Api/Context/CompanyDbContext.cs @@ -14,16 +14,23 @@ namespace BlueWest.WebApi.EF /// /// Companies. /// + [EFAddMethods(typeof(CompanyCreate))] + [EFUpdateMethods(typeof(CompanyUpdate))] + public DbSet Companies { get; set; } /// /// Industries. /// + [EFAddMethods(typeof(IndustryCreate))] + [EFUpdateMethods(typeof(IndustryUpdate))] public DbSet Industries { get; set; } /// /// Products. /// + [EFAddMethods(typeof(ProductCreate))] + [EFUpdateMethods(typeof(IndustryUpdate))] public DbSet Products { get; set; } diff --git a/BlueWest.Api/Context/Extensions/CountryDbExtensions.cs b/BlueWest.Api/Context/Extensions/CountryDbExtensions.cs index a90b3b4..4864aff 100644 --- a/BlueWest.Api/Context/Extensions/CountryDbExtensions.cs +++ b/BlueWest.Api/Context/Extensions/CountryDbExtensions.cs @@ -13,6 +13,19 @@ namespace BlueWest.WebApi.EF public static class CountryDbExtensions { + /// + /// Add a new country + /// + /// + /// + /// + 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)); + } /// /// Updates a country data. /// @@ -41,7 +54,10 @@ namespace BlueWest.WebApi.EF /// /// /// - 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 + /// /// Add Currency with optional duplication checks /// @@ -64,24 +81,21 @@ namespace BlueWest.WebApi.EF /// Data to create currency /// List of expressions /// - public static (bool, string, CountryUnique) AddCurrency( + public static (bool, string, CurrencyUnique) AddCurrency( this CountryDbContext dbContext, int countryId, CurrencyCreate currencyCreate, Expression>[] 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)); } } } diff --git a/BlueWest.Api/Context/Extensions/CurrencyExtensions.cs b/BlueWest.Api/Context/Extensions/CurrencyExtensions.cs index f7406e4..e3ef7d7 100644 --- a/BlueWest.Api/Context/Extensions/CurrencyExtensions.cs +++ b/BlueWest.Api/Context/Extensions/CurrencyExtensions.cs @@ -11,7 +11,7 @@ namespace BlueWest.WebApi.EF /// /// Currency table data extensions /// - public static class CurrencyExtensions + public static partial class CurrencyExtensions { /// diff --git a/BlueWest.Api/Controllers/CountryController.cs b/BlueWest.Api/Controllers/CountryController.cs index 794dc74..d76d95b 100644 --- a/BlueWest.Api/Controllers/CountryController.cs +++ b/BlueWest.Api/Controllers/CountryController.cs @@ -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); } /// diff --git a/BlueWest.Api/Controllers/CurrencyController.cs b/BlueWest.Api/Controllers/CurrencyController.cs index c739418..913c441 100644 --- a/BlueWest.Api/Controllers/CurrencyController.cs +++ b/BlueWest.Api/Controllers/CurrencyController.cs @@ -108,11 +108,12 @@ namespace BlueWest.WebApi.Controllers return new NotFoundResult(); } - + /// - /// Gets a currency by the currency number (id) + /// Gets a specific country id in a country /// - /// The id of the currency to get + /// The id of the currency + /// The id of the country /// [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] diff --git a/BlueWest.Api/RepoGeneratorAttribute.cs b/BlueWest.Api/EFAddMethods.cs similarity index 51% rename from BlueWest.Api/RepoGeneratorAttribute.cs rename to BlueWest.Api/EFAddMethods.cs index 8e9def1..8afc4c7 100644 --- a/BlueWest.Api/RepoGeneratorAttribute.cs +++ b/BlueWest.Api/EFAddMethods.cs @@ -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) { } } } diff --git a/BlueWest.Api/EfUpdateMethods.cs b/BlueWest.Api/EfUpdateMethods.cs new file mode 100644 index 0000000..7024f1a --- /dev/null +++ b/BlueWest.Api/EfUpdateMethods.cs @@ -0,0 +1,11 @@ +using System; + +namespace BlueWest.WebApi +{ + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)] + public class EFUpdateMethods : Attribute + { + public EFUpdateMethods(Type updateDto = null) { } + } +} + diff --git a/BlueWest.Data.Capital/Company/Company.cs b/BlueWest.Data.Capital/Company/Company.cs index a7506a0..aea9f81 100644 --- a/BlueWest.Data.Capital/Company/Company.cs +++ b/BlueWest.Data.Capital/Company/Company.cs @@ -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; } diff --git a/BlueWest.Data.Capital/Company/CompanySeller.cs b/BlueWest.Data.Capital/Company/CompanySeller.cs deleted file mode 100644 index 06d6842..0000000 --- a/BlueWest.Data.Capital/Company/CompanySeller.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace BlueWest.Data -{ - public partial class CompanySeller - { - - } -} - diff --git a/BlueWest.Data.Capital/Company/CompanyType.cs b/BlueWest.Data.Capital/Company/CompanyType/CompanyType.cs similarity index 100% rename from BlueWest.Data.Capital/Company/CompanyType.cs rename to BlueWest.Data.Capital/Company/CompanyType/CompanyType.cs diff --git a/BlueWest.Data.Capital/Company/CompanyTypeCreate.cs b/BlueWest.Data.Capital/Company/CompanyType/CompanyTypeCreate.cs similarity index 100% rename from BlueWest.Data.Capital/Company/CompanyTypeCreate.cs rename to BlueWest.Data.Capital/Company/CompanyType/CompanyTypeCreate.cs diff --git a/BlueWest.Data.Capital/Company/CompanyUpdate.cs b/BlueWest.Data.Capital/Company/CompanyUpdate.cs new file mode 100644 index 0000000..8df2f3b --- /dev/null +++ b/BlueWest.Data.Capital/Company/CompanyUpdate.cs @@ -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; } + } +} + diff --git a/BlueWest.Data.Capital/Company/Product.cs b/BlueWest.Data.Capital/Company/Product/Product.cs similarity index 60% rename from BlueWest.Data.Capital/Company/Product.cs rename to BlueWest.Data.Capital/Company/Product/Product.cs index b044966..831883d 100644 --- a/BlueWest.Data.Capital/Company/Product.cs +++ b/BlueWest.Data.Capital/Company/Product/Product.cs @@ -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 Seller { get; set; } } diff --git a/BlueWest.Data.Capital/Company/Product/ProductCreate.cs b/BlueWest.Data.Capital/Company/Product/ProductCreate.cs new file mode 100644 index 0000000..6b20c5e --- /dev/null +++ b/BlueWest.Data.Capital/Company/Product/ProductCreate.cs @@ -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; } + } +} + diff --git a/BlueWest.Data.Capital/Company/Product/ProductUpdate.cs b/BlueWest.Data.Capital/Company/Product/ProductUpdate.cs new file mode 100644 index 0000000..db11e94 --- /dev/null +++ b/BlueWest.Data.Capital/Company/Product/ProductUpdate.cs @@ -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; } + } +} + diff --git a/BlueWest.Data.Capital/Industry/Industry.cs b/BlueWest.Data.Capital/Industry/Industry.cs index 90a441a..fe50b9d 100644 --- a/BlueWest.Data.Capital/Industry/Industry.cs +++ b/BlueWest.Data.Capital/Industry/Industry.cs @@ -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 IndustryChilds { get; set; } } } diff --git a/BlueWest.Data.Capital/Industry/IndustryCreate.cs b/BlueWest.Data.Capital/Industry/IndustryCreate.cs new file mode 100644 index 0000000..4a71899 --- /dev/null +++ b/BlueWest.Data.Capital/Industry/IndustryCreate.cs @@ -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; } + } +} + diff --git a/BlueWest.Data.Capital/Industry/IndustryUpdate.cs b/BlueWest.Data.Capital/Industry/IndustryUpdate.cs new file mode 100644 index 0000000..4a1a8b3 --- /dev/null +++ b/BlueWest.Data.Capital/Industry/IndustryUpdate.cs @@ -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; } + } +} + diff --git a/BlueWest.sln.DotSettings b/BlueWest.sln.DotSettings new file mode 100644 index 0000000..3d8771c --- /dev/null +++ b/BlueWest.sln.DotSettings @@ -0,0 +1,2 @@ + + False \ No newline at end of file