Skip to content

Commit

Permalink
feat: Add logic to fetch some basic sitemap data
Browse files Browse the repository at this point in the history
  • Loading branch information
Dobefu committed Dec 26, 2024
1 parent ed2528a commit d42adee
Showing 1 changed file with 85 additions and 1 deletion.
86 changes: 85 additions & 1 deletion cmd/server/routes/v1/get-sitemap-data.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,94 @@
package v1

import (
"encoding/json"
"fmt"
"net/http"

api_structs "github.com/Dobefu/csb/cmd/api/structs"
api_utils "github.com/Dobefu/csb/cmd/api/utils"
"github.com/Dobefu/csb/cmd/cs_sdk/structs"
"github.com/Dobefu/csb/cmd/database/query"
db_structs "github.com/Dobefu/csb/cmd/database/structs"
"github.com/Dobefu/csb/cmd/server/utils"
)

func GetSitemapData(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "{}")
sitemapData, err := getEntries()

if err != nil {
utils.PrintError(w, err, false)
return
}

output := utils.ConstructOutput()
output["data"] = sitemapData

json, err := json.Marshal(output)

if err != nil {
utils.PrintError(w, err, true)
return
}

fmt.Fprint(w, string(json))
}

func getEntries() (map[string]interface{}, error) {
rows, err := query.QueryRows("routes",
[]string{"*"},
[]db_structs.QueryWhere{
{
Name: "locale",
Value: "en",
},
},
)

if err != nil {
return nil, err
}

entries := map[string]interface{}{}

for rows.Next() {
var result structs.Route

err = rows.Scan(
&result.Id,
&result.Uid,
&result.Title,
&result.ContentType,
&result.Locale,
&result.Slug,
&result.Url,
&result.Parent,
&result.ExcludeSitemap,
&result.Published,
)

if err != nil {
return entries, err
}

altLocales, err := api_utils.GetAltLocales(result)

if err != nil {
return entries, err
}

entries[result.Uid] = struct {
Uid string `json:"uid"`
Locale string `json:"locale"`
Url string `json:"url"`
AltLocales []api_structs.AltLocale `json:"alt_locales"`
}{
Uid: result.Uid,
Locale: result.Locale,
Url: result.Url,
AltLocales: altLocales,
}
}

return entries, nil
}

0 comments on commit d42adee

Please sign in to comment.