From 515851f7089819520eea8aa3e85f36874c680aa2 Mon Sep 17 00:00:00 2001 From: cedrok Date: Fri, 2 Aug 2024 11:38:56 +0200 Subject: [PATCH] refactor(api) AsteriskLine constant --- API/controllers/auth.go | 44 ++++++++++---------- API/controllers/auth_test.go | 4 +- API/controllers/entity.go | 78 ++++++++++++++++++------------------ API/controllers/event.go | 4 +- API/controllers/image.go | 4 +- API/controllers/schema.go | 4 +- API/controllers/web.go | 12 +++--- API/utils/util.go | 2 + 8 files changed, 78 insertions(+), 74 deletions(-) diff --git a/API/controllers/auth.go b/API/controllers/auth.go index b8aa80886..75a0f72fc 100644 --- a/API/controllers/auth.go +++ b/API/controllers/auth.go @@ -13,6 +13,8 @@ import ( "go.mongodb.org/mongo-driver/bson/primitive" ) +const invalidRequestMsg = "invalid request" + func init() { rand.Seed(time.Now().UnixNano()) } @@ -47,9 +49,9 @@ func init() { // description: Internal server error func CreateAccount(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: CreateAccount ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) DispRequestMetaData(r) if r.Method == "OPTIONS" { @@ -105,16 +107,16 @@ func CreateAccount(w http.ResponseWriter, r *http.Request) { // description: Internal server error func CreateBulkAccount(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: CreateBulkAccount ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) DispRequestMetaData(r) var accounts []models.Account err := json.NewDecoder(r.Body).Decode(&accounts) if err != nil { w.WriteHeader(http.StatusBadRequest) - u.Respond(w, u.Message("Invalid request")) + u.Respond(w, u.Message(invalidRequestMsg)) return } @@ -178,9 +180,9 @@ func randStringBytes(n int) string { // description: Internal server error func Authenticate(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: Authenticate ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) DispRequestMetaData(r) if r.Method == "OPTIONS" { @@ -190,7 +192,7 @@ func Authenticate(w http.ResponseWriter, r *http.Request) { err := json.NewDecoder(r.Body).Decode(&account) if err != nil { w.WriteHeader(http.StatusBadRequest) - u.Respond(w, u.Message("Invalid request")) + u.Respond(w, u.Message(invalidRequestMsg)) return } @@ -221,9 +223,9 @@ func Authenticate(w http.ResponseWriter, r *http.Request) { // description: Internal server error func VerifyToken(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: Verify ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) DispRequestMetaData(r) if r.Method == "OPTIONS" { @@ -247,9 +249,9 @@ func VerifyToken(w http.ResponseWriter, r *http.Request) { // description: Internal server error func GetAllAccounts(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: GetAllAccount ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) DispRequestMetaData(r) if r.Method == "OPTIONS" { @@ -300,9 +302,9 @@ func GetAllAccounts(w http.ResponseWriter, r *http.Request) { // description: Internal server error func RemoveAccount(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: RemoveAccount ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) DispRequestMetaData(r) if r.Method == "OPTIONS" { @@ -378,9 +380,9 @@ func RemoveAccount(w http.ResponseWriter, r *http.Request) { // description: Internal server error func ModifyUserRoles(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: ModifyUserRoles ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) DispRequestMetaData(r) if r.Method == "OPTIONS" { @@ -502,9 +504,9 @@ func getUserRolesFromBody(r *http.Request) (map[string]models.Role, error) { // description: Internal server error func ModifyUserPassword(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: ModifyUserPassword ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) DispRequestMetaData(r) if r.Method == "OPTIONS" { @@ -606,9 +608,9 @@ func getModifyPassDataFromBody(r *http.Request, userEmail string) (string, strin // description: Internal server error func UserForgotPassword(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: UserForgotPassword ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) DispRequestMetaData(r) if r.Method == "OPTIONS" { @@ -619,7 +621,7 @@ func UserForgotPassword(w http.ResponseWriter, r *http.Request) { err := json.NewDecoder(r.Body).Decode(&data) if err != nil { w.WriteHeader(http.StatusBadRequest) - u.Respond(w, u.Message("Invalid request")) + u.Respond(w, u.Message(invalidRequestMsg)) return } userEmail, hasEmail := data["email"].(string) diff --git a/API/controllers/auth_test.go b/API/controllers/auth_test.go index bbaef28fe..7ce3fbf1d 100644 --- a/API/controllers/auth_test.go +++ b/API/controllers/auth_test.go @@ -258,8 +258,8 @@ func TestRequestsWithInvalidBody(t *testing.T) { message string }{ {"CreateUser", "POST", test_utils.GetEndpoint("users"), "Invalid request: wrong format body"}, - {"CreateBulkUsers", "POST", test_utils.GetEndpoint("usersBulk"), "Invalid request"}, - {"Login", "POST", test_utils.GetEndpoint("login"), "Invalid request"}, + {"CreateBulkUsers", "POST", test_utils.GetEndpoint("usersBulk"), "invalid request"}, + {"Login", "POST", test_utils.GetEndpoint("login"), "invalid request"}, {"ModifyUser", "PATCH", test_utils.GetEndpoint("usersInstance", userId), "invalid request"}, {"ModifyPassword", "POST", test_utils.GetEndpoint("changePassword"), "invalid request"}, } diff --git a/API/controllers/entity.go b/API/controllers/entity.go index 7806f74f2..c7afea3fd 100644 --- a/API/controllers/entity.go +++ b/API/controllers/entity.go @@ -116,9 +116,9 @@ func getUserFromToken(w http.ResponseWriter, r *http.Request) *models.Account { // message will be returned.' func CreateEntity(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: CreateEntity ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) DispRequestMetaData(r) // Get entity entStr := mux.Vars(r)["entity"] @@ -203,9 +203,9 @@ func CreateEntity(w http.ResponseWriter, r *http.Request) { // description: 'Bad format: body is not a valid list of domains.' func CreateBulkDomain(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: CreateBulkDomain ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) // Get user roles for permissions user := getUserFromToken(w, r) @@ -292,7 +292,7 @@ func setDomainAttributes(parent string, domain map[string]any) (map[string]any, // Name is the only required attribute name, ok := domain["name"].(string) if !ok { - return nil, errors.New("Invalid format: Name is required for all domains") + return nil, errors.New("invalid format: Name is required for all domains") } domainObj["name"] = name @@ -447,9 +447,9 @@ func listAnyTolistMap(data []any) []map[string]interface{} { // description: Not found. An error message will be returned func HandleGenericObjects(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: HandleGenericObjects ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) DispRequestMetaData(r) matchingObjects := []map[string]interface{}{} @@ -639,9 +639,9 @@ func HandleGenericObjects(w http.ResponseWriter, r *http.Request) { // description: Not found. An error message will be returned func HandleComplexFilters(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: HandleComplexFilters ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) DispRequestMetaData(r) var complexFilters map[string]interface{} var complexFilterExp string @@ -767,9 +767,9 @@ func HandleComplexFilters(w http.ResponseWriter, r *http.Request) { // description: Not Found. An error message will be returned. func GetEntity(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: GetEntity ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) DispRequestMetaData(r) var data map[string]interface{} var id string @@ -845,9 +845,9 @@ func GetEntity(w http.ResponseWriter, r *http.Request) { // description: Not Found. An error message will be returned. func GetLayerObjects(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: GetLayerObjects ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) DispRequestMetaData(r) var data map[string]interface{} var id string @@ -957,9 +957,9 @@ func GetLayerObjects(w http.ResponseWriter, r *http.Request) { // description: Nothing Found. An error message will be returned. func GetAllEntities(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: GetAllEntities ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) DispRequestMetaData(r) var entStr string @@ -1064,9 +1064,9 @@ func getVirtualRootObjects(data []map[string]any) []map[string]any { // description: Not found. An error message will be returned func DeleteEntity(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: DeleteEntity ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) DispRequestMetaData(r) // Get user roles for permissions @@ -1200,9 +1200,9 @@ func DeleteEntity(w http.ResponseWriter, r *http.Request) { // description: Not Found. An error message will be returned. func UpdateEntity(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: UpdateEntity ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) DispRequestMetaData(r) var data map[string]interface{} var modelErr *u.Error @@ -1314,9 +1314,9 @@ func UpdateEntity(w http.ResponseWriter, r *http.Request) { // description: Not found. An error message will be returned. func GetEntityByQuery(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: GetEntityByQuery ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) DispRequestMetaData(r) var data []map[string]interface{} var entStr string @@ -1413,9 +1413,9 @@ func GetEntityByQuery(w http.ResponseWriter, r *http.Request) { // description: 'Nothing Found. An error message will be returned.' func GetSiteAttr(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: GetSiteAttr ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) // Check id id := mux.Vars(r)["id"] @@ -1483,9 +1483,9 @@ func GetSiteAttr(w http.ResponseWriter, r *http.Request) { // description: Nothing Found. An error message will be returned. func GetEntitiesOfAncestor(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: GetEntitiesOfAncestor ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) DispRequestMetaData(r) var id string var e bool @@ -1588,9 +1588,9 @@ func GetEntitiesOfAncestor(w http.ResponseWriter, r *http.Request) { // description: Nothing Found. An error message will be returned. func GetHierarchyByName(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: GetHierarchyByName ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) DispRequestMetaData(r) var limit int @@ -1692,9 +1692,9 @@ func GetHierarchyByName(w http.ResponseWriter, r *http.Request) { // description: Server error. func GetCompleteHierarchy(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: GetCompleteHierarchy ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) DispRequestMetaData(r) // Get user roles for permissions @@ -1734,9 +1734,9 @@ func GetCompleteHierarchy(w http.ResponseWriter, r *http.Request) { // description: Server error. func GetCompleteHierarchyAttributes(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: GetCompleteHierarchyAttributes ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) DispRequestMetaData(r) // Get user roles for permissions @@ -1824,9 +1824,9 @@ func GetCompleteHierarchyAttributes(w http.ResponseWriter, r *http.Request) { // description: 'Internal error. Unable to remove object from stray and create it in an entity.' func LinkEntity(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: LinkEntity ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) DispRequestMetaData(r) var data map[string]interface{} var id string @@ -1934,9 +1934,9 @@ func LinkEntity(w http.ResponseWriter, r *http.Request) { } func BaseOption(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: BaseOption ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) DispRequestMetaData(r) entity, e1 := mux.Vars(r)["entity"] if !e1 || u.EntityStrToInt(entity) == -1 { @@ -1960,9 +1960,9 @@ func BaseOption(w http.ResponseWriter, r *http.Request) { // description: Server error. func GetStats(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: GetStats ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) DispRequestMetaData(r) if r.Method == "OPTIONS" { w.Header().Add("Allow", "GET, HEAD, OPTIONS") @@ -2004,9 +2004,9 @@ func GetStats(w http.ResponseWriter, r *http.Request) { // description: Not Found. An error message will be returned. func ValidateEntity(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: ValidateEntity ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) DispRequestMetaData(r) var obj map[string]interface{} entity, e1 := mux.Vars(r)["entity"] diff --git a/API/controllers/event.go b/API/controllers/event.go index 1d2c1bd17..ec2520481 100644 --- a/API/controllers/event.go +++ b/API/controllers/event.go @@ -32,9 +32,9 @@ func init() { // description: 'Successfully established stream, keep it open.' func CreateEventStream(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: CreateEventStream ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) // Configure SSE stream w.Header().Set("Access-Control-Expose-Headers", "Content-Type") w.Header().Set("Content-Type", "text/event-stream") diff --git a/API/controllers/image.go b/API/controllers/image.go index cfe321e2e..5c75b76a8 100644 --- a/API/controllers/image.go +++ b/API/controllers/image.go @@ -50,9 +50,9 @@ func imageIDToUrl(entity int, object map[string]any) map[string]any { // description: Not Found. An error message will be returned. func GetImage(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: GetImage ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) DispRequestMetaData(r) image, err := models.GetImage(mux.Vars(r)["id"]) diff --git a/API/controllers/schema.go b/API/controllers/schema.go index 4f33aa392..c6ca49874 100644 --- a/API/controllers/schema.go +++ b/API/controllers/schema.go @@ -31,9 +31,9 @@ import ( // description: Not Found. An error message will be returned. func GetSchemaJSON(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: GetSchemaJSON ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) DispRequestMetaData(r) file, err := models.GetSchemaFile(mux.Vars(r)["id"]) diff --git a/API/controllers/web.go b/API/controllers/web.go index 209b3d898..6e98ea5fc 100644 --- a/API/controllers/web.go +++ b/API/controllers/web.go @@ -35,9 +35,9 @@ import ( // description: 'Internal server error.' func GetProjects(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: GetProjects ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) query, _ := url.ParseQuery(r.URL.RawQuery) @@ -121,9 +121,9 @@ func GetProjects(w http.ResponseWriter, r *http.Request) { // description: Internal server error func CreateOrUpdateProject(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: CreateOrUpdateProject ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) project := &models.Project{} err := json.NewDecoder(r.Body).Decode(project) @@ -178,9 +178,9 @@ func CreateOrUpdateProject(w http.ResponseWriter, r *http.Request) { // description: Internal server error func DeleteProject(w http.ResponseWriter, r *http.Request) { - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) fmt.Println("FUNCTION CALL: DeleteProject ") - fmt.Println("******************************************************") + fmt.Println(u.AsteriskLine) err := models.DeleteProject(mux.Vars(r)["id"]) diff --git a/API/utils/util.go b/API/utils/util.go index 61d6b009a..09e439628 100644 --- a/API/utils/util.go +++ b/API/utils/util.go @@ -28,6 +28,8 @@ var BuildTree string var BuildTime string var GitCommitDate string +const AsteriskLine = "******************************************************" + const ( DOMAIN = iota // hierarchical root entities