Compare commits
2 Commits
5f375f654a
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 9645f10353 | |||
| dacffde1f8 |
@@ -1,10 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"gitea.wagshome.duckdns.org/nathan/golearn/internal/models"
|
||||
)
|
||||
|
||||
func (app *application) home(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -34,7 +37,16 @@ func (app *application) snippetView(w http.ResponseWriter, r *http.Request) {
|
||||
app.notFound(w)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(w, "Display a specific snippet with ID %d...", id)
|
||||
snippet, err := app.snippets.Get(id)
|
||||
if err != nil {
|
||||
if errors.Is(err, models.ErrNoRecord) {
|
||||
app.notFound(w)
|
||||
} else {
|
||||
app.serverError(w, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(w, "%+v", snippet)
|
||||
}
|
||||
func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "POST" {
|
||||
|
||||
7
snippetbox/internal/models/errors.go
Normal file
7
snippetbox/internal/models/errors.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
var ErrNoRecord = errors.New("models: no matchin record found")
|
||||
@@ -2,6 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -31,8 +32,19 @@ func (m *SnippetModel) Insert(title string, content string, expires int) (int, e
|
||||
}
|
||||
|
||||
func (m *SnippetModel) Get(id int) (*Snippet, error) {
|
||||
stmt := `SELECT`
|
||||
return nil, nil
|
||||
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
|
||||
|
||||
}
|
||||
|
||||
func (m *SnippetModel) Latest() ([]*Snippet, error) {
|
||||
|
||||
Reference in New Issue
Block a user