Day 9 content, connecting to database
All checks were successful
Build and copy to prod / build-and-copy (push) Successful in 1m31s
All checks were successful
Build and copy to prod / build-and-copy (push) Successful in 1m31s
This commit is contained in:
parent
d3ee10669e
commit
49d33b2366
8
data/database.go
Normal file
8
data/database.go
Normal file
@ -0,0 +1,8 @@
|
||||
package data
|
||||
|
||||
import "database/sql"
|
||||
|
||||
func Connect(file string) *sql.DB {
|
||||
db, _ := sql.Open("sqlite3", file)
|
||||
return db
|
||||
}
|
2
go.mod
2
go.mod
@ -1,3 +1,5 @@
|
||||
module lewisdale.dev/oopsie
|
||||
|
||||
go 1.22.2
|
||||
|
||||
require github.com/mattn/go-sqlite3 v1.14.22 // indirect
|
||||
|
2
go.sum
Normal file
2
go.sum
Normal file
@ -0,0 +1,2 @@
|
||||
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
|
||||
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
20
main.go
20
main.go
@ -2,14 +2,34 @@ package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"lewisdale.dev/oopsie/data"
|
||||
"lewisdale.dev/oopsie/sites"
|
||||
)
|
||||
|
||||
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 main() {
|
||||
db := data.Connect("test.sqlite3")
|
||||
|
||||
sites.CreateTable(db)
|
||||
|
||||
site := sites.Site{Name: "Lewisdale.dev", Url: "https://lewisdale.dev"}
|
||||
site.Save(db)
|
||||
|
||||
http.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("This was a GET request!"))
|
||||
})
|
||||
|
||||
http.HandleFunc("POST /", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("This was a POST request!"))
|
||||
})
|
||||
|
||||
http.ListenAndServe(":8000", nil)
|
||||
}
|
||||
|
41
sites/sites.go
Normal file
41
sites/sites.go
Normal file
@ -0,0 +1,41 @@
|
||||
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)
|
||||
}
|
||||
}
|
BIN
test.sqlite3
Normal file
BIN
test.sqlite3
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user