Skip to content

Commit

Permalink
feat: add /galleryall endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
paoloose committed Nov 28, 2024
1 parent 6b4922d commit 88be509
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
80 changes: 80 additions & 0 deletions stiller-backend/internal/handlers/get_all_gallery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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 GetAllGallery(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

new_dbconn, dbconn_err := dbutils.NewConn()
if loggers.RequestLog(dbconn_err, "", http.StatusInternalServerError, &w) {
return
}

defer dbutils.CloseConn(new_dbconn)

get_galleries := sqlf.
Select("*").
From("gallery")

galleries := make([]dbutils.StillerGallery, 0, 2)
exec_err := sqlitex.ExecuteTransient(new_dbconn, get_galleries.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,
}

galleries = append(galleries, gallery)
return nil
},

Args: get_galleries.Args(),
})

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

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

2 changes: 1 addition & 1 deletion stiller-backend/internal/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var routes = [...]individualHandler{
{
path: "/galleryall",
method: http.MethodGet,
handlefunc: handlers.GetGallery,
handlefunc: handlers.GetAllGallery,
},
{
path: "/gallery/new",
Expand Down

0 comments on commit 88be509

Please sign in to comment.