notatio/database.go

132 lines
4.2 KiB
Go
Raw Normal View History

2023-11-21 04:10:40 +00:00
package main
import (
"fmt"
"path/filepath"
"strings"
"time"
)
// insertFileIntoDatabase inserts a file into the database with the provided details.
func insertFileIntoDatabase(username, filename string, creationTime int64, lastEdited int64, lastOpened int64) error {
// Retrieve the user ID based on the username.
userID, err := getUserUUID(username)
if err != nil {
return err
}
// Insert the file details into the database.
_, err = db.Exec("INSERT INTO files (user_id, filename, creation_time, last_edited, last_opened) VALUES ($1, $2, $3, $4, $5)",
userID, filename, time.Unix(creationTime, 0), time.Unix(lastEdited, 0), time.Unix(lastOpened, 0))
return err
}
// insertUserIntoDatabase inserts a user into the database with the provided details.
func insertUserIntoDatabase(username, hashedPassword string, accountType string, userUUID string, name string, email string) error {
// Insert the user details into the database.
_, err := db.Exec("INSERT INTO users (username, password, accountType, uuid, name, email) VALUES ($1, $2, $3, $4, $5, $6)", username, hashedPassword, accountType, userUUID, name, email)
return err
}
// deleteFileFromDatabase deletes a file from the database for the specified user and filename.
func deleteFileFromDatabase(username, filename string) error {
// Retrieve the user UUID based on the username.
userUUID, err := getUserUUID(username)
if err != nil {
return err
}
// Delete the file from the database.
_, err = db.Exec("DELETE FROM files WHERE user_id = $1 AND filename = $2", userUUID, filename)
return err
}
// UpdateEditedTimestamp updates the last_edited timestamp for a file in the database.
func UpdateEditedTimestamp(username, filename string) error {
// Retrieve the user UUID based on the username.
userUUID, err := getUserUUID(username)
if err != nil {
return err
}
// Update the last_edited timestamp for the file.
_, err = db.Exec("UPDATE files SET last_edited = $1 WHERE user_id = $2 AND filename = $3", time.Now(), userUUID, filename)
return err
}
// getName retrieves the name of a user based on their username.
func getName(username string) (string, error) {
// Retrieve the user UUID based on the username.
userUUID, err := getUserUUID(username)
if err != nil {
return "", err
}
// Retrieve the name of the user from the database.
var name string
err = db.QueryRow("SELECT name FROM users WHERE uuid = $1", userUUID).Scan(&name)
if err != nil {
return "", err
}
return name, nil
}
// updateFilename updates the filename for a file in the database.
func updateFilename(username, oldFilename, newFilename string) error {
// Retrieve the user UUID based on the username.
userUUID, err := getUserUUID(username)
if err != nil {
return err
}
// Check if the new filename already exists.
var count int
err = db.QueryRow("SELECT COUNT(*) FROM files WHERE user_id = $1 AND filename = $2", userUUID, newFilename).Scan(&count)
if err != nil {
return err
}
// If a file with the new filename already exists, append a number to the end.
if count > 0 {
countSuffix := 1
updatedFilename := newFilename
extension := filepath.Ext(newFilename)
filenameWithoutExt := strings.TrimSuffix(newFilename, extension)
// Keep incrementing the countSuffix until a unique filename is found.
for count > 0 {
updatedFilename = fmt.Sprintf("%s_%d%s", filenameWithoutExt, countSuffix, extension)
// Check if the updatedFilename already exists.
err = db.QueryRow("SELECT COUNT(*) FROM files WHERE user_id = $1 AND filename = $2", userUUID, updatedFilename).Scan(&count)
if err != nil {
return err
}
countSuffix++
}
newFilename = updatedFilename
}
// Update the filename in the database.
_, err = db.Exec("UPDATE files SET filename = $1 WHERE user_id = $2 AND filename = $3", newFilename, userUUID, oldFilename)
return err
}
// isUsernameTaken checks if a username is already taken.
func isUsernameTaken(username string) (bool, error) {
username = strings.ToLower(username)
userUUID, err := getUserUUID(username)
if err != nil {
// Username is not taken.
fmt.Println("Username not taken:", username)
return false, nil
}
// Username is taken.
fmt.Println("Username taken by user with UUID:", userUUID)
return true, nil
}