From 47b61fb3b2ca615e115aadfe15702b0a17952fab Mon Sep 17 00:00:00 2001 From: CodeLiturgy Date: Sat, 13 Aug 2022 18:15:43 +0100 Subject: [PATCH] Add disabled artifacts --- BlueWest.Api/BlueWest.Api.csproj | 10 ++-- BlueWest.Api/Controllers/CountryController.cs | 59 +++++++++++++++++-- BlueWest.Api/Dockerfile | 2 +- BlueWest.Api/MySQL/CountriesDbContext.cs | 24 +++++++- BlueWest.Api/Program.cs | 12 ++-- BlueWest.Api/Properties/launchSettings.json | 2 +- BlueWest.Api/Startup.cs | 38 +++++++++--- BlueWest.Data/Finance/Country.cs | 18 +++++- BlueWest.Data/Finance/Currency.cs | 6 +- BlueWest/Artefacts/AccountArtefact.cs | 1 + BlueWest/Artefacts/BlueConsole.cs | 3 +- BlueWest/Core/System/Artefact.cs | 18 +----- BlueWest/Core/System/ArtefactFrequency.cs | 15 +++++ BlueWest/Core/System/DisabledArtefact.cs | 16 +++++ 14 files changed, 174 insertions(+), 50 deletions(-) create mode 100644 BlueWest/Core/System/ArtefactFrequency.cs create mode 100644 BlueWest/Core/System/DisabledArtefact.cs diff --git a/BlueWest.Api/BlueWest.Api.csproj b/BlueWest.Api/BlueWest.Api.csproj index 857f5ac..4a620bb 100644 --- a/BlueWest.Api/BlueWest.Api.csproj +++ b/BlueWest.Api/BlueWest.Api.csproj @@ -5,16 +5,16 @@ 10 BlueWest.WebApi true + true + bin\$(Configuration)\$(AssemblyName).xml + true + - - - - - + diff --git a/BlueWest.Api/Controllers/CountryController.cs b/BlueWest.Api/Controllers/CountryController.cs index 899506a..5358096 100644 --- a/BlueWest.Api/Controllers/CountryController.cs +++ b/BlueWest.Api/Controllers/CountryController.cs @@ -17,6 +17,26 @@ public class CountryController : ControllerBase } + /// + /// Add Country + /// + /// + /// The newly created country + /// /// + /// Creates a Country. + /// + /// + /// Sample request: + /// + /// POST /Countries + /// { + /// "code": 1, + /// "stateName": "United States of America", + /// "tld": "us" + /// } + /// + /// + /// Returns the newly created country [ProducesResponseType(StatusCodes.Status201Created)] [HttpPost] public ActionResult AddCountry(Country country) @@ -26,23 +46,54 @@ public class CountryController : ControllerBase return CreatedAtRoute(nameof(GetCountryById), new {countryId = country.Code}, country); } + /// + /// Updates a Country + /// + /// Payload with country data to update. Note that the Code is the primary key and can't be changed. + /// [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [HttpPut("countries/{country.Code}")] - public ActionResult UpdateCountry(Country country) + public ActionResult UpdateCountry(Country countryToUpdate) { - var array = _dbContext.Countries.FirstOrDefault(x => x.Code == country.Code); + var country = _dbContext.Countries.FirstOrDefault(x => x.Code == countryToUpdate.Code); - if (array != null) + if (country != null) { - return Ok(array); + var updatedCountry = new Country(country.Code, countryToUpdate.StateName, countryToUpdate.TLD); + _dbContext.Countries.Update(updatedCountry); + return Ok(updatedCountry); } return new NotFoundResult(); } + /// + /// Get countries + /// + /// + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [HttpGet("countries")] + public ActionResult GetCountries() + { + var array = _dbContext.Countries; + + if (array != null) + { + return Ok(array.ToArray()); + } + + return new NotFoundResult(); + } + + /// + /// Get Country by Id + /// + /// + /// [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] [HttpGet("countries/{countryId}", Name = nameof(GetCountryById))] diff --git a/BlueWest.Api/Dockerfile b/BlueWest.Api/Dockerfile index a965d8e..e4e1a81 100644 --- a/BlueWest.Api/Dockerfile +++ b/BlueWest.Api/Dockerfile @@ -19,6 +19,6 @@ RUN dotnet publish "BlueWest.Api/BlueWest.Api.csproj" -c Release -o /app/publish FROM base AS final WORKDIR /app COPY --from=publish /app/publish . -#ENV ASPNETCORE_URLS http://0.0.0.0:80 +ENV ASPNETCORE_URLS http://0.0.0.0:80 WORKDIR /app ENTRYPOINT ["dotnet", "BlueWest.Api.dll" ] diff --git a/BlueWest.Api/MySQL/CountriesDbContext.cs b/BlueWest.Api/MySQL/CountriesDbContext.cs index 256b531..1507999 100644 --- a/BlueWest.Api/MySQL/CountriesDbContext.cs +++ b/BlueWest.Api/MySQL/CountriesDbContext.cs @@ -4,6 +4,9 @@ using Microsoft.Extensions.Configuration; namespace BlueWest.WebApi.MySQL; +/// +/// Table containing countries +/// public class CountriesDbContext : DbContext { @@ -15,7 +18,11 @@ public class CountriesDbContext : DbContext public IConfiguration Configuration; - public CountriesDbContext(DbContextOptions options) : base(options) + /// + /// Options to be injected in the app + /// + /// + public CountriesDbContext(DbContextOptions options) : base(options) { Database.EnsureCreated(); @@ -29,8 +36,23 @@ public class CountriesDbContext : DbContext );*/ } + /// + /// The country number is the primary key + /// The currency code is the primary key + /// + /// protected override void OnModelCreating(ModelBuilder modelBuilder) { + base.OnModelCreating(modelBuilder); + modelBuilder.Entity(builder => + { + builder.HasKey(x => x.Code); + }); + modelBuilder.Entity(builder => + { + builder.HasKey(x => x.Num); + }); + } diff --git a/BlueWest.Api/Program.cs b/BlueWest.Api/Program.cs index ad93092..854a391 100644 --- a/BlueWest.Api/Program.cs +++ b/BlueWest.Api/Program.cs @@ -4,6 +4,7 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Threading.Tasks; using BlueWest.Core; @@ -35,20 +36,15 @@ namespace BlueWest.WebApi public static void Main(string[] args) { - Host1 = CreateHostBuilder(args).Build(); + Host1 = CreateHostBuilder(args) + .UseContentRoot(Directory.GetCurrentDirectory()) + .Build(); Host1.RunAsync(); System.Threading.Thread.Sleep(2500); _threadServer = new ThreadServer(EventManager); - BlueWestConsoleBanner(); _threadServer.Init(); } - private static void BlueWestConsoleBanner() - { - Console.WriteLine(" |------------------------------«"); - Console.WriteLine(" » Blue West «"); - Console.WriteLine(" |------------------------------«\n"); - } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) diff --git a/BlueWest.Api/Properties/launchSettings.json b/BlueWest.Api/Properties/launchSettings.json index 991a3f9..3d937ec 100644 --- a/BlueWest.Api/Properties/launchSettings.json +++ b/BlueWest.Api/Properties/launchSettings.json @@ -24,7 +24,7 @@ "launchUrl": "swagger", "applicationUrl": "https://localhost:5001;http://localhost:5000", "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" + "ASPNETCORE_ENVIRONMENT": "Production" } } } diff --git a/BlueWest.Api/Startup.cs b/BlueWest.Api/Startup.cs index 0d1769b..dbdade6 100644 --- a/BlueWest.Api/Startup.cs +++ b/BlueWest.Api/Startup.cs @@ -8,7 +8,9 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; +using System.IO; using System.Linq; +using System.Reflection; using System.Threading.Tasks; using BlueWest.WebApi.MySQL; using BlueWest.WebApi.Tools; @@ -52,9 +54,26 @@ namespace BlueWest.WebApi options.SchemaFilter(); options.SwaggerDoc("v1", new OpenApiInfo { - Title = "BlueWest.Api", - Version = "v1" + Title = "BlueWest.Api Documentation", + Version = "v1", + Description = "A simple example ASP.NET Core Web API", + TermsOfService = new Uri("https://example.com/terms"), + Contact = new OpenApiContact + { + Name = "Benny", + Email = string.Empty, + Url = new Uri("https://git.codeliturgy.com"), + }, + License = new OpenApiLicense + { + Name = "Use under LICX", + Url = new Uri("https://git.codeliturgy.com/license"), + } }); + // Set the comments path for the Swagger JSON and UI. + var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; + var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); + options.IncludeXmlComments(xmlPath); }); services.AddDbContextPool(options => options.GetSqlSettings(Configuration)); @@ -72,12 +91,17 @@ namespace BlueWest.WebApi // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { - if (env.IsDevelopment()) + + //app.UseStaticFiles(); + + app.UseDeveloperExceptionPage(); + app.UseSwagger(); + app.UseSwaggerUI(c => { - app.UseDeveloperExceptionPage(); - app.UseSwagger(); - app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "BlueWest.Api v1")); - } + c.RoutePrefix = "swagger"; + c.SwaggerEndpoint("/swagger/v1/swagger.json", "BlueWest.Api v1"); + + }); //app.UseHttpsRedirection(); diff --git a/BlueWest.Data/Finance/Country.cs b/BlueWest.Data/Finance/Country.cs index c27b8bb..d035ac8 100644 --- a/BlueWest.Data/Finance/Country.cs +++ b/BlueWest.Data/Finance/Country.cs @@ -4,7 +4,19 @@ namespace BlueWest.Data; public class Country { - [Key] public int Code { get; } - public string StateName { get; } - public string TLD { get; } + public int Code { get; set; } // Primary key + public string StateName { get; set; } + public string TLD { get; set; } + + public Country(int code, string stateName, string tld) + { + Code = code; + StateName = stateName; + TLD = tld; + } + + public Country() + { + + } } \ No newline at end of file diff --git a/BlueWest.Data/Finance/Currency.cs b/BlueWest.Data/Finance/Currency.cs index ee40a28..1d76115 100644 --- a/BlueWest.Data/Finance/Currency.cs +++ b/BlueWest.Data/Finance/Currency.cs @@ -2,7 +2,7 @@ namespace BlueWest.Data; public class Currency { - public string Code { get; } - public int Num { get; } - public Country Country { get; } + public int Num { get; set; } // Primary key + public string Code { get; set; } + public Country Country { get; set; } } \ No newline at end of file diff --git a/BlueWest/Artefacts/AccountArtefact.cs b/BlueWest/Artefacts/AccountArtefact.cs index 1b0ab59..0931915 100644 --- a/BlueWest/Artefacts/AccountArtefact.cs +++ b/BlueWest/Artefacts/AccountArtefact.cs @@ -1,6 +1,7 @@ using System; using BlueWest.Core.ComponentSystem; using BlueWest.Tools; +using PerformanceSolution.Core.System; namespace PerformanceSolution.Artefacts { diff --git a/BlueWest/Artefacts/BlueConsole.cs b/BlueWest/Artefacts/BlueConsole.cs index 1c5a61e..4a8f0b9 100644 --- a/BlueWest/Artefacts/BlueConsole.cs +++ b/BlueWest/Artefacts/BlueConsole.cs @@ -3,10 +3,11 @@ using System.IO; using BlueWest.Core.ComponentSystem; using BlueWest.Tools; using PerformanceSolution.Artefacts; +using PerformanceSolution.Core.System; namespace BlueWest.Core { - public sealed class BlueConsole : Artefact + public sealed class BlueConsole : DisabledArtefact { private static string _consolePrompt = ">> "; diff --git a/BlueWest/Core/System/Artefact.cs b/BlueWest/Core/System/Artefact.cs index ebf919a..0c8d431 100644 --- a/BlueWest/Core/System/Artefact.cs +++ b/BlueWest/Core/System/Artefact.cs @@ -5,23 +5,11 @@ using System.Data; using System.Threading; using BlueWest.Coroutines; using BlueWest.Tools; +using PerformanceSolution.Core.System; namespace BlueWest.Core.ComponentSystem { - public enum ArtefactFrequency - { - T120Hz, - T60Hz, - T30Hz, - T20Hz, - T10Hz, - T4Hz , - T3Hz , - T2Hz , - T1Hz - } - public class Artefact { @@ -59,8 +47,6 @@ namespace BlueWest.Core.ComponentSystem private int _componentsCount = 0; - private readonly ArtefactFrequency BehaviorType; - // OLD STUFF @@ -76,7 +62,7 @@ namespace BlueWest.Core.ComponentSystem { _eventManager = eventManager; - switch (BehaviorType) + switch (Frequency) { case ArtefactFrequency.T1Hz: diff --git a/BlueWest/Core/System/ArtefactFrequency.cs b/BlueWest/Core/System/ArtefactFrequency.cs new file mode 100644 index 0000000..7dc4658 --- /dev/null +++ b/BlueWest/Core/System/ArtefactFrequency.cs @@ -0,0 +1,15 @@ +namespace PerformanceSolution.Core.System; + + +public enum ArtefactFrequency +{ + T120Hz, + T60Hz, + T30Hz, + T20Hz, + T10Hz, + T4Hz , + T3Hz , + T2Hz , + T1Hz +} \ No newline at end of file diff --git a/BlueWest/Core/System/DisabledArtefact.cs b/BlueWest/Core/System/DisabledArtefact.cs new file mode 100644 index 0000000..e559e33 --- /dev/null +++ b/BlueWest/Core/System/DisabledArtefact.cs @@ -0,0 +1,16 @@ +using BlueWest.Tools; + +namespace PerformanceSolution.Core.System; + +public class DisabledArtefact +{ + + protected internal ArtefactFrequency Frequency; + protected EventManager _eventManager; + + protected virtual void Update(double delta) + { + + } + +} \ No newline at end of file