-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3356668
commit 33560e4
Showing
12 changed files
with
178 additions
and
159 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package database | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
|
||
_ "modernc.org/sqlite" | ||
) | ||
|
||
func (db *DB) InsertURL(url, key string, userID int64, password string, qrCode string) error { | ||
stmt, err := db.Prepare("INSERT INTO urls (url, key, user_id, password, qr_code) VALUES (?, ?, ?, ?, ?)") | ||
if err != nil { | ||
return fmt.Errorf("error preparing statement: %w", err) | ||
} | ||
defer stmt.Close() | ||
|
||
_, err = stmt.Exec(url, key, userID, password, qrCode) // Add qrCode as an argument | ||
if err != nil { | ||
return fmt.Errorf("error inserting URL: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (db *DB) GetURL(key string) (*URL, error) { | ||
var url URL | ||
err := db.QueryRow("SELECT id, user_id, url, key, created_at, clicks, password FROM urls WHERE key = ?", key).Scan(&url.ID, &url.UserID, &url.URL, &url.Key, &url.CreatedAt, &url.Clicks, &url.Password) | ||
if err != nil { | ||
if err == sql.ErrNoRows { | ||
return nil, fmt.Errorf("no URL found for key: %s", key) | ||
} | ||
return nil, fmt.Errorf("error querying URL: %w", err) | ||
} | ||
return &url, nil | ||
} | ||
|
||
func (db *DB) GetURLsByUserID(userID int64) ([]URL, error) { | ||
rows, err := db.Query("SELECT id, user_id, url, key, created_at, clicks, password FROM urls WHERE user_id = ?", userID) | ||
if err != nil { | ||
return nil, fmt.Errorf("error querying URLs: %w", err) | ||
} | ||
defer rows.Close() | ||
|
||
var urls []URL | ||
for rows.Next() { | ||
var url URL | ||
if err := rows.Scan(&url.ID, &url.UserID, &url.URL, &url.Key, &url.CreatedAt, &url.Clicks, &url.Password); err != nil { | ||
return nil, fmt.Errorf("error scanning row: %w", err) | ||
} | ||
urls = append(urls, url) | ||
} | ||
|
||
if err := rows.Err(); err != nil { | ||
return nil, fmt.Errorf("error iterating rows: %w", err) | ||
} | ||
|
||
return urls, nil | ||
} | ||
|
||
func (db *DB) IncrementClicks(urlID int64) error { | ||
_, err := db.Exec("UPDATE urls SET clicks = clicks + 1 WHERE id = ?", urlID) | ||
if err != nil { | ||
return fmt.Errorf("error incrementing clicks: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func (db *DB) UpdateURL(id int64, url string, password string) error { | ||
_, err := db.Exec("UPDATE urls SET url = ?, password = ? WHERE id = ?", url, password, id) | ||
if err != nil { | ||
return fmt.Errorf("error updating URL: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func (db *DB) DeleteURL(id int64) error { | ||
_, err := db.Exec("DELETE FROM urls WHERE id = ?", id) | ||
if err != nil { | ||
return fmt.Errorf("error deleting URL: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func (db *DB) GetURLByID(id int64) (*URL, error) { | ||
var url URL | ||
err := db.QueryRow("SELECT id, user_id, url, key, created_at, clicks, password, qr_code FROM urls WHERE id = ?", id).Scan( | ||
&url.ID, &url.UserID, &url.URL, &url.Key, &url.CreatedAt, &url.Clicks, &url.Password, &url.QRCode) | ||
if err != nil { | ||
if err == sql.ErrNoRows { | ||
return nil, fmt.Errorf("no URL found for id: %d", id) | ||
} | ||
return nil, fmt.Errorf("error querying URL: %w", err) | ||
} | ||
return &url, nil | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package database | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
|
||
_ "modernc.org/sqlite" | ||
) | ||
|
||
func (db *DB) CreateUser(username, email, password string) (*User, error) { | ||
stmt, err := db.Prepare("INSERT INTO users (username, email, password) VALUES (?, ?, ?)") | ||
if err != nil { | ||
return nil, fmt.Errorf("error preparing statement: %w", err) | ||
} | ||
defer stmt.Close() | ||
|
||
result, err := stmt.Exec(username, email, password) | ||
if err != nil { | ||
return nil, fmt.Errorf("error inserting user: %w", err) | ||
} | ||
|
||
id, err := result.LastInsertId() | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting last insert ID: %w", err) | ||
} | ||
|
||
return &User{ID: id, Username: username, Email: email, Password: password}, nil | ||
} | ||
|
||
func (db *DB) GetUserByUsername(username string) (*User, error) { | ||
var user User | ||
err := db.QueryRow("SELECT id, username, email, password FROM users WHERE username = ?", username).Scan(&user.ID, &user.Username, &user.Email, &user.Password) | ||
if err != nil { | ||
if err == sql.ErrNoRows { | ||
return nil, nil | ||
} | ||
return nil, fmt.Errorf("error querying user: %w", err) | ||
} | ||
return &user, nil | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.