add hide function
This commit is contained in:
parent
f8e0b71840
commit
c9966a6f66
21
main.go
21
main.go
@ -69,16 +69,18 @@ func initDB() (*sql.DB, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- NEW: Safely add new columns for read status ---
|
// --- Safely add new columns for read status ---
|
||||||
addColumn(db, "books", "tuomas_read", "INTEGER DEFAULT 0")
|
addColumn(db, "books", "tuomas_read", "INTEGER DEFAULT 0")
|
||||||
addColumn(db, "books", "jenni_read", "INTEGER DEFAULT 0")
|
addColumn(db, "books", "jenni_read", "INTEGER DEFAULT 0")
|
||||||
// --- END NEW ---
|
|
||||||
|
// --- NEW: Add hidden column ---
|
||||||
|
addColumn(db, "books", "hidden", "INTEGER DEFAULT 0")
|
||||||
|
|
||||||
log.Println("Database initialized successfully.")
|
log.Println("Database initialized successfully.")
|
||||||
return db, nil
|
return db, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- NEW: Helper function to add a column only if it doesn't already exist. ---
|
// Helper function to add a column only if it doesn't already exist.
|
||||||
func addColumn(db *sql.DB, tableName, columnName, columnType string) {
|
func addColumn(db *sql.DB, tableName, columnName, columnType string) {
|
||||||
rows, err := db.Query(fmt.Sprintf("PRAGMA table_info(%s)", tableName))
|
rows, err := db.Query(fmt.Sprintf("PRAGMA table_info(%s)", tableName))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -109,8 +111,6 @@ func addColumn(db *sql.DB, tableName, columnName, columnType string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- END NEW ---
|
|
||||||
|
|
||||||
func startServer(db *sql.DB) {
|
func startServer(db *sql.DB) {
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
@ -128,7 +128,7 @@ func startServer(db *sql.DB) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// --- NEW: Endpoint to handle updating the read status ---
|
// Endpoint to handle updating the read status
|
||||||
r.POST("/books/:id/update", func(c *gin.Context) {
|
r.POST("/books/:id/update", func(c *gin.Context) {
|
||||||
idStr := c.Param("id")
|
idStr := c.Param("id")
|
||||||
var payload struct {
|
var payload struct {
|
||||||
@ -156,13 +156,12 @@ func startServer(db *sql.DB) {
|
|||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{"status": "success", "person": payload.Person, "read": payload.Read})
|
c.JSON(http.StatusOK, gin.H{"status": "success", "person": payload.Person, "read": payload.Read})
|
||||||
})
|
})
|
||||||
// --- END NEW ---
|
|
||||||
|
|
||||||
log.Println("Starting server on http://localhost:8080/books")
|
log.Println("Starting server on http://localhost:8080/books")
|
||||||
r.Run(":8080")
|
r.Run(":8080")
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- NEW: Function to update the read status in the database ---
|
// Function to update the read status in the database
|
||||||
func updateReadStatus(db *sql.DB, bookID int, person string, read bool) error {
|
func updateReadStatus(db *sql.DB, bookID int, person string, read bool) error {
|
||||||
var columnName string
|
var columnName string
|
||||||
switch strings.ToLower(person) {
|
switch strings.ToLower(person) {
|
||||||
@ -186,9 +185,6 @@ func updateReadStatus(db *sql.DB, bookID int, person string, read bool) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- END NEW ---
|
|
||||||
|
|
||||||
// Modified crawl_dua to not require the ticker argument
|
|
||||||
func crawl_dua(db *sql.DB) {
|
func crawl_dua(db *sql.DB) {
|
||||||
// The main page is a better starting point to find all books
|
// The main page is a better starting point to find all books
|
||||||
currenturl := "https://www.service95.com/book-club/"
|
currenturl := "https://www.service95.com/book-club/"
|
||||||
@ -317,8 +313,7 @@ func bookExists(db *sql.DB, name string) (bool, error) {
|
|||||||
|
|
||||||
// getAllBooks now retrieves the read status as well.
|
// getAllBooks now retrieves the read status as well.
|
||||||
func getAllBooks(db *sql.DB) ([]Book, error) {
|
func getAllBooks(db *sql.DB) ([]Book, error) {
|
||||||
// --- UPDATED: Select the new columns ---
|
query := "SELECT id, name, image_path, tuomas_read, jenni_read FROM books WHERE hidden = 0 ORDER BY id DESC"
|
||||||
query := "SELECT id, name, image_path, tuomas_read, jenni_read FROM books ORDER BY id DESC"
|
|
||||||
rows, err := db.Query(query)
|
rows, err := db.Query(query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user