package assets import ( "fmt" "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" 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"` KeyName string `json:"keyName"` KeyContent string `json:"keyContent"` 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"` FileName string `json:"fileName"` 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) { fmt.Println(len(data)) switch len(data) { case 0: // empty slice so just an ok message c.JSON(http.StatusOK, ResponseObject{ Msg: OkMes, }) // single object in our slice (even if the object itself is a slice) case 1: c.JSON(http.StatusOK, ResponseObject{ Msg: OkMes, Data: data[0], }) // multiple objects inside our slice default: 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, }) }