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

View File

@ -31,12 +31,6 @@ internal sealed class BlogContext : DbContext
/// <value>The collection of sessions.</value>
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 />
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
@ -50,6 +44,5 @@ internal sealed class BlogContext : DbContext
{
modelBuilder.ApplyConfiguration(new BlogPostConfiguration());
modelBuilder.ApplyConfiguration(new SessionConfiguration());
modelBuilder.ApplyConfiguration(new UserConfiguration());
}
}

View File

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

View File

@ -1,4 +1,4 @@
namespace OliverBooth.Data.Blog;
namespace OliverBooth.Data.Web;
/// <summary>
/// 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.Text;
using Cysharp.Text;
using OliverBooth.Data.Blog;
namespace OliverBooth.Data.Blog;
namespace OliverBooth.Data.Web;
/// <summary>
/// Represents a user.

View File

@ -55,6 +55,12 @@ internal sealed class WebContext : DbContext
/// <value>The collection of templates.</value>
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 />
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
@ -72,5 +78,6 @@ internal sealed class WebContext : DbContext
modelBuilder.ApplyConfiguration(new ProjectConfiguration());
modelBuilder.ApplyConfiguration(new TemplateConfiguration());
modelBuilder.ApplyConfiguration(new SiteConfigurationConfiguration());
modelBuilder.ApplyConfiguration(new UserConfiguration());
}
}

View File

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

View File

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

View File

@ -3,6 +3,7 @@ using Humanizer;
using Markdig;
using Microsoft.EntityFrameworkCore;
using OliverBooth.Data.Blog;
using OliverBooth.Data.Web;
namespace OliverBooth.Services;
@ -12,7 +13,7 @@ namespace OliverBooth.Services;
internal sealed class BlogPostService : IBlogPostService
{
private readonly IDbContextFactory<BlogContext> _dbContextFactory;
private readonly IBlogUserService _blogUserService;
private readonly IUserService _userService;
private readonly MarkdownPipeline _markdownPipeline;
/// <summary>
@ -21,14 +22,14 @@ internal sealed class BlogPostService : IBlogPostService
/// <param name="dbContextFactory">
/// The <see cref="IDbContextFactory{TContext}" /> used to create a <see cref="BlogContext" />.
/// </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>
public BlogPostService(IDbContextFactory<BlogContext> dbContextFactory,
IBlogUserService blogUserService,
IUserService userService,
MarkdownPipeline markdownPipeline)
{
_dbContextFactory = dbContextFactory;
_blogUserService = blogUserService;
_userService = userService;
_markdownPipeline = markdownPipeline;
}
@ -163,7 +164,7 @@ internal sealed class BlogPostService : IBlogPostService
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;
}

View File

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

View File

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

View File

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

View File

@ -1,26 +1,26 @@
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using Microsoft.EntityFrameworkCore;
using OliverBooth.Data.Blog;
using OliverBooth.Data.Web;
using BC = BCrypt.Net.BCrypt;
namespace OliverBooth.Services;
/// <summary>
/// Represents an implementation of <see cref="IBlogUserService" />.
/// Represents an implementation of <see cref="IUserService" />.
/// </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();
/// <summary>
/// Initializes a new instance of the <see cref="BlogUserService" /> class.
/// Initializes a new instance of the <see cref="UserService" /> class.
/// </summary>
/// <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>
public BlogUserService(IDbContextFactory<BlogContext> dbContextFactory)
public UserService(IDbContextFactory<WebContext> dbContextFactory)
{
_dbContextFactory = dbContextFactory;
}
@ -30,7 +30,7 @@ internal sealed class BlogUserService : IBlogUserService
{
if (_userCache.TryGetValue(id, out user)) return true;
using BlogContext context = _dbContextFactory.CreateDbContext();
using WebContext context = _dbContextFactory.CreateDbContext();
user = context.Users.Find(id);
if (user is not null) _userCache.TryAdd(id, user);
@ -40,7 +40,7 @@ internal sealed class BlogUserService : IBlogUserService
/// <inheritdoc />
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);
return user is not null && BC.Verify(password, ((User)user).Password);
}