photlog/models/posts.go

43 lines
959 B
Go

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
}