Compare commits

..

2 Commits

Author SHA1 Message Date
lbenedar
0f096ff94b ch5.2 2026-03-05 11:16:46 +03:00
lbenedar
dceefea953 ch4.8 2026-03-04 17:25:25 +03:00
5 changed files with 96 additions and 4 deletions

View File

@@ -15,6 +15,12 @@ func (app *application) home(w http.ResponseWriter, r *http.Request) {
app.notFound(w)
return
}
snippets, err := app.snippets.Latest()
if err != nil {
app.serverError(w, err)
return
}
files := []string{
"./ui/html/base.tmpl",
"./ui/html/partials/nav.tmpl",
@@ -25,7 +31,10 @@ func (app *application) home(w http.ResponseWriter, r *http.Request) {
app.serverError(w, err)
return
}
err = ts.ExecuteTemplate(w, "base", nil)
data := &templateData{
Snippets: snippets,
}
err = ts.ExecuteTemplate(w, "base", data)
if err != nil {
app.serverError(w, err)
}
@@ -47,7 +56,25 @@ func (app *application) snippetView(w http.ResponseWriter, r *http.Request) {
}
return
}
fmt.Fprintf(w, "%+v", snippet)
files := []string{
"./ui/html/base.tmpl",
"./ui/html/partials/nav.tmpl",
"./ui/html/pages/view.tmpl",
}
ts, err := template.ParseFiles(files...)
if err != nil {
app.serverError(w, err)
return
}
data := &templateData{
Snippet: snippet,
}
err = ts.ExecuteTemplate(w, "base", data)
if err != nil {
app.serverError(w, err)
}
}
func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) {

10
cmd/web/templates.go Normal file
View File

@@ -0,0 +1,10 @@
package main
import (
"gitea.local.lab/Lbenedar/snippetbox/internal/models"
)
type templateData struct {
Snippet *models.Snippet
Snippets []*models.Snippet
}

View File

@@ -50,5 +50,25 @@ func (m *SnippetModel) Get(id int) (*Snippet, error) {
}
func (m *SnippetModel) Latest() ([]*Snippet, error) {
return nil, nil
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()
snippets := []*Snippet{}
for rows.Next() {
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
}

View File

@@ -2,5 +2,22 @@
{{define "main"}}
<h2>Latest Snippets</h2>
<p>There's nothing to see here yet!</p>
{{if .Snippets}}
<table>
<tr>
<th>Title</th>
<th>Created</th>
<th>ID</th>
</tr>
{{range .Snippets}}
<tr>
<td><a href='/snippet/view?id={{.ID}}'>{{.Title}}</a></td>
<td>{{.Created}}</td>
<td>#{{.ID}}</td>
</tr>
{{end}}
</table>
{{else}}
<p>There's nothing to see here... yet!</p>
{{end}}
{{end}}

18
ui/html/pages/view.tmpl Normal file
View File

@@ -0,0 +1,18 @@
{{define "title"}}Snippet ${{.Snippet.ID}}{{end}}
{{define "main"}}
{{with .Snippet}}
<div class='snippet'>
<div class='metadata'>
<strong>{{.Title}}</strong>
<span>#{{.ID}}</span>
</div>
<pre><code>{{.Content}}</code></pre>
<div class='metadata'>
<time>Created: {{.Created}}</time>
<time>Expires: {{.Expires}}</time>
</div>
</div>
{{end}}
{{end}}