Files
latosa-escrima/backend/api/shortcodes/shortcode.go
2025-02-10 08:52:32 +01:00

46 lines
962 B
Go

package shortcodes
import (
"context"
"fmt"
"net/http"
"fr.latosa-escrima/core"
"fr.latosa-escrima/core/models"
)
func HandleShortcode(w http.ResponseWriter, r *http.Request) {
code := r.PathValue("shortcode")
var shortcode models.Shortcode
err := core.DB.NewSelect().
Model(&shortcode).
Where("code = ?", code).
Relation("Media").
Limit(1).
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 { // Check if the request is over HTTPS
scheme = "https"
}
// Extract the host
host := r.Host
baseURL := fmt.Sprintf("%s://%s", scheme, host)
if shortcode.Media != nil {
shortcode.Media.URL = fmt.Sprintf("%s/media/%s/file", baseURL, shortcode.Media.ID)
}
core.JSONSuccess{
Status: core.Success,
Message: "Shortcode found",
Data: shortcode,
}.Respond(w, http.StatusOK)
}