feat: refac and add key deletion route
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package assets
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
@@ -8,11 +9,16 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
OkMes string = "OK"
|
||||
IntErrMes string = "An internal error occured, contact your administrator"
|
||||
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 BasicResponse struct {
|
||||
type ResponseObject struct {
|
||||
Msg string `json:"msg"`
|
||||
Data any `json:"data"`
|
||||
}
|
||||
@@ -33,13 +39,73 @@ 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"`
|
||||
ExpiresAt time.Time `json:"expiresAt"`
|
||||
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, BasicResponse{
|
||||
c.JSON(http.StatusInternalServerError, ResponseObject{
|
||||
Msg: IntErrMes,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user