diff --git a/directory.go b/directory.go index 6c1ab29..fd9a085 100644 --- a/directory.go +++ b/directory.go @@ -34,6 +34,10 @@ type Results struct { MessageID uint32 `json:"id"` } +type UniqueID struct { + Id int `json:"id"` +} + func handleSearch(w http.ResponseWriter, r *http.Request) { //Get input value by user input := mux.Vars(r)["input"] @@ -43,10 +47,10 @@ func handleSearch(w http.ResponseWriter, r *http.Request) { return } - session, err := store.Get(r, SESSION_NAME) + /**session, err := store.Get(r, SESSION_NAME) if err != nil { return - } + }**/ //Search values with ldap and filter searchRequest := ldap.NewSearchRequest( @@ -79,21 +83,27 @@ func handleSearch(w http.ResponseWriter, r *http.Request) { } } - - //Convert interface to uint32 with Type Assertions and not a simple convert for messageID - if val_Raw, ok_raw := session.Values["MessageID"]; ok_raw { - if val, ok := val_Raw.(uint32); ok { - val += 1 - session.Values["MessageID"] = val - result.MessageID = val - err = session.Save(r, w) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } + if result.Search == nil { + result = Results{ + Search: append(result.Search, SearchResult{ + Identifiant: "", + Name: "", + Email: "", + Description: "", + DN: "", + }), } } + var id UniqueID + //Decode JSON body + err = json.NewDecoder(r.Body).Decode(&id) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + result.MessageID = uint32(id.Id) + //Send JSON through xhttp w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusCreated) diff --git a/static/javascript/search.js b/static/javascript/search.js index 9e078be..6d33fe6 100644 --- a/static/javascript/search.js +++ b/static/javascript/search.js @@ -1,4 +1,5 @@ var perso_id = 0; +var last_id = 0; function searchDirectory() { var input = document.getElementById("search").value; @@ -10,8 +11,8 @@ function searchDirectory() { //Response from Request Ajax var jsonResponse = JSON.parse(xhttp.responseText); - if (perso_id < jsonResponse.id) { - perso_id = jsonResponse.id + if (last_id < jsonResponse.id) { + last_id = jsonResponse.id //We get the old table element, we create an new table element then we increment this new table. //After the new add, we replace the old table by the new one. var old_table = document.getElementById("users"); @@ -29,19 +30,16 @@ function searchDirectory() { identifiant.innerHTML = `${jsonResponse.search[i].identifiant}` name.innerHTML = jsonResponse.search[i].name email.innerHTML = jsonResponse.search[i].email - description.innerHTML = `${jsonResponse.search[i].description}` + description.innerHTML = jsonResponse.search[i].description } old_table.parentNode.replaceChild(table, old_table) } } }; + perso_id += 1 xhttp.overrideMimeType("application/json"); - xhttp.open("GET", "/search/".concat(input), true); - xhttp.send(); + xhttp.open("POST", "/search/".concat(input), true); + xhttp.send(JSON.stringify({"id": perso_id})); } - - - - } \ No newline at end of file