54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
package shortcodes
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
|
|
"fr.latosa-escrima/core"
|
|
"fr.latosa-escrima/core/models"
|
|
"fr.latosa-escrima/utils"
|
|
)
|
|
|
|
func HandleShortcodes(w http.ResponseWriter, r *http.Request) {
|
|
var shortcodes []models.Shortcode
|
|
err := core.DB.NewSelect().
|
|
Model(&shortcodes).
|
|
Relation("Media").
|
|
Scan(context.Background())
|
|
if err != nil {
|
|
core.JSONError{
|
|
Status: core.Error,
|
|
Message: err.Error(),
|
|
}.Respond(w, http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
scheme := "http"
|
|
if r.TLS != nil || os.Getenv("ENVIRONMENT") != "DEV" { // Check if the request is over HTTPS
|
|
scheme = "https"
|
|
}
|
|
|
|
// Extract the host
|
|
host := r.Host
|
|
baseURL := fmt.Sprintf("%s://%s", scheme, host)
|
|
if os.Getenv("ENVIRONMENT") != "DEV" {
|
|
baseURL += "/api"
|
|
}
|
|
shortcodes = utils.Map(shortcodes, func(s models.Shortcode) models.Shortcode {
|
|
if s.MediaID == nil {
|
|
return s
|
|
}
|
|
s.Media.Author = nil
|
|
s.Media.URL = fmt.Sprintf("%s/media/%s/file", baseURL, s.MediaID)
|
|
return s
|
|
})
|
|
|
|
core.JSONSuccess{
|
|
Status: core.Success,
|
|
Message: "Shortcodes retrieved.",
|
|
Data: shortcodes,
|
|
}.Respond(w, http.StatusOK)
|
|
}
|