refactor: move Session entity to Web area

This commit is contained in:
Oliver Booth 2024-02-24 15:27:03 +00:00
parent d0142ec5cf
commit fa394480b1
Signed by: oliverbooth
GPG Key ID: E60B570D1B7557B5
9 changed files with 51 additions and 46 deletions

View File

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

View File

@ -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
/// <value>The collection of blog posts.</value>
public DbSet<BlogPost> BlogPosts { get; private set; } = null!;
/// <summary>
/// Gets the collection of sessions in the database.
/// </summary>
/// <value>The collection of sessions.</value>
public DbSet<Session> Sessions { get; private set; } = null!;
/// <inheritdoc />
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());
}
}

View File

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

View File

@ -1,6 +1,6 @@
using System.Net;
namespace OliverBooth.Data.Blog;
namespace OliverBooth.Data.Web;
/// <summary>
/// Represents a login session.
@ -55,30 +55,3 @@ public interface ISession
/// <value>The user ID.</value>
Guid UserId { get; }
}
internal sealed class Session : ISession
{
/// <inheritdoc />
public DateTimeOffset Created { get; set; }
/// <inheritdoc />
public DateTimeOffset Expires { get; set; }
/// <inheritdoc />
public Guid Id { get; private set; } = Guid.NewGuid();
/// <inheritdoc />
public IPAddress IpAddress { get; set; } = IPAddress.None;
/// <inheritdoc />
public DateTimeOffset LastAccessed { get; set; }
/// <inheritdoc />
public bool RequiresTotp { get; set; }
/// <inheritdoc />
public DateTimeOffset Updated { get; set; }
/// <inheritdoc />
public Guid UserId { get; set; }
}

View File

@ -0,0 +1,30 @@
using System.Net;
namespace OliverBooth.Data.Web;
internal sealed class Session : ISession
{
/// <inheritdoc />
public DateTimeOffset Created { get; set; }
/// <inheritdoc />
public DateTimeOffset Expires { get; set; }
/// <inheritdoc />
public Guid Id { get; private set; } = Guid.NewGuid();
/// <inheritdoc />
public IPAddress IpAddress { get; set; } = IPAddress.None;
/// <inheritdoc />
public DateTimeOffset LastAccessed { get; set; }
/// <inheritdoc />
public bool RequiresTotp { get; set; }
/// <inheritdoc />
public DateTimeOffset Updated { get; set; }
/// <inheritdoc />
public Guid UserId { get; set; }
}

View File

@ -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
/// <value>The collection of projects.</value>
public DbSet<Project> Projects { get; private set; } = null!;
/// <summary>
/// Gets the collection of sessions in the database.
/// </summary>
/// <value>The collection of sessions.</value>
public DbSet<Session> Sessions { get; private set; } = null!;
/// <summary>
/// Gets the set of site configuration items.
/// </summary>
@ -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());
}

View File

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

View File

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

View File

@ -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<SessionService> _logger;
private readonly IUserService _userService;
private readonly IDbContextFactory<BlogContext> _blogContextFactory;
private readonly IDbContextFactory<WebContext> _webContextFactory;
/// <summary>
/// Initializes a new instance of the <see cref="SessionService" /> class.
@ -29,7 +29,7 @@ internal sealed class SessionService : ISessionService
{
_logger = logger;
_userService = userService;
_blogContextFactory = blogContextFactory;
_webContextFactory = webContextFactory;
}
/// <inheritdoc />
@ -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
/// <inheritdoc />
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
/// <inheritdoc />
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;
}