oopsie/sites/sites.go
Lewis Dale 49d33b2366
All checks were successful
Build and copy to prod / build-and-copy (push) Successful in 1m31s
Day 9 content, connecting to database
2024-05-08 09:00:58 +01:00

42 lines
724 B
Go

package sites
import (
"database/sql"
"fmt"
)
const createSitesTable = `CREATE TABLE IF NOT EXISTS sites (
id INTEGER NOT NULL PRIMARY KEY,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
name TEXT NOT NULL,
url TEXT NOT NULL
);`
func CreateTable(db *sql.DB) {
if _, err := db.Exec(createSitesTable); err != nil {
fmt.Println(fmt.Errorf(err.Error()))
}
}
type Site struct {
id uint64
created_at uint64
Name string
Url string
}
func (s *Site) Save(db *sql.DB) {
if s.id != 0 {
query := `UPDATE SITES
SET
name=?,
url=?
WHERE id =?
`
db.Exec(query, s.Name, s.Url, s.id)
} else {
query := `INSERT INTO SITES (name, url) VALUES (?, ?)`
db.Exec(query, s.Name, s.Url)
}
}