refactor: rename BlogUserService to UserService
This commit is contained in:
parent
bd55ac28e3
commit
8ef34d014b
|
@ -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;
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using OliverBooth.Data.Web;
|
||||
|
||||
namespace OliverBooth.Data.Blog.Configuration;
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
namespace OliverBooth.Data.Blog;
|
||||
namespace OliverBooth.Data.Web;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a user which can log in to the blog.
|
|
@ -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.
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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>();
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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.
|
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
}
|
Loading…
Reference in New Issue