2021-07-11 21:19:37 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2021-08-02 20:53:50 +00:00
|
|
|
"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) {
|
2021-08-02 20:53:50 +00:00
|
|
|
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 {
|
2021-07-22 08:23:49 +00:00
|
|
|
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 {
|
2021-07-22 08:23:49 +00:00
|
|
|
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 {
|
2021-07-22 08:23:49 +00:00
|
|
|
c.JSON(http.StatusInternalServerError, json{
|
2021-07-11 21:19:37 +00:00
|
|
|
"success": false,
|
|
|
|
"message": "Error Deleting from Database!",
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-22 08:23:49 +00:00
|
|
|
c.JSON(http.StatusOK, json{
|
2021-07-11 21:19:37 +00:00
|
|
|
"success": true,
|
2021-07-22 08:23:49 +00:00
|
|
|
"message": "Deleted!",
|
2021-07-11 21:19:37 +00:00
|
|
|
})
|
|
|
|
}
|