-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add logic to fetch some basic sitemap data
- Loading branch information
Showing
1 changed file
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |