Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
markgenuine committed Feb 26, 2024
1 parent 9800a12 commit 6355ec9
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 42 deletions.
8 changes: 4 additions & 4 deletions internal/app/auth_v1/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ func (s *User) Create(ctx context.Context, req *desc.CreateRequest) (*desc.Creat
fmt.Printf("Create User email: %s", req.GetEmail())
fmt.Printf("Create User role: %s", req.GetRole())

query, args, err := s.sq.Insert(Users).
Columns(UsersName, UsersEmail, UsersPassword, UsersPasswordConfirm, UsersRole).
Values(req.GetName(), req.GetEmail(), req.GetPassword(), req.GetPasswordConfirm(), req.GetRole()).
Suffix(fmt.Sprintf("RETURNING %s", UsersID)).ToSql()
query, args, err := s.sq.Insert(users).
Columns(usersName, usersEmail, usersPassword, usersRole).
Values(req.GetName(), req.GetEmail(), req.GetPassword(), req.GetRole()).
Suffix(fmt.Sprintf("RETURNING %s", usersID)).ToSql()

if err != nil {
log.Printf("failed to build query create user: %s", err.Error())
Expand Down
4 changes: 2 additions & 2 deletions internal/app/auth_v1/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
func (s *User) Delete(ctx context.Context, req *desc.DeleteRequest) (*empty.Empty, error) {
fmt.Printf("User delete with ID: %d", req.GetId())

query, args, err := s.sq.Delete(Users).Where(squirrel.Eq{
UsersID: req.GetId(),
query, args, err := s.sq.Delete(users).Where(squirrel.Eq{
usersID: req.GetId(),
}).ToSql()

if err != nil {
Expand Down
15 changes: 11 additions & 4 deletions internal/app/auth_v1/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package auth_v1

import (
"context"
"errors"
"fmt"
"log"

"github.com/Masterminds/squirrel"
"github.com/jackc/pgx/v5"
desc "github.com/markgenuine/auth/pkg/auth_v1"
)

Expand All @@ -14,19 +16,24 @@ func (s *User) Get(ctx context.Context, req *desc.GetRequest) (*desc.GetResponse
fmt.Printf("Get user with ID: %d", req.GetId())

query, args, err := s.sq.Select(
UsersID, UsersName, UsersEmail, UsersPassword, UsersPasswordConfirm,
UsersRole, UsersCreatedAt, UsersUpdatedAt).
Where(squirrel.Eq{UsersID: req.GetId()}).
usersID, usersName, usersEmail, usersPassword,
usersRole, usersCreatedAt, usersUpdatedAt).
Where(squirrel.Eq{usersID: req.GetId()}).
ToSql()

if err != nil {
log.Printf("failed to build query get user: %s", err.Error())
return nil, err
}

var res *desc.GetResponse

err = s.poolDB.QueryRow(ctx, query, args...).Scan(res)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {

Check failure on line 33 in internal/app/auth_v1/get.go

View workflow job for this annotation

GitHub Actions / lint

undefined: pgx (typecheck)
return nil, errors.New("user not found")
}

log.Printf("failed to get user: %s", err.Error())
return nil, err
}
Expand Down
34 changes: 8 additions & 26 deletions internal/app/auth_v1/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,14 @@ import (
)

const (
// Users ...
Users = "users"

// UsersID ...
UsersID = "id"

// UsersName ...
UsersName = "name"

// UsersEmail ...
UsersEmail = "email"

// UsersPassword ...
UsersPassword = "password"

// UsersPasswordConfirm ...
UsersPasswordConfirm = "password_confirm"

// UsersRole ...
UsersRole = "role"

// UsersCreatedAt ...
UsersCreatedAt = "created_at"

// UsersUpdatedAt ...
UsersUpdatedAt = "updated_at"
users = "users"
usersID = "id"
usersName = "name"
usersEmail = "email"
usersPassword = "password"
usersRole = "role"
usersCreatedAt = "created_at"
usersUpdatedAt = "updated_at"
)

// User - type for proto implementation
Expand Down
10 changes: 5 additions & 5 deletions internal/app/auth_v1/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,26 @@ import (
func (s *User) Update(ctx context.Context, req *desc.UpdateRequest) (*empty.Empty, error) {
fmt.Printf("Update user with ID: %d", req.GetId())

builder := s.sq.Update(Users).Where(squirrel.Eq{UsersID: req.GetId()})
builder := s.sq.Update(users).Where(squirrel.Eq{usersID: req.GetId()})

var modify bool
if req.GetName() != nil {
builder.Set(UsersName, req.GetName())
builder.Set(usersName, req.GetName())
modify = true
}

if req.GetEmail() != nil {
builder.Set(UsersEmail, req.GetEmail())
builder.Set(usersEmail, req.GetEmail())
modify = true
}

if req.GetRole() != desc.Role_UNKNOWN {
builder.Set(UsersRole, req.GetRole().String())
builder.Set(usersRole, req.GetRole().String())
modify = true
}

if modify {
builder.Set(UsersUpdatedAt, time.Now())
builder.Set(usersUpdatedAt, time.Now())
query, args, err := builder.ToSql()
if err != nil {
log.Printf("failed to build query update user: %s", err.Error())
Expand Down
1 change: 0 additions & 1 deletion migrations/20240224143554_init_db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ create table if not exists users(
name text not null,
email text not null,
password text not null,
password_confirm text not null,
role text not null,
created_at timestamp not null default now(),
updated_at timestamp
Expand Down

0 comments on commit 6355ec9

Please sign in to comment.