working the get statement
This commit is contained in:
@@ -1,10 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"gitea.wagshome.duckdns.org/nathan/golearn/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (app *application) home(w http.ResponseWriter, r *http.Request) {
|
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)
|
app.notFound(w)
|
||||||
return
|
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) {
|
func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != "POST" {
|
if r.Method != "POST" {
|
||||||
@@ -42,5 +54,13 @@ func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) {
|
|||||||
app.clientError(w, http.StatusMethodNotAllowed)
|
app.clientError(w, http.StatusMethodNotAllowed)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.Write([]byte("Create a new snippet"))
|
title := "0 Snail"
|
||||||
|
content := "O snail\nClimb Mount Fuji,\nBut slowly, slowly!\n\n– Kobayashi Issa"
|
||||||
|
expires := 7
|
||||||
|
id, err := app.snippets.Insert(title, content, expires)
|
||||||
|
if err != nil {
|
||||||
|
app.serverError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
http.Redirect(w, r, fmt.Sprintf("/snippet/view?id=%d", id), http.StatusSeeOther)
|
||||||
}
|
}
|
||||||
|
|||||||
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 (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -18,11 +19,31 @@ type SnippetModel struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *SnippetModel) Insert(title string, content string, expires int) (int, error) {
|
func (m *SnippetModel) Insert(title string, content string, expires int) (int, error) {
|
||||||
return 0, nil
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *SnippetModel) Get(id int) (*Snippet, error) {
|
func (m *SnippetModel) Get(id int) (*Snippet, error) {
|
||||||
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) {
|
func (m *SnippetModel) Latest() ([]*Snippet, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user