Add EF Core, Npgsql, Todo entity, and AppDbContext
This commit is contained in:
25
backend/Data/AppDbContext.cs
Normal file
25
backend/Data/AppDbContext.cs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
using Backend.Models;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace Backend.Data;
|
||||||
|
|
||||||
|
public class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(options)
|
||||||
|
{
|
||||||
|
public DbSet<Todo> Todos => Set<Todo>();
|
||||||
|
|
||||||
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
modelBuilder.Entity<Todo>(e =>
|
||||||
|
{
|
||||||
|
e.ToTable("todos", schema: "public");
|
||||||
|
e.HasKey(x => x.Id);
|
||||||
|
e.Property(x => x.Id).HasColumnName("id").ValueGeneratedOnAdd();
|
||||||
|
e.Property(x => x.UserId).HasColumnName("user_id").IsRequired();
|
||||||
|
e.Property(x => x.Title).HasColumnName("title").IsRequired().HasMaxLength(500);
|
||||||
|
e.Property(x => x.Completed).HasColumnName("completed").IsRequired();
|
||||||
|
e.Property(x => x.CreatedAt).HasColumnName("created_at").IsRequired();
|
||||||
|
e.HasIndex(x => new { x.UserId, x.CreatedAt })
|
||||||
|
.HasDatabaseName("todos_user_id_created_at_idx");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
10
backend/Models/Todo.cs
Normal file
10
backend/Models/Todo.cs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
namespace Backend.Models;
|
||||||
|
|
||||||
|
public class Todo
|
||||||
|
{
|
||||||
|
public long Id { get; set; }
|
||||||
|
public Guid UserId { get; set; }
|
||||||
|
public string Title { get; set; } = string.Empty;
|
||||||
|
public bool Completed { get; set; }
|
||||||
|
public DateTimeOffset CreatedAt { get; set; }
|
||||||
|
}
|
||||||
@@ -1,21 +1,23 @@
|
|||||||
var builder = WebApplication.CreateBuilder(args);
|
using Backend.Data;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
// Add services to the container.
|
|
||||||
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
builder.Services.AddControllers();
|
|
||||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
builder.Services.AddDbContext<AppDbContext>(options =>
|
||||||
builder.Services.AddOpenApi();
|
options.UseNpgsql(builder.Configuration.GetConnectionString("Postgres")));
|
||||||
|
|
||||||
var app = builder.Build();
|
// MARKER: CORS SERVICES
|
||||||
|
|
||||||
// Configure the HTTP request pipeline.
|
// MARKER: AUTH SERVICES
|
||||||
if (app.Environment.IsDevelopment())
|
|
||||||
{
|
builder.Services.AddControllers();
|
||||||
app.MapOpenApi();
|
|
||||||
}
|
var app = builder.Build();
|
||||||
|
|
||||||
app.UseAuthorization();
|
// MARKER: CORS MIDDLEWARE
|
||||||
|
|
||||||
app.MapControllers();
|
// MARKER: AUTH MIDDLEWARE
|
||||||
|
|
||||||
app.Run();
|
app.MapControllers();
|
||||||
|
|
||||||
|
app.Run();
|
||||||
|
|||||||
@@ -8,6 +8,8 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.19" />
|
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.19" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.19" />
|
||||||
|
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Reference in New Issue
Block a user