Skip to content

Commit

Permalink
Move redis to infra
Browse files Browse the repository at this point in the history
  • Loading branch information
naeemaei committed Apr 29, 2024
1 parent 610ef63 commit 39be160
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 38 deletions.
22 changes: 11 additions & 11 deletions src/api/middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/golang-jwt/jwt"
"github.com/naeemaei/golang-clean-web-api/api/helper"
"github.com/naeemaei/golang-clean-web-api/config"
constants "github.com/naeemaei/golang-clean-web-api/constant"
constant "github.com/naeemaei/golang-clean-web-api/constant"
"github.com/naeemaei/golang-clean-web-api/pkg/service_errors"
"github.com/naeemaei/golang-clean-web-api/usecase"
)
Expand All @@ -20,7 +20,7 @@ func Authentication(cfg *config.Config) gin.HandlerFunc {
return func(c *gin.Context) {
var err error
claimMap := map[string]interface{}{}
auth := c.GetHeader(constants.AuthorizationHeaderKey)
auth := c.GetHeader(constant.AuthorizationHeaderKey)
token := strings.Split(auth, " ")
if auth == "" || len(token) < 2 {
err = &service_errors.ServiceError{EndUserMessage: service_errors.TokenRequired}
Expand All @@ -42,14 +42,14 @@ func Authentication(cfg *config.Config) gin.HandlerFunc {
return
}

c.Set(constants.UserIdKey, claimMap[constants.UserIdKey])
c.Set(constants.FirstNameKey, claimMap[constants.FirstNameKey])
c.Set(constants.LastNameKey, claimMap[constants.LastNameKey])
c.Set(constants.UsernameKey, claimMap[constants.UsernameKey])
c.Set(constants.EmailKey, claimMap[constants.EmailKey])
c.Set(constants.MobileNumberKey, claimMap[constants.MobileNumberKey])
c.Set(constants.RolesKey, claimMap[constants.RolesKey])
c.Set(constants.ExpireTimeKey, claimMap[constants.ExpireTimeKey])
c.Set(constant.UserIdKey, claimMap[constant.UserIdKey])
c.Set(constant.FirstNameKey, claimMap[constant.FirstNameKey])
c.Set(constant.LastNameKey, claimMap[constant.LastNameKey])
c.Set(constant.UsernameKey, claimMap[constant.UsernameKey])
c.Set(constant.EmailKey, claimMap[constant.EmailKey])
c.Set(constant.MobileNumberKey, claimMap[constant.MobileNumberKey])
c.Set(constant.RolesKey, claimMap[constant.RolesKey])
c.Set(constant.ExpireTimeKey, claimMap[constant.ExpireTimeKey])

c.Next()
}
Expand All @@ -61,7 +61,7 @@ func Authorization(validRoles []string) gin.HandlerFunc {
c.AbortWithStatusJSON(http.StatusForbidden, helper.GenerateBaseResponse(nil, false, helper.ForbiddenError))
return
}
rolesVal := c.Keys[constants.RolesKey]
rolesVal := c.Keys[constant.RolesKey]
fmt.Println(rolesVal)
if rolesVal == nil {
c.AbortWithStatusJSON(http.StatusForbidden, helper.GenerateBaseResponse(nil, false, helper.ForbiddenError))
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"github.com/naeemaei/golang-clean-web-api/api"
"github.com/naeemaei/golang-clean-web-api/config"
"github.com/naeemaei/golang-clean-web-api/data/cache"
"github.com/naeemaei/golang-clean-web-api/infra/cache"
database "github.com/naeemaei/golang-clean-web-api/infra/persistence/database"
"github.com/naeemaei/golang-clean-web-api/infra/persistence/migration"
"github.com/naeemaei/golang-clean-web-api/pkg/logging"
Expand Down
2 changes: 1 addition & 1 deletion src/constant/constanst.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package constants
package constant

const (
// User
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions src/infra/persistence/migration/1_Init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"time"

"github.com/naeemaei/golang-clean-web-api/config"
constants "github.com/naeemaei/golang-clean-web-api/constant"
"github.com/naeemaei/golang-clean-web-api/constant"
models "github.com/naeemaei/golang-clean-web-api/domain/model"
database "github.com/naeemaei/golang-clean-web-api/infra/persistence/database"
"github.com/naeemaei/golang-clean-web-api/pkg/logging"
Expand Down Expand Up @@ -77,13 +77,13 @@ func addNewTable(database *gorm.DB, model interface{}, tables []interface{}) []i

func createDefaultUserInformation(database *gorm.DB) {

adminRole := models.Role{Name: constants.AdminRoleName}
adminRole := models.Role{Name: constant.AdminRoleName}
createRoleIfNotExists(database, &adminRole)

defaultRole := models.Role{Name: constants.DefaultRoleName}
defaultRole := models.Role{Name: constant.DefaultRoleName}
createRoleIfNotExists(database, &defaultRole)

u := models.User{Username: constants.DefaultUserName, FirstName: "Test", LastName: "Test",
u := models.User{Username: constant.DefaultUserName, FirstName: "Test", LastName: "Test",
MobileNumber: "09111112222", Email: "admin@admin.com"}
pass := "12345678"
hashedPassword, _ := bcrypt.GenerateFromPassword([]byte(pass), bcrypt.DefaultCost)
Expand Down
8 changes: 4 additions & 4 deletions src/infra/persistence/repository/postgres_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/naeemaei/golang-clean-web-api/common"
"github.com/naeemaei/golang-clean-web-api/config"
constants "github.com/naeemaei/golang-clean-web-api/constant"
"github.com/naeemaei/golang-clean-web-api/constant"
filter "github.com/naeemaei/golang-clean-web-api/domain/filter"
database "github.com/naeemaei/golang-clean-web-api/infra/persistence/database"
"github.com/naeemaei/golang-clean-web-api/pkg/logging"
Expand Down Expand Up @@ -55,7 +55,7 @@ func (r BaseRepository[TEntity]) Update(ctx context.Context, id int, entity map[
for k, v := range entity {
snakeMap[common.ToSnakeCase(k)] = v
}
snakeMap["modified_by"] = &sql.NullInt64{Int64: int64(ctx.Value(constants.UserIdKey).(float64)), Valid: true}
snakeMap["modified_by"] = &sql.NullInt64{Int64: int64(ctx.Value(constant.UserIdKey).(float64)), Valid: true}
snakeMap["modified_at"] = sql.NullTime{Valid: true, Time: time.Now().UTC()}
model := new(TEntity)
tx := r.database.WithContext(ctx).Begin()
Expand All @@ -79,11 +79,11 @@ func (r BaseRepository[TEntity]) Delete(ctx context.Context, id int) error {
model := new(TEntity)

deleteMap := map[string]interface{}{
"deleted_by": &sql.NullInt64{Int64: int64(ctx.Value(constants.UserIdKey).(float64)), Valid: true},
"deleted_by": &sql.NullInt64{Int64: int64(ctx.Value(constant.UserIdKey).(float64)), Valid: true},
"deleted_at": sql.NullTime{Valid: true, Time: time.Now().UTC()},
}

if ctx.Value(constants.UserIdKey) == nil {
if ctx.Value(constant.UserIdKey) == nil {
return &service_errors.ServiceError{EndUserMessage: service_errors.PermissionDenied}
}
if cnt := tx.
Expand Down
4 changes: 2 additions & 2 deletions src/infra/persistence/repository/postgres_user_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"

"github.com/naeemaei/golang-clean-web-api/config"
constants "github.com/naeemaei/golang-clean-web-api/constant"
"github.com/naeemaei/golang-clean-web-api/constant"
model "github.com/naeemaei/golang-clean-web-api/domain/model"
database "github.com/naeemaei/golang-clean-web-api/infra/persistence/database"
"github.com/naeemaei/golang-clean-web-api/pkg/logging"
Expand Down Expand Up @@ -113,7 +113,7 @@ func (r *PostgresUserRepository) GetDefaultRole(ctx context.Context) (roleId int

if err = r.database.WithContext(ctx).Model(&model.Role{}).
Select("id").
Where("name = ?", constants.DefaultRoleName).
Where("name = ?", constant.DefaultRoleName).
First(&roleId).Error; err != nil {
return 0, err
}
Expand Down
8 changes: 4 additions & 4 deletions src/usecase/otp_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/go-redis/redis/v7"
"github.com/naeemaei/golang-clean-web-api/common"
"github.com/naeemaei/golang-clean-web-api/config"
constants "github.com/naeemaei/golang-clean-web-api/constant"
"github.com/naeemaei/golang-clean-web-api/data/cache"
constant "github.com/naeemaei/golang-clean-web-api/constant"
"github.com/naeemaei/golang-clean-web-api/infra/cache"
"github.com/naeemaei/golang-clean-web-api/pkg/logging"
"github.com/naeemaei/golang-clean-web-api/pkg/service_errors"
)
Expand Down Expand Up @@ -40,7 +40,7 @@ func (u *OtpUsecase) SendOtp(mobileNumber string) error {
}

func (s *OtpUsecase) SetOtp(mobileNumber string, otp string) error {
key := fmt.Sprintf("%s:%s", constants.RedisOtpDefaultKey, mobileNumber)
key := fmt.Sprintf("%s:%s", constant.RedisOtpDefaultKey, mobileNumber)
val := &otpDto{
Value: otp,
Used: false,
Expand All @@ -60,7 +60,7 @@ func (s *OtpUsecase) SetOtp(mobileNumber string, otp string) error {
}

func (s *OtpUsecase) ValidateOtp(mobileNumber string, otp string) error {
key := fmt.Sprintf("%s:%s", constants.RedisOtpDefaultKey, mobileNumber)
key := fmt.Sprintf("%s:%s", constant.RedisOtpDefaultKey, mobileNumber)
res, err := cache.Get[otpDto](s.redisClient, key)
if err != nil {
return err
Expand Down
22 changes: 11 additions & 11 deletions src/usecase/token_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/golang-jwt/jwt"
"github.com/naeemaei/golang-clean-web-api/config"
constants "github.com/naeemaei/golang-clean-web-api/constant"
"github.com/naeemaei/golang-clean-web-api/constant"
"github.com/naeemaei/golang-clean-web-api/pkg/logging"
"github.com/naeemaei/golang-clean-web-api/pkg/service_errors"
dto "github.com/naeemaei/golang-clean-web-api/usecase/dto"
Expand Down Expand Up @@ -41,14 +41,14 @@ func (s *TokenUsecase) GenerateToken(token tokenDto) (*dto.TokenDetail, error) {

atc := jwt.MapClaims{}

atc[constants.UserIdKey] = token.UserId
atc[constants.FirstNameKey] = token.FirstName
atc[constants.LastNameKey] = token.LastName
atc[constants.UsernameKey] = token.Username
atc[constants.EmailKey] = token.Email
atc[constants.MobileNumberKey] = token.MobileNumber
atc[constants.RolesKey] = token.Roles
atc[constants.ExpireTimeKey] = td.AccessTokenExpireTime
atc[constant.UserIdKey] = token.UserId
atc[constant.FirstNameKey] = token.FirstName
atc[constant.LastNameKey] = token.LastName
atc[constant.UsernameKey] = token.Username
atc[constant.EmailKey] = token.Email
atc[constant.MobileNumberKey] = token.MobileNumber
atc[constant.RolesKey] = token.Roles
atc[constant.ExpireTimeKey] = td.AccessTokenExpireTime

at := jwt.NewWithClaims(jwt.SigningMethodHS256, atc)

Expand All @@ -61,8 +61,8 @@ func (s *TokenUsecase) GenerateToken(token tokenDto) (*dto.TokenDetail, error) {

rtc := jwt.MapClaims{}

rtc[constants.UserIdKey] = token.UserId
rtc[constants.ExpireTimeKey] = td.RefreshTokenExpireTime
rtc[constant.UserIdKey] = token.UserId
rtc[constant.ExpireTimeKey] = td.RefreshTokenExpireTime

rt := jwt.NewWithClaims(jwt.SigningMethodHS256, rtc)

Expand Down

0 comments on commit 39be160

Please sign in to comment.