initial commit
This commit is contained in:
263
pkg/actions/actions.go
Normal file
263
pkg/actions/actions.go
Normal file
@ -0,0 +1,263 @@
|
||||
package actions
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/FrankenBotDev/FrankenAPI/ent"
|
||||
"github.com/FrankenBotDev/FrankenAPI/ent/actions"
|
||||
"github.com/FrankenBotDev/FrankenAPI/pkg/logger"
|
||||
"github.com/FrankenBotDev/FrankenAPI/pkg/sys"
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
func ActionRouter() http.Handler {
|
||||
r := chi.NewRouter()
|
||||
|
||||
r.Get("/", getActions)
|
||||
r.Get("/search", getActionsbyID)
|
||||
r.Post("/", addAction)
|
||||
r.Put("/", updateAction)
|
||||
r.Delete("/", deleteAction)
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func getActions(w http.ResponseWriter, r *http.Request) {
|
||||
actionRequest := r.URL.Query()
|
||||
|
||||
if actionRequest.Get("serverid") != "" {
|
||||
if actionRequest.Get("commiterid") != "" {
|
||||
actions, err := r.Context().Value(sys.EntClientKey).(*ent.Client).Actions.Query().
|
||||
Where(actions.Serverid(actionRequest.Get("serverid"))).
|
||||
Order(ent.Desc(actions.FieldID)).
|
||||
All(r.Context().Value(sys.ContextClientKey).(context.Context))
|
||||
|
||||
if err != nil {
|
||||
sys.ErrorJsonResponse(w, "could_not_fetch_from_database", err)
|
||||
}
|
||||
|
||||
sys.SuccessJsonResponse(w, actions)
|
||||
} else {
|
||||
sys.ErrorJsonResponse(w, "missing_param", errors.New("commiterid needs to be set"))
|
||||
}
|
||||
} else {
|
||||
sys.ErrorJsonResponse(w, "missing_param", errors.New("serverid needs to be set"))
|
||||
}
|
||||
}
|
||||
|
||||
func getActionsbyID(w http.ResponseWriter, r *http.Request) {
|
||||
actionRequest := r.URL.Query()
|
||||
|
||||
if actionRequest.Get("serverid") != "" {
|
||||
if actionRequest.Get("commiterid") != "" {
|
||||
if actionRequest.Get("refid") != "" {
|
||||
actions, err := r.Context().Value(sys.EntClientKey).(*ent.Client).Actions.Query().
|
||||
Where(actions.Serverid(actionRequest.Get("serverid")), actions.Refid(actionRequest.Get("refid"))).
|
||||
All(r.Context().Value(sys.ContextClientKey).(context.Context))
|
||||
|
||||
if err != nil {
|
||||
sys.ErrorJsonResponse(w, "could_not_fetch_from_database", err)
|
||||
}
|
||||
|
||||
sys.SuccessJsonResponse(w, actions)
|
||||
} else {
|
||||
sys.ErrorJsonResponse(w, "missing_param", errors.New("refid needs to be set"))
|
||||
}
|
||||
} else {
|
||||
sys.ErrorJsonResponse(w, "missing_param", errors.New("commiterid needs to be set"))
|
||||
}
|
||||
} else {
|
||||
sys.ErrorJsonResponse(w, "missing_param", errors.New("serverid needs to be set"))
|
||||
}
|
||||
}
|
||||
|
||||
func addAction(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
actionData, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
sys.ErrorJsonResponse(w, "could_not_find_data_in_request", errors.New("the request appears to be empty"))
|
||||
}
|
||||
|
||||
// proccess data
|
||||
actionJson := make(map[string]any)
|
||||
err = json.Unmarshal(actionData, &actionJson)
|
||||
if err != nil {
|
||||
sys.ErrorJsonResponse(w, "could_not_proccess_data", errors.New("request data could not be proccessed"))
|
||||
}
|
||||
|
||||
devRefid := sys.RandStringRunes(16)
|
||||
|
||||
if actionJson["serverid"].(string) != "" {
|
||||
if actionJson["comiterid"].(string) != "" {
|
||||
if devRefid != "" {
|
||||
if actionJson["target"].(string) != "" {
|
||||
if actionJson["type"].(string) != "" {
|
||||
if actionJson["duration"].(string) != "" {
|
||||
if actionJson["reason"].(string) != "" {
|
||||
if strconv.FormatBool(actionJson["temp"].(bool)) != "" {
|
||||
_, err := r.Context().Value(sys.EntClientKey).(*ent.Client).Actions.Create().
|
||||
SetRefid(devRefid).
|
||||
SetServerid(actionJson["serverid"].(string)).
|
||||
SetTarget(actionJson["target"].(string)).
|
||||
SetType(actionJson["type"].(string)).
|
||||
SetDuration(actionJson["duration"].(string)).
|
||||
SetCommiter(actionJson["comiterid"].(string)).
|
||||
SetReason(actionJson["reason"].(string)).
|
||||
SetTemp(actionJson["temp"].(bool)).
|
||||
Save(r.Context().Value(sys.ContextClientKey).(context.Context))
|
||||
|
||||
logger.Logtodb([]string{
|
||||
actionJson["comiterid"].(string),
|
||||
"Created action with RefID: " + devRefid,
|
||||
"Action Create",
|
||||
actionJson["serverid"].(string),
|
||||
},
|
||||
r.Context().Value(sys.EntClientKey).(*ent.Client),
|
||||
r.Context().Value(sys.ContextClientKey).(context.Context))
|
||||
|
||||
if err != nil {
|
||||
sys.ErrorJsonResponse(w, "data_could_not_be_updated", err)
|
||||
}
|
||||
|
||||
sys.SuccessJsonResponse(w, "data_updated")
|
||||
} else {
|
||||
sys.ErrorJsonResponse(w, "missing_param", errors.New("temp needs to be set"))
|
||||
}
|
||||
} else {
|
||||
sys.ErrorJsonResponse(w, "missing_param", errors.New("reason needs to be set"))
|
||||
}
|
||||
} else {
|
||||
sys.ErrorJsonResponse(w, "missing_param", errors.New("duration needs to be set"))
|
||||
}
|
||||
} else {
|
||||
sys.ErrorJsonResponse(w, "missing_param", errors.New("type needs to be set"))
|
||||
}
|
||||
} else {
|
||||
sys.ErrorJsonResponse(w, "missing_param", errors.New("target need to be set"))
|
||||
}
|
||||
} else {
|
||||
sys.ErrorJsonResponse(w, "missing_param", errors.New("refid needs to be set"))
|
||||
}
|
||||
} else {
|
||||
sys.ErrorJsonResponse(w, "missing_param", errors.New("commiterid needs to be set"))
|
||||
}
|
||||
} else {
|
||||
sys.ErrorJsonResponse(w, "missing_param", errors.New("serverid needs to be set"))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func deleteAction(w http.ResponseWriter, r *http.Request) {
|
||||
actionData, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
sys.ErrorJsonResponse(w, "could_not_find_data_in_request", errors.New("the request appears to be empty"))
|
||||
}
|
||||
|
||||
// proccess data
|
||||
actionJson := make(map[string]any)
|
||||
err = json.Unmarshal(actionData, &actionJson)
|
||||
if err != nil {
|
||||
sys.ErrorJsonResponse(w, "could_not_proccess_data", errors.New("request data could not be proccessed"))
|
||||
}
|
||||
|
||||
if actionJson["comiterid"].(string) != "" {
|
||||
if actionJson["serverid"].(string) != "" {
|
||||
if actionJson["refid"].(string) != "" {
|
||||
|
||||
_, err = r.Context().Value(sys.EntClientKey).(*ent.Client).Actions.Delete().
|
||||
Where(actions.Serverid(actionJson["serverid"].(string)), actions.Refid(actionJson["refid"].(string))).
|
||||
Exec(r.Context().Value(sys.ContextClientKey).(context.Context))
|
||||
|
||||
if err != nil {
|
||||
sys.ErrorJsonResponse(w, "data_could_not_be_removed", err)
|
||||
}
|
||||
|
||||
logger.Logtodb([]string{
|
||||
actionJson["comiterid"].(string),
|
||||
"Deleted action with RefID: " + actionJson["refid"].(string),
|
||||
"Action Delete",
|
||||
actionJson["serverid"].(string),
|
||||
}, r.Context().Value(sys.EntClientKey).(*ent.Client),
|
||||
r.Context().Value(sys.ContextClientKey).(context.Context))
|
||||
|
||||
sys.SuccessJsonResponse(w, "data_updated")
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func updateAction(w http.ResponseWriter, r *http.Request) {
|
||||
actionData, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
sys.ErrorJsonResponse(w, "could_not_find_data_in_request", errors.New("the request appears to be empty"))
|
||||
}
|
||||
|
||||
// proccess data
|
||||
actionJson := make(map[string]any)
|
||||
err = json.Unmarshal(actionData, &actionJson)
|
||||
if err != nil {
|
||||
sys.ErrorJsonResponse(w, "could_not_proccess_data", errors.New("request data could not be proccessed"))
|
||||
}
|
||||
|
||||
if actionJson["serverid"].(string) != "" {
|
||||
if actionJson["commiter"].(string) != "" {
|
||||
if actionJson["refid"].(string) != "" {
|
||||
if actionJson["target"].(string) != "" {
|
||||
if actionJson["type"].(string) != "" {
|
||||
if actionJson["duration"].(string) != "" {
|
||||
if actionJson["reason"].(string) != "" {
|
||||
if strconv.FormatBool(actionJson["temp"].(bool)) != "" {
|
||||
_, err := r.Context().Value(sys.EntClientKey).(*ent.Client).Actions.Update().
|
||||
Where(actions.Serverid(actionJson["serverid"].(string)), actions.Refid(actionJson["refid"].(string))).
|
||||
SetRefid(actionJson["refid"].(string)).
|
||||
SetTarget(actionJson["target"].(string)).
|
||||
SetType(actionJson["type"].(string)).
|
||||
SetDuration(actionJson["duration"].(string)).
|
||||
SetCommiter(actionJson["commiter"].(string)).
|
||||
SetReason(actionJson["reason"].(string)).
|
||||
SetTemp(actionJson["temp"].(bool)).
|
||||
Save(r.Context().Value(sys.ContextClientKey).(context.Context))
|
||||
if err != nil {
|
||||
sys.ErrorJsonResponse(w, "data_could_not_be_updated", err)
|
||||
}
|
||||
|
||||
logger.Logtodb([]string{
|
||||
actionJson["commiter"].(string),
|
||||
"Updated Action with RefID: " + actionJson["refid"].(string),
|
||||
"Action Update",
|
||||
actionJson["serverid"].(string),
|
||||
}, r.Context().Value(sys.EntClientKey).(*ent.Client),
|
||||
r.Context().Value(sys.ContextClientKey).(context.Context))
|
||||
|
||||
sys.SuccessJsonResponse(w, "data_updated")
|
||||
} else {
|
||||
sys.ErrorJsonResponse(w, "missing_param", errors.New("temp needs to be set"))
|
||||
}
|
||||
} else {
|
||||
sys.ErrorJsonResponse(w, "missing_param", errors.New("reason needs to be set"))
|
||||
}
|
||||
} else {
|
||||
sys.ErrorJsonResponse(w, "missing_param", errors.New("duration needs to be set"))
|
||||
}
|
||||
} else {
|
||||
sys.ErrorJsonResponse(w, "missing_param", errors.New("type needs to be set"))
|
||||
}
|
||||
} else {
|
||||
sys.ErrorJsonResponse(w, "missing_param", errors.New("target need to be set"))
|
||||
}
|
||||
} else {
|
||||
sys.ErrorJsonResponse(w, "missing_param", errors.New("refid needs to be set"))
|
||||
}
|
||||
} else {
|
||||
sys.ErrorJsonResponse(w, "missing_param", errors.New("commiterid needs to be set"))
|
||||
}
|
||||
} else {
|
||||
sys.ErrorJsonResponse(w, "missing_param", errors.New("serverid needs to be set"))
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user