Skip to content

Commit

Permalink
add endpoint to get gallery by slug
Browse files Browse the repository at this point in the history
  • Loading branch information
paoloose committed Nov 29, 2024
1 parent 831feed commit 75d756f
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
83 changes: 83 additions & 0 deletions stiller-backend/internal/handlers/get_gallery_detail.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package handlers

import (
"net/http"
"stiller/pkg/dbutils"
"stiller/pkg/jwt"
"stiller/pkg/loggers"
"stiller/pkg/netwrappers"

jsonexp "github.com/go-json-experiment/json"
"github.com/julienschmidt/httprouter"
"github.com/leporo/sqlf"
"zombiezen.com/go/sqlite"
"zombiezen.com/go/sqlite/sqlitex"
)

func GetGalleryDetail(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
if netwrappers.CORS(w, r) {
return
}

type ResPayload []dbutils.StillerGallery

user_token := r.Header.Get("token")
user_tk, token_decode_err := jwt.Decode(user_token)
if loggers.RequestLog(token_decode_err, "", http.StatusUnauthorized, &w) {
return
}

user_id := user_tk.UserId
gallery_slug := params.ByName("slug")

new_dbconn, dbconn_err := dbutils.NewConn()
defer dbutils.CloseConn(new_dbconn)

if loggers.RequestLog(dbconn_err, "", http.StatusInternalServerError, &w) {
return
}


get_gallery := sqlf.
Select("*").
From("gallery").
Where("owner = ? AND slug = ?", user_id, gallery_slug).
Limit(1)

gallery := dbutils.StillerGallery{}

exec_err := sqlitex.ExecuteTransient(new_dbconn, get_gallery.String(), &sqlitex.ExecOptions{
ResultFunc: func(stmt *sqlite.Stmt) error {
gall_template := int(stmt.GetInt64("template"))

gall_id := int(stmt.GetInt64("id"))
gallery_data, slots_err := dbutils.GetGalleryData(gall_id)
if slots_err != nil {
return slots_err
}

gallery = dbutils.StillerGallery{
Id: gall_id,
OwnerId: user_id,
Title: stmt.GetText("title"),
Description: stmt.GetText("description"),
TemplateId: gall_template,
Slug: stmt.GetText("slug"),
Data: *gallery_data,
}
return nil
},

Args: get_gallery.Args(),
})

if loggers.RequestLog(exec_err, "", http.StatusInternalServerError, &w) {
return
}

writing_err := jsonexp.MarshalWrite(w, gallery, jsonexp.DefaultOptionsV2())
if loggers.RequestLog(writing_err, "", http.StatusInternalServerError, &w) {
return
}
}

5 changes: 5 additions & 0 deletions stiller-backend/internal/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ var routes = [...]individualHandler{
method: http.MethodGet,
handlefunc: handlers.GetGallery,
},
{
path: "/gallery/:slug",
method: http.MethodGet,
handlefunc: handlers.GetGallery,
},
{
path: "/galleryall",
method: http.MethodGet,
Expand Down

0 comments on commit 75d756f

Please sign in to comment.