mirror of
https://github.com/hanzo-fi/stack.git
synced 2026-10-30 20:34:43 +00:00
38 lines
851 B
Go
38 lines
851 B
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
auth "github.com/formancehq/auth/pkg"
|
|
"github.com/gorilla/mux"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func addUserRoutes(db *gorm.DB, router *mux.Router) {
|
|
router.Path("/users").Methods(http.MethodGet).HandlerFunc(listUsers(db))
|
|
router.Path("/users/{userId}").Methods(http.MethodGet).HandlerFunc(readUser(db))
|
|
}
|
|
|
|
func listUsers(db *gorm.DB) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
users := make([]auth.User, 0)
|
|
if err := db.
|
|
WithContext(r.Context()).
|
|
Find(&users).Error; err != nil {
|
|
internalServerError(w, r, err)
|
|
return
|
|
}
|
|
writeJSONObject(w, r, users)
|
|
}
|
|
}
|
|
|
|
func readUser(db *gorm.DB) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
user := findById[auth.User](w, r, db, "userId")
|
|
if user == nil {
|
|
return
|
|
}
|
|
writeJSONObject(w, r, user)
|
|
}
|
|
}
|