fix: create DTO in model and handle it in service level
This commit is contained in:
parent
382da3d811
commit
606289be1a
@ -64,19 +64,14 @@ type createParams struct {
|
|||||||
// Since we use JWT method, this token is not stored anywhere. Thus it
|
// Since we use JWT method, this token is not stored anywhere. Thus it
|
||||||
// stops at the controller level.
|
// stops at the controller level.
|
||||||
func (sc *SessionController) Create(ctx *gin.Context) {
|
func (sc *SessionController) Create(ctx *gin.Context) {
|
||||||
var params createParams
|
var user model.UserExistDTO
|
||||||
|
|
||||||
if err := ctx.Bind(¶ms); err != nil {
|
if err := ctx.Bind(&user); err != nil {
|
||||||
log.ErrorLog("param error", "err", err)
|
log.ErrorLog("param error", "err", err)
|
||||||
core.WriteResponse(ctx, UserParamsErr, nil)
|
core.WriteResponse(ctx, UserParamsErr, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
user := model.User{
|
|
||||||
Email: params.Email,
|
|
||||||
Password: params.Password,
|
|
||||||
}
|
|
||||||
|
|
||||||
err := sc.userUsecase.Exist(ctx, &user)
|
err := sc.userUsecase.Exist(ctx, &user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
core.WriteResponse(ctx, err, nil)
|
core.WriteResponse(ctx, err, nil)
|
||||||
|
@ -35,11 +35,11 @@ func NewtestUserUsecase() usecase.User {
|
|||||||
return &testUserUsecase{}
|
return &testUserUsecase{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*testUserUsecase) Create(ctx context.Context, u *model.User) (*model.User, error) {
|
func (*testUserUsecase) Create(ctx context.Context, u *model.UserCreateDTO) (*model.User, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*testUserUsecase) Exist(ctx context.Context, u *model.User) error {
|
func (*testUserUsecase) Exist(ctx context.Context, u *model.UserExistDTO) error {
|
||||||
switch u.Email {
|
switch u.Email {
|
||||||
case "a@b.c":
|
case "a@b.c":
|
||||||
if u.Password == "strong password" {
|
if u.Password == "strong password" {
|
||||||
|
@ -57,26 +57,14 @@ func NewUserController(us usecase.User) User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (uc *UserController) Create(ctx core.Context) {
|
func (uc *UserController) Create(ctx core.Context) {
|
||||||
var params struct {
|
var userDTO model.UserCreateDTO
|
||||||
Email string `json:"email" binding:"required,email"`
|
|
||||||
FirstName string `json:"first_name" binding:"required"`
|
|
||||||
LastName string `json:"last_name" binding:"required"`
|
|
||||||
Password string `json:"password" binding:"required"`
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := ctx.Bind(¶ms); err != nil {
|
if err := ctx.Bind(&userDTO); err != nil {
|
||||||
core.WriteResponse(ctx, UserParamsErr, nil)
|
core.WriteResponse(ctx, UserParamsErr, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
user := model.User{
|
_, err := uc.userUsecase.Create(ctx, &userDTO)
|
||||||
Email: params.Email,
|
|
||||||
FirstName: params.FirstName,
|
|
||||||
LastName: params.LastName,
|
|
||||||
Password: params.Password,
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := uc.userUsecase.Create(ctx, &user)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
core.WriteResponse(ctx, err, nil)
|
core.WriteResponse(ctx, err, nil)
|
||||||
return
|
return
|
||||||
|
@ -24,6 +24,18 @@ package model
|
|||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
|
type UserCreateDTO struct {
|
||||||
|
Email string `json:"email" binding:"required,email"`
|
||||||
|
FirstName string `json:"first_name" binding:"required"`
|
||||||
|
LastName string `json:"last_name" binding:"required"`
|
||||||
|
Password string `json:"password" binding:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserExistDTO struct {
|
||||||
|
Email string `json:"email" binding:"required,email"`
|
||||||
|
Password string `json:"password" binding:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
// User model
|
// User model
|
||||||
type User struct {
|
type User struct {
|
||||||
ID int
|
ID int
|
||||||
|
@ -60,8 +60,8 @@ type userUsecase struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type User interface {
|
type User interface {
|
||||||
Create(ctx context.Context, u *model.User) (*model.User, error)
|
Create(ctx context.Context, u *model.UserCreateDTO) (*model.User, error)
|
||||||
Exist(ctx context.Context, u *model.User) error
|
Exist(ctx context.Context, u *model.UserExistDTO) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUserUsecase(r repo.UserRepository, d repo.DBRepository) User {
|
func NewUserUsecase(r repo.UserRepository, d repo.DBRepository) User {
|
||||||
@ -71,7 +71,7 @@ func NewUserUsecase(r repo.UserRepository, d repo.DBRepository) User {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (uuc *userUsecase) Create(ctx context.Context, u *model.User) (*model.User, error) {
|
func (uuc *userUsecase) Create(ctx context.Context, u *model.UserCreateDTO) (*model.User, error) {
|
||||||
// Hash the password
|
// Hash the password
|
||||||
encrypted, err := bcrypt.GenerateFromPassword([]byte(u.Password), 12)
|
encrypted, err := bcrypt.GenerateFromPassword([]byte(u.Password), 12)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -82,7 +82,12 @@ func (uuc *userUsecase) Create(ctx context.Context, u *model.User) (*model.User,
|
|||||||
data, err := uuc.dbRepo.Transaction(
|
data, err := uuc.dbRepo.Transaction(
|
||||||
ctx,
|
ctx,
|
||||||
func(txCtx context.Context, tx interface{}) (interface{}, error) {
|
func(txCtx context.Context, tx interface{}) (interface{}, error) {
|
||||||
u, err := uuc.userRepo.Create(txCtx, tx, u)
|
created, err := uuc.userRepo.Create(txCtx, tx, &model.User{
|
||||||
|
Email: u.Email,
|
||||||
|
Password: u.Password,
|
||||||
|
FirstName: u.FirstName,
|
||||||
|
LastName: u.LastName,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
match, _ := regexp.MatchString("SQLSTATE 23505", err.Error())
|
match, _ := regexp.MatchString("SQLSTATE 23505", err.Error())
|
||||||
if match {
|
if match {
|
||||||
@ -100,7 +105,7 @@ func (uuc *userUsecase) Create(ctx context.Context, u *model.User) (*model.User,
|
|||||||
fmt.Sprintf("%s %s", u.FirstName, u.LastName),
|
fmt.Sprintf("%s %s", u.FirstName, u.LastName),
|
||||||
)
|
)
|
||||||
|
|
||||||
return u, err
|
return created, err
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -113,7 +118,7 @@ func (uuc *userUsecase) Create(ctx context.Context, u *model.User) (*model.User,
|
|||||||
return user, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (uuc *userUsecase) Exist(ctx context.Context, u *model.User) error {
|
func (uuc *userUsecase) Exist(ctx context.Context, u *model.UserExistDTO) error {
|
||||||
got, err := uuc.userRepo.GetByEmail(ctx, u.Email)
|
got, err := uuc.userRepo.GetByEmail(ctx, u.Email)
|
||||||
// Any query error?
|
// Any query error?
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -29,30 +29,42 @@ import (
|
|||||||
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/model"
|
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/model"
|
||||||
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/usecase/usecase/repomock"
|
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/usecase/usecase/repomock"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCreateUser(t *testing.T) {
|
func TestCreateUser(t *testing.T) {
|
||||||
t.Run("normal create", func(t *testing.T) {
|
t.Run("normal create", func(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
userUsecase := NewUserUsecase(&repomock.TestUserRepository{}, &repomock.TestDBRepository{})
|
userUsecase := NewUserUsecase(&repomock.TestUserRepository{}, &repomock.TestDBRepository{})
|
||||||
input := &model.User{
|
input := &model.UserCreateDTO{
|
||||||
Email: "a@b.c",
|
Email: "a@b.c",
|
||||||
FirstName: "James",
|
FirstName: "James",
|
||||||
LastName: "Bond",
|
LastName: "Bond",
|
||||||
Password: "verystrong",
|
Password: "verystrong",
|
||||||
}
|
}
|
||||||
want := input
|
want := &model.User{
|
||||||
want.ID = 123
|
ID: 123,
|
||||||
|
Email: input.Email,
|
||||||
|
FirstName: input.FirstName,
|
||||||
|
LastName: input.LastName,
|
||||||
|
// Password is hashed
|
||||||
|
Password: "verystrong",
|
||||||
|
}
|
||||||
|
|
||||||
got, err := userUsecase.Create(ctx, input)
|
got, err := userUsecase.Create(ctx, input)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, want, got)
|
assert.Equal(t, want.ID, got.ID)
|
||||||
|
|
||||||
|
assert.NoError(
|
||||||
|
t,
|
||||||
|
bcrypt.CompareHashAndPassword([]byte(got.Password), []byte(want.Password)),
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("duplicate create", func(t *testing.T) {
|
t.Run("duplicate create", func(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
userUsecase := NewUserUsecase(&repomock.TestUserRepository{}, &repomock.TestDBRepository{})
|
userUsecase := NewUserUsecase(&repomock.TestUserRepository{}, &repomock.TestDBRepository{})
|
||||||
input := &model.User{
|
input := &model.UserCreateDTO{
|
||||||
Email: "duplicate@error.com",
|
Email: "duplicate@error.com",
|
||||||
FirstName: "James",
|
FirstName: "James",
|
||||||
LastName: "Bond",
|
LastName: "Bond",
|
||||||
@ -67,22 +79,22 @@ func TestCreateUser(t *testing.T) {
|
|||||||
func TestUserExist(t *testing.T) {
|
func TestUserExist(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
Name string
|
Name string
|
||||||
User *model.User
|
User *model.UserExistDTO
|
||||||
ExpErr error
|
ExpErr error
|
||||||
}{
|
}{
|
||||||
{"user exists", &model.User{
|
{"user exists", &model.UserExistDTO{
|
||||||
Email: "a@b.c",
|
Email: "a@b.c",
|
||||||
Password: "strongHashed",
|
Password: "strongHashed",
|
||||||
}, nil},
|
}, nil},
|
||||||
{"query error", &model.User{
|
{"query error", &model.UserExistDTO{
|
||||||
Email: "query@error.com",
|
Email: "query@error.com",
|
||||||
Password: "strongHashed",
|
Password: "strongHashed",
|
||||||
}, repomock.UserTestDummyErr},
|
}, repomock.UserTestDummyErr},
|
||||||
{"user doesn not exist", &model.User{
|
{"user doesn not exist", &model.UserExistDTO{
|
||||||
Email: "inexist@error.com",
|
Email: "inexist@error.com",
|
||||||
Password: "strongHashed",
|
Password: "strongHashed",
|
||||||
}, UserNotExist},
|
}, UserNotExist},
|
||||||
{"wrong password", &model.User{
|
{"wrong password", &model.UserExistDTO{
|
||||||
Email: "a@b.c",
|
Email: "a@b.c",
|
||||||
Password: "wrongHashed",
|
Password: "wrongHashed",
|
||||||
}, UserWrongPassword},
|
}, UserWrongPassword},
|
||||||
|
Loading…
Reference in New Issue
Block a user