You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.1 KiB
Go

1 year ago
package models
import (
"database/sql"
"errors"
1 year ago
"time"
)
type Snippet struct {
ID int
Title string
Content string
Created time.Time
Expires time.Time
}
type SnippetModel struct {
DB *sql.DB
}
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
1 year ago
}
func (m *SnippetModel) Get(id int) (*Snippet, error) {
stmt := `SELECT id, title, content, created, expires FROM snippets WHERE expires > UTC_TIMESTAMP() and id = ?`
row := m.DB.QueryRow(stmt, id)
s := &Snippet{}
err := row.Scan(&s.ID, &s.Title, &s.Content, &s.Created, &s.Expires)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrNoRecord
} else {
return nil, err
}
}
return s, nil
1 year ago
}
func (m *SnippetModel) Latest() ([]*Snippet, error) {
return nil, nil
}