Files
orbits/internal/server/api/assets/response.go
T
2026-04-29 23:38:43 +02:00

96 lines
2.2 KiB
Go

package assets
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
)
const (
OkMes string = "OK"
CreationMes string = "Object successfully created"
DeletionMes string = "Object successfully deleted"
NotFoundMes string = "Requested object not found"
BadRequestMes string = "Request did not satisfy requirements (bad request)"
ConflictMes string = "Duplicate object"
IntErrMes string = "An internal error occured, contact your administrator"
)
type ResponseObject struct {
Msg string `json:"msg"`
Data any `json:"data"`
}
// we swap out the hash for the keycontent
type KeyResponse struct {
ID int `json:"ID"`
MetaName string `json:"metaName"`
KeyID string `json:"keyID"`
KeySecret string `json:"keySecret"`
Revoked bool `json:"revoked"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
ExpiresAt time.Time `json:"expiresAt"`
}
type FileResponse struct {
ID int `json:"ID"`
MetaName string `json:"metaName"`
FileID string `json:"fileID"`
FilePath string `json:"filePath"`
Checksum string `json:"checksum"`
MediaType string `json:"mediaType"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
func FileDownloadResponse(c *gin.Context, fp string) {
c.File(fp)
}
func BasicResponse(c *gin.Context, data any) {
// single object in our slice (even if the object itself is a slice)
c.JSON(http.StatusOK, ResponseObject{
Msg: OkMes,
Data: data,
})
}
func CreationResponse(c *gin.Context, data any) {
c.JSON(http.StatusCreated, ResponseObject{
Msg: CreationMes,
Data: data,
})
}
func DeletionResponse(c *gin.Context) {
c.JSON(http.StatusOK, ResponseObject{
Msg: DeletionMes,
})
}
func NotFoundResponse(c *gin.Context) {
c.JSON(http.StatusNotFound, ResponseObject{
Msg: NotFoundMes,
})
}
func BadRequestResponse(c *gin.Context) {
c.JSON(http.StatusBadRequest, ResponseObject{
Msg: BadRequestMes,
})
}
func DuplicateResponse(c *gin.Context) {
c.JSON(http.StatusConflict, ResponseObject{
Msg: ConflictMes,
})
}
func InternalErrorResponse(c *gin.Context) {
c.JSON(http.StatusInternalServerError, ResponseObject{
Msg: IntErrMes,
})
}