Refactor code

This commit is contained in:
root
2020-08-13 16:33:37 +00:00
parent 3557907ce9
commit 9ee441ec02

View File

@@ -1,138 +1,104 @@
package main package main
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"reflect" "reflect"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
//"go.mongodb.org/mongo-driver/bson" //"go.mongodb.org/mongo-driver/bson"
) )
func (e mainEnv) createLegalBasis(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { func (e mainEnv) createLegalBasis(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
brief := ps.ByName("brief") brief := ps.ByName("brief")
authResult := e.enforceAdmin(w, r) authResult := e.enforceAdmin(w, r)
if authResult == "" { if authResult == "" {
return return
} }
brief = normalizeBrief(brief) brief = normalizeBrief(brief)
if isValidBrief(brief) == false { if isValidBrief(brief) == false {
returnError(w, r, "bad brief format", 405, nil, nil) returnError(w, r, "bad brief format", 405, nil, nil)
return return
} }
records, err := getJSONPostData(r) records, err := getJSONPostData(r)
if err != nil { if err != nil {
returnError(w, r, "failed to decode request body", 405, err, nil) returnError(w, r, "failed to decode request body", 405, err, nil)
return return
} }
status := "active"; status := "active";
module := "" module := getStringValue(records, "module")
fulldesc := "" fulldesc := getStringValue(records, "fulldesc")
newbrief := "" newbrief := getStringValue(records, "newbrief")
shortdesc := "" shortdesc := getStringValue(records, "shortdesc")
basistype := "" basistype := getStringValue(records, "basistype")
requiredmsg := "" requiredmsg := getStringValue(records, "requiredmsg")
usercontrol := false usercontrol := false
requiredflag := false requiredflag := false
if value, ok := records["status"]; ok {
if value, ok := records["module"]; ok { if reflect.TypeOf(value) == reflect.TypeOf("string") {
if reflect.TypeOf(value) == reflect.TypeOf("string") { if value.(string) == "disabled" {
module = value.(string) status = value.(string)
} }
} }
if value, ok := records["fulldesc"]; ok { }
if reflect.TypeOf(value) == reflect.TypeOf("string") { if value, ok := records["usercontrol"]; ok {
fulldesc = value.(string) if reflect.TypeOf(value).Kind() == reflect.Bool {
} usercontrol = value.(bool)
} }
if value, ok := records["brief"]; ok { }
if reflect.TypeOf(value) == reflect.TypeOf("string") { if value, ok := records["requiredflag"]; ok {
newbrief = value.(string) if reflect.TypeOf(value).Kind() == reflect.Bool {
} requiredflag = value.(bool)
} }
if value, ok := records["shortdesc"]; ok { }
if reflect.TypeOf(value) == reflect.TypeOf("string") { e.db.createLegalBasis(brief, newbrief, module, shortdesc, fulldesc, basistype, requiredmsg, status, usercontrol, requiredflag)
shortdesc = value.(string) /*
} notifyURL := e.conf.Notification.NotificationURL
} if newStatus == true && len(notifyURL) > 0 {
if value, ok := records["basistype"]; ok { // change notificate on new record or if status change
if reflect.TypeOf(value) == reflect.TypeOf("string") { if len(userTOKEN) > 0 {
basistype = value.(string) notifyConsentChange(notifyURL, brief, status, "token", userTOKEN)
} } else {
} notifyConsentChange(notifyURL, brief, status, mode, address)
basistype = normalizeBasisType(basistype) }
if value, ok := records["requiredmsg"]; ok { }
if reflect.TypeOf(value) == reflect.TypeOf("string") { */
requiredmsg = value.(string) w.Header().Set("Content-Type", "application/json; charset=utf-8")
} w.WriteHeader(200)
} w.Write([]byte(`{"status":"ok"}`))
if value, ok := records["status"]; ok {
if reflect.TypeOf(value) == reflect.TypeOf("string") {
if value.(string) == "disabled" {
status = value.(string)
}
}
}
if value, ok := records["usercontrol"]; ok {
if reflect.TypeOf(value).Kind() == reflect.Bool {
usercontrol = value.(bool)
}
}
if value, ok := records["requiredflag"]; ok {
if reflect.TypeOf(value).Kind() == reflect.Bool {
requiredflag = value.(bool)
}
}
e.db.createLegalBasis(brief, newbrief, module, shortdesc, fulldesc, basistype, requiredmsg, status, usercontrol, requiredflag)
/*
notifyURL := e.conf.Notification.NotificationURL
if newStatus == true && len(notifyURL) > 0 {
// change notificate on new record or if status change
if len(userTOKEN) > 0 {
notifyConsentChange(notifyURL, brief, status, "token", userTOKEN)
} else {
notifyConsentChange(notifyURL, brief, status, mode, address)
}
}
*/
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200)
w.Write([]byte(`{"status":"ok"}`))
} }
func (e mainEnv) deleteLegalBasis(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { func (e mainEnv) deleteLegalBasis(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
brief := ps.ByName("brief") brief := ps.ByName("brief")
authResult := e.enforceAdmin(w, r) authResult := e.enforceAdmin(w, r)
if authResult == "" { if authResult == "" {
return return
} }
brief = normalizeBrief(brief) brief = normalizeBrief(brief)
if isValidBrief(brief) == false { if isValidBrief(brief) == false {
returnError(w, r, "bad brief format", 405, nil, nil) returnError(w, r, "bad brief format", 405, nil, nil)
return return
} }
e.db.unlinkProcessingActivityBrief(brief) e.db.unlinkProcessingActivityBrief(brief)
e.db.deleteLegalBasis(brief); e.db.deleteLegalBasis(brief);
w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200) w.WriteHeader(200)
w.Write([]byte(`{"status":"ok"}`)) w.Write([]byte(`{"status":"ok"}`))
} }
func (e mainEnv) listLegalBasisRecords(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { func (e mainEnv) listLegalBasisRecords(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
authResult := e.enforceAdmin(w, r) authResult := e.enforceAdmin(w, r)
if authResult == "" { if authResult == "" {
return return
} }
resultJSON, numRecords, err := e.db.getLegalBasisRecords() resultJSON, numRecords, err := e.db.getLegalBasisRecords()
if err != nil { if err != nil {
returnError(w, r, "internal error", 405, err, nil) returnError(w, r, "internal error", 405, err, nil)
return return
} }
fmt.Printf("Total count of rows: %d\n", numRecords) fmt.Printf("Total count of rows: %d\n", numRecords)
w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200) w.WriteHeader(200)
str := fmt.Sprintf(`{"status":"ok","total":%d,"rows":%s}`, numRecords, resultJSON) str := fmt.Sprintf(`{"status":"ok","total":%d,"rows":%s}`, numRecords, resultJSON)
w.Write([]byte(str)) w.Write([]byte(str))
} }