From 48cffcbcfedcf7f5680aba13d2692c6a0a42e3fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=20Jurmanovi=C4=87?= Date: Sat, 7 Oct 2023 01:05:44 +0200 Subject: [PATCH] fix user login and register --- pkg/controller/user.go | 8 ++++++++ pkg/filter/user.go | 1 + pkg/repository/user.go | 20 +++++++++++++++----- pkg/service/user.go | 7 +++++-- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/pkg/controller/user.go b/pkg/controller/user.go index a33ecc6..ff13c7f 100644 --- a/pkg/controller/user.go +++ b/pkg/controller/user.go @@ -51,6 +51,14 @@ func (rc *UserController) PostLogin(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } + if body.Email == "" { + c.JSON(400, "Email cannot be empty!") + return + } + if body.Password == "" { + c.JSON(400, "Password cannot be empty!") + return + } returnedUser, exceptionReturn := rc.service.Login(c, body) if exceptionReturn.Message != "" { diff --git a/pkg/filter/user.go b/pkg/filter/user.go index 3075707..d4ad215 100644 --- a/pkg/filter/user.go +++ b/pkg/filter/user.go @@ -5,6 +5,7 @@ import "wallet-api/pkg/model" type UserFilter struct { model.Params BaseFilter + Email string } func NewUserFilter(params model.Params) *UserFilter { diff --git a/pkg/repository/user.go b/pkg/repository/user.go index 0fa4b97..e8747a5 100644 --- a/pkg/repository/user.go +++ b/pkg/repository/user.go @@ -42,6 +42,7 @@ Gets row from transaction table by id. func (us *UserRepository) Get(ctx context.Context, flt *filter.UserFilter, tx *pg.Tx) (*model.User, error) { wm := new(model.User) wm.Id = flt.Id + wm.Email = flt.Email commit := false if tx == nil { @@ -52,7 +53,16 @@ func (us *UserRepository) Get(ctx context.Context, flt *filter.UserFilter, tx *p } qry := tx.Model(wm) - err := common.GenerateEmbed(qry, flt.Embed).WherePK().Select() + + var query = common.GenerateEmbed(qry, flt.Embed) + if wm.Id != "" { + query.WherePK() + } + if wm.Email != "" { + query.Where("? = ?", pg.Ident("email"), wm.Email) + } + + err := query.Select() if err != nil { return nil, err } @@ -116,7 +126,7 @@ Inserts new row to users table. *model.User: User object from database *model.Exception */ -func (us *UserRepository) Check(ctx context.Context, tx *pg.Tx, checkBody *model.User) (*model.User, error) { +func (us *UserRepository) Check(ctx context.Context, tx *pg.Tx, checkBody *model.User) (bool, error) { check := new(model.User) commit := false @@ -127,14 +137,14 @@ func (us *UserRepository) Check(ctx context.Context, tx *pg.Tx, checkBody *model defer tx.Rollback() } - err := tx.Model(check).Where("? = ?", pg.Ident("username"), checkBody.Username).WhereOr("? = ?", pg.Ident("email"), checkBody.Email).Select() + exists, err := tx.Model(check).Where("? = ?", pg.Ident("username"), checkBody.Username).WhereOr("? = ?", pg.Ident("email"), checkBody.Email).Exists() if err != nil { - return nil, err + return false, err } if commit { tx.Commit() } - return check, nil + return exists, nil } /* diff --git a/pkg/service/user.go b/pkg/service/user.go index e448451..1627d08 100644 --- a/pkg/service/user.go +++ b/pkg/service/user.go @@ -50,7 +50,7 @@ func (us *UserService) Create(ctx context.Context, registerBody *model.User) (*m return nil, exceptionReturn } - if check.Username != "" || check.Email != "" { + if check { exceptionReturn.Message = "User already exists" exceptionReturn.ErrorCode = "400101" exceptionReturn.StatusCode = 400 @@ -94,10 +94,13 @@ func (us *UserService) Login(ctx context.Context, body *model.Login) (*model.Tok loginBody.Email = body.Email loginBody.Username = body.Email + flt := filter.NewUserFilter(model.Params{}) + flt.Email = body.Email + tx, _ := us.repository.CreateTx(ctx) defer tx.Rollback() - check, err := us.repository.Check(ctx, tx, loginBody) + check, err := us.repository.Get(ctx, flt, tx) if err != nil { exceptionReturn.Message = "Error checking user" exceptionReturn.ErrorCode = "400139"