feat: Implement SQLite Database Persistence with GORM#23
Open
frye wants to merge 1 commit into
Open
Conversation
Replace in-memory user slice with SQLite database using GORM ORM. - Add gorm.io/gorm and gorm.io/driver/sqlite dependencies - Create database package with InitDB and SeedDB functions - Update UserProfile model with GORM tags (uint primary key) - Add CreateUserRequest/UpdateUserRequest DTOs for safe input binding - Refactor controllers to struct-based UserController with DB injection - Implement DeleteUser endpoint (DELETE /api/v1/users/:id) - Add proper error handling: invalid ID -> 400, not found -> 404 - Use deterministic ordering (ORDER BY id) for list queries - Add 15 unit tests covering all CRUD operations and edge cases Closes #15 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replaces the in-memory user slice with a real SQLite database using GORM, ensuring data persistence across application restarts.
Closes #15
Changes
New files
database/database.go—InitDB(opens SQLite + auto-migrate) andSeedDB(populates default users if empty)database/testhelper.go— Shared in-memory SQLite helper for testsdatabase/database_test.go— Tests for InitDB, SeedDB, and seed idempotencycontrollers/user_controller_test.go— 12 tests covering all CRUD endpoints and edge casesModified files
models/user.go— Added GORMprimaryKeytag, changed ID fromstringtouint, addedCreateUserRequest/UpdateUserRequestDTOscontrollers/user_controller.go— Refactored to struct-basedUserControllerwith DB injection; all handlers now use GORM queries; implementedDeleteUserapi/routes.go— Accepts*gorm.DB, wires up controller methods, addedDELETE /:idroutemain.go— Initializes database, seeds data, passes DB to routergo.mod/go.sum— Addedgorm.io/gormandgorm.io/driver/sqliteTesting
15 tests pass covering: