From 2f447c8e58377a12b237332e8903cc0b896f532c Mon Sep 17 00:00:00 2001 From: pieceowater Date: Sun, 15 Dec 2024 20:09:04 +0500 Subject: [PATCH] feat: check if username is unique #4 --- .../db/pg/migrations/20241215144535.sql | 2 ++ cmd/server/db/pg/migrations/atlas.sum | 3 ++- internal/pkg/auth/svc/auth.svc.go | 2 -- internal/pkg/user/ctrl/user.ctrl.go | 16 --------------- internal/pkg/user/ent/user.ent.go | 6 ++---- internal/pkg/user/svc/user.svc.go | 20 +------------------ 6 files changed, 7 insertions(+), 42 deletions(-) create mode 100644 cmd/server/db/pg/migrations/20241215144535.sql diff --git a/cmd/server/db/pg/migrations/20241215144535.sql b/cmd/server/db/pg/migrations/20241215144535.sql new file mode 100644 index 0000000..6a86d0b --- /dev/null +++ b/cmd/server/db/pg/migrations/20241215144535.sql @@ -0,0 +1,2 @@ +-- Modify "users" table +ALTER TABLE "public"."users" ADD CONSTRAINT "uni_users_username" UNIQUE ("username"); diff --git a/cmd/server/db/pg/migrations/atlas.sum b/cmd/server/db/pg/migrations/atlas.sum index a06fa2a..f847f13 100644 --- a/cmd/server/db/pg/migrations/atlas.sum +++ b/cmd/server/db/pg/migrations/atlas.sum @@ -1,2 +1,3 @@ -h1:1+jHxSQv5CtJQzmPjUrQ8E1amoPSgYLGifsr7kJb0OQ= +h1:8I10qPxan1M69VysUB8sFDCPn0I7xQCKR5oUwDqmSMA= 20241214073731.sql h1:H/c5Cccdg1k8DzB+qpeI5Ay2Oh2CBrAIWXKTepKSREM= +20241215144535.sql h1:Pp7oLdKWMHkwqaafy7RSuHSBkOGVHRoSL+zmEiwQm9Q= diff --git a/internal/pkg/auth/svc/auth.svc.go b/internal/pkg/auth/svc/auth.svc.go index 84057f7..2241eb8 100644 --- a/internal/pkg/auth/svc/auth.svc.go +++ b/internal/pkg/auth/svc/auth.svc.go @@ -102,12 +102,10 @@ func (s *AuthService) Register(user *ent.User) (*string, *ent.User, error) { user.CreatedAt = time.Now() user.State = ent.Suspended - // Save user to DB if err := s.db.GetDB().Create(user).Error; err != nil { return nil, nil, err } - // Generate JWT token token, err := s.generateJWT(user) if err != nil { return nil, nil, err diff --git a/internal/pkg/user/ctrl/user.ctrl.go b/internal/pkg/user/ctrl/user.ctrl.go index f35a328..e3af782 100644 --- a/internal/pkg/user/ctrl/user.ctrl.go +++ b/internal/pkg/user/ctrl/user.ctrl.go @@ -60,22 +60,6 @@ func (c UserController) GetUser(_ context.Context, request *pb.GetUserRequest) ( }, nil } -func (c UserController) CreateUser(_ context.Context, request *pb.CreateUserRequest) (*pb.User, error) { - user, err := c.userService.CreateUser(&ent.User{ - Username: request.Username, - Password: request.Password, - Email: request.Email, - }) - if err != nil { - return nil, err - } - return &pb.User{ - Id: user.ID.String(), - Username: user.Username, - Email: user.Email, - }, err -} - func (c UserController) UpdateUser(_ context.Context, request *pb.UpdateUserRequest) (*pb.User, error) { user, err := c.userService.UpdateUser(&ent.User{ ID: uuid.MustParse(request.Id), diff --git a/internal/pkg/user/ent/user.ent.go b/internal/pkg/user/ent/user.ent.go index 55e1f0a..ca3d2e5 100644 --- a/internal/pkg/user/ent/user.ent.go +++ b/internal/pkg/user/ent/user.ent.go @@ -16,17 +16,15 @@ const ( type User struct { gorm.Model ID uuid.UUID `gorm:"type:uuid;primaryKey"` - Username string `gorm:"type:varchar(255);not null"` + Username string `gorm:"type:varchar(255);unique;not null"` Email string `gorm:"type:varchar(255);unique;not null"` Password string `gorm:"type:varchar(255);not null"` State UserState `gorm:"type:smallint;default:100"` // Default to Suspended Friends []*User `gorm:"many2many:friendships;joinForeignKey:UserID;joinReferences:FriendID"` } -// BeforeCreate Hook for generating custom UUID +// BeforeCreate Hook for generating UUID func (u *User) BeforeCreate(tx *gorm.DB) (err error) { - // Generate custom UUID for user u.ID = uuid.New() - //todo: generate hashed password here ONLY return nil } diff --git a/internal/pkg/user/svc/user.svc.go b/internal/pkg/user/svc/user.svc.go index b1a65c1..873254a 100644 --- a/internal/pkg/user/svc/user.svc.go +++ b/internal/pkg/user/svc/user.svc.go @@ -3,11 +3,8 @@ package svc import ( pb "app/internal/core/grpc/generated" "app/internal/pkg/user/ent" - "errors" "fmt" gossiper "github.com/pieceowater-dev/lotof.lib.gossiper/v2" - "golang.org/x/crypto/bcrypt" - "gorm.io/gorm" ) type UserService struct { @@ -18,24 +15,9 @@ func NewUserService(db gossiper.Database) *UserService { return &UserService{db: db} } -func (s *UserService) CreateUser(user *ent.User) (*ent.User, error) { // todo: delete this later - hashedPassword, err := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost) - if err != nil { - return nil, err - } - user.Password = string(hashedPassword) - if err := s.db.GetDB().Create(user).Error; err != nil { - if errors.Is(err, gorm.ErrDuplicatedKey) { - return nil, errors.New("email already exists") - } - return nil, err - } - return user, nil -} - func (s *UserService) GetUserByID(id string) (*ent.User, error) { var user ent.User - if err := s.db.GetDB().Preload("Friends").First(&user, "id = ?", id).Error; err != nil { + if err := s.db.GetDB().First(&user, "id = ?", id).Error; err != nil { return nil, err } return &user, nil