add architecture skeleton

This commit is contained in:
2026-04-19 13:12:04 +03:00
parent 312708531f
commit 2738f31f26
84 changed files with 2710 additions and 70 deletions
+19
View File
@@ -0,0 +1,19 @@
using GymLogAI.Core.Entities;
using Microsoft.EntityFrameworkCore;
namespace GymLogAI.Persistence;
public class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(options)
{
public DbSet<User> Users => Set<User>();
public DbSet<TelegramMessage> TelegramMessages => Set<TelegramMessage>();
public DbSet<Workout> Workouts => Set<Workout>();
public DbSet<Exercise> Exercises => Set<Exercise>();
public DbSet<WorkoutExercise> WorkoutExercises => Set<WorkoutExercise>();
public DbSet<ExerciseSet> ExerciseSets => Set<ExerciseSet>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly);
}
}
@@ -0,0 +1,20 @@
using GymLogAI.Core.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace GymLogAI.Persistence.Configurations;
public sealed class ExerciseConfiguration : IEntityTypeConfiguration<Exercise>
{
public void Configure(EntityTypeBuilder<Exercise> builder)
{
builder.ToTable("exercises");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id).ValueGeneratedNever();
builder.Property(x => x.Name).HasMaxLength(256).IsRequired();
builder.Property(x => x.Description).HasMaxLength(2000);
builder.Property(x => x.Embedding).HasColumnType("real[]");
builder.HasIndex(x => x.Name).IsUnique();
}
}
@@ -0,0 +1,20 @@
using GymLogAI.Core.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace GymLogAI.Persistence.Configurations;
public sealed class ExerciseSetConfiguration : IEntityTypeConfiguration<ExerciseSet>
{
public void Configure(EntityTypeBuilder<ExerciseSet> builder)
{
builder.ToTable("exercise_sets");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id).ValueGeneratedNever();
builder.Property(x => x.WeightKg).HasPrecision(8, 2);
builder.Property(x => x.DistanceMeters).HasPrecision(10, 2);
builder.Property(x => x.Notes).HasMaxLength(1000);
builder.HasIndex(x => new { x.WorkoutExerciseId, x.Order }).IsUnique();
}
}
@@ -0,0 +1,31 @@
using GymLogAI.Core.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace GymLogAI.Persistence.Configurations;
public sealed class TelegramMessageConfiguration : IEntityTypeConfiguration<TelegramMessage>
{
public void Configure(EntityTypeBuilder<TelegramMessage> builder)
{
builder.ToTable("telegram_messages");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id).ValueGeneratedNever();
builder.Property(x => x.Text).HasMaxLength(4000).IsRequired();
builder.Property(x => x.ParsingError).HasMaxLength(2000);
builder.HasOne(x => x.User)
.WithMany(x => x.TelegramMessages)
.HasForeignKey(x => x.UserId)
.OnDelete(DeleteBehavior.Cascade);
builder.HasOne(x => x.Workout)
.WithOne(x => x.SourceTelegramMessage)
.HasForeignKey<Workout>(x => x.SourceTelegramMessageId)
.IsRequired(false)
.OnDelete(DeleteBehavior.SetNull);
builder.HasIndex(x => new { x.UserId, x.ParsingStatus });
builder.HasIndex(x => new { x.TelegramChatId, x.TelegramMessageId }).IsUnique();
}
}
@@ -0,0 +1,22 @@
using GymLogAI.Core.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace GymLogAI.Persistence.Configurations;
public sealed class UserConfiguration : IEntityTypeConfiguration<User>
{
public void Configure(EntityTypeBuilder<User> builder)
{
builder.ToTable("users");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id).ValueGeneratedNever();
builder.Property(x => x.TelegramUserId).IsRequired();
builder.Property(x => x.Username).HasMaxLength(128);
builder.Property(x => x.FirstName).HasMaxLength(128);
builder.Property(x => x.LastName).HasMaxLength(128);
builder.Property(x => x.CreatedAtUtc).IsRequired();
builder.HasIndex(x => x.TelegramUserId).IsUnique();
}
}
@@ -0,0 +1,29 @@
using GymLogAI.Core.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace GymLogAI.Persistence.Configurations;
public sealed class WorkoutConfiguration : IEntityTypeConfiguration<Workout>
{
public void Configure(EntityTypeBuilder<Workout> builder)
{
builder.ToTable("workouts");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id).ValueGeneratedNever();
builder.Property(x => x.Title).HasMaxLength(256);
builder.Property(x => x.Notes).HasMaxLength(4000);
builder.HasOne(x => x.User)
.WithMany(x => x.Workouts)
.HasForeignKey(x => x.UserId)
.OnDelete(DeleteBehavior.Cascade);
builder.HasMany(x => x.WorkoutExercises)
.WithOne(x => x.Workout)
.HasForeignKey(x => x.WorkoutId)
.OnDelete(DeleteBehavior.Cascade);
builder.HasIndex(x => new { x.UserId, x.PerformedAtUtc });
}
}
@@ -0,0 +1,28 @@
using GymLogAI.Core.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace GymLogAI.Persistence.Configurations;
public sealed class WorkoutExerciseConfiguration : IEntityTypeConfiguration<WorkoutExercise>
{
public void Configure(EntityTypeBuilder<WorkoutExercise> builder)
{
builder.ToTable("workout_exercises");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id).ValueGeneratedNever();
builder.Property(x => x.Notes).HasMaxLength(2000);
builder.HasOne(x => x.Exercise)
.WithMany(x => x.WorkoutExercises)
.HasForeignKey(x => x.ExerciseId)
.OnDelete(DeleteBehavior.Restrict);
builder.HasMany(x => x.Sets)
.WithOne(x => x.WorkoutExercise)
.HasForeignKey(x => x.WorkoutExerciseId)
.OnDelete(DeleteBehavior.Cascade);
builder.HasIndex(x => new { x.WorkoutId, x.Order }).IsUnique();
}
}
@@ -0,0 +1,29 @@
using GymLogAI.Application.Interfaces;
using GymLogAI.Core.Abstractions;
using GymLogAI.Persistence.Infrastructure;
using GymLogAI.Persistence.Repositories;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace GymLogAI.Persistence;
public static class DependencyInjection
{
public static IServiceCollection AddPersistence(this IServiceCollection services, IConfiguration configuration)
{
var connectionString = configuration.GetConnectionString("Postgres")
?? "Host=localhost;Port=5432;Database=gymlogai;Username=postgres;Password=postgres";
services.AddDbContext<AppDbContext>(options => options.UseNpgsql(connectionString));
services.AddScoped<IUserRepository, UserRepository>();
services.AddScoped<ITelegramMessageRepository, TelegramMessageRepository>();
services.AddScoped<IWorkoutRepository, WorkoutRepository>();
services.AddScoped<IExerciseRepository, ExerciseRepository>();
services.AddSingleton<IClock, SystemClock>();
services.AddSingleton<IIdGenerator, GuidIdGenerator>();
return services;
}
}
@@ -0,0 +1,15 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
namespace GymLogAI.Persistence;
public sealed class DesignTimeAppDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>
{
public AppDbContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<AppDbContext>();
optionsBuilder.UseNpgsql("Host=localhost;Port=5432;Database=gymlogai;Username=postgres;Password=postgres");
return new AppDbContext(optionsBuilder.Options);
}
}
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\GymLogAI.Application\GymLogAI.Application.csproj" />
<ProjectReference Include="..\GymLogAI.Core\GymLogAI.Core.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,8 @@
using GymLogAI.Core.Abstractions;
namespace GymLogAI.Persistence.Infrastructure;
public sealed class GuidIdGenerator : IIdGenerator
{
public Guid New() => Guid.NewGuid();
}
@@ -0,0 +1,8 @@
using GymLogAI.Core.Abstractions;
namespace GymLogAI.Persistence.Infrastructure;
public sealed class SystemClock : IClock
{
public DateTime UtcNow => DateTime.UtcNow;
}
@@ -0,0 +1,326 @@
// <auto-generated />
using System;
using GymLogAI.Persistence;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace GymLogAI.Persistence.Migrations
{
[DbContext(typeof(AppDbContext))]
[Migration("20260419100551_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "10.0.4")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("GymLogAI.Core.Entities.Exercise", b =>
{
b.Property<Guid>("Id")
.HasColumnType("uuid");
b.Property<DateTime>("CreatedAtUtc")
.HasColumnType("timestamp with time zone");
b.Property<string>("Description")
.HasMaxLength(2000)
.HasColumnType("character varying(2000)");
b.PrimitiveCollection<float[]>("Embedding")
.HasColumnType("real[]");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(256)
.HasColumnType("character varying(256)");
b.HasKey("Id");
b.HasIndex("Name")
.IsUnique();
b.ToTable("exercises", (string)null);
});
modelBuilder.Entity("GymLogAI.Core.Entities.ExerciseSet", b =>
{
b.Property<Guid>("Id")
.HasColumnType("uuid");
b.Property<decimal?>("DistanceMeters")
.HasPrecision(10, 2)
.HasColumnType("numeric(10,2)");
b.Property<int?>("DurationSeconds")
.HasColumnType("integer");
b.Property<string>("Notes")
.HasMaxLength(1000)
.HasColumnType("character varying(1000)");
b.Property<int>("Order")
.HasColumnType("integer");
b.Property<int?>("Repetitions")
.HasColumnType("integer");
b.Property<decimal?>("WeightKg")
.HasPrecision(8, 2)
.HasColumnType("numeric(8,2)");
b.Property<Guid>("WorkoutExerciseId")
.HasColumnType("uuid");
b.HasKey("Id");
b.HasIndex("WorkoutExerciseId", "Order")
.IsUnique();
b.ToTable("exercise_sets", (string)null);
});
modelBuilder.Entity("GymLogAI.Core.Entities.TelegramMessage", b =>
{
b.Property<Guid>("Id")
.HasColumnType("uuid");
b.Property<DateTime>("ImportedAtUtc")
.HasColumnType("timestamp with time zone");
b.Property<DateTime?>("ParsedAtUtc")
.HasColumnType("timestamp with time zone");
b.Property<string>("ParsingError")
.HasMaxLength(2000)
.HasColumnType("character varying(2000)");
b.Property<int>("ParsingStatus")
.HasColumnType("integer");
b.Property<DateTime>("SentAtUtc")
.HasColumnType("timestamp with time zone");
b.Property<long>("TelegramChatId")
.HasColumnType("bigint");
b.Property<int>("TelegramMessageId")
.HasColumnType("integer");
b.Property<string>("Text")
.IsRequired()
.HasMaxLength(4000)
.HasColumnType("character varying(4000)");
b.Property<Guid>("UserId")
.HasColumnType("uuid");
b.Property<Guid?>("WorkoutId")
.HasColumnType("uuid");
b.HasKey("Id");
b.HasIndex("TelegramChatId", "TelegramMessageId")
.IsUnique();
b.HasIndex("UserId", "ParsingStatus");
b.ToTable("telegram_messages", (string)null);
});
modelBuilder.Entity("GymLogAI.Core.Entities.User", b =>
{
b.Property<Guid>("Id")
.HasColumnType("uuid");
b.Property<DateTime>("CreatedAtUtc")
.HasColumnType("timestamp with time zone");
b.Property<string>("FirstName")
.HasMaxLength(128)
.HasColumnType("character varying(128)");
b.Property<string>("LastName")
.HasMaxLength(128)
.HasColumnType("character varying(128)");
b.Property<long>("TelegramUserId")
.HasColumnType("bigint");
b.Property<string>("Username")
.HasMaxLength(128)
.HasColumnType("character varying(128)");
b.HasKey("Id");
b.HasIndex("TelegramUserId")
.IsUnique();
b.ToTable("users", (string)null);
});
modelBuilder.Entity("GymLogAI.Core.Entities.Workout", b =>
{
b.Property<Guid>("Id")
.HasColumnType("uuid");
b.Property<DateTime>("CreatedAtUtc")
.HasColumnType("timestamp with time zone");
b.Property<string>("Notes")
.HasMaxLength(4000)
.HasColumnType("character varying(4000)");
b.Property<DateTime>("PerformedAtUtc")
.HasColumnType("timestamp with time zone");
b.Property<Guid?>("SourceTelegramMessageId")
.HasColumnType("uuid");
b.Property<int>("SourceType")
.HasColumnType("integer");
b.Property<string>("Title")
.HasMaxLength(256)
.HasColumnType("character varying(256)");
b.Property<Guid>("UserId")
.HasColumnType("uuid");
b.HasKey("Id");
b.HasIndex("SourceTelegramMessageId")
.IsUnique();
b.HasIndex("UserId", "PerformedAtUtc");
b.ToTable("workouts", (string)null);
});
modelBuilder.Entity("GymLogAI.Core.Entities.WorkoutExercise", b =>
{
b.Property<Guid>("Id")
.HasColumnType("uuid");
b.Property<Guid>("ExerciseId")
.HasColumnType("uuid");
b.Property<string>("Notes")
.HasMaxLength(2000)
.HasColumnType("character varying(2000)");
b.Property<int>("Order")
.HasColumnType("integer");
b.Property<Guid>("WorkoutId")
.HasColumnType("uuid");
b.HasKey("Id");
b.HasIndex("ExerciseId");
b.HasIndex("WorkoutId", "Order")
.IsUnique();
b.ToTable("workout_exercises", (string)null);
});
modelBuilder.Entity("GymLogAI.Core.Entities.ExerciseSet", b =>
{
b.HasOne("GymLogAI.Core.Entities.WorkoutExercise", "WorkoutExercise")
.WithMany("Sets")
.HasForeignKey("WorkoutExerciseId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("WorkoutExercise");
});
modelBuilder.Entity("GymLogAI.Core.Entities.TelegramMessage", b =>
{
b.HasOne("GymLogAI.Core.Entities.User", "User")
.WithMany("TelegramMessages")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("User");
});
modelBuilder.Entity("GymLogAI.Core.Entities.Workout", b =>
{
b.HasOne("GymLogAI.Core.Entities.TelegramMessage", "SourceTelegramMessage")
.WithOne("Workout")
.HasForeignKey("GymLogAI.Core.Entities.Workout", "SourceTelegramMessageId")
.OnDelete(DeleteBehavior.SetNull);
b.HasOne("GymLogAI.Core.Entities.User", "User")
.WithMany("Workouts")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("SourceTelegramMessage");
b.Navigation("User");
});
modelBuilder.Entity("GymLogAI.Core.Entities.WorkoutExercise", b =>
{
b.HasOne("GymLogAI.Core.Entities.Exercise", "Exercise")
.WithMany("WorkoutExercises")
.HasForeignKey("ExerciseId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
b.HasOne("GymLogAI.Core.Entities.Workout", "Workout")
.WithMany("WorkoutExercises")
.HasForeignKey("WorkoutId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Exercise");
b.Navigation("Workout");
});
modelBuilder.Entity("GymLogAI.Core.Entities.Exercise", b =>
{
b.Navigation("WorkoutExercises");
});
modelBuilder.Entity("GymLogAI.Core.Entities.TelegramMessage", b =>
{
b.Navigation("Workout");
});
modelBuilder.Entity("GymLogAI.Core.Entities.User", b =>
{
b.Navigation("TelegramMessages");
b.Navigation("Workouts");
});
modelBuilder.Entity("GymLogAI.Core.Entities.Workout", b =>
{
b.Navigation("WorkoutExercises");
});
modelBuilder.Entity("GymLogAI.Core.Entities.WorkoutExercise", b =>
{
b.Navigation("Sets");
});
#pragma warning restore 612, 618
}
}
}
@@ -0,0 +1,227 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace GymLogAI.Persistence.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "exercises",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
Name = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: false),
Description = table.Column<string>(type: "character varying(2000)", maxLength: 2000, nullable: true),
Embedding = table.Column<float[]>(type: "real[]", nullable: true),
CreatedAtUtc = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_exercises", x => x.Id);
});
migrationBuilder.CreateTable(
name: "users",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
TelegramUserId = table.Column<long>(type: "bigint", nullable: false),
Username = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: true),
FirstName = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: true),
LastName = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: true),
CreatedAtUtc = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_users", x => x.Id);
});
migrationBuilder.CreateTable(
name: "telegram_messages",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
UserId = table.Column<Guid>(type: "uuid", nullable: false),
TelegramChatId = table.Column<long>(type: "bigint", nullable: false),
TelegramMessageId = table.Column<int>(type: "integer", nullable: false),
Text = table.Column<string>(type: "character varying(4000)", maxLength: 4000, nullable: false),
SentAtUtc = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
ImportedAtUtc = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
ParsingStatus = table.Column<int>(type: "integer", nullable: false),
ParsingError = table.Column<string>(type: "character varying(2000)", maxLength: 2000, nullable: true),
WorkoutId = table.Column<Guid>(type: "uuid", nullable: true),
ParsedAtUtc = table.Column<DateTime>(type: "timestamp with time zone", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_telegram_messages", x => x.Id);
table.ForeignKey(
name: "FK_telegram_messages_users_UserId",
column: x => x.UserId,
principalTable: "users",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "workouts",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
UserId = table.Column<Guid>(type: "uuid", nullable: false),
SourceTelegramMessageId = table.Column<Guid>(type: "uuid", nullable: true),
PerformedAtUtc = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
Title = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
Notes = table.Column<string>(type: "character varying(4000)", maxLength: 4000, nullable: true),
SourceType = table.Column<int>(type: "integer", nullable: false),
CreatedAtUtc = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_workouts", x => x.Id);
table.ForeignKey(
name: "FK_workouts_telegram_messages_SourceTelegramMessageId",
column: x => x.SourceTelegramMessageId,
principalTable: "telegram_messages",
principalColumn: "Id",
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "FK_workouts_users_UserId",
column: x => x.UserId,
principalTable: "users",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "workout_exercises",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
WorkoutId = table.Column<Guid>(type: "uuid", nullable: false),
ExerciseId = table.Column<Guid>(type: "uuid", nullable: false),
Order = table.Column<int>(type: "integer", nullable: false),
Notes = table.Column<string>(type: "character varying(2000)", maxLength: 2000, nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_workout_exercises", x => x.Id);
table.ForeignKey(
name: "FK_workout_exercises_exercises_ExerciseId",
column: x => x.ExerciseId,
principalTable: "exercises",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_workout_exercises_workouts_WorkoutId",
column: x => x.WorkoutId,
principalTable: "workouts",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "exercise_sets",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
WorkoutExerciseId = table.Column<Guid>(type: "uuid", nullable: false),
Order = table.Column<int>(type: "integer", nullable: false),
WeightKg = table.Column<decimal>(type: "numeric(8,2)", precision: 8, scale: 2, nullable: true),
Repetitions = table.Column<int>(type: "integer", nullable: true),
DurationSeconds = table.Column<int>(type: "integer", nullable: true),
DistanceMeters = table.Column<decimal>(type: "numeric(10,2)", precision: 10, scale: 2, nullable: true),
Notes = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_exercise_sets", x => x.Id);
table.ForeignKey(
name: "FK_exercise_sets_workout_exercises_WorkoutExerciseId",
column: x => x.WorkoutExerciseId,
principalTable: "workout_exercises",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_exercise_sets_WorkoutExerciseId_Order",
table: "exercise_sets",
columns: new[] { "WorkoutExerciseId", "Order" },
unique: true);
migrationBuilder.CreateIndex(
name: "IX_exercises_Name",
table: "exercises",
column: "Name",
unique: true);
migrationBuilder.CreateIndex(
name: "IX_telegram_messages_TelegramChatId_TelegramMessageId",
table: "telegram_messages",
columns: new[] { "TelegramChatId", "TelegramMessageId" },
unique: true);
migrationBuilder.CreateIndex(
name: "IX_telegram_messages_UserId_ParsingStatus",
table: "telegram_messages",
columns: new[] { "UserId", "ParsingStatus" });
migrationBuilder.CreateIndex(
name: "IX_users_TelegramUserId",
table: "users",
column: "TelegramUserId",
unique: true);
migrationBuilder.CreateIndex(
name: "IX_workout_exercises_ExerciseId",
table: "workout_exercises",
column: "ExerciseId");
migrationBuilder.CreateIndex(
name: "IX_workout_exercises_WorkoutId_Order",
table: "workout_exercises",
columns: new[] { "WorkoutId", "Order" },
unique: true);
migrationBuilder.CreateIndex(
name: "IX_workouts_SourceTelegramMessageId",
table: "workouts",
column: "SourceTelegramMessageId",
unique: true);
migrationBuilder.CreateIndex(
name: "IX_workouts_UserId_PerformedAtUtc",
table: "workouts",
columns: new[] { "UserId", "PerformedAtUtc" });
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "exercise_sets");
migrationBuilder.DropTable(
name: "workout_exercises");
migrationBuilder.DropTable(
name: "exercises");
migrationBuilder.DropTable(
name: "workouts");
migrationBuilder.DropTable(
name: "telegram_messages");
migrationBuilder.DropTable(
name: "users");
}
}
}
@@ -0,0 +1,323 @@
// <auto-generated />
using System;
using GymLogAI.Persistence;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace GymLogAI.Persistence.Migrations
{
[DbContext(typeof(AppDbContext))]
partial class AppDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "10.0.4")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("GymLogAI.Core.Entities.Exercise", b =>
{
b.Property<Guid>("Id")
.HasColumnType("uuid");
b.Property<DateTime>("CreatedAtUtc")
.HasColumnType("timestamp with time zone");
b.Property<string>("Description")
.HasMaxLength(2000)
.HasColumnType("character varying(2000)");
b.PrimitiveCollection<float[]>("Embedding")
.HasColumnType("real[]");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(256)
.HasColumnType("character varying(256)");
b.HasKey("Id");
b.HasIndex("Name")
.IsUnique();
b.ToTable("exercises", (string)null);
});
modelBuilder.Entity("GymLogAI.Core.Entities.ExerciseSet", b =>
{
b.Property<Guid>("Id")
.HasColumnType("uuid");
b.Property<decimal?>("DistanceMeters")
.HasPrecision(10, 2)
.HasColumnType("numeric(10,2)");
b.Property<int?>("DurationSeconds")
.HasColumnType("integer");
b.Property<string>("Notes")
.HasMaxLength(1000)
.HasColumnType("character varying(1000)");
b.Property<int>("Order")
.HasColumnType("integer");
b.Property<int?>("Repetitions")
.HasColumnType("integer");
b.Property<decimal?>("WeightKg")
.HasPrecision(8, 2)
.HasColumnType("numeric(8,2)");
b.Property<Guid>("WorkoutExerciseId")
.HasColumnType("uuid");
b.HasKey("Id");
b.HasIndex("WorkoutExerciseId", "Order")
.IsUnique();
b.ToTable("exercise_sets", (string)null);
});
modelBuilder.Entity("GymLogAI.Core.Entities.TelegramMessage", b =>
{
b.Property<Guid>("Id")
.HasColumnType("uuid");
b.Property<DateTime>("ImportedAtUtc")
.HasColumnType("timestamp with time zone");
b.Property<DateTime?>("ParsedAtUtc")
.HasColumnType("timestamp with time zone");
b.Property<string>("ParsingError")
.HasMaxLength(2000)
.HasColumnType("character varying(2000)");
b.Property<int>("ParsingStatus")
.HasColumnType("integer");
b.Property<DateTime>("SentAtUtc")
.HasColumnType("timestamp with time zone");
b.Property<long>("TelegramChatId")
.HasColumnType("bigint");
b.Property<int>("TelegramMessageId")
.HasColumnType("integer");
b.Property<string>("Text")
.IsRequired()
.HasMaxLength(4000)
.HasColumnType("character varying(4000)");
b.Property<Guid>("UserId")
.HasColumnType("uuid");
b.Property<Guid?>("WorkoutId")
.HasColumnType("uuid");
b.HasKey("Id");
b.HasIndex("TelegramChatId", "TelegramMessageId")
.IsUnique();
b.HasIndex("UserId", "ParsingStatus");
b.ToTable("telegram_messages", (string)null);
});
modelBuilder.Entity("GymLogAI.Core.Entities.User", b =>
{
b.Property<Guid>("Id")
.HasColumnType("uuid");
b.Property<DateTime>("CreatedAtUtc")
.HasColumnType("timestamp with time zone");
b.Property<string>("FirstName")
.HasMaxLength(128)
.HasColumnType("character varying(128)");
b.Property<string>("LastName")
.HasMaxLength(128)
.HasColumnType("character varying(128)");
b.Property<long>("TelegramUserId")
.HasColumnType("bigint");
b.Property<string>("Username")
.HasMaxLength(128)
.HasColumnType("character varying(128)");
b.HasKey("Id");
b.HasIndex("TelegramUserId")
.IsUnique();
b.ToTable("users", (string)null);
});
modelBuilder.Entity("GymLogAI.Core.Entities.Workout", b =>
{
b.Property<Guid>("Id")
.HasColumnType("uuid");
b.Property<DateTime>("CreatedAtUtc")
.HasColumnType("timestamp with time zone");
b.Property<string>("Notes")
.HasMaxLength(4000)
.HasColumnType("character varying(4000)");
b.Property<DateTime>("PerformedAtUtc")
.HasColumnType("timestamp with time zone");
b.Property<Guid?>("SourceTelegramMessageId")
.HasColumnType("uuid");
b.Property<int>("SourceType")
.HasColumnType("integer");
b.Property<string>("Title")
.HasMaxLength(256)
.HasColumnType("character varying(256)");
b.Property<Guid>("UserId")
.HasColumnType("uuid");
b.HasKey("Id");
b.HasIndex("SourceTelegramMessageId")
.IsUnique();
b.HasIndex("UserId", "PerformedAtUtc");
b.ToTable("workouts", (string)null);
});
modelBuilder.Entity("GymLogAI.Core.Entities.WorkoutExercise", b =>
{
b.Property<Guid>("Id")
.HasColumnType("uuid");
b.Property<Guid>("ExerciseId")
.HasColumnType("uuid");
b.Property<string>("Notes")
.HasMaxLength(2000)
.HasColumnType("character varying(2000)");
b.Property<int>("Order")
.HasColumnType("integer");
b.Property<Guid>("WorkoutId")
.HasColumnType("uuid");
b.HasKey("Id");
b.HasIndex("ExerciseId");
b.HasIndex("WorkoutId", "Order")
.IsUnique();
b.ToTable("workout_exercises", (string)null);
});
modelBuilder.Entity("GymLogAI.Core.Entities.ExerciseSet", b =>
{
b.HasOne("GymLogAI.Core.Entities.WorkoutExercise", "WorkoutExercise")
.WithMany("Sets")
.HasForeignKey("WorkoutExerciseId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("WorkoutExercise");
});
modelBuilder.Entity("GymLogAI.Core.Entities.TelegramMessage", b =>
{
b.HasOne("GymLogAI.Core.Entities.User", "User")
.WithMany("TelegramMessages")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("User");
});
modelBuilder.Entity("GymLogAI.Core.Entities.Workout", b =>
{
b.HasOne("GymLogAI.Core.Entities.TelegramMessage", "SourceTelegramMessage")
.WithOne("Workout")
.HasForeignKey("GymLogAI.Core.Entities.Workout", "SourceTelegramMessageId")
.OnDelete(DeleteBehavior.SetNull);
b.HasOne("GymLogAI.Core.Entities.User", "User")
.WithMany("Workouts")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("SourceTelegramMessage");
b.Navigation("User");
});
modelBuilder.Entity("GymLogAI.Core.Entities.WorkoutExercise", b =>
{
b.HasOne("GymLogAI.Core.Entities.Exercise", "Exercise")
.WithMany("WorkoutExercises")
.HasForeignKey("ExerciseId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
b.HasOne("GymLogAI.Core.Entities.Workout", "Workout")
.WithMany("WorkoutExercises")
.HasForeignKey("WorkoutId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Exercise");
b.Navigation("Workout");
});
modelBuilder.Entity("GymLogAI.Core.Entities.Exercise", b =>
{
b.Navigation("WorkoutExercises");
});
modelBuilder.Entity("GymLogAI.Core.Entities.TelegramMessage", b =>
{
b.Navigation("Workout");
});
modelBuilder.Entity("GymLogAI.Core.Entities.User", b =>
{
b.Navigation("TelegramMessages");
b.Navigation("Workouts");
});
modelBuilder.Entity("GymLogAI.Core.Entities.Workout", b =>
{
b.Navigation("WorkoutExercises");
});
modelBuilder.Entity("GymLogAI.Core.Entities.WorkoutExercise", b =>
{
b.Navigation("Sets");
});
#pragma warning restore 612, 618
}
}
}
@@ -0,0 +1,27 @@
using GymLogAI.Application.Interfaces;
using GymLogAI.Core.Entities;
using Microsoft.EntityFrameworkCore;
namespace GymLogAI.Persistence.Repositories;
public sealed class ExerciseRepository(AppDbContext dbContext) : IExerciseRepository
{
public async Task<Exercise?> GetByNameAsync(string name, CancellationToken cancellationToken)
{
return await dbContext.Exercises.SingleOrDefaultAsync(x => x.Name == name, cancellationToken);
}
public async Task<Exercise> AddAsync(Exercise exercise, CancellationToken cancellationToken)
{
dbContext.Exercises.Add(exercise);
await dbContext.SaveChangesAsync(cancellationToken);
return exercise;
}
public async Task<Exercise> UpdateAsync(Exercise exercise, CancellationToken cancellationToken)
{
dbContext.Exercises.Update(exercise);
await dbContext.SaveChangesAsync(cancellationToken);
return exercise;
}
}
@@ -0,0 +1,37 @@
using GymLogAI.Application.Interfaces;
using GymLogAI.Core.Entities;
using GymLogAI.Core.Enums;
using Microsoft.EntityFrameworkCore;
namespace GymLogAI.Persistence.Repositories;
public sealed class TelegramMessageRepository(AppDbContext dbContext) : ITelegramMessageRepository
{
public async Task<TelegramMessage> AddAsync(TelegramMessage message, CancellationToken cancellationToken)
{
dbContext.TelegramMessages.Add(message);
await dbContext.SaveChangesAsync(cancellationToken);
return message;
}
public async Task<TelegramMessage?> GetByIdAsync(Guid id, CancellationToken cancellationToken)
{
return await dbContext.TelegramMessages.SingleOrDefaultAsync(x => x.Id == id, cancellationToken);
}
public async Task<IReadOnlyCollection<TelegramMessage>> GetPendingAsync(int take, CancellationToken cancellationToken)
{
return await dbContext.TelegramMessages
.Where(x => x.ParsingStatus == ParsingStatus.Pending)
.OrderBy(x => x.ImportedAtUtc)
.Take(take)
.ToListAsync(cancellationToken);
}
public async Task<TelegramMessage> UpdateAsync(TelegramMessage message, CancellationToken cancellationToken)
{
dbContext.TelegramMessages.Update(message);
await dbContext.SaveChangesAsync(cancellationToken);
return message;
}
}
@@ -0,0 +1,32 @@
using GymLogAI.Application.Interfaces;
using GymLogAI.Core.Entities;
using Microsoft.EntityFrameworkCore;
namespace GymLogAI.Persistence.Repositories;
public sealed class UserRepository(AppDbContext dbContext) : IUserRepository
{
public async Task<User?> GetByIdAsync(Guid id, CancellationToken cancellationToken)
{
return await dbContext.Users.SingleOrDefaultAsync(x => x.Id == id, cancellationToken);
}
public async Task<User?> GetByTelegramUserIdAsync(long telegramUserId, CancellationToken cancellationToken)
{
return await dbContext.Users.SingleOrDefaultAsync(x => x.TelegramUserId == telegramUserId, cancellationToken);
}
public async Task<User> AddAsync(User user, CancellationToken cancellationToken)
{
dbContext.Users.Add(user);
await dbContext.SaveChangesAsync(cancellationToken);
return user;
}
public async Task<User> UpdateAsync(User user, CancellationToken cancellationToken)
{
dbContext.Users.Update(user);
await dbContext.SaveChangesAsync(cancellationToken);
return user;
}
}
@@ -0,0 +1,28 @@
using GymLogAI.Application.Interfaces;
using GymLogAI.Core.Entities;
using Microsoft.EntityFrameworkCore;
namespace GymLogAI.Persistence.Repositories;
public sealed class WorkoutRepository(AppDbContext dbContext) : IWorkoutRepository
{
public async Task<Workout> AddAsync(Workout workout, CancellationToken cancellationToken)
{
dbContext.Workouts.Add(workout);
await dbContext.SaveChangesAsync(cancellationToken);
return workout;
}
public async Task<IReadOnlyCollection<Workout>> GetHistoryAsync(Guid userId, CancellationToken cancellationToken)
{
return await dbContext.Workouts
.AsNoTracking()
.Include(x => x.WorkoutExercises)
.ThenInclude(x => x.Exercise)
.Include(x => x.WorkoutExercises)
.ThenInclude(x => x.Sets)
.Where(x => x.UserId == userId)
.OrderByDescending(x => x.PerformedAtUtc)
.ToListAsync(cancellationToken);
}
}