Skip to content

Commit

Permalink
add rand photo in gallery
Browse files Browse the repository at this point in the history
  • Loading branch information
sxc committed Jul 7, 2023
1 parent 18c4988 commit 37f8dec
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
33 changes: 33 additions & 0 deletions controllers/galleries.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controllers
import (
"errors"
"fmt"
"math/rand"
"net/http"
"strconv"

Expand All @@ -14,6 +15,7 @@ import (

type Galleries struct {
Templates struct {
Show Template
New Template
Edit Template
Index Template
Expand Down Expand Up @@ -128,3 +130,34 @@ func (g Galleries) Index(w http.ResponseWriter, r *http.Request) {
}
g.Templates.Index.Execute(w, r, data)
}

func (g Galleries) Show(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
http.Error(w, "Invalid ID", http.StatusNotFound)
return
}
gallery, err := g.GalleryService.ByID(id)
if err != nil {
if errors.Is(err, models.ErrNotFound) {
http.Error(w, "Gallery not found", http.StatusNotFound)
return
}
http.Error(w, "Something went wrong", http.StatusInternalServerError)
return
}
var data struct {
ID int
Title string
Images []string
}
data.ID = gallery.ID
data.Title = gallery.Title
for i := 0; i < 20; i++ {
w, h := rand.Intn(500)+200, rand.Intn(500)+200
catImageURL := fmt.Sprintf("https://placekitten.com/%d/%d", w, h)
data.Images = append(data.Images, catImageURL)
}

g.Templates.Show.Execute(w, r, data)
}
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ func main() {
"galleries/index.gohtml", "tailwind.gohtml",
))

galleriesC.Templates.Show = views.Must(views.ParseFS(
templates.FS,
"galleries/show.gohtml", "tailwind.gohtml",
))

umw := controllers.UserMiddleware{
SessionService: sessionService,
}
Expand Down Expand Up @@ -202,6 +207,7 @@ func main() {
})

r.Route("/galleries", func(r chi.Router) {
r.Get("/{id}", galleriesC.Show)
r.Group(func(r chi.Router) {
r.Use(umw.RequireUser)
r.Get("/", galleriesC.Index)
Expand Down
16 changes: 16 additions & 0 deletions templates/galleries/show.gohtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{{template "header" .}}
<div class="p-8 w-full">
<h1 class="p-4 pb-8 text-3xl font-bold text-gray-800">
{{.Title}}
</h1>
<div class="columns-4 gap-4 space-y-4">
{{range .Images}}
<div clas="h-min w-full">
<a href="{{.}}">
<img class="w-full" src="{{.}}">
</a>
</div>
{{end}}
</div>
</div>
{{template "footer" .}}
2 changes: 1 addition & 1 deletion templates/tailwind.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href= "https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
<script src="https://cdn.tailwindcss.com"></script>
<title>My Awesome Site</title>
</head>
<body class="min-h-screen bg-gray-100">
Expand Down

0 comments on commit 37f8dec

Please sign in to comment.