29 lines
581 B
Go
29 lines
581 B
Go
package controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
userService "git.farahty.com/nimer/go-mongo/services/user"
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
func UserRouter() chi.Router {
|
|
router := chi.NewRouter()
|
|
router.Get("/", getUsers)
|
|
return router
|
|
}
|
|
|
|
func getUsers(rw http.ResponseWriter, r *http.Request) {
|
|
rw.Header().Set("content-type", "application/json")
|
|
|
|
users, err := userService.Find(r.Context())
|
|
if err != nil {
|
|
rw.WriteHeader(http.StatusInternalServerError)
|
|
rw.Write([]byte(`{"message": ` + err.Error() + `}`))
|
|
return
|
|
}
|
|
|
|
json.NewEncoder(rw).Encode(users)
|
|
}
|