diff --git a/README.md b/README.md index 7fcd2d6..d073bdc 100644 --- a/README.md +++ b/README.md @@ -39,9 +39,6 @@ It will trigger few questions that need to be answered before the user can use t You can find out all the bot commands available by sending `help` to Hatcher -## How to use the API -Hatcher API documentation is available [here](https://documenter.getpostman.com/view/3454833/RWM9uVgF) - ## Resources: https://blog.gopheracademy.com/advent-2017/go-slackbot/ diff --git a/api/handler.go b/api/handler.go index e207c93..a1d98ff 100644 --- a/api/handler.go +++ b/api/handler.go @@ -17,9 +17,5 @@ func Handler(s *common.Slack) (http.Handler, error) { // main API endpoint with slack router.POST("/slack", bot.SlackPostHandler(s)) - //API endpoints to get informations about the slack users - router.GET("/api/slack/allusers", getAllUsersHandler) - router.GET("/api/slack/user/:userid", getUserHandler) - return router, nil } diff --git a/api/user.go b/api/user.go deleted file mode 100644 index 24e4578..0000000 --- a/api/user.go +++ /dev/null @@ -1,142 +0,0 @@ -package api - -import ( - "database/sql" - "encoding/json" - "fmt" - "net/http" - - "github.com/julienschmidt/httprouter" - - "github.com/matthieudolci/hatcher/database" -) - -type userSummary struct { - UserID string `json:"userid"` - Name string `json:"name"` - Email string `json:"email"` - ManagerID sql.NullString `json:"managerid"` - IsManager bool `json:"ismanager"` - DisplayName sql.NullString `json:"displayname"` -} - -type users struct { - Users []userSummary -} - -func getAllUsersHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { - - u := users{} - - err := getAllUsers(&u) - if err != nil { - http.Error(w, err.Error(), 500) - return - } - - out, err := json.Marshal(&u) - if err != nil { - http.Error(w, err.Error(), 500) - return - } - - fmt.Fprintf(w, string(out)) -} - -func getAllUsers(u *users) error { - - rows, err := database.DB.Query(` - SELECT - userid, - full_name, - email, - managerid, - ismanager, - displayname - FROM hatcher.users; - `) - if err != nil { - return err - } - defer rows.Close() - for rows.Next() { - result := userSummary{} - err = rows.Scan( - &result.UserID, - &result.Name, - &result.Email, - &result.ManagerID, - &result.IsManager, - &result.DisplayName, - ) - if err != nil { - return err - } - u.Users = append(u.Users, result) - } - err = rows.Err() - if err != nil { - return err - } - return nil -} - -func getUserHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - - u := users{} - - userid := ps.ByName("userid") - - err := getUser(&u, userid) - if err != nil { - http.Error(w, err.Error(), 500) - return - } - - out, err := json.Marshal(&u) - if err != nil { - http.Error(w, err.Error(), 500) - return - } - - fmt.Fprintf(w, string(out)) -} - -func getUser(u *users, userid string) error { - - rows, err := database.DB.Query(` - SELECT - userid, - full_name, - email, - managerid, - ismanager, - displayname - FROM hatcher.users - WHERE userid=$1; - `, userid) - if err != nil { - return err - } - defer rows.Close() - for rows.Next() { - result := userSummary{} - err = rows.Scan( - &result.UserID, - &result.Name, - &result.Email, - &result.ManagerID, - &result.IsManager, - &result.DisplayName, - ) - if err != nil { - return err - } - u.Users = append(u.Users, result) - } - err = rows.Err() - if err != nil { - return err - } - return nil -}