BRAKING refactor project structure.
Refactor: the project is now divided into a more clear structure, with **Infrastructure** and **Application** layers added. Refactor: configurations are split into sections for different layers. Fix: now EF Core related operations, such as migration, should be invoked in `OptixServe.Infrastructure`, with config file and data dir passed into `dotnet ef` command. See `OptixServe.Infrastructure/Utilites/DesignTimeDbContextFactory.cs` for details. Fix: EF migrations are ignored in gitignore on purpose in early development.
This commit is contained in:
@ -1,21 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using OptixServe.Core.Models;
|
||||
|
||||
namespace OptixServe.Core.Data;
|
||||
|
||||
public class AppDbContext(DbContextOptions options) : DbContext(options)
|
||||
{
|
||||
public DbSet<User> Users { get; set; } = null!;
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<User>(user =>
|
||||
{
|
||||
user.HasKey(u => u.Id);
|
||||
});
|
||||
|
||||
modelBuilder.Entity<User>().HasData([
|
||||
new() {Id = "1", UserName = "admin", Password = "admin12345"}
|
||||
]);
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
namespace OptixServe.Core.Data;
|
||||
|
||||
public class DbInitializer(AppDbContext dbContext)
|
||||
{
|
||||
private readonly AppDbContext _context = dbContext;
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
_context.Database.EnsureCreated();
|
||||
}
|
||||
}
|
@ -6,8 +6,5 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.6" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -1,31 +0,0 @@
|
||||
using OptixServe.Core.Data;
|
||||
using OptixServe.Core.Models;
|
||||
|
||||
namespace OptixServe.Core.Services;
|
||||
|
||||
public interface IUserService
|
||||
{
|
||||
IEnumerable<User> GetUsers();
|
||||
User? GetUserById(string id);
|
||||
User? GetUserByUsername(string username);
|
||||
}
|
||||
|
||||
public class UserService(AppDbContext dbContext) : IUserService
|
||||
{
|
||||
private readonly AppDbContext _dbContext = dbContext;
|
||||
|
||||
public User? GetUserById(string id)
|
||||
{
|
||||
return _dbContext.Users.FirstOrDefault(u => u.Id == id);
|
||||
}
|
||||
|
||||
public User? GetUserByUsername(string username)
|
||||
{
|
||||
return _dbContext.Users.FirstOrDefault(u => u.UserName == username);
|
||||
}
|
||||
|
||||
public IEnumerable<User> GetUsers()
|
||||
{
|
||||
return _dbContext.Users.AsEnumerable();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user