Compare commits

...

2 Commits

Author SHA1 Message Date
c8e33884c0 fixes lint issues
All checks were successful
gitea-deepak/gogmagog/pipeline/head This commit looks good
2021-01-11 20:39:29 -06:00
55c8c739b0 fmt
A
2021-01-11 20:38:11 -06:00
3 changed files with 44 additions and 1 deletions

View File

@@ -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,

View File

@@ -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
View 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)
}
}
}