go-pingbot/website/routes/api/delete.go

65 lines
1.1 KiB
Go
Raw Normal View History

2021-07-11 21:19:37 +00:00
package api
import (
"encoding/base64"
"net/http"
"github.com/gin-gonic/gin"
"gitlab.com/gaming0skar123/go/pingbot/config"
2021-07-29 18:51:15 +00:00
"gitlab.com/gaming0skar123/go/pingbot/database/mongo"
2021-07-11 21:19:37 +00:00
)
func Delete(c *gin.Context) {
const BEARER_SCHEMA = "Password"
authHeader := c.GetHeader("Authorization")
passwordString := authHeader[len(BEARER_SCHEMA)+1:]
if passwordString != config.Password {
c.JSON(http.StatusUnauthorized, json{
"success": false,
"message": "Unauth!",
})
return
}
2021-07-11 21:19:37 +00:00
url := c.Param("url")
d, err := base64.StdEncoding.DecodeString(url)
if err != nil {
c.JSON(http.StatusBadRequest, json{
2021-07-11 21:19:37 +00:00
"success": false,
"message": "Error Parsing Base64!",
})
return
}
url = string(d)
2021-08-01 17:43:59 +00:00
r, err := mongo.Delete(url)
2021-07-11 21:19:37 +00:00
if r.DeletedCount <= 0 {
c.JSON(http.StatusNotFound, json{
2021-07-11 21:19:37 +00:00
"success": false,
2021-08-01 18:26:48 +00:00
"message": "Not Found!",
2021-07-11 21:19:37 +00:00
})
return
}
if err != nil {
c.JSON(http.StatusInternalServerError, json{
2021-07-11 21:19:37 +00:00
"success": false,
"message": "Error Deleting from Database!",
})
return
}
c.JSON(http.StatusOK, json{
2021-07-11 21:19:37 +00:00
"success": true,
"message": "Deleted!",
2021-07-11 21:19:37 +00:00
})
}