135 lines
4.7 KiB
Go
135 lines
4.7 KiB
Go
package punishments
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/FrankenBotDev/FrankenAPI/ent"
|
|
"github.com/FrankenBotDev/FrankenAPI/ent/punishments"
|
|
"github.com/FrankenBotDev/FrankenAPI/pkg/logger"
|
|
"github.com/FrankenBotDev/FrankenAPI/pkg/sys"
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
func PunRouter() http.Handler {
|
|
r := chi.NewRouter()
|
|
|
|
r.Get("/", getPun)
|
|
r.Post("/", createPun)
|
|
r.Delete("/", deletePun)
|
|
return r
|
|
}
|
|
|
|
// WEB-SYS ONLY WHOLE ROUTE
|
|
func createPun(w http.ResponseWriter, r *http.Request) {
|
|
punData := sys.ProccessRequest(w, r.Body)
|
|
if punData == nil {
|
|
return
|
|
}
|
|
punJson := make(map[string]any)
|
|
|
|
if punJson["commiterid"] != "" {
|
|
if punJson["serverid"].(string) != "" {
|
|
if punJson["warnamount"].(string) != "" {
|
|
if punJson["actiontype"].(string) != "" {
|
|
if punJson["duration"].(string) != "" {
|
|
if punJson["reason"].(string) != "" {
|
|
|
|
json.Unmarshal(punData, &punJson)
|
|
err := r.Context().Value(sys.EntClientKey).(*ent.Client).Punishments.Create().
|
|
SetServerid(punJson["serverid"].(string)).
|
|
SetWarnamount(punJson["warnamount"].(string)).
|
|
SetActiontype(punJson["actiontype"].(string)).
|
|
SetDuration(punJson["duration"].(string)).
|
|
SetReason(punJson["reason"].(string)).
|
|
Exec(r.Context().Value(sys.ContextClientKey).(context.Context))
|
|
if err != nil {
|
|
sys.ErrorJsonResponse(w, "pun_could_not_be_created", err)
|
|
}
|
|
|
|
logger.Logtodb([]string{punJson["commiterid"].(string), "type: " + punJson["actiontype"].(string) + ", duration: " + punJson["duration"].(string) + ", reason: " + punJson["reason"].(string) + ", warnamount: " + punJson["warnamount"].(string), "puncreate", punJson["serverid"].(string)}, r.Context().Value(sys.EntClientKey).(*ent.Client), r.Context().Value(sys.ContextClientKey).(context.Context))
|
|
w.Write([]byte(`{"type":"success", "code":"pun_was_created"}`))
|
|
|
|
} else {
|
|
sys.ErrorJsonResponse(w, "missing_parameter", errors.New("reason needs to be set"))
|
|
}
|
|
} else {
|
|
sys.ErrorJsonResponse(w, "missing_parameter", errors.New("duration needs to be set"))
|
|
}
|
|
} else {
|
|
sys.ErrorJsonResponse(w, "missing_parameter", errors.New("actiontype needs to be set"))
|
|
}
|
|
} else {
|
|
sys.ErrorJsonResponse(w, "missing_parameter", errors.New("warnamount needs to be set"))
|
|
}
|
|
} else {
|
|
sys.ErrorJsonResponse(w, "missing_parameter", errors.New("serverid needs to be set"))
|
|
}
|
|
} else {
|
|
sys.ErrorJsonResponse(w, "missing_parameter", errors.New("commiter needs to be set"))
|
|
}
|
|
}
|
|
|
|
func getPun(w http.ResponseWriter, r *http.Request) {
|
|
userdata := r.URL.Query()
|
|
|
|
if userdata.Get("commiterid") != "" {
|
|
if userdata.Get("serverid") != "" {
|
|
if userdata.Get("warnamount") != "" {
|
|
|
|
pun, err := r.Context().Value(sys.EntClientKey).(*ent.Client).Punishments.Query().
|
|
Where(punishments.Serverid(userdata.Get("serverid")), punishments.Warnamount(userdata.Get("warnamount"))).
|
|
All(r.Context().Value(sys.ContextClientKey).(context.Context))
|
|
if err != nil {
|
|
sys.ErrorJsonResponse(w, "could_not_get_pun", err)
|
|
return
|
|
}
|
|
|
|
sys.SuccessJsonResponse(w, pun)
|
|
} else {
|
|
sys.ErrorJsonResponse(w, "missing_parameter", errors.New("warnamount needs to be set"))
|
|
}
|
|
} else {
|
|
sys.ErrorJsonResponse(w, "missing_parameter", errors.New("serverid needs to be set"))
|
|
}
|
|
} else {
|
|
sys.ErrorJsonResponse(w, "missing_paramert", errors.New("commiterid needs to be set"))
|
|
}
|
|
}
|
|
|
|
func deletePun(w http.ResponseWriter, r *http.Request) {
|
|
punData := sys.ProccessRequest(w, r.Body)
|
|
if punData != nil {
|
|
return
|
|
}
|
|
|
|
punJson := make(map[string]any)
|
|
|
|
if punJson["commiterid"].(string) != "" {
|
|
if punJson["serverid"].(string) != "" {
|
|
if punJson["warnamount"].(string) != "" {
|
|
|
|
json.Unmarshal(punData, &punJson)
|
|
_, err := r.Context().Value(sys.EntClientKey).(*ent.Client).Punishments.Delete().
|
|
Where(punishments.Serverid(punJson["serverid"].(string)), punishments.Warnamount(punJson["warnamount"].(string))).
|
|
Exec(r.Context().Value(sys.ContextClientKey).(context.Context))
|
|
if err != nil {
|
|
sys.ErrorJsonResponse(w, "pun_could_not_be_created", err)
|
|
}
|
|
|
|
logger.Logtodb([]string{punJson["commiterid"].(string), "warnamount: " + punJson["warnamount"].(string), "punremove", punJson["serverid"].(string)}, r.Context().Value(sys.EntClientKey).(*ent.Client), r.Context().Value(sys.ContextClientKey).(context.Context))
|
|
w.Write([]byte(`{"type":"success", "code":"pun_was_removed"}`))
|
|
|
|
} else {
|
|
sys.ErrorJsonResponse(w, "missing_parameter", errors.New("warnamount needs to be set"))
|
|
}
|
|
} else {
|
|
sys.ErrorJsonResponse(w, "missing_parameter", errors.New("serverid needs to be set"))
|
|
}
|
|
} else {
|
|
sys.ErrorJsonResponse(w, "missing_paramert", errors.New("commiterid needs to be set"))
|
|
}
|
|
}
|