forked from mirrors/homebox
align types with new db schema
This commit is contained in:
parent
63cfeffc4d
commit
b83505104a
30 changed files with 1491 additions and 263 deletions
|
@ -4,13 +4,14 @@ import (
|
|||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/hay-kot/content/backend/ent"
|
||||
"github.com/hay-kot/content/backend/internal/mocks"
|
||||
"github.com/hay-kot/content/backend/internal/mocks/factories"
|
||||
"github.com/hay-kot/content/backend/internal/types"
|
||||
)
|
||||
|
||||
var mockHandler = &V1Controller{}
|
||||
var users = []types.UserOut{}
|
||||
var users = []*ent.User{}
|
||||
|
||||
func userPool() func() {
|
||||
create := []types.UserCreate{
|
||||
|
@ -20,7 +21,7 @@ func userPool() func() {
|
|||
factories.UserFactory(),
|
||||
}
|
||||
|
||||
userOut := []types.UserOut{}
|
||||
userOut := []*ent.User{}
|
||||
|
||||
for _, user := range create {
|
||||
usrOut, _ := mockHandler.svc.Admin.Create(context.Background(), user)
|
||||
|
|
|
@ -17,7 +17,7 @@ import (
|
|||
// @Summary Gets all users from the database
|
||||
// @Tags Admin: Users
|
||||
// @Produce json
|
||||
// @Success 200 {object} server.Result{item=[]types.UserOut}
|
||||
// @Success 200 {object} server.Result{item=[]ent.User}
|
||||
// @Router /v1/admin/users [get]
|
||||
// @Security Bearer
|
||||
func (ctrl *V1Controller) HandleAdminUserGetAll() http.HandlerFunc {
|
||||
|
@ -38,7 +38,7 @@ func (ctrl *V1Controller) HandleAdminUserGetAll() http.HandlerFunc {
|
|||
// @Tags Admin: Users
|
||||
// @Produce json
|
||||
// @Param id path string true "User ID"
|
||||
// @Success 200 {object} server.Result{item=types.UserOut}
|
||||
// @Success 200 {object} server.Result{item=ent.User}
|
||||
// @Router /v1/admin/users/{id} [get]
|
||||
// @Security Bearer
|
||||
func (ctrl *V1Controller) HandleAdminUserGet() http.HandlerFunc {
|
||||
|
@ -71,7 +71,7 @@ func (ctrl *V1Controller) HandleAdminUserGet() http.HandlerFunc {
|
|||
// @Tags Admin: Users
|
||||
// @Produce json
|
||||
// @Param payload body types.UserCreate true "User Data"
|
||||
// @Success 200 {object} server.Result{item=types.UserOut}
|
||||
// @Success 200 {object} server.Result{item=ent.User}
|
||||
// @Router /v1/admin/users [POST]
|
||||
// @Security Bearer
|
||||
func (ctrl *V1Controller) HandleAdminUserCreate() http.HandlerFunc {
|
||||
|
@ -129,7 +129,7 @@ func (ctrl *V1Controller) HandleAdminUserCreate() http.HandlerFunc {
|
|||
// @Param id path string true "User ID"
|
||||
// @Param payload body types.UserUpdate true "User Data"
|
||||
// @Produce json
|
||||
// @Success 200 {object} server.Result{item=types.UserOut}
|
||||
// @Success 200 {object} server.Result{item=ent.User}
|
||||
// @Router /v1/admin/users/{id} [PUT]
|
||||
// @Security Bearer
|
||||
func (ctrl *V1Controller) HandleAdminUserUpdate() http.HandlerFunc {
|
||||
|
|
|
@ -9,9 +9,9 @@ import (
|
|||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/hay-kot/content/backend/ent"
|
||||
"github.com/hay-kot/content/backend/internal/mocks/chimocker"
|
||||
"github.com/hay-kot/content/backend/internal/mocks/factories"
|
||||
"github.com/hay-kot/content/backend/internal/types"
|
||||
"github.com/hay-kot/content/backend/pkgs/server"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -23,11 +23,11 @@ const (
|
|||
)
|
||||
|
||||
type usersResponse struct {
|
||||
Users []types.UserOut `json:"item"`
|
||||
Users []*ent.User `json:"item"`
|
||||
}
|
||||
|
||||
type userResponse struct {
|
||||
User types.UserOut `json:"item"`
|
||||
User *ent.User `json:"item"`
|
||||
}
|
||||
|
||||
func Test_HandleAdminUserGetAll_Success(t *testing.T) {
|
||||
|
@ -37,7 +37,7 @@ func Test_HandleAdminUserGetAll_Success(t *testing.T) {
|
|||
mockHandler.HandleAdminUserGetAll()(r, req)
|
||||
|
||||
response := usersResponse{
|
||||
Users: []types.UserOut{},
|
||||
Users: []*ent.User{},
|
||||
}
|
||||
|
||||
_ = json.Unmarshal(r.Body.Bytes(), &response)
|
||||
|
@ -68,7 +68,7 @@ func Test_HandleAdminUserGet_Success(t *testing.T) {
|
|||
assert.Equal(t, http.StatusOK, res.Code)
|
||||
|
||||
response := userResponse{
|
||||
User: types.UserOut{},
|
||||
User: &ent.User{},
|
||||
}
|
||||
|
||||
_ = json.Unmarshal(res.Body.Bytes(), &response)
|
||||
|
|
|
@ -4,24 +4,53 @@ import (
|
|||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/hay-kot/content/backend/internal/services"
|
||||
"github.com/hay-kot/content/backend/internal/types"
|
||||
"github.com/hay-kot/content/backend/pkgs/logger"
|
||||
"github.com/hay-kot/content/backend/pkgs/server"
|
||||
)
|
||||
|
||||
// HandleUserSelf godoc
|
||||
// @Summary Get the current user
|
||||
// @Tags User
|
||||
// @Produce json
|
||||
// @Param payload body types.UserRegistration true "User Data"
|
||||
// @Success 200 {object} server.Result{item=ent.User}
|
||||
// @Router /v1/users/register [Post]
|
||||
func (ctrl *V1Controller) HandleUserRegistration() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
regData := types.UserRegistration{}
|
||||
|
||||
if err := server.Decode(r, ®Data); err != nil {
|
||||
ctrl.log.Error(err, nil)
|
||||
server.RespondError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
usr, err := ctrl.svc.User.RegisterUser(r.Context(), regData)
|
||||
if err != nil {
|
||||
ctrl.log.Error(err, nil)
|
||||
server.RespondError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
_ = server.Respond(w, http.StatusOK, server.Wrap(usr))
|
||||
}
|
||||
}
|
||||
|
||||
// HandleUserSelf godoc
|
||||
// @Summary Get the current user
|
||||
// @Tags User
|
||||
// @Produce json
|
||||
// @Success 200 {object} server.Result{item=types.UserOut}
|
||||
// @Success 200 {object} server.Result{item=ent.User}
|
||||
// @Router /v1/users/self [GET]
|
||||
// @Security Bearer
|
||||
func (ctrl *V1Controller) HandleUserSelf() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
token := services.UseTokenCtx(r.Context())
|
||||
usr, err := ctrl.svc.User.GetSelf(r.Context(), token)
|
||||
if usr.IsNull() || err != nil {
|
||||
if usr.ID == uuid.Nil || err != nil {
|
||||
ctrl.log.Error(errors.New("no user within request context"), nil)
|
||||
server.RespondInternalServerError(w)
|
||||
return
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue