Move lab4 to lab5

This commit is contained in:
2026-11-16 19:54:38 +03:00
parent 276dbdad8a
commit cd84a6fd36
208 changed files with 9611 additions and 25 deletions
@@ -0,0 +1,30 @@
---
name: messenger-kotlin-exposed
description: Implement and review Kotlin repository, service, transaction, DI and library-DSL changes in this messenger project using its existing Exposed JDBC conventions. Use when editing persistence or its Kotlin callers, rather than for unrelated infrastructure or contract-only work.
---
# Messenger Kotlin and Exposed
Use the project's existing implementations to choose APIs, layout and transaction boundaries. Repository-root paths below identify live sources; inspect the relevant ones instead of copying snippets or rereading the whole application.
## Choose the smallest useful context
- For CRUD, start with the affected interface/model and `ExposedUserRepository` or `ExposedSessionRepository`.
- For multi-write operations or pagination, use `ExposedChatRepository` and its controller/test.
- For services/DI, use `DefaultChatService`, `RepositoriesModule` and `plugins/Koin.kt`.
- Read only the relevant section of [project patterns](references/project-patterns.md) for source links, transaction ownership, pinned dependencies and checks.
- The attachment/upload/storage implementations are not default style anchors merely because they are nearby; compare their changes with the established examples above.
## Implement in the project's idiom
- Follow the chosen analogue's multiline signatures and constructor parameters, named arguments, vertical query chains, small mapping functions and blank lines between logical stages.
- Keep Exposed **1.0.0** and its `org.jetbrains.exposed.v1.*` imports; recheck `gradle/libs.versions.toml` if dependencies have changed.
- Use the Table/column DSL for CRUD and expressions. Check `utils/exposed/operations` before introducing a separate SQL path.
- Before raw SQL, establish which required operation the pinned DSL and existing extensions cannot express; explain that limitation and parameterize values in the smallest fallback. Migration SQL is a separate, existing workflow.
- Follow the caller's transaction boundary. Ordinary repositories operate inside it so related writes commit/roll back together; reuse `plugins.suspendedTransaction` at the existing orchestration layer rather than wrapping each repository method.
- Use Kotlin library DSLs only where the dependency provides them. The project has Ktor/Koin/Exposed DSLs and the AWS SDK for Kotlin; builders in Java-based integrations can still be appropriate. Check the dependency and local wrapper before deciding.
- Preserve `@Single`, constructor injection, interface-based dependencies and existing Koin/KSP module discovery.
## Finish the change
Compare the complete diff with the chosen analogues, including other code affected by the user's applicable corrections. Run the relevant checks from the reference, scale them to the change, and state which ran. A passing compile alone does not establish query semantics or transaction atomicity.
@@ -0,0 +1,4 @@
interface:
display_name: "Messenger Kotlin/Exposed"
short_description: "Project Kotlin DSL, Exposed and transaction patterns"
default_prompt: "Use $messenger-kotlin-exposed to implement this change using the messenger project conventions."
@@ -0,0 +1,50 @@
# Project patterns
Read the section relevant to the change. Source links point to the repository's current files, not frozen examples.
## Dependencies and build
- [Version catalog](../../../../gradle/libs.versions.toml): Kotlin **2.3.20**, Exposed **1.0.0** (`core`, `jdbc`, `kotlin-datetime`, migration modules). Preserve `org.jetbrains.exposed.v1.*` imports and follow the affected model's types.
- [Settings](../../../../settings.gradle.kts): Ktor catalog **3.3.0**, AWS SDK for Kotlin catalog **1.6.41**. Do not substitute MinIO Java builders for the installed Kotlin SDK.
- [Build](../../../../build.gradle.kts): JVM **21**, Koin annotations/KSP with configuration checking, JUnit Platform. There is no configured ktlint/detekt task to assume or claim ran.
The catalogs/build files remain the source of truth if versions or plugins change. Check API details against those versions when the local examples do not establish an operation.
## CRUD, layout and query expressions
- [ExposedUserRepository](../../../../src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/user/ExposedUserRepository.kt): multiline function parameters, `insertIgnore`, `selectAll().where(...)` split into vertical chains, nullable results and `ResultRow` mapping.
- [ExposedSessionRepository](../../../../src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/ExposedSessionRepository.kt): `insert`, `deleteWhere`, typed time predicates and result mapping.
- [ExposedChatRepository](../../../../src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ExposedChatRepository.kt): related writes via `insert`/`batchInsert`, joins, keyset pagination and grouped member loading. Keep result ordering and transaction atomicity intact.
- [ExposedMessageIdempotencyRepository](../../../../src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/idempotency/ExposedMessageIdempotencyRepository.kt): conflict handling, `update`, `deleteWhere` and reusable typed conditions.
- [RowGreater](../../../../src/main/kotlin/com/mtusi/messenger/main/utils/exposed/operations/RowGreater.kt) and [InstantPlusSeconds](../../../../src/main/kotlin/com/mtusi/messenger/main/utils/exposed/operations/InstantPlusSeconds.kt): existing typed expression extensions. Inspect the relevant operation before implementing another SQL escape hatch.
- [ChatModel](../../../../src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/chat/ChatModel.kt): the model/table namespace used by queries. Read the affected feature's own model before changing its columns or mapping.
These are focused references, not a requirement to open every listed file on every task. Prefer User/Chat for established layout; newer attachment/storage code is not automatically the style authority.
## Transaction ownership
- [Database.kt](../../../../src/main/kotlin/com/mtusi/messenger/main/plugins/Database.kt) defines `suspendedTransaction` using `newSuspendedTransaction` on `Dispatchers.IO`. The database configuration sets `defaultMaxAttempts = 1`; do not silently introduce retries around side effects.
- [ChatController](../../../../src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/chat/ChatController.kt) and [UserController](../../../../src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/user/UserController.kt) open transactions around their service calls. [DefaultChatService](../../../../src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/DefaultChatService.kt) and ordinary Exposed repositories assume that active transaction.
- Authentication and attachment orchestration also own transactions in services. Inspect the actual caller of the method you change; do not impose a controller-only rule or create nested/per-operation boundaries.
- [ExposedChatRepositoryTest](../../../../src/test/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ExposedChatRepositoryTest.kt) demonstrates caller-owned transactions and rollback when a related write fails.
- Flyway migrations live in `src/main/resources/migrations`. Database.kt's `generateMigration` connects to the development database and applies existing migrations before generating SQL; it is not a harmless formatting/check command.
## DI and library API choice
- [RepositoriesModule](../../../../src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/RepositoriesModule.kt) and [ServicesModule](../../../../src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/ServicesModule.kt) use `@Module`/`@ComponentScan`.
- [Koin.kt](../../../../src/main/kotlin/com/mtusi/messenger/main/plugins/Koin.kt) installs the generated modules; concrete implementations use `@Single` and constructor dependencies. Reuse this flow instead of adding a parallel registry.
- Ktor routing, Koin modules, Exposed queries and the installed AWS Kotlin SDK expose Kotlin DSLs. Check the affected dependency's real API: Java integrations such as Hikari/Lettuce already use builders/configuration objects, so a blanket builder ban would conflict with the project.
## Focused verification
From the repository root, choose the checks that exercise the change:
```sh
./gradlew compileKotlin
./gradlew compileTestKotlin
./gradlew test --tests '*ExposedChatRepositoryTest'
```
Replace the test selector with the affected class; use `./gradlew test` when broader coverage is warranted. [TestApp](../../../../src/test/kotlin/com/mtusi/messenger/main/support/TestApp.kt) and [TestDatabase](../../../../src/test/kotlin/com/mtusi/messenger/main/support/TestDatabase.kt) show that API/database tests use Testcontainers and require Docker; those API helpers also start Redis and S3 test dependencies.
[Makefile](../../../../Makefile) provides `make check` for offline structure/contract checks and `make test` for smoke checks against a running API (`BASE_URL` defaults to `http://localhost:8080`). Use the existing Gradle wrapper; do not invent lint tasks or count an unavailable/skipped check as passing. Instruction-only changes do not need an application build or test suite.