Skip to content

Commit

Permalink
refactor: why should you use uint? ditch it
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyoptimist committed Jun 16, 2024
1 parent 7c2813a commit 53cb36b
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 23 deletions.
17 changes: 8 additions & 9 deletions internal/domain/auth/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
)

const (
AccessTokenKeyId uint = iota + 1
AccessTokenKeyId int = iota + 1
RefreshTokenKeyId
)

func GenerateAccessToken(userId uint) (string, error) {
func GenerateAccessToken(userId int) (string, error) {

secretKey := []byte(config.Global.JwtAccessTokenSecret)
expiresIn := config.Global.JwtAccessTokenExpiresIn
Expand All @@ -38,7 +38,7 @@ func GenerateAccessToken(userId uint) (string, error) {
return accessToken, nil
}

func GenerateRefreshToken(userId uint) (string, error) {
func GenerateRefreshToken(userId int) (string, error) {

secretKey := []byte(config.Global.JwtRefreshTokenSecret)
expiresIn := config.Global.JwtRefreshTokenExpiresIn
Expand All @@ -60,7 +60,7 @@ func GenerateRefreshToken(userId uint) (string, error) {
return refreshToken, nil
}

func GenerateTokenPair(userId uint) (accessToken, refreshToken string, err error) {
func GenerateTokenPair(userId int) (accessToken, refreshToken string, err error) {

accessToken, err = GenerateAccessToken(userId)
if err != nil {
Expand All @@ -75,7 +75,7 @@ func GenerateTokenPair(userId uint) (accessToken, refreshToken string, err error
return
}

func ValidateToken(tokenString string) (isValid bool, userId uint, keyId uint, err error) {
func ValidateToken(tokenString string) (isValid bool, userId int, keyId int, err error) {

var key []byte

Expand All @@ -86,7 +86,7 @@ func ValidateToken(tokenString string) (isValid bool, userId uint, keyId uint, e
claims,
func(token *jwt.Token) (interface{}, error) {

keyId = uint(token.Header["kid"].(float64))
keyId = int(token.Header["kid"].(float64))

switch keyId {
case AccessTokenKeyId:
Expand All @@ -111,9 +111,8 @@ func ValidateToken(tokenString string) (isValid bool, userId uint, keyId uint, e

isValid = true

sub, _ := claims.GetSubject()
subAsInt, _ := strconv.Atoi(sub)
userId = uint(subAsInt)
sub, err := claims.GetSubject()
userId, err = strconv.Atoi(sub)

return
}
2 changes: 1 addition & 1 deletion internal/domain/auth/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestGenerateAccessToken(t *testing.T) {

t.Run("Generate a valid access token", func(t *testing.T) {

userId := uint(1)
userId := 1
accessToken, err := GenerateAccessToken(userId)
assert.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion internal/domain/model/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

// Same as gorm.Model, but including column and json tags
type Common struct {
ID uint `gorm:"primaryKey;column:id" json:"id"`
ID int `gorm:"primaryKey;column:id" json:"id"`
CreatedAt time.Time `gorm:"column:created_at" json:"createdAt"`
UpdatedAt time.Time `gorm:"column:updated_at" json:"updatedAt"`
DeletedAt *time.Time `gorm:"column:deleted_at" json:"deletedAt"`
Expand Down
2 changes: 1 addition & 1 deletion internal/domain/user/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type UpdateUserDto struct {
Password string `json:"password"`
}

func MapUpdateUserDto(dto *UpdateUserDto, id uint) model.User {
func MapUpdateUserDto(dto *UpdateUserDto, id int) model.User {
var hashedPassword string
if dto.Password != "" {
hashedPassword, _ = utils.HashPassword(dto.Password)
Expand Down
2 changes: 1 addition & 1 deletion internal/domain/user/repository_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (r *userRepositoryStub) FindAll(
return r.records, 2, nil
}

func (r *userRepositoryStub) FindById(id uint) (*model.User, error) {
func (r *userRepositoryStub) FindById(id int) (*model.User, error) {
for _, record := range r.records {
if record.ID == id {
return &record, nil
Expand Down
8 changes: 4 additions & 4 deletions internal/domain/user/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type UserRepository interface {
sortParams []common.SortParam,
filterParams []common.FilterParam,
) ([]model.User, int64, error)
FindById(id uint) (*model.User, error)
FindById(id int) (*model.User, error)
FindByEmail(email string) (*model.User, error)
Create(user model.User) (*model.User, error)
Update(user model.User) (*model.User, error)
Expand All @@ -35,18 +35,18 @@ func (u *UserService) FindAll(
return u.UserRepository.FindAll(paginationParam, sortParams, filterParams)
}

func (u *UserService) FindById(id uint) (*model.User, error) {
func (u *UserService) FindById(id int) (*model.User, error) {
return u.UserRepository.FindById(id)
}

func (u *UserService) Create(createUserDto *CreateUserDto) (*model.User, error) {
return u.UserRepository.Create(MapCreateUserDto(createUserDto))
}

func (u *UserService) Update(updateUserDto *UpdateUserDto, id uint) (*model.User, error) {
func (u *UserService) Update(updateUserDto *UpdateUserDto, id int) (*model.User, error) {
return u.UserRepository.Update(MapUpdateUserDto(updateUserDto, id))
}

func (u *UserService) Delete(id uint) error {
func (u *UserService) Delete(id int) error {
return u.UserRepository.Delete(model.User{Common: model.Common{ID: id}})
}
10 changes: 5 additions & 5 deletions internal/infrastructure/controller/user_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (u *userController) FindById(c *gin.Context) {
return
}

user, err := u.UserService.FindById(uint(id))
user, err := u.UserService.FindById(id)
if err != nil {
common.RaiseHttpError(c, http.StatusNotFound, err)
return
Expand Down Expand Up @@ -114,7 +114,7 @@ func (u *userController) Create(c *gin.Context) {
func (u *userController) Me(c *gin.Context) {
id, _ := c.Get("user")

user, err := u.UserService.FindById(uint(id.(int)))
user, err := u.UserService.FindById(id.(int))
if err != nil {
common.RaiseHttpError(c, http.StatusNotFound, err)
return
Expand All @@ -141,7 +141,7 @@ func (u *userController) Update(c *gin.Context) {
return
}

if _, err := u.UserService.FindById(uint(id)); err != nil {
if _, err := u.UserService.FindById(id); err != nil {
common.RaiseHttpError(c, http.StatusNotFound, err)
return
}
Expand All @@ -152,7 +152,7 @@ func (u *userController) Update(c *gin.Context) {
return
}

user, err := u.UserService.Update(&updateUserDto, uint(id))
user, err := u.UserService.Update(&updateUserDto, id)
if err != nil {
common.RaiseHttpError(c, http.StatusInternalServerError, err)
return
Expand All @@ -178,7 +178,7 @@ func (u *userController) Delete(c *gin.Context) {
return
}

if err := u.UserService.Delete(uint(id)); err != nil {
if err := u.UserService.Delete(id); err != nil {
common.RaiseHttpError(c, http.StatusInternalServerError, err)
return
}
Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/repository/user_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (u *userRepository) FindAll(
return users, totalCount, nil
}

func (u *userRepository) FindById(id uint) (*model.User, error) {
func (u *userRepository) FindById(id int) (*model.User, error) {
var user model.User

err := u.DB.Where("id = ?", id).First(&user).Error
Expand Down

0 comments on commit 53cb36b

Please sign in to comment.