Shortcodes
This commit is contained in:
@@ -36,7 +36,8 @@ func HandleMedia(w http.ResponseWriter, r *http.Request) {
|
||||
Model((*models.Media)(nil)).
|
||||
Count(context.Background())
|
||||
|
||||
totalPages := int(math.Max(1, float64(total/limit)))
|
||||
upperBound := float64(total) / float64(limit)
|
||||
totalPages := int(math.Max(1, math.Ceil(upperBound)))
|
||||
|
||||
var media []models.Media
|
||||
err = core.DB.NewSelect().
|
||||
|
||||
@@ -1,3 +1,52 @@
|
||||
package media
|
||||
|
||||
// TODO
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func HandleUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
var media models.Media
|
||||
err := json.NewDecoder(r.Body).Decode(&media)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
media_uuid := r.PathValue("media_uuid")
|
||||
media.ID, err = uuid.Parse(media_uuid)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = core.DB.NewUpdate().
|
||||
Model(&media).
|
||||
OmitZero().
|
||||
WherePK().
|
||||
Exec(context.Background())
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Media updated",
|
||||
Data: media,
|
||||
}.Respond(w, http.StatusOK)
|
||||
|
||||
}
|
||||
|
||||
@@ -28,10 +28,10 @@ var MediaRoutes = map[string]core.Handler{
|
||||
Handler: media.HandleMediaFile,
|
||||
Middlewares: []core.Middleware{Methods("GET")},
|
||||
},
|
||||
// "/media/{media_uuid}/update": {
|
||||
// Handler: HandleGetMediaFile,
|
||||
// Middlewares: []core.Middleware{Methods("PATCH"), AuthJWT},
|
||||
// },
|
||||
"/media/{media_uuid}/update": {
|
||||
Handler: media.HandleUpdate,
|
||||
Middlewares: []core.Middleware{Methods("PATCH"), AuthJWT},
|
||||
},
|
||||
"/media/{media_uuid}/delete": {
|
||||
Handler: media.HandleDelete,
|
||||
Middlewares: []core.Middleware{Methods("DELETE"), AuthJWT},
|
||||
|
||||
@@ -2,6 +2,7 @@ package shortcodes
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/core"
|
||||
@@ -14,6 +15,7 @@ func HandleShortcode(w http.ResponseWriter, r *http.Request) {
|
||||
err := core.DB.NewSelect().
|
||||
Model(&shortcode).
|
||||
Where("code = ?", code).
|
||||
Relation("Media").
|
||||
Limit(1).
|
||||
Scan(context.Background())
|
||||
if err != nil {
|
||||
@@ -24,6 +26,17 @@ func HandleShortcode(w http.ResponseWriter, r *http.Request) {
|
||||
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",
|
||||
|
||||
Reference in New Issue
Block a user