page_album_detail.go 1.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
package main

import (
	"net/http"
	"fmt"
	"path"
)

type AlbumDetailData struct {
	User   UserInfo
Janne Koschinski's avatar
Janne Koschinski committed
11
	Album  Album
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
	IsMine bool
}

func pageAlbumDetail(ctx PageContext) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		user := parseUser(r)

		_, albumId := path.Split(r.URL.Path)
		result, err := ctx.Database.Query(`
			SELECT
				id,
				owner,
				coalesce(title,  ''),
				coalesce(description, ''),
        		coalesce(created_at, to_timestamp(0))
			FROM albums
			WHERE id = $1
			`, albumId)
		if err != nil {
			panic(err)
		}

		var info Album
		if result.Next() {
			var owner string
			err := result.Scan(&info.Id, &owner, &info.Title, &info.Description, &info.CreatedAt)
			if err != nil {
				panic(err)
			}

			result, err := ctx.Database.Query(`
			SELECT
				image,
				title,
				description,
				position
			FROM album_images
			WHERE album = $1
			ORDER BY position ASC
			`, albumId)
			if err != nil {
				panic(err)
			}

			for result.Next() {
				var image AlbumImage
				err := result.Scan(&image.Id, &owner, &image.Title, &image.Description, &image.Position)
				if err != nil {
					panic(err)
				}

				info.Images = append(info.Images, image)
			}

			if err = formatTemplate(w, "album_detail.html", AlbumDetailData{
				user,
				info,
				owner == user.Id,
			}); err != nil {
				panic(err)
			}

			return
		}

		w.WriteHeader(http.StatusNotFound)
		fmt.Fprint(w, "Album not found")
	})
}