Initial commit

This commit is contained in:
2019-03-05 00:07:59 +09:00
commit e5d13f19ad
14 changed files with 15042 additions and 0 deletions
+18
View File
@@ -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
}
+42
View File
@@ -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
}