learn-go/snippetbox/internal/models/snippets.go
tamsin johnson 569f2fc0a9 lets-go:4-x
2024-01-25 12:41:08 -08:00

87 lines
1.6 KiB
Go

package models
import (
"database/sql"
"errors"
"time"
)
type Snippet struct {
ID int
Title string
Content string
Created time.Time
Expires time.Time
}
type SnippetModel struct {
DB *sql.DB
}
// Insert ...
func (m *SnippetModel) Insert(title string, content string, expires int) (int, error) {
stmt := `INSERT INTO snippets (title, content, created, expires)
VALUES(?, ?, UTC_TIMESTAMP(), DATE_ADD(UTC_TIMESTAMP(), INTERVAL ? DAY))`
result, err := m.DB.Exec(stmt, title, content, expires)
if err != nil {
return 0, err
}
id, err := result.LastInsertId()
if err != nil {
return 0, err
}
return int(id), nil
}
// Get ...
func (m *SnippetModel) Get(id int) (Snippet, error) {
stmt := `SELECT id, title, content, created, expires FROM snippets
WHERE expires > UTC_TIMESTAMP() and id = ?`
var s Snippet
err := m.DB.QueryRow(stmt, id).Scan(&s.ID, &s.Title, &s.Content, &s.Created, &s.Expires)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return Snippet{}, ErrNoRecord
} else {
return Snippet{}, err
}
}
return s, nil
}
// Latest ...
func (m *SnippetModel) Latest() ([]Snippet, error) {
stmt := `SELECT id, title, content, created, expires FROM snippets
WHERE expires > UTC_TIMESTAMP() ORDER BY id DESC LIMIT 10`
rows, err := m.DB.Query(stmt)
if err != nil {
return nil, err
}
defer rows.Close()
var snippets []Snippet
for rows.Next() {
var s Snippet
err = rows.Scan(&s.ID, &s.Title, &s.Content, &s.Created, &s.Expires)
if err != nil {
return nil, err
}
snippets = append(snippets, s)
}
if err = rows.Err(); err != nil {
return nil, err
}
return snippets, nil
}