Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
add gallery image
  • Loading branch information
sxc committed Jul 17, 2023
2 parents c927276 + 4456251 commit 4f3bb57
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 17 deletions.
70 changes: 58 additions & 12 deletions controllers/galleries.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package controllers
import (
"errors"
"fmt"
"math/rand"
"net/http"
"strconv"

"github.com/go-chi/chi/v5"
// "github.com/go-chi/chi"
"github.com/sxc/aerialcamp/context"
"github.com/sxc/aerialcamp/models"
)
Expand Down Expand Up @@ -54,10 +52,6 @@ func (g Galleries) Edit(w http.ResponseWriter, r *http.Request) {
return
}

// err = userMustOwnGallery(w, r, gallery)
// if err != nil {
// return
// }
var data struct {
ID int
Title string
Expand Down Expand Up @@ -114,18 +108,28 @@ func (g Galleries) Show(w http.ResponseWriter, r *http.Request) {
if err != nil {
return
}

type Image struct {
GalleryID int
Filename string
}
var data struct {
ID int
Title string
Images []string
Images []Image
}
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)
images, err := g.GalleryService.Images(gallery.ID)
if err != nil {
fmt.Println(err)
http.Error(w, "Something went wrong", http.StatusInternalServerError)
return
}
for _, image := range images {
data.Images = append(data.Images, Image{
GalleryID: image.GalleryID,
Filename: image.Filename,
})
}
g.Templates.Show.Execute(w, r, data)
}
Expand All @@ -143,6 +147,48 @@ func (g Galleries) Delete(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/galleries", http.StatusFound)
}

// func (g Galleries) Image(w http.ResponseWriter, r *http.Request) {
// gallery, err := g.galleryByID(w, r)
// if err != nil {
// return
// }
// image, err := g.GalleryService.Image(gallery.ID)
// if err != nil {
// http.Error(w, "Something went wrong", http.StatusInternalServerError)
// return
// }
// http.ServeFile(w, r, image.Path)
// }

func (g Galleries) Image(w http.ResponseWriter, r *http.Request) {
filename := chi.URLParam(r, "filename")
galleryID, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
http.Error(w, "Invalid ID", http.StatusNotFound)
return
}
images, err := g.GalleryService.Images(galleryID)
if err != nil {
fmt.Println(err)
http.Error(w, "Something went wrong", http.StatusInternalServerError)
return
}
var requestdImage models.Image
imageFound := false
for _, image := range images {
if image.Filename == filename {
requestdImage = image
imageFound = true
break
}
}
if !imageFound {
http.Error(w, "Image not found", http.StatusNotFound)
return
}
http.ServeFile(w, r, requestdImage.Path)
}

type galleryOpt func(http.ResponseWriter, *http.Request, *models.Gallery) error

func (g Galleries) galleryByID(w http.ResponseWriter, r *http.Request, opts ...galleryOpt) (*models.Gallery, error) {
Expand Down
8 changes: 6 additions & 2 deletions models/gallery.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ type Gallery struct {
}

type Image struct {
Path string
GalleryID int
Path string
Filename string
}

type GalleryService struct {
Expand Down Expand Up @@ -117,7 +119,9 @@ func (service *GalleryService) Images(galleryID int) ([]Image, error) {
if hasExtension(file, service.extensions()) {
// imagePaths = append(imagePaths, file)
images = append(images, Image{
Path: file,
GalleryID: galleryID,
Path: file,
Filename: filepath.Base(file),
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions templates/galleries/show.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<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 href="/galleries/{{.GalleryID}}/images/{{.Filename}}">
<img class="w-full" src="/galleries/{{.GalleryID}}/images/{{.Filename}}">
</a>
</div>
{{end}}
Expand Down
3 changes: 2 additions & 1 deletion templates/signup.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
<form action="/users" method="post">
<div class="hidden">
{{/* {{csrfField}} */}}
{{.CSRFField}}
{{/* {{.CSRFField}} */}}
{{csrfField}}
</div>
<div class="py-2">
<label for="email" class="text-sm fotn-semibold text-gray-800">Email Address</label>
Expand Down

0 comments on commit 4f3bb57

Please sign in to comment.