Skip to content

Commit

Permalink
refactor(api) AsteriskLine constant
Browse files Browse the repository at this point in the history
  • Loading branch information
Cedrok committed Aug 2, 2024
1 parent b2d7215 commit 515851f
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 74 deletions.
44 changes: 23 additions & 21 deletions API/controllers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive"
)

const invalidRequestMsg = "invalid request"

func init() {
rand.Seed(time.Now().UnixNano())
}
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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" {
Expand All @@ -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
}

Expand Down Expand Up @@ -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" {
Expand All @@ -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" {
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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" {
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions API/controllers/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
}
Expand Down
78 changes: 39 additions & 39 deletions API/controllers/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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{}{}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
Expand Down Expand Up @@ -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"]
Expand Down
Loading

0 comments on commit 515851f

Please sign in to comment.