mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-07 16:34:26 +00:00
Rework db schema
This commit is contained in:
@@ -5,27 +5,65 @@ import (
|
||||
)
|
||||
|
||||
func (db *DB) CreateUser(user *models.User) error {
|
||||
_, err := db.Exec("INSERT INTO users (username, email, password_hash) VALUES (?, ?, ?)",
|
||||
user.Username, user.Email, user.PasswordHash)
|
||||
_, err := db.Exec(`
|
||||
INSERT INTO users (email, display_name, password_hash)
|
||||
VALUES (?, ?, ?)`,
|
||||
user.Email, user.DisplayName, user.PasswordHash)
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *DB) GetUserByID(id int) (*models.User, error) {
|
||||
user := &models.User{}
|
||||
err := db.QueryRow("SELECT id, username, email, created_at FROM users WHERE id = ?", id).
|
||||
Scan(&user.ID, &user.Username, &user.Email, &user.CreatedAt)
|
||||
err := db.QueryRow(`
|
||||
SELECT id, email, display_name, created_at, last_workspace_id, last_opened_file_path
|
||||
FROM users WHERE id = ?`, id).
|
||||
Scan(&user.ID, &user.Email, &user.DisplayName, &user.CreatedAt,
|
||||
&user.LastWorkspaceID, &user.LastOpenedFilePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (db *DB) GetUserByUsername(username string) (*models.User, error) {
|
||||
func (db *DB) GetUserByEmail(email string) (*models.User, error) {
|
||||
user := &models.User{}
|
||||
err := db.QueryRow("SELECT id, username, email, password_hash, created_at FROM users WHERE username = ?", username).
|
||||
Scan(&user.ID, &user.Username, &user.Email, &user.PasswordHash, &user.CreatedAt)
|
||||
err := db.QueryRow(`
|
||||
SELECT id, email, display_name, password_hash, created_at, last_workspace_id, last_opened_file_path
|
||||
FROM users WHERE email = ?`, email).
|
||||
Scan(&user.ID, &user.Email, &user.DisplayName, &user.PasswordHash, &user.CreatedAt,
|
||||
&user.LastWorkspaceID, &user.LastOpenedFilePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (db *DB) UpdateUser(user *models.User) error {
|
||||
_, err := db.Exec(`
|
||||
UPDATE users
|
||||
SET email = ?, display_name = ?, last_workspace_id = ?, last_opened_file_path = ?
|
||||
WHERE id = ?`,
|
||||
user.Email, user.DisplayName, user.LastWorkspaceID, user.LastOpenedFilePath, user.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *DB) UpdateLastWorkspace(userID, workspaceID int) error {
|
||||
_, err := db.Exec("UPDATE users SET last_workspace_id = ? WHERE id = ?", workspaceID, userID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *DB) UpdateLastOpenedFile(userID int, filePath string) error {
|
||||
_, err := db.Exec("UPDATE users SET last_opened_file_path = ? WHERE id = ?", filePath, userID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *DB) DeleteUser(id int) error {
|
||||
_, err := db.Exec("DELETE FROM users WHERE id = ?", id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *DB) GetLastWorkspaceID(userID int) (int, error) {
|
||||
var workspaceID int
|
||||
err := db.QueryRow("SELECT last_workspace_id FROM users WHERE id = ?", userID).Scan(&workspaceID)
|
||||
return workspaceID, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user