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,20 +1,22 @@
|
||||
using Backend.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddDbContext<AppDbContext>(options =>
|
||||
options.UseNpgsql(builder.Configuration.GetConnectionString("Postgres")));
|
||||
|
||||
// MARKER: CORS SERVICES
|
||||
|
||||
// MARKER: AUTH SERVICES
|
||||
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||
builder.Services.AddOpenApi();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.MapOpenApi();
|
||||
}
|
||||
// MARKER: CORS MIDDLEWARE
|
||||
|
||||
app.UseAuthorization();
|
||||
// MARKER: AUTH MIDDLEWARE
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
|
||||
<ItemGroup>
|
||||
<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>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user