diff --git a/OliverBooth/Controllers/AdminController.cs b/OliverBooth/Controllers/AdminController.cs index 84d4f24..3040604 100644 --- a/OliverBooth/Controllers/AdminController.cs +++ b/OliverBooth/Controllers/AdminController.cs @@ -2,7 +2,7 @@ using System.Net; using Microsoft.AspNetCore.Mvc; using OliverBooth.Data.Web; using OliverBooth.Services; -using ISession = OliverBooth.Data.Blog.ISession; +using ISession = OliverBooth.Data.Web.ISession; namespace OliverBooth.Controllers; diff --git a/OliverBooth/Data/Blog/BlogContext.cs b/OliverBooth/Data/Blog/BlogContext.cs index 2d4b937..c20282b 100644 --- a/OliverBooth/Data/Blog/BlogContext.cs +++ b/OliverBooth/Data/Blog/BlogContext.cs @@ -1,5 +1,6 @@ using Microsoft.EntityFrameworkCore; using OliverBooth.Data.Blog.Configuration; +using OliverBooth.Data.Web; namespace OliverBooth.Data.Blog; @@ -25,12 +26,6 @@ internal sealed class BlogContext : DbContext /// The collection of blog posts. public DbSet BlogPosts { get; private set; } = null!; - /// - /// Gets the collection of sessions in the database. - /// - /// The collection of sessions. - public DbSet Sessions { get; private set; } = null!; - /// protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { @@ -43,6 +38,5 @@ internal sealed class BlogContext : DbContext protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.ApplyConfiguration(new BlogPostConfiguration()); - modelBuilder.ApplyConfiguration(new SessionConfiguration()); } } diff --git a/OliverBooth/Data/Blog/Configuration/SessionConfiguration.cs b/OliverBooth/Data/Web/Configuration/SessionConfiguration.cs similarity index 94% rename from OliverBooth/Data/Blog/Configuration/SessionConfiguration.cs rename to OliverBooth/Data/Web/Configuration/SessionConfiguration.cs index cfb5079..2b47a27 100644 --- a/OliverBooth/Data/Blog/Configuration/SessionConfiguration.cs +++ b/OliverBooth/Data/Web/Configuration/SessionConfiguration.cs @@ -2,7 +2,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -namespace OliverBooth.Data.Blog.Configuration; +namespace OliverBooth.Data.Web.Configuration; internal sealed class SessionConfiguration : IEntityTypeConfiguration { diff --git a/OliverBooth/Data/Blog/ISession.cs b/OliverBooth/Data/Web/ISession.cs similarity index 70% rename from OliverBooth/Data/Blog/ISession.cs rename to OliverBooth/Data/Web/ISession.cs index f54164f..88f7b9c 100644 --- a/OliverBooth/Data/Blog/ISession.cs +++ b/OliverBooth/Data/Web/ISession.cs @@ -1,6 +1,6 @@ using System.Net; -namespace OliverBooth.Data.Blog; +namespace OliverBooth.Data.Web; /// /// Represents a login session. @@ -54,31 +54,4 @@ public interface ISession /// /// The user ID. Guid UserId { get; } -} - -internal sealed class Session : ISession -{ - /// - public DateTimeOffset Created { get; set; } - - /// - public DateTimeOffset Expires { get; set; } - - /// - public Guid Id { get; private set; } = Guid.NewGuid(); - - /// - public IPAddress IpAddress { get; set; } = IPAddress.None; - - /// - public DateTimeOffset LastAccessed { get; set; } - - /// - public bool RequiresTotp { get; set; } - - /// - public DateTimeOffset Updated { get; set; } - - /// - public Guid UserId { get; set; } -} +} \ No newline at end of file diff --git a/OliverBooth/Data/Web/Session.cs b/OliverBooth/Data/Web/Session.cs new file mode 100644 index 0000000..8d42b3c --- /dev/null +++ b/OliverBooth/Data/Web/Session.cs @@ -0,0 +1,30 @@ +using System.Net; + +namespace OliverBooth.Data.Web; + +internal sealed class Session : ISession +{ + /// + public DateTimeOffset Created { get; set; } + + /// + public DateTimeOffset Expires { get; set; } + + /// + public Guid Id { get; private set; } = Guid.NewGuid(); + + /// + public IPAddress IpAddress { get; set; } = IPAddress.None; + + /// + public DateTimeOffset LastAccessed { get; set; } + + /// + public bool RequiresTotp { get; set; } + + /// + public DateTimeOffset Updated { get; set; } + + /// + public Guid UserId { get; set; } +} diff --git a/OliverBooth/Data/Web/WebContext.cs b/OliverBooth/Data/Web/WebContext.cs index 4d54ea3..c445a3b 100644 --- a/OliverBooth/Data/Web/WebContext.cs +++ b/OliverBooth/Data/Web/WebContext.cs @@ -1,4 +1,5 @@ using Microsoft.EntityFrameworkCore; +using OliverBooth.Data.Blog.Configuration; using OliverBooth.Data.Web.Configuration; namespace OliverBooth.Data.Web; @@ -43,6 +44,12 @@ internal sealed class WebContext : DbContext /// The collection of projects. public DbSet Projects { get; private set; } = null!; + /// + /// Gets the collection of sessions in the database. + /// + /// The collection of sessions. + public DbSet Sessions { get; private set; } = null!; + /// /// Gets the set of site configuration items. /// @@ -77,6 +84,7 @@ internal sealed class WebContext : DbContext modelBuilder.ApplyConfiguration(new ProgrammingLanguageConfiguration()); modelBuilder.ApplyConfiguration(new ProjectConfiguration()); modelBuilder.ApplyConfiguration(new TemplateConfiguration()); + modelBuilder.ApplyConfiguration(new SessionConfiguration()); modelBuilder.ApplyConfiguration(new SiteConfigurationConfiguration()); modelBuilder.ApplyConfiguration(new UserConfiguration()); } diff --git a/OliverBooth/Pages/Admin/Index.cshtml.cs b/OliverBooth/Pages/Admin/Index.cshtml.cs index 64ac6d1..f3af901 100644 --- a/OliverBooth/Pages/Admin/Index.cshtml.cs +++ b/OliverBooth/Pages/Admin/Index.cshtml.cs @@ -2,7 +2,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using OliverBooth.Data.Web; using OliverBooth.Services; -using ISession = OliverBooth.Data.Blog.ISession; +using ISession = OliverBooth.Data.Web.ISession; namespace OliverBooth.Pages.Admin; diff --git a/OliverBooth/Services/ISessionService.cs b/OliverBooth/Services/ISessionService.cs index 166002e..becaf5a 100644 --- a/OliverBooth/Services/ISessionService.cs +++ b/OliverBooth/Services/ISessionService.cs @@ -1,7 +1,7 @@ using System.Diagnostics.CodeAnalysis; using Microsoft.AspNetCore.Mvc; using OliverBooth.Data.Web; -using ISession = OliverBooth.Data.Blog.ISession; +using ISession = OliverBooth.Data.Web.ISession; namespace OliverBooth.Services; diff --git a/OliverBooth/Services/SessionService.cs b/OliverBooth/Services/SessionService.cs index faa1290..e33b9aa 100644 --- a/OliverBooth/Services/SessionService.cs +++ b/OliverBooth/Services/SessionService.cs @@ -5,7 +5,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using OliverBooth.Data.Blog; using OliverBooth.Data.Web; -using ISession = OliverBooth.Data.Blog.ISession; +using ISession = OliverBooth.Data.Web.ISession; namespace OliverBooth.Services; @@ -13,7 +13,7 @@ internal sealed class SessionService : ISessionService { private readonly ILogger _logger; private readonly IUserService _userService; - private readonly IDbContextFactory _blogContextFactory; + private readonly IDbContextFactory _webContextFactory; /// /// Initializes a new instance of the class. @@ -29,7 +29,7 @@ internal sealed class SessionService : ISessionService { _logger = logger; _userService = userService; - _blogContextFactory = blogContextFactory; + _webContextFactory = webContextFactory; } /// @@ -38,7 +38,7 @@ internal sealed class SessionService : ISessionService if (request is null) throw new ArgumentNullException(nameof(request)); if (user is null) throw new ArgumentNullException(nameof(user)); - using BlogContext context = _blogContextFactory.CreateDbContext(); + using WebContext context = _webContextFactory.CreateDbContext(); var now = DateTimeOffset.UtcNow; var session = new Session { @@ -58,7 +58,7 @@ internal sealed class SessionService : ISessionService /// public void DeleteSession(ISession session) { - using BlogContext context = _blogContextFactory.CreateDbContext(); + using WebContext context = _webContextFactory.CreateDbContext(); context.Sessions.Remove((Session)session); context.SaveChanges(); } @@ -66,7 +66,7 @@ internal sealed class SessionService : ISessionService /// public bool TryGetSession(Guid sessionId, [NotNullWhen(true)] out ISession? session) { - using BlogContext context = _blogContextFactory.CreateDbContext(); + using WebContext context = _webContextFactory.CreateDbContext(); session = context.Sessions.FirstOrDefault(s => s.Id == sessionId); return session is not null; }