Skip to content

Commit

Permalink
feat: check if username is unique
Browse files Browse the repository at this point in the history
  • Loading branch information
pieceowater committed Dec 15, 2024
1 parent 4d2df89 commit 2f447c8
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 42 deletions.
2 changes: 2 additions & 0 deletions cmd/server/db/pg/migrations/20241215144535.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Modify "users" table
ALTER TABLE "public"."users" ADD CONSTRAINT "uni_users_username" UNIQUE ("username");
3 changes: 2 additions & 1 deletion cmd/server/db/pg/migrations/atlas.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
h1:1+jHxSQv5CtJQzmPjUrQ8E1amoPSgYLGifsr7kJb0OQ=
h1:8I10qPxan1M69VysUB8sFDCPn0I7xQCKR5oUwDqmSMA=
20241214073731.sql h1:H/c5Cccdg1k8DzB+qpeI5Ay2Oh2CBrAIWXKTepKSREM=
20241215144535.sql h1:Pp7oLdKWMHkwqaafy7RSuHSBkOGVHRoSL+zmEiwQm9Q=
2 changes: 0 additions & 2 deletions internal/pkg/auth/svc/auth.svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 0 additions & 16 deletions internal/pkg/user/ctrl/user.ctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
6 changes: 2 additions & 4 deletions internal/pkg/user/ent/user.ent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
20 changes: 1 addition & 19 deletions internal/pkg/user/svc/user.svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 2f447c8

Please sign in to comment.