diff --git a/backend/Data/AppDbContext.cs b/backend/Data/AppDbContext.cs new file mode 100644 index 0000000..7ebbdef --- /dev/null +++ b/backend/Data/AppDbContext.cs @@ -0,0 +1,25 @@ +using Backend.Models; +using Microsoft.EntityFrameworkCore; + +namespace Backend.Data; + +public class AppDbContext(DbContextOptions options) : DbContext(options) +{ + public DbSet Todos => Set(); + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity(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"); + }); + } +} diff --git a/backend/Models/Todo.cs b/backend/Models/Todo.cs new file mode 100644 index 0000000..def8286 --- /dev/null +++ b/backend/Models/Todo.cs @@ -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; } +} diff --git a/backend/Program.cs b/backend/Program.cs index 35c8ff9..a4f6724 100644 --- a/backend/Program.cs +++ b/backend/Program.cs @@ -1,21 +1,23 @@ -var builder = WebApplication.CreateBuilder(args); - -// Add services to the container. - -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(); -} - -app.UseAuthorization(); - -app.MapControllers(); - -app.Run(); +using Backend.Data; +using Microsoft.EntityFrameworkCore; + +var builder = WebApplication.CreateBuilder(args); + +builder.Services.AddDbContext(options => + options.UseNpgsql(builder.Configuration.GetConnectionString("Postgres"))); + +// MARKER: CORS SERVICES + +// MARKER: AUTH SERVICES + +builder.Services.AddControllers(); + +var app = builder.Build(); + +// MARKER: CORS MIDDLEWARE + +// MARKER: AUTH MIDDLEWARE + +app.MapControllers(); + +app.Run(); diff --git a/backend/backend.csproj b/backend/backend.csproj index eb5883c..4bd1142 100644 --- a/backend/backend.csproj +++ b/backend/backend.csproj @@ -8,6 +8,8 @@ + +