Initial commit
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
// import sqlite driver
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
// InitDB returns sqlite3 connection
|
||||
func InitDB(dataSource string) (*sql.DB, error) {
|
||||
db, err := sql.Open("sqlite3", dataSource)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return db, nil
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
// Post is the main data
|
||||
type Post struct {
|
||||
Title string
|
||||
Subtitle string
|
||||
Description string
|
||||
Media string
|
||||
}
|
||||
|
||||
// GetPost returns Post with the specified uuid
|
||||
func GetPost(db *sql.DB, uuid string) (*Post, error) {
|
||||
rows, err := db.Query("SELECT title, subtitle, description, media FROM data WHERE uuid = ?", uuid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var post Post
|
||||
for rows.Next() {
|
||||
if err = rows.Scan(&post.Title, &post.Subtitle, &post.Description, &post.Media); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &post, nil
|
||||
}
|
||||
|
||||
// UpdatePost update Post record with the matching uuid
|
||||
func UpdatePost(db *sql.DB, post Post, uuid string) error {
|
||||
stmt := "UPDATE data SET title = ?, subtitle = ?, description = ?, media = $5 WHERE uuid = ?"
|
||||
_, err := db.Exec(stmt, post.Title, post.Subtitle, post.Description, post.Media, uuid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user