From 61f4a0abac1dc621a3ba98700b5dccfad35708d8 Mon Sep 17 00:00:00 2001 From: code liturgy Date: Wed, 16 Nov 2022 22:17:37 +0000 Subject: [PATCH] Refactor and remove redundant user db context --- CodeLiturgy.Dashboard.sln | 8 +- CodeLiturgy.Data.Capital/User/User.cs | 25 ------- CodeLiturgy.Data.Capital/User/UserCreate.cs | 12 --- CodeLiturgy.Data.Capital/User/UserUnique.cs | 6 -- .../Capital/Country/Country.cs | 3 +- .../Capital/Country/CountryCreate.cs | 0 .../Capital/Country/CountryUnique.cs | 0 .../Capital/Country/CountryUpdate.cs | 0 .../CodeLiturgy.Data.Sites.csproj | 1 + .../EnvironmentType.cs | 0 .../Geo/Coordinate.cs | 0 .../Sites/Site.cs | 2 + .../Sites/SiteCreate.cs | 3 + .../Sites/SiteUnique.cs | 3 + CodeLiturgy.Domain/CodeLiturgy.Domain.csproj | 2 +- .../ApplicationUserDbContext.cs | 0 .../{ => DbContexts}/CountryDbContext.cs | 0 .../DbContexts/SiteDbContext.cs | 27 +++++++ .../Extensions/ModelBuilderExtensions.cs | 8 -- CodeLiturgy.Domain/SiteDbContext.cs | 26 ------- CodeLiturgy.Domain/UserDbContext.cs | 44 ----------- .../CodeLiturgy.SiteManager.csproj | 13 ---- CodeLiturgy.SiteManager/SiteManager.cs | 6 -- .../Controllers/AccountController.cs | 5 +- .../Controllers/AuthController.cs | 4 +- .../Controllers/EnvironmentsController.cs | 39 ---------- .../Controllers/SitesController.cs | 75 +++++++++++++++++++ .../Controllers/UserController.cs | 11 ++- CodeLiturgy.Views/Models/ErrorViewModel.cs | 2 +- CodeLiturgy.Views/StartupExtensions.cs | 1 - CodeLiturgy.Views/Views/Home/Login.cshtml | 2 +- 31 files changed, 127 insertions(+), 201 deletions(-) delete mode 100644 CodeLiturgy.Data.Capital/User/User.cs delete mode 100644 CodeLiturgy.Data.Capital/User/UserCreate.cs delete mode 100644 CodeLiturgy.Data.Capital/User/UserUnique.cs rename {CodeLiturgy.Data.Capital => CodeLiturgy.Data.Sites}/Capital/Country/Country.cs (90%) rename {CodeLiturgy.Data.Capital => CodeLiturgy.Data.Sites}/Capital/Country/CountryCreate.cs (100%) rename {CodeLiturgy.Data.Capital => CodeLiturgy.Data.Sites}/Capital/Country/CountryUnique.cs (100%) rename {CodeLiturgy.Data.Capital => CodeLiturgy.Data.Sites}/Capital/Country/CountryUpdate.cs (100%) rename CodeLiturgy.Data.Capital/CodeLiturgy.Data.Capital.csproj => CodeLiturgy.Data.Sites/CodeLiturgy.Data.Sites.csproj (91%) rename {CodeLiturgy.Data.Capital => CodeLiturgy.Data.Sites}/EnvironmentType.cs (100%) rename {CodeLiturgy.Data.Capital => CodeLiturgy.Data.Sites}/Geo/Coordinate.cs (100%) rename {CodeLiturgy.Data.Capital => CodeLiturgy.Data.Sites}/Sites/Site.cs (89%) rename {CodeLiturgy.Data.Capital => CodeLiturgy.Data.Sites}/Sites/SiteCreate.cs (80%) rename {CodeLiturgy.Data.Capital => CodeLiturgy.Data.Sites}/Sites/SiteUnique.cs (80%) rename CodeLiturgy.Domain/{ => DbContexts}/ApplicationUserDbContext.cs (100%) rename CodeLiturgy.Domain/{ => DbContexts}/CountryDbContext.cs (100%) create mode 100644 CodeLiturgy.Domain/DbContexts/SiteDbContext.cs delete mode 100644 CodeLiturgy.Domain/SiteDbContext.cs delete mode 100644 CodeLiturgy.Domain/UserDbContext.cs delete mode 100644 CodeLiturgy.SiteManager/CodeLiturgy.SiteManager.csproj delete mode 100644 CodeLiturgy.SiteManager/SiteManager.cs delete mode 100644 CodeLiturgy.Views/Controllers/EnvironmentsController.cs create mode 100644 CodeLiturgy.Views/Controllers/SitesController.cs diff --git a/CodeLiturgy.Dashboard.sln b/CodeLiturgy.Dashboard.sln index 37748ac..3e2ea66 100644 --- a/CodeLiturgy.Dashboard.sln +++ b/CodeLiturgy.Dashboard.sln @@ -5,7 +5,7 @@ VisualStudioVersion = 17.3.32819.101 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodeLiturgy", "CodeLiturgy\CodeLiturgy.csproj", "{293E7852-8AFD-4EFB-8C2D-F1BBE68AEE78}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodeLiturgy.Data.Capital", "CodeLiturgy.Data.Capital\CodeLiturgy.Data.Capital.csproj", "{E518C62D-768C-4885-9C9D-FD5761605B54}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodeLiturgy.Data.Sites", "CodeLiturgy.Data.Sites\CodeLiturgy.Data.Sites.csproj", "{E518C62D-768C-4885-9C9D-FD5761605B54}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlueWest.MapTo", "include\BlueWest.MapTo\src\BlueWest.MapTo\BlueWest.MapTo.csproj", "{72B37540-A12F-466E-A58F-7BA2B247CB74}" EndProject @@ -41,8 +41,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeLiturgy.Data.Auth", "Co EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sharp.Augeas", "include\Sharp.Augeas\Sharp.Augeas\Sharp.Augeas.csproj", "{F12DB2F1-2F2C-4A4C-BBAA-944AF5FB0E59}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeLiturgy.SiteManager", "CodeLiturgy.SiteManager\CodeLiturgy.SiteManager.csproj", "{CE32D9DC-DDC8-47FE-AFCA-71DF894D06DE}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sharp.Augeas.Test", "include\Sharp.Augeas\Sharp.Augeas.Test\Sharp.Augeas.Test.csproj", "{03308684-19AC-454B-8CD3-AB1C26B8BBBF}" EndProject Global @@ -99,10 +97,6 @@ Global {F12DB2F1-2F2C-4A4C-BBAA-944AF5FB0E59}.Debug|Any CPU.Build.0 = Debug|Any CPU {F12DB2F1-2F2C-4A4C-BBAA-944AF5FB0E59}.Release|Any CPU.ActiveCfg = Release|Any CPU {F12DB2F1-2F2C-4A4C-BBAA-944AF5FB0E59}.Release|Any CPU.Build.0 = Release|Any CPU - {CE32D9DC-DDC8-47FE-AFCA-71DF894D06DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {CE32D9DC-DDC8-47FE-AFCA-71DF894D06DE}.Debug|Any CPU.Build.0 = Debug|Any CPU - {CE32D9DC-DDC8-47FE-AFCA-71DF894D06DE}.Release|Any CPU.ActiveCfg = Release|Any CPU - {CE32D9DC-DDC8-47FE-AFCA-71DF894D06DE}.Release|Any CPU.Build.0 = Release|Any CPU {03308684-19AC-454B-8CD3-AB1C26B8BBBF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {03308684-19AC-454B-8CD3-AB1C26B8BBBF}.Debug|Any CPU.Build.0 = Debug|Any CPU {03308684-19AC-454B-8CD3-AB1C26B8BBBF}.Release|Any CPU.ActiveCfg = Release|Any CPU diff --git a/CodeLiturgy.Data.Capital/User/User.cs b/CodeLiturgy.Data.Capital/User/User.cs deleted file mode 100644 index a098542..0000000 --- a/CodeLiturgy.Data.Capital/User/User.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; -using MapTo; - -namespace BlueWest.Data -{ - [MapFrom( typeof(UserCreate))] - [UseUpdate] - public partial class User - { - public string ApplicationUserId { get; set; } - public int Id { get; set; } - public string Name { get; set; } - - [ForeignKey("CountryId")] public Country Country { get; set; } - - public User(int id, string name) - { - Id = id; - Name = name; - } - } -} \ No newline at end of file diff --git a/CodeLiturgy.Data.Capital/User/UserCreate.cs b/CodeLiturgy.Data.Capital/User/UserCreate.cs deleted file mode 100644 index 7a8c114..0000000 --- a/CodeLiturgy.Data.Capital/User/UserCreate.cs +++ /dev/null @@ -1,12 +0,0 @@ -using MapTo; - -namespace BlueWest.Data -{ - [MapFrom(typeof(User))] - - public partial class UserCreate - { - public string Name { get; set; } - - } -} \ No newline at end of file diff --git a/CodeLiturgy.Data.Capital/User/UserUnique.cs b/CodeLiturgy.Data.Capital/User/UserUnique.cs deleted file mode 100644 index 369d07f..0000000 --- a/CodeLiturgy.Data.Capital/User/UserUnique.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace BlueWest.Data; - -public class UserUnique -{ - -} \ No newline at end of file diff --git a/CodeLiturgy.Data.Capital/Capital/Country/Country.cs b/CodeLiturgy.Data.Sites/Capital/Country/Country.cs similarity index 90% rename from CodeLiturgy.Data.Capital/Capital/Country/Country.cs rename to CodeLiturgy.Data.Sites/Capital/Country/Country.cs index 05fbcc6..d940413 100644 --- a/CodeLiturgy.Data.Capital/Capital/Country/Country.cs +++ b/CodeLiturgy.Data.Sites/Capital/Country/Country.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Text.Json.Serialization; +using BlueWest.Data.Application.Users; using MapTo; namespace BlueWest.Data @@ -31,7 +32,7 @@ namespace BlueWest.Data [MaxLength(2)] public string Alpha2Code { get; set; } public string TLD { get; set; } - public List Users { get; set; } + public List Users { get; set; } public DateTime CreationDate { get; set; } diff --git a/CodeLiturgy.Data.Capital/Capital/Country/CountryCreate.cs b/CodeLiturgy.Data.Sites/Capital/Country/CountryCreate.cs similarity index 100% rename from CodeLiturgy.Data.Capital/Capital/Country/CountryCreate.cs rename to CodeLiturgy.Data.Sites/Capital/Country/CountryCreate.cs diff --git a/CodeLiturgy.Data.Capital/Capital/Country/CountryUnique.cs b/CodeLiturgy.Data.Sites/Capital/Country/CountryUnique.cs similarity index 100% rename from CodeLiturgy.Data.Capital/Capital/Country/CountryUnique.cs rename to CodeLiturgy.Data.Sites/Capital/Country/CountryUnique.cs diff --git a/CodeLiturgy.Data.Capital/Capital/Country/CountryUpdate.cs b/CodeLiturgy.Data.Sites/Capital/Country/CountryUpdate.cs similarity index 100% rename from CodeLiturgy.Data.Capital/Capital/Country/CountryUpdate.cs rename to CodeLiturgy.Data.Sites/Capital/Country/CountryUpdate.cs diff --git a/CodeLiturgy.Data.Capital/CodeLiturgy.Data.Capital.csproj b/CodeLiturgy.Data.Sites/CodeLiturgy.Data.Sites.csproj similarity index 91% rename from CodeLiturgy.Data.Capital/CodeLiturgy.Data.Capital.csproj rename to CodeLiturgy.Data.Sites/CodeLiturgy.Data.Sites.csproj index a7934c2..918581e 100644 --- a/CodeLiturgy.Data.Capital/CodeLiturgy.Data.Capital.csproj +++ b/CodeLiturgy.Data.Sites/CodeLiturgy.Data.Sites.csproj @@ -8,6 +8,7 @@ + diff --git a/CodeLiturgy.Data.Capital/EnvironmentType.cs b/CodeLiturgy.Data.Sites/EnvironmentType.cs similarity index 100% rename from CodeLiturgy.Data.Capital/EnvironmentType.cs rename to CodeLiturgy.Data.Sites/EnvironmentType.cs diff --git a/CodeLiturgy.Data.Capital/Geo/Coordinate.cs b/CodeLiturgy.Data.Sites/Geo/Coordinate.cs similarity index 100% rename from CodeLiturgy.Data.Capital/Geo/Coordinate.cs rename to CodeLiturgy.Data.Sites/Geo/Coordinate.cs diff --git a/CodeLiturgy.Data.Capital/Sites/Site.cs b/CodeLiturgy.Data.Sites/Sites/Site.cs similarity index 89% rename from CodeLiturgy.Data.Capital/Sites/Site.cs rename to CodeLiturgy.Data.Sites/Sites/Site.cs index 03236d9..b03d2de 100644 --- a/CodeLiturgy.Data.Capital/Sites/Site.cs +++ b/CodeLiturgy.Data.Sites/Sites/Site.cs @@ -20,6 +20,8 @@ namespace CodeLiturgy.Data.Capital public string Domain { get; set; } public string Ip { get; set; } + + public EnvironmentType EnvironmentType { get; set; } public DateTime CreatedDate { get; set; } diff --git a/CodeLiturgy.Data.Capital/Sites/SiteCreate.cs b/CodeLiturgy.Data.Sites/Sites/SiteCreate.cs similarity index 80% rename from CodeLiturgy.Data.Capital/Sites/SiteCreate.cs rename to CodeLiturgy.Data.Sites/Sites/SiteCreate.cs index a4ba989..12faa2a 100644 --- a/CodeLiturgy.Data.Capital/Sites/SiteCreate.cs +++ b/CodeLiturgy.Data.Sites/Sites/SiteCreate.cs @@ -12,5 +12,8 @@ namespace CodeLiturgy.Data.Capital public string Domain { get; set; } public string Ip { get; set; } + + public EnvironmentType EnvironmentType { get; set; } + } } diff --git a/CodeLiturgy.Data.Capital/Sites/SiteUnique.cs b/CodeLiturgy.Data.Sites/Sites/SiteUnique.cs similarity index 80% rename from CodeLiturgy.Data.Capital/Sites/SiteUnique.cs rename to CodeLiturgy.Data.Sites/Sites/SiteUnique.cs index 9a9f147..4618e06 100644 --- a/CodeLiturgy.Data.Capital/Sites/SiteUnique.cs +++ b/CodeLiturgy.Data.Sites/Sites/SiteUnique.cs @@ -14,6 +14,9 @@ namespace CodeLiturgy.Data.Capital public string Domain { get; set; } public string Ip { get; set; } + + public EnvironmentType EnvironmentType { get; set; } + } } diff --git a/CodeLiturgy.Domain/CodeLiturgy.Domain.csproj b/CodeLiturgy.Domain/CodeLiturgy.Domain.csproj index 603775b..abdcfe1 100644 --- a/CodeLiturgy.Domain/CodeLiturgy.Domain.csproj +++ b/CodeLiturgy.Domain/CodeLiturgy.Domain.csproj @@ -33,7 +33,7 @@ - + diff --git a/CodeLiturgy.Domain/ApplicationUserDbContext.cs b/CodeLiturgy.Domain/DbContexts/ApplicationUserDbContext.cs similarity index 100% rename from CodeLiturgy.Domain/ApplicationUserDbContext.cs rename to CodeLiturgy.Domain/DbContexts/ApplicationUserDbContext.cs diff --git a/CodeLiturgy.Domain/CountryDbContext.cs b/CodeLiturgy.Domain/DbContexts/CountryDbContext.cs similarity index 100% rename from CodeLiturgy.Domain/CountryDbContext.cs rename to CodeLiturgy.Domain/DbContexts/CountryDbContext.cs diff --git a/CodeLiturgy.Domain/DbContexts/SiteDbContext.cs b/CodeLiturgy.Domain/DbContexts/SiteDbContext.cs new file mode 100644 index 0000000..dd72c6c --- /dev/null +++ b/CodeLiturgy.Domain/DbContexts/SiteDbContext.cs @@ -0,0 +1,27 @@ +using CodeLiturgy.Data.Capital; +using Microsoft.EntityFrameworkCore; +using BlueWest.EfMethods; + +namespace CodeLiturgy.Domain +{ + [EfGenerator] + public class SiteDbContext: DbContext + { + [EfGetOneBy(nameof(Site.Id), typeof(SiteUnique))] + [EfGetOne(typeof(SiteUnique))] + [EfAddMethods(createType: typeof(SiteCreate), returnType: typeof(SiteUnique))] + [EfGetMany(typeof(SiteUnique))] + + public DbSet Sites { get; set; } + + /// + /// SiteDbContext Constructor. + /// + /// + public SiteDbContext(DbContextOptions options) : base(options) + { + Database.EnsureCreated(); + } + } +} + diff --git a/CodeLiturgy.Domain/Extensions/ModelBuilderExtensions.cs b/CodeLiturgy.Domain/Extensions/ModelBuilderExtensions.cs index c53169d..503a3db 100644 --- a/CodeLiturgy.Domain/Extensions/ModelBuilderExtensions.cs +++ b/CodeLiturgy.Domain/Extensions/ModelBuilderExtensions.cs @@ -98,20 +98,12 @@ namespace CodeLiturgy.Domain.Model private static ModelBuilder ConfigureDatabaseKeys(this ModelBuilder modelBuilder) { - // User - modelBuilder - .Entity(builder => builder - .HasKey(user => user.Id)) - .Entity().Property(user => user.Id) - .ValueGeneratedOnAdd(); - // Country modelBuilder .Entity(builder => builder .HasKey(country => country.Id)) .Entity().Property(country => country.Id) .ValueGeneratedOnAdd(); - // FinanceOp return diff --git a/CodeLiturgy.Domain/SiteDbContext.cs b/CodeLiturgy.Domain/SiteDbContext.cs deleted file mode 100644 index 928216f..0000000 --- a/CodeLiturgy.Domain/SiteDbContext.cs +++ /dev/null @@ -1,26 +0,0 @@ -using CodeLiturgy.Data.Capital; -using Microsoft.EntityFrameworkCore; -using BlueWest.EfMethods; - -namespace CodeLiturgy.Domain; - -public class SiteDbContext: DbContext -{ - [EfGetOneBy(nameof(Site.Id), typeof(SiteUnique))] - [EfGetOne(typeof(SiteUnique))] - [EfAddMethods(createType: typeof(SiteCreate), returnType: typeof(SiteUnique))] - [EfGetMany(typeof(SiteUnique))] - - public DbSet Sites { get; set; } - - /// - /// SiteDbContext Constructor. - /// - /// - public SiteDbContext(DbContextOptions options) : base(options) - { - Database.EnsureCreated(); - } - - -} \ No newline at end of file diff --git a/CodeLiturgy.Domain/UserDbContext.cs b/CodeLiturgy.Domain/UserDbContext.cs deleted file mode 100644 index ead635e..0000000 --- a/CodeLiturgy.Domain/UserDbContext.cs +++ /dev/null @@ -1,44 +0,0 @@ -using BlueWest.Data; -using CodeLiturgy.Domain.Model; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Configuration; - -namespace CodeLiturgy.Domain -{ - - /// - /// Database context for app users - /// - public sealed class UserDbContext : DbContext - { - /// - /// Users entity. - /// - public DbSet Users { get; set; } - - - /// - /// App configuration. - /// - public IConfiguration Configuration; - - /// - /// Database for the context of database users - /// - /// - public UserDbContext(DbContextOptions options) : base(options) - { - Database.EnsureCreated(); - } - - /// - /// On model creating. - /// - /// Builder model of the database - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - base.OnModelCreating(modelBuilder); - modelBuilder.ConfigureCurrentDbModel(); - } - } -} diff --git a/CodeLiturgy.SiteManager/CodeLiturgy.SiteManager.csproj b/CodeLiturgy.SiteManager/CodeLiturgy.SiteManager.csproj deleted file mode 100644 index 593aa7d..0000000 --- a/CodeLiturgy.SiteManager/CodeLiturgy.SiteManager.csproj +++ /dev/null @@ -1,13 +0,0 @@ - - - - net7.0 - enable - enable - - - - - - - diff --git a/CodeLiturgy.SiteManager/SiteManager.cs b/CodeLiturgy.SiteManager/SiteManager.cs deleted file mode 100644 index 0f9f4d8..0000000 --- a/CodeLiturgy.SiteManager/SiteManager.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace CodeLiturgy.SiteManager; - -public class SiteManager -{ - -} \ No newline at end of file diff --git a/CodeLiturgy.Views/Controllers/AccountController.cs b/CodeLiturgy.Views/Controllers/AccountController.cs index 040141e..fb2c60a 100644 --- a/CodeLiturgy.Views/Controllers/AccountController.cs +++ b/CodeLiturgy.Views/Controllers/AccountController.cs @@ -4,14 +4,11 @@ using BlueWest.Data.Auth.Context.Users; using Microsoft.AspNetCore.Mvc; using CodeLiturgy.Views.Models; using CodeLiturgy.Views.Utils; -using Controller = Microsoft.AspNetCore.Mvc.Controller; namespace CodeLiturgy.Views.Controllers; public class AccountController : UserController { - private readonly ILogger _logger; - public AccountController(ApplicationUserManager userManager, ILogger logger) : base(userManager, logger) { _userManager = userManager; @@ -37,7 +34,7 @@ public class AccountController : UserController [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] - public async Task Error() + public IActionResult Error() { return View(new ErrorViewModel {RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier}); } diff --git a/CodeLiturgy.Views/Controllers/AuthController.cs b/CodeLiturgy.Views/Controllers/AuthController.cs index 42abf89..9ea291b 100644 --- a/CodeLiturgy.Views/Controllers/AuthController.cs +++ b/CodeLiturgy.Views/Controllers/AuthController.cs @@ -21,9 +21,9 @@ namespace CodeLiturgy.Views.Controllers _authManager = authManager; } - public IActionResult Index() + public async Task Index() { - OnEveryAction(); + await OnEveryAction(); return View(); } diff --git a/CodeLiturgy.Views/Controllers/EnvironmentsController.cs b/CodeLiturgy.Views/Controllers/EnvironmentsController.cs deleted file mode 100644 index 191aeba..0000000 --- a/CodeLiturgy.Views/Controllers/EnvironmentsController.cs +++ /dev/null @@ -1,39 +0,0 @@ -using BlueWest.Data.Auth.Context.Users; -using CodeLiturgy.Data.Capital; -using CodeLiturgy.Domain; -using CodeLiturgy.Views.Utils; -using Microsoft.AspNetCore.Authentication.Cookies; -using Microsoft.AspNetCore.Authentication.JwtBearer; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Cors; -using Microsoft.AspNetCore.Mvc; - - -namespace CodeLiturgy.Views.Controllers -{ - [Route("api/envs")] - [ApiController] - [Authorize] - - public class EnvironmentsController : ControllerBase - { - private ApplicationUserManager _userManager; - private ILogger _logger; - private SiteDbContext _siteDbContext; - - public EnvironmentsController(ApplicationUserManager userManager, ILogger logger, SiteDbContext siteDbContext) - { - _logger = logger; - _userManager = userManager; - _siteDbContext = siteDbContext; - } - - [HttpGet] - public async Task> GetSites() - { - return Ok(new Site()); - } - - } -} - diff --git a/CodeLiturgy.Views/Controllers/SitesController.cs b/CodeLiturgy.Views/Controllers/SitesController.cs new file mode 100644 index 0000000..fc38b05 --- /dev/null +++ b/CodeLiturgy.Views/Controllers/SitesController.cs @@ -0,0 +1,75 @@ +using BlueWest.Data.Auth.Context.Users; +using CodeLiturgy.Data.Capital; +using CodeLiturgy.Domain; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + + +namespace CodeLiturgy.Views.Controllers +{ + [Route("api/sites")] + [ApiController] + [Authorize] + + public class SitesController : ControllerBase + { + private ApplicationUserManager _userManager; + private ILogger _logger; + private readonly SiteDbContext _siteDbContext; + + public SitesController(ApplicationUserManager userManager, ILogger logger, SiteDbContext siteDbContext) + { + _logger = logger; + _userManager = userManager; + _siteDbContext = siteDbContext; + } + + [HttpGet] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + + public ActionResult GetSites( + + int skip = 0, int take = 50, int orderDir = 1) + { + var (success, sites) = _siteDbContext.GetSites(skip, take, orderDir); + + if (!success) return new NotFoundResult(); + + return Ok(sites); + } + + /// + /// Get Country by Id + /// + /// ISO 3166-1 countryId numeric code + /// + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [HttpGet("{siteId}", Name = nameof(GetSiteById))] + public ActionResult GetSiteById(string siteId) + { + var (success, site) = _siteDbContext.GetOneSiteById(siteId); + + if (success) + { + return Ok(site); + } + + return new NotFoundResult(); + } + + [ProducesResponseType(StatusCodes.Status201Created)] + [ProducesResponseType(StatusCodes.Status406NotAcceptable)] + [HttpPost] + public ActionResult AddSite(SiteCreate siteToCreate) + { + var (success, site) = _siteDbContext.AddSite(siteToCreate); + if (!success) return new BadRequestResult(); + return CreatedAtRoute(nameof(GetSiteById), new {countryId = site.Id}, site); + } + + } +} + diff --git a/CodeLiturgy.Views/Controllers/UserController.cs b/CodeLiturgy.Views/Controllers/UserController.cs index a5d64e4..30a7033 100644 --- a/CodeLiturgy.Views/Controllers/UserController.cs +++ b/CodeLiturgy.Views/Controllers/UserController.cs @@ -73,15 +73,18 @@ public class UserController : Controller { if (User.IsAuthenticated()) { - ApplicationUser? user = await _userManager.GetUserAsync(User); - return new ApplicationUserUnique(user); + ApplicationUser user = await _userManager.GetUserAsync(User); + if (user != null) + { + return new ApplicationUserUnique(user); + } } return null; } - public IpInformation ExtractIpInformation(IPAddress? ipAddress) + public IpInformation ExtractIpInformation(IPAddress ipAddress) { string ipAddressString = ""; IpType ipType = IpType.Unknown; @@ -117,7 +120,7 @@ public class UserController : Controller } ViewData[LanguageViewStorage] = currentCultureName; - IPAddress? remoteIpAddress = Request.HttpContext.Connection.RemoteIpAddress; + IPAddress remoteIpAddress = Request.HttpContext.Connection.RemoteIpAddress; IpInformation ipInformation = ExtractIpInformation(Request.HttpContext.Connection.RemoteIpAddress); diff --git a/CodeLiturgy.Views/Models/ErrorViewModel.cs b/CodeLiturgy.Views/Models/ErrorViewModel.cs index 80ccb04..e554846 100644 --- a/CodeLiturgy.Views/Models/ErrorViewModel.cs +++ b/CodeLiturgy.Views/Models/ErrorViewModel.cs @@ -2,7 +2,7 @@ namespace CodeLiturgy.Views.Models; public class ErrorViewModel { - public string? RequestId { get; set; } + public string RequestId { get; set; } public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); } \ No newline at end of file diff --git a/CodeLiturgy.Views/StartupExtensions.cs b/CodeLiturgy.Views/StartupExtensions.cs index 1195e3f..8ac66da 100644 --- a/CodeLiturgy.Views/StartupExtensions.cs +++ b/CodeLiturgy.Views/StartupExtensions.cs @@ -189,7 +189,6 @@ public static class StartupExtensions IConfiguration configuration, IWebHostEnvironment environment) { return serviceCollection - .AddDbContextPool(options => options.UsePsqlConfiguration(configuration)) .AddDbContextPool(options => options.UsePsqlConfiguration(configuration)) .AddDbContextPool(options => options.UsePsqlConfiguration(configuration)) .AddDbContextPool(options => diff --git a/CodeLiturgy.Views/Views/Home/Login.cshtml b/CodeLiturgy.Views/Views/Home/Login.cshtml index 464bb95..9c64eec 100644 --- a/CodeLiturgy.Views/Views/Home/Login.cshtml +++ b/CodeLiturgy.Views/Views/Home/Login.cshtml @@ -8,4 +8,4 @@ -@Html.Partial("_FooterMenu"); \ No newline at end of file +@await Html.PartialAsync("_FooterMenu"); \ No newline at end of file