refactor: rename BlogUserService to UserService

This commit is contained in:
Oliver Booth 2024-02-24 14:52:43 +00:00
parent bd55ac28e3
commit 8ef34d014b
Signed by: oliverbooth
GPG Key ID: E60B570D1B7557B5
13 changed files with 37 additions and 33 deletions

View File

@ -1,6 +1,7 @@
using Humanizer; using Humanizer;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using OliverBooth.Data.Blog; using OliverBooth.Data.Blog;
using OliverBooth.Data.Web;
using OliverBooth.Services; using OliverBooth.Services;
namespace OliverBooth.Controllers.Blog; namespace OliverBooth.Controllers.Blog;
@ -14,14 +15,14 @@ namespace OliverBooth.Controllers.Blog;
public sealed class BlogApiController : ControllerBase public sealed class BlogApiController : ControllerBase
{ {
private readonly IBlogPostService _blogPostService; private readonly IBlogPostService _blogPostService;
private readonly IBlogUserService _userService; private readonly IUserService _userService;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="BlogApiController" /> class. /// Initializes a new instance of the <see cref="BlogApiController" /> class.
/// </summary> /// </summary>
/// <param name="blogPostService">The <see cref="IBlogPostService" />.</param> /// <param name="blogPostService">The <see cref="IBlogPostService" />.</param>
/// <param name="userService">The <see cref="IBlogUserService" />.</param> /// <param name="userService">The <see cref="IUserService" />.</param>
public BlogApiController(IBlogPostService blogPostService, IBlogUserService userService) public BlogApiController(IBlogPostService blogPostService, IUserService userService)
{ {
_blogPostService = blogPostService; _blogPostService = blogPostService;
_userService = userService; _userService = userService;

View File

@ -31,12 +31,6 @@ internal sealed class BlogContext : DbContext
/// <value>The collection of sessions.</value> /// <value>The collection of sessions.</value>
public DbSet<Session> Sessions { get; private set; } = null!; public DbSet<Session> Sessions { get; private set; } = null!;
/// <summary>
/// Gets the collection of users in the database.
/// </summary>
/// <value>The collection of users.</value>
public DbSet<User> Users { get; private set; } = null!;
/// <inheritdoc /> /// <inheritdoc />
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{ {
@ -50,6 +44,5 @@ internal sealed class BlogContext : DbContext
{ {
modelBuilder.ApplyConfiguration(new BlogPostConfiguration()); modelBuilder.ApplyConfiguration(new BlogPostConfiguration());
modelBuilder.ApplyConfiguration(new SessionConfiguration()); modelBuilder.ApplyConfiguration(new SessionConfiguration());
modelBuilder.ApplyConfiguration(new UserConfiguration());
} }
} }

View File

@ -1,5 +1,6 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Metadata.Builders;
using OliverBooth.Data.Web;
namespace OliverBooth.Data.Blog.Configuration; namespace OliverBooth.Data.Blog.Configuration;

View File

@ -1,4 +1,4 @@
namespace OliverBooth.Data.Blog; namespace OliverBooth.Data.Web;
/// <summary> /// <summary>
/// Represents a user which can log in to the blog. /// Represents a user which can log in to the blog.

View File

@ -2,8 +2,9 @@ using System.ComponentModel.DataAnnotations.Schema;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using Cysharp.Text; using Cysharp.Text;
using OliverBooth.Data.Blog;
namespace OliverBooth.Data.Blog; namespace OliverBooth.Data.Web;
/// <summary> /// <summary>
/// Represents a user. /// Represents a user.

View File

@ -55,6 +55,12 @@ internal sealed class WebContext : DbContext
/// <value>The collection of templates.</value> /// <value>The collection of templates.</value>
public DbSet<Template> Templates { get; private set; } = null!; public DbSet<Template> Templates { get; private set; } = null!;
/// <summary>
/// Gets the collection of users in the database.
/// </summary>
/// <value>The collection of users.</value>
public DbSet<User> Users { get; private set; } = null!;
/// <inheritdoc /> /// <inheritdoc />
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{ {
@ -72,5 +78,6 @@ internal sealed class WebContext : DbContext
modelBuilder.ApplyConfiguration(new ProjectConfiguration()); modelBuilder.ApplyConfiguration(new ProjectConfiguration());
modelBuilder.ApplyConfiguration(new TemplateConfiguration()); modelBuilder.ApplyConfiguration(new TemplateConfiguration());
modelBuilder.ApplyConfiguration(new SiteConfigurationConfiguration()); modelBuilder.ApplyConfiguration(new SiteConfigurationConfiguration());
modelBuilder.ApplyConfiguration(new UserConfiguration());
} }
} }

View File

@ -1,7 +1,7 @@
using System.Net; using System.Net;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.RazorPages;
using OliverBooth.Data.Blog; using OliverBooth.Data.Web;
using OliverBooth.Services; using OliverBooth.Services;
using ISession = OliverBooth.Data.Blog.ISession; using ISession = OliverBooth.Data.Blog.ISession;

View File

@ -36,8 +36,8 @@ builder.Services.AddHttpClient();
builder.Services.AddSingleton<IContactService, ContactService>(); builder.Services.AddSingleton<IContactService, ContactService>();
builder.Services.AddSingleton<ITemplateService, TemplateService>(); builder.Services.AddSingleton<ITemplateService, TemplateService>();
builder.Services.AddSingleton<IBlogPostService, BlogPostService>(); builder.Services.AddSingleton<IBlogPostService, BlogPostService>();
builder.Services.AddSingleton<IBlogUserService, BlogUserService>();
builder.Services.AddSingleton<ISessionService, SessionService>(); builder.Services.AddSingleton<ISessionService, SessionService>();
builder.Services.AddSingleton<IUserService, UserService>();
builder.Services.AddSingleton<IProjectService, ProjectService>(); builder.Services.AddSingleton<IProjectService, ProjectService>();
builder.Services.AddSingleton<IMastodonService, MastodonService>(); builder.Services.AddSingleton<IMastodonService, MastodonService>();
builder.Services.AddSingleton<IReadingListService, ReadingListService>(); builder.Services.AddSingleton<IReadingListService, ReadingListService>();

View File

@ -3,6 +3,7 @@ using Humanizer;
using Markdig; using Markdig;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using OliverBooth.Data.Blog; using OliverBooth.Data.Blog;
using OliverBooth.Data.Web;
namespace OliverBooth.Services; namespace OliverBooth.Services;
@ -12,7 +13,7 @@ namespace OliverBooth.Services;
internal sealed class BlogPostService : IBlogPostService internal sealed class BlogPostService : IBlogPostService
{ {
private readonly IDbContextFactory<BlogContext> _dbContextFactory; private readonly IDbContextFactory<BlogContext> _dbContextFactory;
private readonly IBlogUserService _blogUserService; private readonly IUserService _userService;
private readonly MarkdownPipeline _markdownPipeline; private readonly MarkdownPipeline _markdownPipeline;
/// <summary> /// <summary>
@ -21,14 +22,14 @@ internal sealed class BlogPostService : IBlogPostService
/// <param name="dbContextFactory"> /// <param name="dbContextFactory">
/// The <see cref="IDbContextFactory{TContext}" /> used to create a <see cref="BlogContext" />. /// The <see cref="IDbContextFactory{TContext}" /> used to create a <see cref="BlogContext" />.
/// </param> /// </param>
/// <param name="blogUserService">The <see cref="IBlogUserService" />.</param> /// <param name="userService">The <see cref="IUserService" />.</param>
/// <param name="markdownPipeline">The <see cref="MarkdownPipeline" />.</param> /// <param name="markdownPipeline">The <see cref="MarkdownPipeline" />.</param>
public BlogPostService(IDbContextFactory<BlogContext> dbContextFactory, public BlogPostService(IDbContextFactory<BlogContext> dbContextFactory,
IBlogUserService blogUserService, IUserService userService,
MarkdownPipeline markdownPipeline) MarkdownPipeline markdownPipeline)
{ {
_dbContextFactory = dbContextFactory; _dbContextFactory = dbContextFactory;
_blogUserService = blogUserService; _userService = userService;
_markdownPipeline = markdownPipeline; _markdownPipeline = markdownPipeline;
} }
@ -163,7 +164,7 @@ internal sealed class BlogPostService : IBlogPostService
return post; return post;
} }
if (_blogUserService.TryGetUser(post.AuthorId, out IUser? user) && user is IBlogAuthor author) if (_userService.TryGetUser(post.AuthorId, out IUser? user) && user is IBlogAuthor author)
{ {
post.Author = author; post.Author = author;
} }

View File

@ -1,5 +1,5 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using OliverBooth.Data.Blog; using OliverBooth.Data.Web;
using ISession = OliverBooth.Data.Blog.ISession; using ISession = OliverBooth.Data.Blog.ISession;
namespace OliverBooth.Services; namespace OliverBooth.Services;

View File

@ -1,12 +1,12 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using OliverBooth.Data.Blog; using OliverBooth.Data.Web;
namespace OliverBooth.Services; namespace OliverBooth.Services;
/// <summary> /// <summary>
/// Represents a service for managing users. /// Represents a service for managing users.
/// </summary> /// </summary>
public interface IBlogUserService public interface IUserService
{ {
/// <summary> /// <summary>
/// Attempts to find a user with the specified ID. /// Attempts to find a user with the specified ID.

View File

@ -11,7 +11,7 @@ namespace OliverBooth.Services;
internal sealed class SessionService : ISessionService internal sealed class SessionService : ISessionService
{ {
private readonly ILogger<SessionService> _logger; private readonly ILogger<SessionService> _logger;
private readonly IBlogUserService _userService; private readonly IUserService _userService;
private readonly IDbContextFactory<BlogContext> _blogContextFactory; private readonly IDbContextFactory<BlogContext> _blogContextFactory;
/// <summary> /// <summary>
@ -22,7 +22,7 @@ internal sealed class SessionService : ISessionService
/// <param name="blogContextFactory">The <see cref="BlogContext" /> factory.</param> /// <param name="blogContextFactory">The <see cref="BlogContext" /> factory.</param>
/// <param name="webContextFactory">The <see cref="WebContext" /> factory.</param> /// <param name="webContextFactory">The <see cref="WebContext" /> factory.</param>
public SessionService(ILogger<SessionService> logger, public SessionService(ILogger<SessionService> logger,
IBlogUserService userService, IUserService userService,
IDbContextFactory<BlogContext> blogContextFactory, IDbContextFactory<BlogContext> blogContextFactory,
IDbContextFactory<WebContext> webContextFactory) IDbContextFactory<WebContext> webContextFactory)
{ {

View File

@ -1,26 +1,26 @@
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using OliverBooth.Data.Blog; using OliverBooth.Data.Web;
using BC = BCrypt.Net.BCrypt; using BC = BCrypt.Net.BCrypt;
namespace OliverBooth.Services; namespace OliverBooth.Services;
/// <summary> /// <summary>
/// Represents an implementation of <see cref="IBlogUserService" />. /// Represents an implementation of <see cref="IUserService" />.
/// </summary> /// </summary>
internal sealed class BlogUserService : IBlogUserService internal sealed class UserService : IUserService
{ {
private readonly IDbContextFactory<BlogContext> _dbContextFactory; private readonly IDbContextFactory<WebContext> _dbContextFactory;
private readonly ConcurrentDictionary<Guid, IUser> _userCache = new(); private readonly ConcurrentDictionary<Guid, IUser> _userCache = new();
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="BlogUserService" /> class. /// Initializes a new instance of the <see cref="UserService" /> class.
/// </summary> /// </summary>
/// <param name="dbContextFactory"> /// <param name="dbContextFactory">
/// The <see cref="IDbContextFactory{TContext}" /> used to create a <see cref="BlogContext" />. /// The <see cref="IDbContextFactory{TContext}" /> used to create a <see cref="WebContext" />.
/// </param> /// </param>
public BlogUserService(IDbContextFactory<BlogContext> dbContextFactory) public UserService(IDbContextFactory<WebContext> dbContextFactory)
{ {
_dbContextFactory = dbContextFactory; _dbContextFactory = dbContextFactory;
} }
@ -30,7 +30,7 @@ internal sealed class BlogUserService : IBlogUserService
{ {
if (_userCache.TryGetValue(id, out user)) return true; if (_userCache.TryGetValue(id, out user)) return true;
using BlogContext context = _dbContextFactory.CreateDbContext(); using WebContext context = _dbContextFactory.CreateDbContext();
user = context.Users.Find(id); user = context.Users.Find(id);
if (user is not null) _userCache.TryAdd(id, user); if (user is not null) _userCache.TryAdd(id, user);
@ -40,7 +40,7 @@ internal sealed class BlogUserService : IBlogUserService
/// <inheritdoc /> /// <inheritdoc />
public bool VerifyLogin(string email, string password, [NotNullWhen(true)] out IUser? user) public bool VerifyLogin(string email, string password, [NotNullWhen(true)] out IUser? user)
{ {
using BlogContext context = _dbContextFactory.CreateDbContext(); using WebContext context = _dbContextFactory.CreateDbContext();
user = context.Users.FirstOrDefault(u => u.EmailAddress == email); user = context.Users.FirstOrDefault(u => u.EmailAddress == email);
return user is not null && BC.Verify(password, ((User)user).Password); return user is not null && BC.Verify(password, ((User)user).Password);
} }