Skip to content

Commit

Permalink
Merge pull request #142 from joshtyf/feat/get-all-users
Browse files Browse the repository at this point in the history
Get all users
  • Loading branch information
joshtyf authored May 19, 2024
2 parents 7390171 + 0bcd7f6 commit 96c60e7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
3 changes: 3 additions & 0 deletions backend/src/database/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ var (
WHERE user_id = $1
AND deleted = false`

SelectAllUsersStatement = `SELECT user_id, name, identity_provider, created_on, deleted
FROM public."user" WHERE deleted = false`

SelectAllUsersByOrgIdStatement = `SELECT u.user_id, u.name, u.identity_provider, u.created_on, u.deleted, m.role, m.joined_on
FROM public."user" u
INNER JOIN public."membership" m
Expand Down
16 changes: 16 additions & 0 deletions backend/src/database/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ func (u *User) GetUserById(user_id string) (*models.UserModel, error) {
}
return um, nil
}
func (u *User) GetAllUsers() ([]*models.UserModel, error) {
rows, err := u.c.Query(SelectAllUsersStatement)
if err != nil {
return nil, err
}
defer rows.Close()
users := make([]*models.UserModel, 0)
for rows.Next() {
um := &models.UserModel{}
if err := rows.Scan(&um.UserId, &um.Name, &um.IdentityProvider, &um.CreatedOn, &um.Deleted); err != nil {
return nil, err
}
users = append(users, um)
}
return users, nil
}

type GetAllUsersByOrdIdResponse struct {
UserId string `json:"user_id"`
Expand Down
15 changes: 13 additions & 2 deletions backend/src/server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ func (s *ServerHandler) registerRoutes(r *mux.Router) {
r.Handle("/api/pipeline", isAuthenticated(validateCreatePipelineRequest(handleCreatePipeline(s.logger, s.mongoClient), s.logger), s.logger)).Methods("POST").Headers("Content-Type", "application/json")

// User
// TODO: fix this
// r.Handle("/api/user", isAuthenticated(handleGetUserById(s.logger, s.psqlClient), s.logger)).Methods("GET")
r.Handle("/api/user", isAuthenticated(handleGetAllUsers(s.logger, s.psqlClient), s.logger)).Methods("GET")
r.Handle("/api/user/{userId}", isAuthenticated(handleGetUserById(s.logger, s.psqlClient), s.logger)).Methods("GET")
r.Handle("/api/login", isAuthenticated(handleUserLogin(s.logger, s.psqlClient), s.logger)).Methods("POST").Headers("Content-Type", "application/json")

Expand Down Expand Up @@ -754,6 +753,18 @@ func handleGetUserById(logger logger.ServerLogger, client *sql.DB) http.Handler
})
}

func handleGetAllUsers(logger logger.ServerLogger, client *sql.DB) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
users, err := database.NewUser(client).GetAllUsers()
if err != nil {
logger.Error(fmt.Sprintf("error encountered while handling API request: %s", err))
encode(w, r, http.StatusInternalServerError, newHandlerError(ErrUserRetrieve, http.StatusInternalServerError))
return
}
encode(w, r, http.StatusOK, users)
})
}

func handleUpdateMembership(logger logger.ServerLogger, client *sql.DB) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mm, err := decode[models.MembershipModel](r)
Expand Down
11 changes: 11 additions & 0 deletions flowforge_api_bruno/user/all users.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
meta {
name: all users
type: http
seq: 1
}

get {
url: {{HOST}}/user
body: none
auth: inherit
}

0 comments on commit 96c60e7

Please sign in to comment.