Compare commits
2 Commits
1d45635530
...
c8e33884c0
| Author | SHA1 | Date | |
|---|---|---|---|
|
c8e33884c0
|
|||
|
55c8c739b0
|
@@ -9,6 +9,8 @@ type User struct {
|
||||
Password []byte
|
||||
}
|
||||
|
||||
// UserNoPassword contains the non password user fields.
|
||||
// This is preferred outside of the model / store.
|
||||
type UserNoPassword struct {
|
||||
UserID int64 `json:"user_id"`
|
||||
Username string `json:"username"`
|
||||
@@ -24,6 +26,7 @@ func (m *Model) User(id int) (*UserNoPassword, error) {
|
||||
return user.NoPassword(), wrapNotFound(err)
|
||||
}
|
||||
|
||||
// NoPassword strips the user of password.
|
||||
func (u *User) NoPassword() *UserNoPassword {
|
||||
return &UserNoPassword{
|
||||
UserID: u.UserID,
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package models_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gitea.deepak.science/deepak/gogmagog/models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
||||
40
routes/users.go
Normal file
40
routes/users.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"gitea.deepak.science/deepak/gogmagog/models"
|
||||
"github.com/go-chi/chi"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func newUserRouter(m *models.Model) http.Handler {
|
||||
router := chi.NewRouter()
|
||||
// router.Post("/", postUserFunc(m))
|
||||
router.Get("/{userid}", getUserByIDFunc(m))
|
||||
return router
|
||||
}
|
||||
|
||||
func getUserByIDFunc(m *models.Model) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.Atoi(chi.URLParam(r, "userid"))
|
||||
if err != nil {
|
||||
notFoundHandler(w, r)
|
||||
return
|
||||
}
|
||||
user, err := m.User(id)
|
||||
if err != nil {
|
||||
if models.IsNotFoundError(err) {
|
||||
notFoundHandler(w, r)
|
||||
return
|
||||
}
|
||||
serverError(w, err)
|
||||
return
|
||||
|
||||
}
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
if err := json.NewEncoder(w).Encode(user); err != nil {
|
||||
serverError(w, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user