diff --git a/controller/auth/auth_change_password_controller.go b/controller/auth/auth_change_password_controller.go new file mode 100644 index 0000000000000000000000000000000000000000..80aa4ac00b85babbbbbcab4de6c7d1fa3514ea6a --- /dev/null +++ b/controller/auth/auth_change_password_controller.go @@ -0,0 +1,21 @@ +package user + +import ( + "api.qobiltu.id/controller" + "api.qobiltu.id/models" + "api.qobiltu.id/services" + "github.com/gin-gonic/gin" +) + +func ChangePassword(c *gin.Context) { + authentication := services.AuthenticationService{} + changePasswordController := controller.Controller[models.ChangePasswordRequest, models.Account, models.AuthenticatedUser]{ + Service: &authentication.Service, + } + changePasswordController.HeaderParse(c, func() { + changePasswordController.Service.Constructor.Id = uint(changePasswordController.AccountData.UserID) + }) + changePasswordController.RequestJSON(c, func() { + authentication.Update(changePasswordController.Request.OldPassword, changePasswordController.Request.NewPassword) + }) +} diff --git a/controller/auth/auth_login_controller.go b/controller/auth/auth_login_controller.go new file mode 100644 index 0000000000000000000000000000000000000000..679fc5a018d75e815dea00cca2012d5d56034eed --- /dev/null +++ b/controller/auth/auth_login_controller.go @@ -0,0 +1,20 @@ +package user + +import ( + "api.qobiltu.id/controller" + "api.qobiltu.id/models" + "api.qobiltu.id/services" + "github.com/gin-gonic/gin" +) + +func Login(c *gin.Context) { + authentication := services.AuthenticationService{} + loginController := controller.Controller[models.LoginRequest, models.Account, models.AuthenticatedUser]{ + Service: &authentication.Service, + } + loginController.RequestJSON(c, func() { + loginController.Service.Constructor.Email = loginController.Request.Email + loginController.Service.Constructor.Password = loginController.Request.Password + authentication.Authenticate() + }) +} diff --git a/controller/auth/auth_profile_controller.go b/controller/auth/auth_profile_controller.go new file mode 100644 index 0000000000000000000000000000000000000000..f30ee7177fa8bd15be000bcfe941ceb2732a74c1 --- /dev/null +++ b/controller/auth/auth_profile_controller.go @@ -0,0 +1,21 @@ +package user + +import ( + "api.qobiltu.id/controller" + "api.qobiltu.id/models" + "api.qobiltu.id/services" + "github.com/gin-gonic/gin" +) + +func Profile(c *gin.Context) { + userProfile := services.UserProfileService{} + userProfileController := controller.Controller[any, models.AccountDetails, models.AccountDetails]{ + Service: &userProfile.Service, + } + userProfileController.HeaderParse(c, func() { + userProfileController.Service.Constructor.AccountID = uint(userProfileController.AccountData.UserID) + userProfile.Retrieve() + userProfileController.Response(c) + }, + ) +} diff --git a/controller/auth/auth_register_controller.go b/controller/auth/auth_register_controller.go new file mode 100644 index 0000000000000000000000000000000000000000..17051bee42f7b1d3fe144cedb6e8f11dbb5ac5b2 --- /dev/null +++ b/controller/auth/auth_register_controller.go @@ -0,0 +1,20 @@ +package user + +import ( + "api.qobiltu.id/controller" + "api.qobiltu.id/models" + "api.qobiltu.id/services" + "github.com/gin-gonic/gin" +) + +func Register(c *gin.Context) { + register := services.RegisterService{} + registerController := controller.Controller[models.RegisterRequest, models.Account, models.Account]{ + Service: ®ister.Service, + } + registerController.RequestJSON(c, func() { + registerController.Service.Constructor.Password = registerController.Request.Password + registerController.Service.Constructor.Email = registerController.Request.Email + register.Create() + }) +} diff --git a/controller/auth/auth_update_profile_controller.go b/controller/auth/auth_update_profile_controller.go new file mode 100644 index 0000000000000000000000000000000000000000..4821ed13bbf913726e6512251db3bf43728a8a19 --- /dev/null +++ b/controller/auth/auth_update_profile_controller.go @@ -0,0 +1,24 @@ +package user + +import ( + "api.qobiltu.id/controller" + "api.qobiltu.id/models" + "api.qobiltu.id/services" + "github.com/gin-gonic/gin" +) + +func UpdateProfile(c *gin.Context) { + userProfile := services.UserProfileService{} + userUpdateProfileController := controller.Controller[models.AccountDetails, models.AccountDetails, models.AccountDetails]{ + Service: &userProfile.Service, + } + + userUpdateProfileController.RequestJSON(c, func() { + userUpdateProfileController.Service.Constructor = userUpdateProfileController.Request + userUpdateProfileController.HeaderParse(c, func() { + userUpdateProfileController.Service.Constructor.AccountID = uint(userUpdateProfileController.AccountData.UserID) + }) + userProfile.Update() + }, + ) +} diff --git a/go.mod b/go.mod index 0c2b7a92ff9c9eddbfa0d1506ccb9b4d0614163e..405345ca21ac9dd77eec5b3d4e48775287157552 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module api.qobiltu.id go 1.24.0 require ( - github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/gin-gonic/gin v1.10.0 github.com/golang-jwt/jwt/v5 v5.2.1 github.com/joho/godotenv v1.5.1 diff --git a/go.sum b/go.sum index b7d196affb731541ab18f7ba85fbb21dd47867ac..fc9ac2ce0112e916606dc13ba95b03d9c01f9373 100644 --- a/go.sum +++ b/go.sum @@ -10,8 +10,6 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= -github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM= github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8= github.com/gin-contrib/sse v1.0.0 h1:y3bT1mUWUxDpW4JLQg/HnTqV4rozuW4tC9eFKTxYI9E= diff --git a/logserror_log.txt b/logserror_log.txt index e1cfb40c47f1a7696081230712522f0d8f6d4a02..58309e5256ae56f8b8feef85a64e685cfdc86000 100644 --- a/logserror_log.txt +++ b/logserror_log.txt @@ -1,2 +1,3 @@ 2025/03/18 21:08:07 Error Log : ERROR: relasi « email_verifications » tidak ada (SQLSTATE 42P01) 2025/03/22 14:00:34 Error Log : duplicated key not allowed +2025/03/22 16:57:13 Error Log : ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification (SQLSTATE 42P10) diff --git a/middleware/authentication_middleware.go b/middleware/authentication_middleware.go index 41ed5f2fb2eefa1f43c991f2f502d39505c76e3b..e413a98e080bcf0700af246cd083a9337263dba1 100644 --- a/middleware/authentication_middleware.go +++ b/middleware/authentication_middleware.go @@ -3,52 +3,22 @@ package middleware import ( - "time" - - "api.qobiltu.id/config" "api.qobiltu.id/models" + "api.qobiltu.id/services" "api.qobiltu.id/utils" "github.com/gin-gonic/gin" - "github.com/golang-jwt/jwt/v5" ) -var salt = config.Salt -var secretKey = []byte(salt) - -// VerifyPassword verifies if the provided password matches the hashed password - -type CustomClaims struct { - jwt.RegisteredClaims - UserID int `json:"id"` -} - -func VerifyToken(bearer_token string) (int, string, error) { - // fmt.Println(bearer_token) - token, err := jwt.ParseWithClaims(bearer_token, &CustomClaims{}, func(token *jwt.Token) (interface{}, error) { - return secretKey, nil - }) - if err != nil { - return 0, "invalid-token", err - } - - // Extract the claims - claims, ok := token.Claims.(*CustomClaims) - if !ok || !token.Valid { - return 0, "invalid-token", err - } - if claims.ExpiresAt != nil && claims.ExpiresAt.Time.Before(time.Now()) { - return 0, "expired", err - } - - return claims.UserID, "valid", err -} - func AuthUser(c *gin.Context) { var currAccData models.AccountData - if c.Request.Header["Auth-Bearer-Token"] != nil { - token := c.Request.Header["Auth-Bearer-Token"] - currAccData.UserID, currAccData.VerifyStatus, currAccData.ErrVerif = VerifyToken(token[0]) - // fmt.Println("Verify Status :", currAccData.verifyStatus) + if c.Request.Header["Authorization"] != nil { + token := c.Request.Header["Authorization"] + // fmt.Println("Authorization :", token) + + currAccData.UserID, currAccData.VerifyStatus, currAccData.ErrVerif = services.VerifyToken(token[0]) + // fmt.Println("Verify Status :", currAccData.VerifyStatus) + // fmt.Println("Verify UserID :", currAccData.UserID) + if currAccData.VerifyStatus == "invalid-token" || currAccData.VerifyStatus == "expired" { currAccData.UserID = 0 utils.ResponseFAIL(c, 401, models.Exception{Unauthorized: true, Message: "Your session is expired, Please re-Login!"}) diff --git a/models/authentication_payload_model.go b/models/authentication_payload_model.go index 0203b9b340d066705491edf66830674602f89655..41f31eb02e14a1ad1e3b6cf372a69dd91deb12b8 100644 --- a/models/authentication_payload_model.go +++ b/models/authentication_payload_model.go @@ -1,7 +1,7 @@ package models type AccountData struct { - UserID int + UserID uint VerifyStatus string ErrVerif error } diff --git a/models/custom_claim.go b/models/custom_claim.go new file mode 100644 index 0000000000000000000000000000000000000000..858c6b95b7c9bc880b5d163dd4469b129e9355cf --- /dev/null +++ b/models/custom_claim.go @@ -0,0 +1,8 @@ +package models + +import "github.com/golang-jwt/jwt/v5" + +type CustomClaims struct { + jwt.RegisteredClaims + UserID uint `json:"id"` +} diff --git a/models/response_model.go b/models/response_model.go index c45df0dbc642de90cc3537722b359a2f09a06b02..7e049aac3f7c5a196f9a54ce924b5424f5986553 100644 --- a/models/response_model.go +++ b/models/response_model.go @@ -13,6 +13,7 @@ type ErrorResponse struct { Errors Exception `json:"errors"` MetaData any `json:"meta_data"` } + type AuthenticatedUser struct { Account Account `json:"account"` Token string `json:"token"` diff --git a/repositories/account_repository.go b/repositories/account_repository.go index d56c1262ec89f2dabee886d89b3fb5b67a41b797..686ccc92e7a56587581e345bbb3dca284fb1d7da 100644 --- a/repositories/account_repository.go +++ b/repositories/account_repository.go @@ -17,9 +17,9 @@ func GetAccountbyEmail(email string) Repository[models.Account, models.Account] return *repo } -func GetAccountbyId(account_id uint) Repository[models.Account, models.Account] { +func GetAccountById(accountId uint) Repository[models.Account, models.Account] { repo := Construct[models.Account, models.Account]( - models.Account{Id: account_id}, + models.Account{Id: accountId}, ) repo.Transactions( WhereGivenConstructor[models.Account, models.Account], @@ -27,6 +27,7 @@ func GetAccountbyId(account_id uint) Repository[models.Account, models.Account] ) return *repo } + func UpdateAccount(account models.Account) Repository[models.Account, models.Account] { repo := Construct[models.Account, models.Account]( account, @@ -34,17 +35,20 @@ func UpdateAccount(account models.Account) Repository[models.Account, models.Acc Update(repo) return *repo } -func GetAccountDetailsbyId(account_id uint) Repository[models.AccountDetails, models.AccountDetails] { + +func GetDetailAccountById(accountId uint) Repository[models.AccountDetails, models.AccountDetails] { repo := Construct[models.AccountDetails, models.AccountDetails]( - models.AccountDetails{AccountID: account_id}, + models.AccountDetails{AccountID: accountId}, ) - fmt.Println("Account ID:", repo.Constructor.AccountID) + + // fmt.Println("Account ID:", repo.Constructor.AccountID) repo.Transactions( WhereGivenConstructor[models.AccountDetails, models.AccountDetails], Find[models.AccountDetails, models.AccountDetails], ) return *repo } + func CreateAccount(account models.Account) Repository[models.Account, models.Account] { repo := Construct[models.Account, models.Account]( account, @@ -52,6 +56,7 @@ func CreateAccount(account models.Account) Repository[models.Account, models.Acc Create(repo) return *repo } + func CreateAccountDetails(accountDetails models.AccountDetails) Repository[models.AccountDetails, models.AccountDetails] { repo := Construct[models.AccountDetails, models.AccountDetails]( accountDetails, @@ -61,6 +66,7 @@ func CreateAccountDetails(accountDetails models.AccountDetails) Repository[model Create(repo) return *repo } + func UpdateAccountDetails(accountDetails models.AccountDetails) Repository[models.AccountDetails, models.AccountDetails] { repo := Construct[models.AccountDetails, models.AccountDetails]( accountDetails, diff --git a/repositories/email_verification_repository.go b/repositories/email_verification_repository.go index c737e6d4b782e17630568f3616f0bf7ef36dc31f..0072ea0952f85bc719bf4bd0d1cf90e6a7270870 100644 --- a/repositories/email_verification_repository.go +++ b/repositories/email_verification_repository.go @@ -19,10 +19,10 @@ func CreateEmailVerification(accountId uint, dueTime time.Time, token uint) Repo return *repo } -func GetEmailVerification(account_id uint, token uint) Repository[models.EmailVerification, models.EmailVerification] { +func GetEmailVerification(accountId uint, token uint) Repository[models.EmailVerification, models.EmailVerification] { repo := Construct[models.EmailVerification, models.EmailVerification]( models.EmailVerification{ - AccountID: account_id, + AccountID: accountId, IsExpired: false, Token: token, }, diff --git a/repositories/repository.go b/repositories/repository.go index 0ad72d912c734386edc16a3fe9d74465b2352382..0c6b1ee5c41b63f4414e7013717d7fbd58a7dbf4 100644 --- a/repositories/repository.go +++ b/repositories/repository.go @@ -48,6 +48,7 @@ func Construct[TConstructor any, TResult any](constructor ...TConstructor) *Repo Transaction: config.DB.Begin(), } } + func (repo *Repository[T1, T2]) Transactions(transactions ...func(*Repository[T1, T2]) *gorm.DB) { for _, tx := range transactions { repo.Transaction = tx(repo) @@ -56,6 +57,7 @@ func (repo *Repository[T1, T2]) Transactions(transactions ...func(*Repository[T1 } } } + func WhereGivenConstructor[T1 any, T2 any](repo *Repository[T1, T2]) *gorm.DB { tx := repo.Transaction.Where(&repo.Constructor) repo.RowsCount = int(tx.RowsAffected) @@ -63,6 +65,7 @@ func WhereGivenConstructor[T1 any, T2 any](repo *Repository[T1, T2]) *gorm.DB { repo.RowsError = tx.Error return tx } + func Find[T1 any, T2 any](repo *Repository[T1, T2]) *gorm.DB { tx := repo.Transaction.Find(&repo.Result) repo.RowsCount = int(tx.RowsAffected) diff --git a/router/auth_route.go b/router/auth_route.go new file mode 100644 index 0000000000000000000000000000000000000000..4bd0d347f3705976e9973248b50103893c3f603e --- /dev/null +++ b/router/auth_route.go @@ -0,0 +1,18 @@ +package router + +import ( + AuthController "api.qobiltu.id/controller/auth" + "api.qobiltu.id/middleware" + "github.com/gin-gonic/gin" +) + +func AuthRoute(router *gin.Engine) { + routerGroup := router.Group("/api/v1/auth") + { + routerGroup.POST("/login", AuthController.Login) + routerGroup.POST("/register", AuthController.Register) + routerGroup.GET("/me", middleware.AuthUser, AuthController.Profile) + routerGroup.PUT("/me", middleware.AuthUser, AuthController.UpdateProfile) + routerGroup.PUT("/change-password", middleware.AuthUser, AuthController.ChangePassword) + } +} diff --git a/router/router.go b/router/router.go index a7dde2e239a2d254991cd00c712b9eeebe6102d1..10274c77b2c0c311425de7e04d692ace51eafff8 100644 --- a/router/router.go +++ b/router/router.go @@ -9,7 +9,7 @@ import ( func StartService() { router := gin.Default() router.GET("/", controller.HomeController) - UserRoute(router) + AuthRoute(router) EmailRoute(router) router.Run(config.TCP_ADDRESS) } diff --git a/services/authentication_service.go b/services/authentication_service.go index 22a82cb0fe7042139cfa992ca4ac35bfcc9b4490..aa5b33fd1b03a769c6e7bc0232542c2aa44c5472 100644 --- a/services/authentication_service.go +++ b/services/authentication_service.go @@ -45,7 +45,7 @@ func (s *AuthenticationService) Update(oldPassword string, newPassword string) { s.Exception.Message = "Password must have at least 8 characters!" return } - accountData := repositories.GetAccountbyId(s.Constructor.Id) + accountData := repositories.GetAccountById(s.Constructor.Id) if accountData.NoRecord { s.Exception.DataNotFound = true diff --git a/services/email_verification_service.go b/services/email_verification_service.go index d94c65bc93550413c285e2817b68392664fb3b2a..8501fe321f043a4d462964389d33822bfd62c75d 100644 --- a/services/email_verification_service.go +++ b/services/email_verification_service.go @@ -14,7 +14,7 @@ type EmailVerificationService struct { } func (s *EmailVerificationService) Create() { - accountRepo := repositories.GetAccountbyId(s.Constructor.AccountID) + accountRepo := repositories.GetAccountById(s.Constructor.AccountID) if accountRepo.NoRecord { s.Error = accountRepo.RowsError s.Exception.DataNotFound = true diff --git a/services/jwt_service.go b/services/jwt_service.go index 92024df8afa7fe83da747bddc969cc1066cf431c..3fdce4e4735d65dabdd71357a6d915f07219ddfb 100644 --- a/services/jwt_service.go +++ b/services/jwt_service.go @@ -2,11 +2,12 @@ package services import ( "errors" + "strings" "time" "api.qobiltu.id/config" "api.qobiltu.id/models" - "github.com/dgrijalva/jwt-go" + "github.com/golang-jwt/jwt/v5" "golang.org/x/crypto/bcrypt" ) @@ -14,22 +15,56 @@ var salt = config.Salt var secretKey = []byte(salt) func GenerateToken(user *models.Account) (string, error) { + claims := models.CustomClaims{ + UserID: user.Id, + RegisteredClaims: jwt.RegisteredClaims{ + ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour * 24)), // Token berlaku 24 jam + IssuedAt: jwt.NewNumericDate(time.Now()), + Issuer: "qobiltu.id", + }, + } + + // Buat token dengan metode signing + token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) + return token.SignedString(secretKey) +} + +func ExtractBearerToken(authHeader string) (string, error) { + parts := strings.Split(authHeader, " ") + if len(parts) != 2 || parts[0] != "Bearer" { + return "", errors.New("invalid authorization header format") + } + return parts[1], nil +} - // Create a new token - token := jwt.New(jwt.SigningMethodHS256) +func VerifyToken(bearerToken string) (uint, string, error) { + // fmt.Println("bearerToken :", bearerToken) - // Set claims - claims := token.Claims.(jwt.MapClaims) - claims["id"] = user.Id - claims["exp"] = time.Now().Add(time.Hour * 24).Unix() // Token expires in 24 hours + tokenData, err := ExtractBearerToken(bearerToken) + if err != nil { + return 0, "invalid-token", err + } else { + // fmt.Println("Extracted Token:", tokenData) + } + + token, err := jwt.ParseWithClaims(tokenData, &models.CustomClaims{}, func(token *jwt.Token) (interface{}, error) { + return secretKey, nil + }) - // Sign the token with the secret key - tokenString, err := token.SignedString(secretKey) if err != nil { - return "", err + return 0, "invalid-token", err + } + + // Extract the claims + claims, ok := token.Claims.(*models.CustomClaims) + if !ok || !token.Valid { + return 0, "invalid-token", err + } + if claims.ExpiresAt != nil && claims.ExpiresAt.Time.Before(time.Now()) { + return 0, "expired", err } - return tokenString, nil + return claims.UserID, "valid", err } func VerifyPassword(hashedPassword, password string) error { diff --git a/services/user_profile_service.go b/services/user_profile_service.go index b7bff87a987cd39f2f3232c84a0f22018a22d559..419458a7d1e4845f1e4049bc3c3edabbbef748fb 100644 --- a/services/user_profile_service.go +++ b/services/user_profile_service.go @@ -20,7 +20,7 @@ func (s *UserProfileService) Create() { s.Result = userProfile.Result } func (s *UserProfileService) Retrieve() { - userProfile := repositories.GetAccountDetailsbyId(s.Constructor.AccountID) + userProfile := repositories.GetDetailAccountById(s.Constructor.AccountID) s.Error = userProfile.RowsError if userProfile.NoRecord { s.Exception.DataNotFound = true diff --git a/space/controller/user/user_change_password_controller.go b/space/controller/user/user_change_password_controller.go new file mode 100644 index 0000000000000000000000000000000000000000..80aa4ac00b85babbbbbcab4de6c7d1fa3514ea6a --- /dev/null +++ b/space/controller/user/user_change_password_controller.go @@ -0,0 +1,21 @@ +package user + +import ( + "api.qobiltu.id/controller" + "api.qobiltu.id/models" + "api.qobiltu.id/services" + "github.com/gin-gonic/gin" +) + +func ChangePassword(c *gin.Context) { + authentication := services.AuthenticationService{} + changePasswordController := controller.Controller[models.ChangePasswordRequest, models.Account, models.AuthenticatedUser]{ + Service: &authentication.Service, + } + changePasswordController.HeaderParse(c, func() { + changePasswordController.Service.Constructor.Id = uint(changePasswordController.AccountData.UserID) + }) + changePasswordController.RequestJSON(c, func() { + authentication.Update(changePasswordController.Request.OldPassword, changePasswordController.Request.NewPassword) + }) +} diff --git a/space/controller/user/user_login_controller.go b/space/controller/user/user_login_controller.go index b7b9673901e26d2dca8bff51c4fdf834eef30646..679fc5a018d75e815dea00cca2012d5d56034eed 100644 --- a/space/controller/user/user_login_controller.go +++ b/space/controller/user/user_login_controller.go @@ -9,7 +9,7 @@ import ( func Login(c *gin.Context) { authentication := services.AuthenticationService{} - loginController := controller.Controller[models.LoginRequest, services.LoginConstructor, models.AuthenticatedUser]{ + loginController := controller.Controller[models.LoginRequest, models.Account, models.AuthenticatedUser]{ Service: &authentication.Service, } loginController.RequestJSON(c, func() { diff --git a/space/controller/user/user_profile_controller.go b/space/controller/user/user_profile_controller.go index 177520bc297345eaaf66099fe9a71efb8e53b5c5..f30ee7177fa8bd15be000bcfe941ceb2732a74c1 100644 --- a/space/controller/user/user_profile_controller.go +++ b/space/controller/user/user_profile_controller.go @@ -1,8 +1,6 @@ package user import ( - "fmt" - "api.qobiltu.id/controller" "api.qobiltu.id/models" "api.qobiltu.id/services" @@ -11,12 +9,11 @@ import ( func Profile(c *gin.Context) { userProfile := services.UserProfileService{} - userProfileController := controller.Controller[any, services.UserProfileConstructor, models.Account]{ + userProfileController := controller.Controller[any, models.AccountDetails, models.AccountDetails]{ Service: &userProfile.Service, } - fmt.Println(userProfileController.AccountData) userProfileController.HeaderParse(c, func() { - userProfileController.Service.Constructor.AccountId = userProfileController.AccountData.UserID + userProfileController.Service.Constructor.AccountID = uint(userProfileController.AccountData.UserID) userProfile.Retrieve() userProfileController.Response(c) }, diff --git a/space/controller/user/user_update_profile_controller.go b/space/controller/user/user_update_profile_controller.go index 64072242e2eea309c68624c2b86ad02b3596095b..4821ed13bbf913726e6512251db3bf43728a8a19 100644 --- a/space/controller/user/user_update_profile_controller.go +++ b/space/controller/user/user_update_profile_controller.go @@ -1,8 +1,6 @@ package user import ( - "fmt" - "api.qobiltu.id/controller" "api.qobiltu.id/models" "api.qobiltu.id/services" @@ -11,14 +9,16 @@ import ( func UpdateProfile(c *gin.Context) { userProfile := services.UserProfileService{} - userUpdateProfileController := controller.Controller[any, services.UserProfileConstructor, models.Account]{ + userUpdateProfileController := controller.Controller[models.AccountDetails, models.AccountDetails, models.AccountDetails]{ Service: &userProfile.Service, } - fmt.Println(userUpdateProfileController.AccountData) - userUpdateProfileController.HeaderParse(c, func() { - userUpdateProfileController.Service.Constructor.AccountId = userUpdateProfileController.AccountData.UserID - userProfile.Retrieve() - userUpdateProfileController.Response(c) + + userUpdateProfileController.RequestJSON(c, func() { + userUpdateProfileController.Service.Constructor = userUpdateProfileController.Request + userUpdateProfileController.HeaderParse(c, func() { + userUpdateProfileController.Service.Constructor.AccountID = uint(userUpdateProfileController.AccountData.UserID) + }) + userProfile.Update() }, ) } diff --git a/space/logserror_log.txt b/space/logserror_log.txt index 30ecdec40a51b2cc4d2181b7108e953e7c77166f..58309e5256ae56f8b8feef85a64e685cfdc86000 100644 --- a/space/logserror_log.txt +++ b/space/logserror_log.txt @@ -1 +1,3 @@ 2025/03/18 21:08:07 Error Log : ERROR: relasi « email_verifications » tidak ada (SQLSTATE 42P01) +2025/03/22 14:00:34 Error Log : duplicated key not allowed +2025/03/22 16:57:13 Error Log : ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification (SQLSTATE 42P10) diff --git a/space/models/request_model.go b/space/models/request_model.go index bb4170dfd5e176800d18a19e52ba8feb55531b1d..dfb4bfd35cd08440c3d106dd517a1821d90460c8 100644 --- a/space/models/request_model.go +++ b/space/models/request_model.go @@ -14,5 +14,9 @@ type RegisterRequest struct { type CreateEmailVerificationRequest struct { AccountID int `json:"account_id" binding:"required"` +} +type ChangePasswordRequest struct { + OldPassword string `json:"old_password" binding:"required" ` + NewPassword string `json:"new_password" binding:"required" ` } diff --git a/space/repositories/account_repository.go b/space/repositories/account_repository.go index ef028bd358dd2e0f419ad4cbbad7eb3769fb1477..d56c1262ec89f2dabee886d89b3fb5b67a41b797 100644 --- a/space/repositories/account_repository.go +++ b/space/repositories/account_repository.go @@ -1,6 +1,8 @@ package repositories import ( + "fmt" + "api.qobiltu.id/models" ) @@ -25,6 +27,24 @@ func GetAccountbyId(account_id uint) Repository[models.Account, models.Account] ) return *repo } +func UpdateAccount(account models.Account) Repository[models.Account, models.Account] { + repo := Construct[models.Account, models.Account]( + account, + ) + Update(repo) + return *repo +} +func GetAccountDetailsbyId(account_id uint) Repository[models.AccountDetails, models.AccountDetails] { + repo := Construct[models.AccountDetails, models.AccountDetails]( + models.AccountDetails{AccountID: account_id}, + ) + fmt.Println("Account ID:", repo.Constructor.AccountID) + repo.Transactions( + WhereGivenConstructor[models.AccountDetails, models.AccountDetails], + Find[models.AccountDetails, models.AccountDetails], + ) + return *repo +} func CreateAccount(account models.Account) Repository[models.Account, models.Account] { repo := Construct[models.Account, models.Account]( account, @@ -32,7 +52,21 @@ func CreateAccount(account models.Account) Repository[models.Account, models.Acc Create(repo) return *repo } - -// func UpdateAccount(account models.Account) Repository[models.Account, models.Account] { -// repo := Construct -// } +func CreateAccountDetails(accountDetails models.AccountDetails) Repository[models.AccountDetails, models.AccountDetails] { + repo := Construct[models.AccountDetails, models.AccountDetails]( + accountDetails, + ) + fmt.Println(accountDetails) + fmt.Println("Account ID : ", accountDetails.AccountID) + Create(repo) + return *repo +} +func UpdateAccountDetails(accountDetails models.AccountDetails) Repository[models.AccountDetails, models.AccountDetails] { + repo := Construct[models.AccountDetails, models.AccountDetails]( + accountDetails, + ) + fmt.Println(accountDetails) + fmt.Println("Account ID : ", accountDetails.AccountID) + Update(repo) + return *repo +} diff --git a/space/repositories/repository.go b/space/repositories/repository.go index 3617e40936bf9e50e39a50285105622f080d210f..0ad72d912c734386edc16a3fe9d74465b2352382 100644 --- a/space/repositories/repository.go +++ b/space/repositories/repository.go @@ -93,6 +93,7 @@ func Update[T1 any](repo *Repository[T1, T1]) *gorm.DB { repo.RowsCount = int(tx.RowsAffected) repo.NoRecord = repo.RowsCount == 0 repo.RowsError = tx.Error + repo.Result = repo.Constructor return tx } diff --git a/space/router/user_route.go b/space/router/user_route.go index c1abce045debeff19dabe5af38b9903abd5083a0..1e5f9e405b0d936a86aed41c552cd8030fdc12bb 100644 --- a/space/router/user_route.go +++ b/space/router/user_route.go @@ -12,5 +12,7 @@ func UserRoute(router *gin.Engine) { routerGroup.POST("/login", UserController.Login) routerGroup.POST("/register", UserController.Register) routerGroup.GET("/me", middleware.AuthUser, UserController.Profile) + routerGroup.PUT("/me", middleware.AuthUser, UserController.UpdateProfile) + routerGroup.PUT("/change-password", middleware.AuthUser, UserController.ChangePassword) } } diff --git a/space/services/authentication_service.go b/space/services/authentication_service.go new file mode 100644 index 0000000000000000000000000000000000000000..22a82cb0fe7042139cfa992ca4ac35bfcc9b4490 --- /dev/null +++ b/space/services/authentication_service.go @@ -0,0 +1,71 @@ +package services + +import ( + "errors" + "fmt" + + "api.qobiltu.id/models" + "api.qobiltu.id/repositories" +) + +type AuthenticationService struct { + Service[models.Account, models.AuthenticatedUser] +} + +func (s *AuthenticationService) Authenticate() { + accountData := repositories.GetAccountbyEmail(s.Constructor.Email) + if accountData.NoRecord { + s.Exception.DataNotFound = true + s.Exception.Message = "there is no account with given credentials!" + return + } + if VerifyPassword(accountData.Result.Password, s.Constructor.Password) != nil { + s.Exception.Unauthorized = true + s.Exception.Message = "incorrect password!" + return + } + + token, err_tok := GenerateToken(&accountData.Result) + + if err_tok != nil { + s.Error = errors.Join(s.Error, err_tok) + } + + accountData.Result.Password = "SECRET" + s.Result = models.AuthenticatedUser{ + Account: accountData.Result, + Token: token, + } + s.Error = accountData.RowsError +} + +func (s *AuthenticationService) Update(oldPassword string, newPassword string) { + if len(newPassword) < 8 { + s.Exception.InvalidPasswordLength = true + s.Exception.Message = "Password must have at least 8 characters!" + return + } + accountData := repositories.GetAccountbyId(s.Constructor.Id) + + if accountData.NoRecord { + s.Exception.DataNotFound = true + s.Exception.Message = "there is no account with given credentials!" + return + } + fmt.Println("Result Password", accountData.Result.Password) + fmt.Println("old Password given", oldPassword) + if VerifyPassword(accountData.Result.Password, oldPassword) != nil { + s.Exception.Unauthorized = true + s.Exception.Message = "incorrect old password!" + return + } + accountData.Result.Password = newPassword + changePassword := repositories.UpdateAccount(accountData.Result) + changePassword.Result.Password = "SECRET" + s.Result = models.AuthenticatedUser{ + Account: changePassword.Result, + } + s.Error = changePassword.RowsError +} + +// LoginHandler handles user login diff --git a/space/services/register_service.go b/space/services/register_service.go index f9e0d2216bd033155ffcbd73dd1b9de44d20bfa8..4802c35b8bd0a7f5b29214a68fc8f6eb1ea8dfde 100644 --- a/space/services/register_service.go +++ b/space/services/register_service.go @@ -33,6 +33,13 @@ func (s *RegisterService) Create() { s.Exception.Message = "Bad request!" return } + userProfile := UserProfileService{} + userProfile.Constructor.AccountID = accountCreated.Result.Id + userProfile.Create() + if userProfile.Error != nil { + s.Error = userProfile.Error + return + } s.Error = accountCreated.RowsError s.Result = accountCreated.Result s.Result.Password = "SECRET" diff --git a/space/services/user_profile_service.go b/space/services/user_profile_service.go index afea5ccbf0885293e2f782692e229b6948e2228e..b7bff87a987cd39f2f3232c84a0f22018a22d559 100644 --- a/space/services/user_profile_service.go +++ b/space/services/user_profile_service.go @@ -5,33 +5,38 @@ import ( "api.qobiltu.id/repositories" ) -type UserProfileConstructor struct { - AccountId int -} type UserProfileService struct { - Service[UserProfileConstructor, models.Account] + Service[models.AccountDetails, models.AccountDetails] } +func (s *UserProfileService) Create() { + userProfile := repositories.CreateAccountDetails(s.Constructor) + s.Error = userProfile.RowsError + if userProfile.NoRecord { + s.Exception.DataNotFound = true + s.Exception.Message = "There is no account with given credentials!" + return + } + s.Result = userProfile.Result +} func (s *UserProfileService) Retrieve() { - userProfile := repositories.GetAccountbyId(uint(s.Constructor.AccountId)) + userProfile := repositories.GetAccountDetailsbyId(s.Constructor.AccountID) s.Error = userProfile.RowsError if userProfile.NoRecord { s.Exception.DataNotFound = true s.Exception.Message = "There is no account with given credentials!" return } - s.Result.Password = "SECRET" s.Result = userProfile.Result } func (s *UserProfileService) Update() { - userProfile := repositories.GetAccountbyId(uint(s.Constructor.AccountId)) + userProfile := repositories.UpdateAccountDetails(s.Constructor) s.Error = userProfile.RowsError if userProfile.NoRecord { s.Exception.DataNotFound = true s.Exception.Message = "There is no account with given credentials!" return } - s.Result.Password = "SECRET" s.Result = userProfile.Result } diff --git a/space/space/.github/workflows/main.yml b/space/space/.github/workflows/main.yml index 39a67571ac3a2e6a6e2a972b9550becd551bdded..65c8cabfc8ef2a61bf1694229cb9bf3126a94f4b 100644 --- a/space/space/.github/workflows/main.yml +++ b/space/space/.github/workflows/main.yml @@ -1,4 +1,4 @@ -name: Deploy to Development via Huggingface +name: Deploy Golang App on: push: @@ -6,47 +6,23 @@ on: - main jobs: - deploy-to-huggingface: + deploy: runs-on: ubuntu-latest steps: - # Checkout repository - name: Checkout Repository uses: actions/checkout@v3 - # Setup Git - - name: Setup Git for Huggingface - run: | - git config --global user.email "abdan.hafidz@gmail.com" - git config --global user.name "abdanhafidz" - - # Clone Huggingface Space Repository - - name: Clone Huggingface Space - env: - HF_TOKEN: ${{ secrets.HF_TOKEN }} - run: | - git clone https://huggingface.co/spaces/lifedebugger/api-qobiltu-dev space - - # Update Git Remote URL and Pull Latest Changes - - name: Update Remote and Pull Changes - env: - HF_TOKEN: ${{ secrets.HF_TOKEN }} - run: | - cd space - git remote set-url origin https://lifedebugger:$HF_TOKEN@huggingface.co/spaces/lifedebugger/api-qobiltu-dev - git pull origin main || echo "No changes to pull" - - # Copy Files to Huggingface Space - - name: Copy Files to Space - run: | - rsync -av --exclude='.git' ./ space/ + - name: Set up SSH key + uses: webfactory/ssh-agent@v0.5.4 + with: + ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} - # Commit and Push to Huggingface Space - - name: Commit and Push to Huggingface - env: - HF_TOKEN: ${{ secrets.HF_TOKEN }} + - name: Deploy to VPS run: | - cd space - git add . - git commit -m "Deploy files from GitHub repository" || echo "No changes to commit" - git push origin main || echo "No changes to push" + ssh -o StrictHostKeyChecking=no qobiltu@103.23.199.136 << 'EOF' + cd /home/qobiltu/api-qobiltu + git pull origin main + docker-compose down -v + docker-compose up --build -d + EOF diff --git a/space/space/.github/workflows/main_huggingface.yml b/space/space/.github/workflows/main_huggingface.yml new file mode 100644 index 0000000000000000000000000000000000000000..39a67571ac3a2e6a6e2a972b9550becd551bdded --- /dev/null +++ b/space/space/.github/workflows/main_huggingface.yml @@ -0,0 +1,52 @@ +name: Deploy to Development via Huggingface + +on: + push: + branches: + - main + +jobs: + deploy-to-huggingface: + runs-on: ubuntu-latest + + steps: + # Checkout repository + - name: Checkout Repository + uses: actions/checkout@v3 + + # Setup Git + - name: Setup Git for Huggingface + run: | + git config --global user.email "abdan.hafidz@gmail.com" + git config --global user.name "abdanhafidz" + + # Clone Huggingface Space Repository + - name: Clone Huggingface Space + env: + HF_TOKEN: ${{ secrets.HF_TOKEN }} + run: | + git clone https://huggingface.co/spaces/lifedebugger/api-qobiltu-dev space + + # Update Git Remote URL and Pull Latest Changes + - name: Update Remote and Pull Changes + env: + HF_TOKEN: ${{ secrets.HF_TOKEN }} + run: | + cd space + git remote set-url origin https://lifedebugger:$HF_TOKEN@huggingface.co/spaces/lifedebugger/api-qobiltu-dev + git pull origin main || echo "No changes to pull" + + # Copy Files to Huggingface Space + - name: Copy Files to Space + run: | + rsync -av --exclude='.git' ./ space/ + + # Commit and Push to Huggingface Space + - name: Commit and Push to Huggingface + env: + HF_TOKEN: ${{ secrets.HF_TOKEN }} + run: | + cd space + git add . + git commit -m "Deploy files from GitHub repository" || echo "No changes to commit" + git push origin main || echo "No changes to push" diff --git a/space/space/Dockerfile b/space/space/Dockerfile index 6183c94745bf456bad5ea308482bd6c3602ad543..d7d9de6f6ae86a458866ad773e9886db45197e9b 100644 --- a/space/space/Dockerfile +++ b/space/space/Dockerfile @@ -1,30 +1,37 @@ -# Gunakan image dasar Golang versi 1.21.6 -FROM golang:1.21.6 - -# Set working directory -WORKDIR /app - -# Copy go.mod dan go.sum -COPY go.mod go.sum ./ - -# Download dependencies -RUN go mod download - -# Copy seluruh kode -COPY . . - -# Buat file .env dengan variabel environment yang dibutuhkan -RUN echo "DB_HOST=aws-0-ap-southeast-1.pooler.supabase.com" >> .env && \ - echo "DB_USER=postgres.rdscploxoikqsevhduii" >> .env && \ - echo "DB_PASSWORD=Qobiltu12233334444" >> .env && \ - echo "DB_PORT=5432" >> .env && \ - echo "DB_NAME=postgres" >> .env && \ - echo "HOST_ADDRESS = 0.0.0.0" >> .env && \ - echo "HOST_PORT = 7860" >> .env && \ - echo "SALT=NZNZtY7dNPz8l0dWINJZLKafWaJrql1s" >> .env && \ - echo "LOG_PATH = logs" >> .env -# Build aplikasi -RUN go build -o main . - -# Jalankan aplikasi -CMD ["./main"] +# Gunakan image dasar Golang versi 1.24.1 +FROM golang:1.24.1 AS builder + +# Set working directory +WORKDIR /app + +# Copy go.mod dan go.sum +COPY go.mod go.sum ./ + +# Download dependencies +RUN go mod download + +# Copy seluruh kode +COPY . . + +# Build aplikasi +# RUN go build -o /app/main . + +# Build aplikasi untuk Linux +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /app/main . + +# Stage 2: Gunakan image runtime yang lebih kecil +FROM alpine:latest + +# Set working directory +WORKDIR /app + +# Copy hasil build dari builder ke image runtime +COPY --from=builder /app/main . + +# Copy file .env untuk konfigurasi environment +COPY .env .env + +RUN chmod +x /app/main + +# Jalankan aplikasi +CMD ["./main"] diff --git a/space/space/README.md b/space/space/README.md index 2516fae6fbcff0dc2e33c51fc780aad8ec3b97e4..dddd6963c3e353a01d81b3aac9c4091a40856c42 100644 --- a/space/space/README.md +++ b/space/space/README.md @@ -62,10 +62,26 @@ The API uses the `errors` package to handle errors. You can set the `ERROR_PATH` # Command Documentation -## Update Golang Project version +## GO Command +### Update Golang Project version > go mod edit -go 1.24 > go mod tidy -## Update all package +### Update all package > go get -u ./... -> go mod tidy \ No newline at end of file +> go mod tidy + +## Docker Command +### Enter docker container +> docker exec -it api-qobiltu sh + +### Docker Running Process +> docker ps + +### Rebuild Docker Image +> docker-compose down -v +> docker-compose up --build -d + +### View Log container +> docker logs --tail=50 -f api-qobiltu +> docker logs postgres-db \ No newline at end of file diff --git a/space/space/docker-compose.yml b/space/space/docker-compose.yml new file mode 100644 index 0000000000000000000000000000000000000000..a821c467525af186355b5837f8673e77a84f7563 --- /dev/null +++ b/space/space/docker-compose.yml @@ -0,0 +1,31 @@ +version: '3.8' + +services: + app: + container_name: api-qobiltu + build: . + depends_on: + - db + env_file: .env + ports: + - "8080:8080" + # volumes: + # - ./logs:/app/logs + # - /home/qobiltu/api-qobiltu:/app + restart: unless-stopped + + db: + image: postgres:15 + container_name: postgres-db + environment: + POSTGRES_USER: ${DB_USER} + POSTGRES_PASSWORD: ${DB_PASSWORD} + POSTGRES_DB: ${DB_NAME} + ports: + - "5432:5432" + volumes: + - db-data:/var/lib/postgresql/data + restart: always + +volumes: + db-data: diff --git a/space/space/space/.gitignore b/space/space/space/.gitignore index bd10d926e1c503ff128e21bf273fe2732e526abf..df8cf1c15e2917803db7f00fbc386495fe8f5479 100644 --- a/space/space/space/.gitignore +++ b/space/space/space/.gitignore @@ -1,4 +1,5 @@ .env vendor/ quzuu-be.exe -README.md \ No newline at end of file +README.md +.qodo diff --git a/space/space/space/config/config.go b/space/space/space/config/config.go index 96d88fd641bf8a6b538d1324abff58c8d5b912a0..823888a64fd867db2cee438c8cb64f792ec16920 100644 --- a/space/space/space/config/config.go +++ b/space/space/space/config/config.go @@ -2,6 +2,7 @@ package config import ( "os" + "strconv" "github.com/joho/godotenv" ) @@ -10,6 +11,7 @@ var TCP_ADDRESS string var LOG_PATH string var HOST_ADDRESS string var HOST_PORT string +var EMAIL_VERIFICATION_DURATION int func init() { godotenv.Load() @@ -17,5 +19,6 @@ func init() { HOST_PORT = os.Getenv("HOST_PORT") TCP_ADDRESS = HOST_ADDRESS + ":" + HOST_PORT LOG_PATH = os.Getenv("LOG_PATH") + EMAIL_VERIFICATION_DURATION, _ = strconv.Atoi(os.Getenv("EMAIL_VERIFICATION_DURATION")) // Menampilkan nilai variabel lingkungan } diff --git a/space/space/space/config/database_connection_config.go b/space/space/space/config/database_connection_config.go index 458388d26dd8408234352e2a835552d6a278f267..3eea63fb7b3d4ffd51970404cadf1af8267c046d 100644 --- a/space/space/space/config/database_connection_config.go +++ b/space/space/space/config/database_connection_config.go @@ -51,7 +51,17 @@ func AutoMigrateAll(db *gorm.DB) { err := db.AutoMigrate( &models.Account{}, &models.AccountDetails{}, + &models.EmailVerification{}, + &models.ExternalAuth{}, + &models.FCM{}, + &models.ForgotPassword{}, + &models.Academy{}, + &models.AcademyMaterial{}, + &models.AcademyContent{}, + &models.AcademyMaterialProgress{}, + &models.AcademyContentProgress{}, ) + if err != nil { log.Fatal(err) } diff --git a/space/space/space/controller/controller.go b/space/space/space/controller/controller.go index 32a528d703f974515cc1baa4960e87ac56481629..456c424696e845d0f994bd91a43f42623d427045 100644 --- a/space/space/space/controller/controller.go +++ b/space/space/space/controller/controller.go @@ -19,6 +19,13 @@ type ( } ) +func (controller *Controller[T1, T2, T3]) HeaderParse(c *gin.Context, act func()) { + cParam, _ := c.Get("accountData") + if cParam != nil { + controller.AccountData = cParam.(models.AccountData) + } + act() +} func (controller *Controller[T1, T2, T3]) RequestJSON(c *gin.Context, act func()) { cParam, _ := c.Get("accountData") if cParam != nil { diff --git a/space/space/space/controller/email/email_create_verification.go b/space/space/space/controller/email/email_create_verification.go new file mode 100644 index 0000000000000000000000000000000000000000..44ee9b005702e30354ef25f46d80e9551dbfcf6b --- /dev/null +++ b/space/space/space/controller/email/email_create_verification.go @@ -0,0 +1,22 @@ +package controller + +import ( + "strconv" + + "api.qobiltu.id/controller" + "api.qobiltu.id/models" + "api.qobiltu.id/services" + "github.com/gin-gonic/gin" +) + +func CreateVerification(c *gin.Context) { + emailVerification := services.EmailVerificationService{} + emailVerificationController := controller.Controller[any, models.EmailVerification, models.EmailVerification]{ + Service: &emailVerification.Service, + } + query, _ := c.GetQuery("account_id") + accountId, _ := strconv.Atoi(query) + emailVerificationController.Service.Constructor.AccountID = uint(accountId) + emailVerification.Create() + emailVerificationController.Response(c) +} diff --git a/space/space/space/controller/email/email_delete_verification.go b/space/space/space/controller/email/email_delete_verification.go new file mode 100644 index 0000000000000000000000000000000000000000..2cd98fbc53a1234cdcbec565d8d42c4247936dd7 --- /dev/null +++ b/space/space/space/controller/email/email_delete_verification.go @@ -0,0 +1,22 @@ +package controller + +import ( + "strconv" + + "api.qobiltu.id/controller" + "api.qobiltu.id/models" + "api.qobiltu.id/services" + "github.com/gin-gonic/gin" +) + +func DeleteVerification(c *gin.Context) { + emailVerification := services.EmailVerificationService{} + emailVerificationController := controller.Controller[any, models.EmailVerification, models.EmailVerification]{ + Service: &emailVerification.Service, + } + query, _ := c.GetQuery("account_id") + accountId, _ := strconv.Atoi(query) + emailVerificationController.Service.Constructor.AccountID = uint(accountId) + emailVerification.Delete() + emailVerificationController.Response(c) +} diff --git a/space/space/space/controller/email/email_verify.go b/space/space/space/controller/email/email_verify.go new file mode 100644 index 0000000000000000000000000000000000000000..695bbccdbfbd8dedf63048f8734c4fe879544d33 --- /dev/null +++ b/space/space/space/controller/email/email_verify.go @@ -0,0 +1,22 @@ +package controller + +import ( + "strconv" + + "api.qobiltu.id/controller" + "api.qobiltu.id/models" + "api.qobiltu.id/services" + "github.com/gin-gonic/gin" +) + +func Verify(c *gin.Context) { + emailVerification := services.EmailVerificationService{} + emailVerificationController := controller.Controller[any, models.EmailVerification, models.EmailVerification]{ + Service: &emailVerification.Service, + } + query, _ := c.GetQuery("account_id") + accountId, _ := strconv.Atoi(query) + emailVerificationController.Service.Constructor.AccountID = uint(accountId) + emailVerification.Validate() + emailVerificationController.Response(c) +} diff --git a/space/space/space/controller/user/user_login_controller.go b/space/space/space/controller/user/user_login_controller.go new file mode 100644 index 0000000000000000000000000000000000000000..b7b9673901e26d2dca8bff51c4fdf834eef30646 --- /dev/null +++ b/space/space/space/controller/user/user_login_controller.go @@ -0,0 +1,20 @@ +package user + +import ( + "api.qobiltu.id/controller" + "api.qobiltu.id/models" + "api.qobiltu.id/services" + "github.com/gin-gonic/gin" +) + +func Login(c *gin.Context) { + authentication := services.AuthenticationService{} + loginController := controller.Controller[models.LoginRequest, services.LoginConstructor, models.AuthenticatedUser]{ + Service: &authentication.Service, + } + loginController.RequestJSON(c, func() { + loginController.Service.Constructor.Email = loginController.Request.Email + loginController.Service.Constructor.Password = loginController.Request.Password + authentication.Authenticate() + }) +} diff --git a/space/space/space/controller/user/user_profile_controller.go b/space/space/space/controller/user/user_profile_controller.go new file mode 100644 index 0000000000000000000000000000000000000000..177520bc297345eaaf66099fe9a71efb8e53b5c5 --- /dev/null +++ b/space/space/space/controller/user/user_profile_controller.go @@ -0,0 +1,24 @@ +package user + +import ( + "fmt" + + "api.qobiltu.id/controller" + "api.qobiltu.id/models" + "api.qobiltu.id/services" + "github.com/gin-gonic/gin" +) + +func Profile(c *gin.Context) { + userProfile := services.UserProfileService{} + userProfileController := controller.Controller[any, services.UserProfileConstructor, models.Account]{ + Service: &userProfile.Service, + } + fmt.Println(userProfileController.AccountData) + userProfileController.HeaderParse(c, func() { + userProfileController.Service.Constructor.AccountId = userProfileController.AccountData.UserID + userProfile.Retrieve() + userProfileController.Response(c) + }, + ) +} diff --git a/space/space/space/controller/user/user_register_controller.go b/space/space/space/controller/user/user_register_controller.go new file mode 100644 index 0000000000000000000000000000000000000000..17051bee42f7b1d3fe144cedb6e8f11dbb5ac5b2 --- /dev/null +++ b/space/space/space/controller/user/user_register_controller.go @@ -0,0 +1,20 @@ +package user + +import ( + "api.qobiltu.id/controller" + "api.qobiltu.id/models" + "api.qobiltu.id/services" + "github.com/gin-gonic/gin" +) + +func Register(c *gin.Context) { + register := services.RegisterService{} + registerController := controller.Controller[models.RegisterRequest, models.Account, models.Account]{ + Service: ®ister.Service, + } + registerController.RequestJSON(c, func() { + registerController.Service.Constructor.Password = registerController.Request.Password + registerController.Service.Constructor.Email = registerController.Request.Email + register.Create() + }) +} diff --git a/space/space/space/controller/user/user_update_profile_controller.go b/space/space/space/controller/user/user_update_profile_controller.go new file mode 100644 index 0000000000000000000000000000000000000000..64072242e2eea309c68624c2b86ad02b3596095b --- /dev/null +++ b/space/space/space/controller/user/user_update_profile_controller.go @@ -0,0 +1,24 @@ +package user + +import ( + "fmt" + + "api.qobiltu.id/controller" + "api.qobiltu.id/models" + "api.qobiltu.id/services" + "github.com/gin-gonic/gin" +) + +func UpdateProfile(c *gin.Context) { + userProfile := services.UserProfileService{} + userUpdateProfileController := controller.Controller[any, services.UserProfileConstructor, models.Account]{ + Service: &userProfile.Service, + } + fmt.Println(userUpdateProfileController.AccountData) + userUpdateProfileController.HeaderParse(c, func() { + userUpdateProfileController.Service.Constructor.AccountId = userUpdateProfileController.AccountData.UserID + userProfile.Retrieve() + userUpdateProfileController.Response(c) + }, + ) +} diff --git a/space/space/space/docker-compose.yaml b/space/space/space/docker-compose.yaml new file mode 100644 index 0000000000000000000000000000000000000000..daf0d5d58dca553ce0c035978ec48d8d4fe52456 --- /dev/null +++ b/space/space/space/docker-compose.yaml @@ -0,0 +1,10 @@ +services: + postgres: + container_name: postgres + image: postgres + environment: + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=password + - POSTGRES_DB=qobiltu + ports: + - 5432:5432 diff --git a/space/space/space/go.mod b/space/space/space/go.mod index 9b6fd06b10d941bc76ba173ba5f2983e9c1408d8..0c2b7a92ff9c9eddbfa0d1506ccb9b4d0614163e 100644 --- a/space/space/space/go.mod +++ b/space/space/space/go.mod @@ -3,6 +3,7 @@ module api.qobiltu.id go 1.24.0 require ( + github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/gin-gonic/gin v1.10.0 github.com/golang-jwt/jwt/v5 v5.2.1 github.com/joho/godotenv v1.5.1 @@ -46,4 +47,4 @@ require ( golang.org/x/text v0.23.0 // indirect google.golang.org/protobuf v1.36.5 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect -) \ No newline at end of file +) diff --git a/space/space/space/go.sum b/space/space/space/go.sum index 6f39995754d2f66f294c1866a62d1810e8d524c5..b7d196affb731541ab18f7ba85fbb21dd47867ac 100644 --- a/space/space/space/go.sum +++ b/space/space/space/go.sum @@ -10,6 +10,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM= github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8= github.com/gin-contrib/sse v1.0.0 h1:y3bT1mUWUxDpW4JLQg/HnTqV4rozuW4tC9eFKTxYI9E= @@ -115,4 +117,4 @@ gorm.io/driver/postgres v1.5.11 h1:ubBVAfbKEUld/twyKZ0IYn9rSQh448EdelLYk9Mv314= gorm.io/driver/postgres v1.5.11/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI= gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8= gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ= -nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= \ No newline at end of file +nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= diff --git a/space/space/space/logserror_log.txt b/space/space/space/logserror_log.txt new file mode 100644 index 0000000000000000000000000000000000000000..30ecdec40a51b2cc4d2181b7108e953e7c77166f --- /dev/null +++ b/space/space/space/logserror_log.txt @@ -0,0 +1 @@ +2025/03/18 21:08:07 Error Log : ERROR: relasi « email_verifications » tidak ada (SQLSTATE 42P01) diff --git a/space/space/space/middleware/authentication_middleware.go b/space/space/space/middleware/authentication_middleware.go index 44178a96738817742fd4f6f7fe1ca63bbecc2d27..41ed5f2fb2eefa1f43c991f2f502d39505c76e3b 100644 --- a/space/space/space/middleware/authentication_middleware.go +++ b/space/space/space/middleware/authentication_middleware.go @@ -3,52 +3,19 @@ package middleware import ( - "errors" "time" "api.qobiltu.id/config" "api.qobiltu.id/models" + "api.qobiltu.id/utils" "github.com/gin-gonic/gin" "github.com/golang-jwt/jwt/v5" - "golang.org/x/crypto/bcrypt" ) -// Define a secret key for signing the JWT token var salt = config.Salt var secretKey = []byte(salt) -// GenerateToken generates a JWT token for the given user -func GenerateToken(user *models.Account) (string, error) { - - // Create a new token - token := jwt.New(jwt.SigningMethodHS256) - - // Set claims - claims := token.Claims.(jwt.MapClaims) - claims["id"] = user.Id - claims["exp"] = time.Now().Add(time.Hour * 24).Unix() // Token expires in 24 hours - - // Sign the token with the secret key - tokenString, err := token.SignedString(secretKey) - if err != nil { - return "", err - } - - return tokenString, nil -} - // VerifyPassword verifies if the provided password matches the hashed password -func VerifyPassword(hashedPassword, password string) error { - err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password)) - if err != nil { - return errors.New("invalid password") - } - return nil -} -func HashPassword(password string) (string, error) { - bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14) - return string(bytes), err -} type CustomClaims struct { jwt.RegisteredClaims @@ -84,21 +51,20 @@ func AuthUser(c *gin.Context) { // fmt.Println("Verify Status :", currAccData.verifyStatus) if currAccData.VerifyStatus == "invalid-token" || currAccData.VerifyStatus == "expired" { currAccData.UserID = 0 - message := "Your session is expired, Please re-Login!" - SendJSON401(c, &currAccData.VerifyStatus, &message) + utils.ResponseFAIL(c, 401, models.Exception{Unauthorized: true, Message: "Your session is expired, Please re-Login!"}) c.Abort() return + } else { + c.Set("accountData", currAccData) + c.Next() } } else { currAccData.UserID = 0 currAccData.VerifyStatus = "no-token" currAccData.ErrVerif = nil - message := "You have to Login First!" - SendJSON401(c, &currAccData.VerifyStatus, &message) + utils.ResponseFAIL(c, 401, models.Exception{Unauthorized: true, Message: "You have to login first!"}) c.Abort() return } - c.Set("accountData", currAccData) - c.Next() } diff --git a/space/space/space/middleware/response_middleware.go b/space/space/space/middleware/response_middleware.go index 55f3385077839115eadb8dace294229f4b4c3df2..d52391cd3607e6ec2697e6a5813b348d49ff5ee2 100644 --- a/space/space/space/middleware/response_middleware.go +++ b/space/space/space/middleware/response_middleware.go @@ -9,31 +9,37 @@ import ( // SendJSON200 sends a JSON response with HTTP status code 200 func SendJSON200(c *gin.Context, data interface{}) { c.JSON(http.StatusOK, gin.H{"status": "success", "data": data}) + return } // SendJSON400 sends a JSON response with HTTP status code 400 func SendJSON400(c *gin.Context, error_status *string, message *string) { c.JSON(http.StatusBadRequest, gin.H{"status": "error", "error-status": error_status, "message": message}) + return } // SendJSON401 sends a JSON response with HTTP status code 401 func SendJSON401(c *gin.Context, error_status *string, message *string) { c.JSON(http.StatusUnauthorized, gin.H{"status": "error", "error-status": error_status, "message": message}) + return } // SendJSON403 sends a JSON response with HTTP status code 403 func SendJSON403(c *gin.Context, message *string) { c.JSON(http.StatusForbidden, gin.H{"status": "error", "message": message}) + return } // SendJSON404 sends a JSON response with HTTP status code 404 func SendJSON404(c *gin.Context, message *string) { c.JSON(http.StatusNotFound, gin.H{"status": "error", "message": message}) + return } // SendJSON500 sends a JSON response with HTTP status code 500 func SendJSON500(c *gin.Context, error_status *string, message *string) { c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "error-status": error_status, "message": message}) + return } // JSONResponseMiddleware is a middleware that provides functions for sending JSON responses diff --git a/space/space/space/models/database_orm_model.go b/space/space/space/models/database_orm_model.go index 82e8b95b39b86be5fb0a4ce538d697e9df8fd0d5..6d1cb93a61de13e3ae0f178cc701c77ed791ae7a 100644 --- a/space/space/space/models/database_orm_model.go +++ b/space/space/space/models/database_orm_model.go @@ -7,25 +7,113 @@ import ( ) type Account struct { - Id uint `gorm:"primaryKey" json:"id"` - UUID uuid.UUID `gorm:"type:uuid" json:"uuid" ` - Email string `gorm:"uniqueIndex" json:"email"` - Password string `json:"password"` - IsEmailVerified bool `json:"is_email_verified"` - CreatedAt time.Time `json:"created_at"` - DeletedAt time.Time `json:"deleted_at"` + Id uint `gorm:"primaryKey" json:"id"` + UUID uuid.UUID `gorm:"type:uuid" json:"uuid" ` + Email string `gorm:"uniqueIndex" json:"email"` + Password string `json:"password"` + IsEmailVerified bool `json:"is_email_verified"` + IsDetailCompleted bool `json:"is_detail_completed"` + CreatedAt time.Time `json:"created_at"` + DeletedAt *time.Time `json:"deleted_at,omitempty" gorm:"default:null"` } type AccountDetails struct { - IDDetail uint `gorm:"primaryKey" json:"id_detail"` - Account_id uint `json:"id_account"` - Province string `json:"province"` - City string `json:"city"` - Institution string `json:"institution"` - UpdatedAt time.Time `json:"updated_at"` - DeletedAt time.Time `json:"deleted_at"` + ID uint `gorm:"primaryKey" json:"id"` + AccountID uint `json:"account_id"` + InitialName string `json:"initial_name"` + FullName *string `json:"full_name,omitempty"` + DateOfBirth *time.Time `json:"date_of_birth,omitempty"` + PlaceOfBirth *string `json:"place_of_birth,omitempty"` + Domicile *string `json:"domicile,omitempty"` + LastJob *string `json:"last_job,omitempty"` + Gender *bool `json:"gender,omitempty"` + LastEducation *string `json:"last_education,omitempty"` + MaritalStatus *bool `json:"marital_status,omitempty"` + Avatar *string `json:"avatar,omitempty"` + PhoneNumber *uint `json:"phone_number,omitempty"` +} + +type EmailVerification struct { + ID uint `gorm:"primaryKey" json:"id"` + Token uint `json:"token"` + AccountID uint `json:"account_id"` + IsExpired bool `json:"is_expired"` + CreatedAt time.Time `json:"created_at"` + ExpiredAt time.Time `json:"expired_at"` +} + +type ExternalAuth struct { + ID uint `gorm:"primaryKey" json:"id"` + OauthID string `json:"oauth_id"` + AccountID uint `json:"account_id"` + OauthProvider string `json:"oauth_provider"` +} + +type FCM struct { + ID uint `gorm:"primaryKey" json:"id"` + AccountID uint `json:"account_id"` + FCMToken string `json:"fcm_token"` +} + +type ForgotPassword struct { + ID uint `gorm:"primaryKey" json:"id"` + UUID uint `json:"uuid"` + AccountID uint `json:"account_id"` + IsExpired bool `json:"is_expired"` + CreatedAt time.Time `json:"created_at"` + ExpiredAt time.Time `json:"expired_at"` +} + +type Academy struct { + ID uint `gorm:"primaryKey" json:"id"` + UUID uuid.UUID `gorm:"type:uuid" json:"uuid"` + Title string `json:"title"` + Slug string `json:"slug"` + Description string `json:"description"` +} + +type AcademyMaterial struct { + ID uint `gorm:"primaryKey" json:"id"` + UUID uuid.UUID `gorm:"type:uuid" json:"uuid"` + AcademyID uint `json:"academy_id"` + Title string `json:"title"` + Slug string `json:"slug"` + Description string `json:"description"` +} + +type AcademyContent struct { + ID uint `gorm:"primaryKey" json:"id"` + UUID uint `json:"uuid"` + Title string `json:"title"` + Order uint `json:"order"` + AcademyMaterialID uint `json:"academy_material_id"` + Description string `json:"description"` +} + +type AcademyMaterialProgress struct { + ID uint `gorm:"primaryKey" json:"id"` + UUID uuid.UUID `gorm:"type:uuid" json:"uuid"` + AccountID uint `json:"account_id"` + AcademyMaterialID uint `json:"academy_material_id"` + Progress uint `json:"progress"` +} + +type AcademyContentProgress struct { + ID uint `gorm:"primaryKey" json:"id"` + UUID uuid.UUID `gorm:"type:uuid" json:"uuid"` + AccountID uint `json:"account_id"` + AcademyID uint `json:"academy_id"` } // Gorm table name settings -func (Account) TableName() string { return "account" } -func (AccountDetails) TableName() string { return "account_details" } +func (Account) TableName() string { return "account" } +func (AccountDetails) TableName() string { return "account_details" } +func (EmailVerification) TableName() string { return "email_verifications" } +func (ExternalAuth) TableName() string { return "extern_auth" } +func (FCM) TableName() string { return "fcm" } +func (ForgotPassword) TableName() string { return "forgot_password" } +func (Academy) TableName() string { return "academy" } +func (AcademyMaterial) TableName() string { return "academy_materials" } +func (AcademyContent) TableName() string { return "academy_contents" } +func (AcademyMaterialProgress) TableName() string { return "academy_materials_progress" } +func (AcademyContentProgress) TableName() string { return "academy_contents_progress" } diff --git a/space/space/space/models/request_model.go b/space/space/space/models/request_model.go index cfae888ec34edbdd3f5b6588050e2b382c63792d..bb4170dfd5e176800d18a19e52ba8feb55531b1d 100644 --- a/space/space/space/models/request_model.go +++ b/space/space/space/models/request_model.go @@ -11,3 +11,8 @@ type RegisterRequest struct { Phone int `json:"phone"` Password string `json:"password" binding:"required"` } + +type CreateEmailVerificationRequest struct { + AccountID int `json:"account_id" binding:"required"` + +} diff --git a/space/space/space/repositories/account_repository.go b/space/space/space/repositories/account_repository.go index 41bd903aff7bf12c9d81cbed1ad00521ae629640..ef028bd358dd2e0f419ad4cbbad7eb3769fb1477 100644 --- a/space/space/space/repositories/account_repository.go +++ b/space/space/space/repositories/account_repository.go @@ -15,6 +15,16 @@ func GetAccountbyEmail(email string) Repository[models.Account, models.Account] return *repo } +func GetAccountbyId(account_id uint) Repository[models.Account, models.Account] { + repo := Construct[models.Account, models.Account]( + models.Account{Id: account_id}, + ) + repo.Transactions( + WhereGivenConstructor[models.Account, models.Account], + Find[models.Account, models.Account], + ) + return *repo +} func CreateAccount(account models.Account) Repository[models.Account, models.Account] { repo := Construct[models.Account, models.Account]( account, @@ -22,3 +32,7 @@ func CreateAccount(account models.Account) Repository[models.Account, models.Acc Create(repo) return *repo } + +// func UpdateAccount(account models.Account) Repository[models.Account, models.Account] { +// repo := Construct +// } diff --git a/space/space/space/repositories/email_verification_repository.go b/space/space/space/repositories/email_verification_repository.go new file mode 100644 index 0000000000000000000000000000000000000000..c737e6d4b782e17630568f3616f0bf7ef36dc31f --- /dev/null +++ b/space/space/space/repositories/email_verification_repository.go @@ -0,0 +1,49 @@ +package repositories + +import ( + "time" + + "api.qobiltu.id/models" +) + +func CreateEmailVerification(accountId uint, dueTime time.Time, token uint) Repository[models.EmailVerification, models.EmailVerification] { + repo := Construct[models.EmailVerification, models.EmailVerification]( + models.EmailVerification{ + AccountID: accountId, + IsExpired: false, + ExpiredAt: dueTime, + Token: token, + }, + ) + Create(repo) + return *repo +} + +func GetEmailVerification(account_id uint, token uint) Repository[models.EmailVerification, models.EmailVerification] { + repo := Construct[models.EmailVerification, models.EmailVerification]( + models.EmailVerification{ + AccountID: account_id, + IsExpired: false, + Token: token, + }, + ) + repo.Transactions( + WhereGivenConstructor[models.EmailVerification, models.EmailVerification], + Find[models.EmailVerification, models.EmailVerification], + ) + return *repo +} + +func DeleteEmailVerification(token uint) Repository[models.EmailVerification, models.EmailVerification] { + repo := Construct[models.EmailVerification, models.EmailVerification]( + models.EmailVerification{ + Token: token, + }, + ) + + repo.Transactions( + WhereGivenConstructor[models.EmailVerification, models.EmailVerification], + Delete[models.EmailVerification], + ) + return *repo +} diff --git a/space/space/space/router/email_route.go b/space/space/space/router/email_route.go new file mode 100644 index 0000000000000000000000000000000000000000..8f2f59e88a8cf958d200ee36174549e839a0a33c --- /dev/null +++ b/space/space/space/router/email_route.go @@ -0,0 +1,15 @@ +package router + +import ( + EmailController "api.qobiltu.id/controller/email" + "github.com/gin-gonic/gin" +) + +func EmailRoute(router *gin.Engine) { + routerGroup := router.Group("/api/v1/email") + { + routerGroup.POST("/verify", EmailController.CreateVerification) + routerGroup.POST("/create-verification", EmailController.CreateVerification) + routerGroup.DELETE("/delete-verification", EmailController.DeleteVerification) + } +} diff --git a/space/space/space/router/router.go b/space/space/space/router/router.go index 114a87bb4eb5bd9713a0cd097c9b46f05eb761df..a7dde2e239a2d254991cd00c712b9eeebe6102d1 100644 --- a/space/space/space/router/router.go +++ b/space/space/space/router/router.go @@ -8,11 +8,8 @@ import ( func StartService() { router := gin.Default() - routerGroup := router.Group("/api/v1") - { - routerGroup.GET("/", controller.HomeController) - routerGroup.POST("/login", controller.LoginController) - routerGroup.POST("/register", controller.RegisterController) - } + router.GET("/", controller.HomeController) + UserRoute(router) + EmailRoute(router) router.Run(config.TCP_ADDRESS) } diff --git a/space/space/space/router/user_route.go b/space/space/space/router/user_route.go new file mode 100644 index 0000000000000000000000000000000000000000..c1abce045debeff19dabe5af38b9903abd5083a0 --- /dev/null +++ b/space/space/space/router/user_route.go @@ -0,0 +1,16 @@ +package router + +import ( + UserController "api.qobiltu.id/controller/user" + "api.qobiltu.id/middleware" + "github.com/gin-gonic/gin" +) + +func UserRoute(router *gin.Engine) { + routerGroup := router.Group("/api/v1/user") + { + routerGroup.POST("/login", UserController.Login) + routerGroup.POST("/register", UserController.Register) + routerGroup.GET("/me", middleware.AuthUser, UserController.Profile) + } +} diff --git a/space/space/space/services/email_verification_service.go b/space/space/space/services/email_verification_service.go new file mode 100644 index 0000000000000000000000000000000000000000..d94c65bc93550413c285e2817b68392664fb3b2a --- /dev/null +++ b/space/space/space/services/email_verification_service.go @@ -0,0 +1,57 @@ +package services + +import ( + "math/rand" + "time" + + "api.qobiltu.id/config" + "api.qobiltu.id/models" + "api.qobiltu.id/repositories" +) + +type EmailVerificationService struct { + Service[models.EmailVerification, models.EmailVerification] +} + +func (s *EmailVerificationService) Create() { + accountRepo := repositories.GetAccountbyId(s.Constructor.AccountID) + if accountRepo.NoRecord { + s.Error = accountRepo.RowsError + s.Exception.DataNotFound = true + s.Exception.Message = "There is no account data with given credentials!" + return + } + + remainingTime := time.Duration(config.EMAIL_VERIFICATION_DURATION) * time.Hour + dueTime := CalculateDueTime(remainingTime) + + randomizer := rand.New(rand.NewSource(10)) + token := uint(randomizer.Int()) + + repo := repositories.CreateEmailVerification(s.Constructor.AccountID, dueTime, token) + + s.Error = repo.RowsError + s.Result = repo.Result +} + +func (s *EmailVerificationService) Validate() { + repo := repositories.GetEmailVerification(s.Constructor.AccountID, s.Constructor.Token) + s.Error = repo.RowsError + if repo.NoRecord { + s.Exception.DataNotFound = true + s.Exception.Message = "Invalid token!" + return + } + s.Result = repo.Result +} + +func (s *EmailVerificationService) Delete() { + repo := repositories.DeleteEmailVerification(s.Constructor.Token) + s.Error = repo.RowsError + if repo.NoRecord { + s.Exception.DataNotFound = true + s.Exception.Message = "Invalid token!" + return + } + s.Result = repo.Result +} diff --git a/space/space/space/services/jwt_service.go b/space/space/space/services/jwt_service.go new file mode 100644 index 0000000000000000000000000000000000000000..92024df8afa7fe83da747bddc969cc1066cf431c --- /dev/null +++ b/space/space/space/services/jwt_service.go @@ -0,0 +1,45 @@ +package services + +import ( + "errors" + "time" + + "api.qobiltu.id/config" + "api.qobiltu.id/models" + "github.com/dgrijalva/jwt-go" + "golang.org/x/crypto/bcrypt" +) + +var salt = config.Salt +var secretKey = []byte(salt) + +func GenerateToken(user *models.Account) (string, error) { + + // Create a new token + token := jwt.New(jwt.SigningMethodHS256) + + // Set claims + claims := token.Claims.(jwt.MapClaims) + claims["id"] = user.Id + claims["exp"] = time.Now().Add(time.Hour * 24).Unix() // Token expires in 24 hours + + // Sign the token with the secret key + tokenString, err := token.SignedString(secretKey) + if err != nil { + return "", err + } + + return tokenString, nil +} + +func VerifyPassword(hashedPassword, password string) error { + err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password)) + if err != nil { + return errors.New("invalid password") + } + return nil +} +func HashPassword(password string) (string, error) { + bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14) + return string(bytes), err +} diff --git a/space/space/space/services/login_service.go b/space/space/space/services/login_service.go index 06ea6bb57e6ccdb5fa55784476af50fcda77eff1..93256b042c4daf76a9f3501111792b940a5ff200 100644 --- a/space/space/space/services/login_service.go +++ b/space/space/space/services/login_service.go @@ -3,7 +3,6 @@ package services import ( "errors" - "api.qobiltu.id/middleware" "api.qobiltu.id/models" "api.qobiltu.id/repositories" ) @@ -24,13 +23,13 @@ func (s *AuthenticationService) Authenticate() { s.Exception.Message = "there is no account with given credentials!" return } - if middleware.VerifyPassword(accountData.Result.Password, s.Constructor.Password) != nil { + if VerifyPassword(accountData.Result.Password, s.Constructor.Password) != nil { s.Exception.Unauthorized = true s.Exception.Message = "incorrect password!" return } - token, err_tok := middleware.GenerateToken(&accountData.Result) + token, err_tok := GenerateToken(&accountData.Result) if err_tok != nil { s.Error = errors.Join(s.Error, err_tok) diff --git a/space/space/space/services/register_service.go b/space/space/space/services/register_service.go index 6732f99fabbd315504042de844f59841294e2025..f9e0d2216bd033155ffcbd73dd1b9de44d20bfa8 100644 --- a/space/space/space/services/register_service.go +++ b/space/space/space/services/register_service.go @@ -3,7 +3,6 @@ package services import ( "errors" - "api.qobiltu.id/middleware" "api.qobiltu.id/models" "api.qobiltu.id/repositories" uuid "github.com/satori/go.uuid" @@ -20,7 +19,7 @@ func (s *RegisterService) Create() { s.Exception.Message = "Password must have at least 8 characters!" return } - hashed_password, err_hash := middleware.HashPassword(s.Constructor.Password) + hashed_password, err_hash := HashPassword(s.Constructor.Password) s.Error = err_hash s.Constructor.Password = hashed_password s.Constructor.UUID = uuid.NewV4() diff --git a/space/space/space/services/service.go b/space/space/space/services/service.go index 7d56d45d633a3e7fd485bc9348527494512a0a50..3e6cda14fae0b3b8b7c50747311d2d8c67ee4d66 100644 --- a/space/space/space/services/service.go +++ b/space/space/space/services/service.go @@ -1,6 +1,10 @@ package services -import "api.qobiltu.id/models" +import ( + "time" + + "api.qobiltu.id/models" +) type ( Services interface { @@ -29,3 +33,7 @@ func Construct[TConstructor any, TResult any](constructor ...TConstructor) *Serv Constructor: constructor[0], } } + +func CalculateDueTime(duration time.Duration) time.Time { + return time.Now().Add(duration) +} diff --git a/space/space/space/services/user_profile_service.go b/space/space/space/services/user_profile_service.go new file mode 100644 index 0000000000000000000000000000000000000000..afea5ccbf0885293e2f782692e229b6948e2228e --- /dev/null +++ b/space/space/space/services/user_profile_service.go @@ -0,0 +1,37 @@ +package services + +import ( + "api.qobiltu.id/models" + "api.qobiltu.id/repositories" +) + +type UserProfileConstructor struct { + AccountId int +} +type UserProfileService struct { + Service[UserProfileConstructor, models.Account] +} + +func (s *UserProfileService) Retrieve() { + userProfile := repositories.GetAccountbyId(uint(s.Constructor.AccountId)) + s.Error = userProfile.RowsError + if userProfile.NoRecord { + s.Exception.DataNotFound = true + s.Exception.Message = "There is no account with given credentials!" + return + } + s.Result.Password = "SECRET" + s.Result = userProfile.Result +} + +func (s *UserProfileService) Update() { + userProfile := repositories.GetAccountbyId(uint(s.Constructor.AccountId)) + s.Error = userProfile.RowsError + if userProfile.NoRecord { + s.Exception.DataNotFound = true + s.Exception.Message = "There is no account with given credentials!" + return + } + s.Result.Password = "SECRET" + s.Result = userProfile.Result +} diff --git a/space/space/space/space/LICENSE b/space/space/space/space/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..79da4b69760d1658f4dbc42604187316b368d857 --- /dev/null +++ b/space/space/space/space/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2025 Abdan Hafidz + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/space/space/space/space/space/.github/workflows/main.yml b/space/space/space/space/space/.github/workflows/main.yml index 37c1b056c4f04eaa76005b0f10c04f8764cd2d13..39a67571ac3a2e6a6e2a972b9550becd551bdded 100644 --- a/space/space/space/space/space/.github/workflows/main.yml +++ b/space/space/space/space/space/.github/workflows/main.yml @@ -49,4 +49,4 @@ jobs: cd space git add . git commit -m "Deploy files from GitHub repository" || echo "No changes to commit" - git push --force origin main || echo "No changes to push" + git push origin main || echo "No changes to push" diff --git a/space/space/space/space/space/space/.github/workflows/main.yml b/space/space/space/space/space/space/.github/workflows/main.yml index 39a67571ac3a2e6a6e2a972b9550becd551bdded..37c1b056c4f04eaa76005b0f10c04f8764cd2d13 100644 --- a/space/space/space/space/space/space/.github/workflows/main.yml +++ b/space/space/space/space/space/space/.github/workflows/main.yml @@ -49,4 +49,4 @@ jobs: cd space git add . git commit -m "Deploy files from GitHub repository" || echo "No changes to commit" - git push origin main || echo "No changes to push" + git push --force origin main || echo "No changes to push" diff --git a/space/space/space/space/space/space/space/.gitignore b/space/space/space/space/space/space/space/.gitignore index deeb9479f4cb982ccd42fa0d7210b9777509e4ae..bd10d926e1c503ff128e21bf273fe2732e526abf 100644 --- a/space/space/space/space/space/space/space/.gitignore +++ b/space/space/space/space/space/space/space/.gitignore @@ -1,3 +1,4 @@ .env vendor/ -quzuu-be.exe \ No newline at end of file +quzuu-be.exe +README.md \ No newline at end of file diff --git a/space/space/space/space/space/space/space/config/config.go b/space/space/space/space/space/space/space/config/config.go new file mode 100644 index 0000000000000000000000000000000000000000..96d88fd641bf8a6b538d1324abff58c8d5b912a0 --- /dev/null +++ b/space/space/space/space/space/space/space/config/config.go @@ -0,0 +1,21 @@ +package config + +import ( + "os" + + "github.com/joho/godotenv" +) + +var TCP_ADDRESS string +var LOG_PATH string +var HOST_ADDRESS string +var HOST_PORT string + +func init() { + godotenv.Load() + HOST_ADDRESS = os.Getenv("HOST_ADDRESS") + HOST_PORT = os.Getenv("HOST_PORT") + TCP_ADDRESS = HOST_ADDRESS + ":" + HOST_PORT + LOG_PATH = os.Getenv("LOG_PATH") + // Menampilkan nilai variabel lingkungan +} diff --git a/space/space/space/space/space/space/space/config/database_connection_config.go b/space/space/space/space/space/space/space/config/database_connection_config.go new file mode 100644 index 0000000000000000000000000000000000000000..458388d26dd8408234352e2a835552d6a278f267 --- /dev/null +++ b/space/space/space/space/space/space/space/config/database_connection_config.go @@ -0,0 +1,60 @@ +package config + +import ( + "fmt" + "log" + "os" + + "gorm.io/driver/postgres" + "gorm.io/gorm" + "gorm.io/gorm/logger" + + "api.qobiltu.id/models" + "github.com/joho/godotenv" +) + +var DB *gorm.DB +var err error +var Salt string + +func init() { + godotenv.Load() + if err != nil { + fmt.Println("Gagal membaca file .env") + return + } + os.Setenv("TZ", "Asia/Jakarta") + dbHost := os.Getenv("DB_HOST") + dbPort := os.Getenv("DB_PORT") + dbUser := os.Getenv("DB_USER") + dbPassword := os.Getenv("DB_PASSWORD") + dbName := os.Getenv("DB_NAME") + Salt := os.Getenv("SALT") + dsn := "host=" + dbHost + " user=" + dbUser + " password=" + dbPassword + " dbname=" + dbName + " port=" + dbPort + " sslmode=disable TimeZone=Asia/Jakarta" + DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{TranslateError: true}) + if err != nil { + panic(err) + } + if Salt == "" { + Salt = "D3f4u|t" + } + + // Call AutoMigrateAll to perform auto-migration + AutoMigrateAll(DB) +} + +func AutoMigrateAll(db *gorm.DB) { + // Enable logger to see SQL logs + db.Logger.LogMode(logger.Info) + + // Auto-migrate all models + err := db.AutoMigrate( + &models.Account{}, + &models.AccountDetails{}, + ) + if err != nil { + log.Fatal(err) + } + + fmt.Println("Migration completed successfully.") +} diff --git a/space/space/space/space/space/space/space/controller/home_controller.go b/space/space/space/space/space/space/space/controller/home_controller.go new file mode 100644 index 0000000000000000000000000000000000000000..06cfc3d7fac0385d6aef847a186de2eae7dbb4bb --- /dev/null +++ b/space/space/space/space/space/space/space/controller/home_controller.go @@ -0,0 +1,9 @@ +package controller + +import "github.com/gin-gonic/gin" + +func HomeController(c *gin.Context) { + c.JSON(200, gin.H{ + "message": "Api Qobiltu 2025!", + }) +} diff --git a/space/space/space/space/space/space/space/controller/login_controller.go b/space/space/space/space/space/space/space/controller/login_controller.go new file mode 100644 index 0000000000000000000000000000000000000000..b411fa438a6d877515eca1d40dcb21060edd68c4 --- /dev/null +++ b/space/space/space/space/space/space/space/controller/login_controller.go @@ -0,0 +1,19 @@ +package controller + +import ( + "api.qobiltu.id/models" + "api.qobiltu.id/services" + "github.com/gin-gonic/gin" +) + +func LoginController(c *gin.Context) { + authentication := services.AuthenticationService{} + loginController := Controller[models.LoginRequest, services.LoginConstructor, models.AuthenticatedUser]{ + Service: &authentication.Service, + } + loginController.RequestJSON(c, func() { + loginController.Service.Constructor.Email = loginController.Request.Email + loginController.Service.Constructor.Password = loginController.Request.Password + authentication.Authenticate() + }) +} diff --git a/space/space/space/space/space/space/space/controller/register_controller.go b/space/space/space/space/space/space/space/controller/register_controller.go new file mode 100644 index 0000000000000000000000000000000000000000..3dc0f3c21343fe3279a492db3e994cbf8eef3561 --- /dev/null +++ b/space/space/space/space/space/space/space/controller/register_controller.go @@ -0,0 +1,19 @@ +package controller + +import ( + "api.qobiltu.id/models" + "api.qobiltu.id/services" + "github.com/gin-gonic/gin" +) + +func RegisterController(c *gin.Context) { + register := services.RegisterService{} + registerController := Controller[models.RegisterRequest, models.Account, models.Account]{ + Service: ®ister.Service, + } + registerController.RequestJSON(c, func() { + registerController.Service.Constructor.Password = registerController.Request.Password + registerController.Service.Constructor.Email = registerController.Request.Email + register.Create() + }) +} diff --git a/space/space/space/space/space/space/space/middleware/authentication_middleware.go b/space/space/space/space/space/space/space/middleware/authentication_middleware.go new file mode 100644 index 0000000000000000000000000000000000000000..44178a96738817742fd4f6f7fe1ca63bbecc2d27 --- /dev/null +++ b/space/space/space/space/space/space/space/middleware/authentication_middleware.go @@ -0,0 +1,104 @@ +// auth/auth.go + +package middleware + +import ( + "errors" + "time" + + "api.qobiltu.id/config" + "api.qobiltu.id/models" + "github.com/gin-gonic/gin" + "github.com/golang-jwt/jwt/v5" + "golang.org/x/crypto/bcrypt" +) + +// Define a secret key for signing the JWT token +var salt = config.Salt +var secretKey = []byte(salt) + +// GenerateToken generates a JWT token for the given user +func GenerateToken(user *models.Account) (string, error) { + + // Create a new token + token := jwt.New(jwt.SigningMethodHS256) + + // Set claims + claims := token.Claims.(jwt.MapClaims) + claims["id"] = user.Id + claims["exp"] = time.Now().Add(time.Hour * 24).Unix() // Token expires in 24 hours + + // Sign the token with the secret key + tokenString, err := token.SignedString(secretKey) + if err != nil { + return "", err + } + + return tokenString, nil +} + +// VerifyPassword verifies if the provided password matches the hashed password +func VerifyPassword(hashedPassword, password string) error { + err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password)) + if err != nil { + return errors.New("invalid password") + } + return nil +} +func HashPassword(password string) (string, error) { + bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14) + return string(bytes), err +} + +type CustomClaims struct { + jwt.RegisteredClaims + UserID int `json:"id"` +} + +func VerifyToken(bearer_token string) (int, string, error) { + // fmt.Println(bearer_token) + token, err := jwt.ParseWithClaims(bearer_token, &CustomClaims{}, func(token *jwt.Token) (interface{}, error) { + return secretKey, nil + }) + if err != nil { + return 0, "invalid-token", err + } + + // Extract the claims + claims, ok := token.Claims.(*CustomClaims) + if !ok || !token.Valid { + return 0, "invalid-token", err + } + if claims.ExpiresAt != nil && claims.ExpiresAt.Time.Before(time.Now()) { + return 0, "expired", err + } + + return claims.UserID, "valid", err +} + +func AuthUser(c *gin.Context) { + var currAccData models.AccountData + if c.Request.Header["Auth-Bearer-Token"] != nil { + token := c.Request.Header["Auth-Bearer-Token"] + currAccData.UserID, currAccData.VerifyStatus, currAccData.ErrVerif = VerifyToken(token[0]) + // fmt.Println("Verify Status :", currAccData.verifyStatus) + if currAccData.VerifyStatus == "invalid-token" || currAccData.VerifyStatus == "expired" { + currAccData.UserID = 0 + message := "Your session is expired, Please re-Login!" + SendJSON401(c, &currAccData.VerifyStatus, &message) + c.Abort() + return + } + } else { + currAccData.UserID = 0 + currAccData.VerifyStatus = "no-token" + currAccData.ErrVerif = nil + message := "You have to Login First!" + SendJSON401(c, &currAccData.VerifyStatus, &message) + c.Abort() + return + } + + c.Set("accountData", currAccData) + c.Next() +} diff --git a/space/space/space/space/space/space/space/middleware/response_middleware.go b/space/space/space/space/space/space/space/middleware/response_middleware.go new file mode 100644 index 0000000000000000000000000000000000000000..55f3385077839115eadb8dace294229f4b4c3df2 --- /dev/null +++ b/space/space/space/space/space/space/space/middleware/response_middleware.go @@ -0,0 +1,39 @@ +package middleware + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +// SendJSON200 sends a JSON response with HTTP status code 200 +func SendJSON200(c *gin.Context, data interface{}) { + c.JSON(http.StatusOK, gin.H{"status": "success", "data": data}) +} + +// SendJSON400 sends a JSON response with HTTP status code 400 +func SendJSON400(c *gin.Context, error_status *string, message *string) { + c.JSON(http.StatusBadRequest, gin.H{"status": "error", "error-status": error_status, "message": message}) +} + +// SendJSON401 sends a JSON response with HTTP status code 401 +func SendJSON401(c *gin.Context, error_status *string, message *string) { + c.JSON(http.StatusUnauthorized, gin.H{"status": "error", "error-status": error_status, "message": message}) +} + +// SendJSON403 sends a JSON response with HTTP status code 403 +func SendJSON403(c *gin.Context, message *string) { + c.JSON(http.StatusForbidden, gin.H{"status": "error", "message": message}) +} + +// SendJSON404 sends a JSON response with HTTP status code 404 +func SendJSON404(c *gin.Context, message *string) { + c.JSON(http.StatusNotFound, gin.H{"status": "error", "message": message}) +} + +// SendJSON500 sends a JSON response with HTTP status code 500 +func SendJSON500(c *gin.Context, error_status *string, message *string) { + c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "error-status": error_status, "message": message}) +} + +// JSONResponseMiddleware is a middleware that provides functions for sending JSON responses diff --git a/space/space/space/space/space/space/space/models/authentication_payload_model.go b/space/space/space/space/space/space/space/models/authentication_payload_model.go new file mode 100644 index 0000000000000000000000000000000000000000..0203b9b340d066705491edf66830674602f89655 --- /dev/null +++ b/space/space/space/space/space/space/space/models/authentication_payload_model.go @@ -0,0 +1,7 @@ +package models + +type AccountData struct { + UserID int + VerifyStatus string + ErrVerif error +} diff --git a/space/space/space/space/space/space/space/models/database_orm_model.go b/space/space/space/space/space/space/space/models/database_orm_model.go new file mode 100644 index 0000000000000000000000000000000000000000..82e8b95b39b86be5fb0a4ce538d697e9df8fd0d5 --- /dev/null +++ b/space/space/space/space/space/space/space/models/database_orm_model.go @@ -0,0 +1,31 @@ +package models + +import ( + "time" + + uuid "github.com/satori/go.uuid" +) + +type Account struct { + Id uint `gorm:"primaryKey" json:"id"` + UUID uuid.UUID `gorm:"type:uuid" json:"uuid" ` + Email string `gorm:"uniqueIndex" json:"email"` + Password string `json:"password"` + IsEmailVerified bool `json:"is_email_verified"` + CreatedAt time.Time `json:"created_at"` + DeletedAt time.Time `json:"deleted_at"` +} + +type AccountDetails struct { + IDDetail uint `gorm:"primaryKey" json:"id_detail"` + Account_id uint `json:"id_account"` + Province string `json:"province"` + City string `json:"city"` + Institution string `json:"institution"` + UpdatedAt time.Time `json:"updated_at"` + DeletedAt time.Time `json:"deleted_at"` +} + +// Gorm table name settings +func (Account) TableName() string { return "account" } +func (AccountDetails) TableName() string { return "account_details" } diff --git a/space/space/space/space/space/space/space/models/exception_model.go b/space/space/space/space/space/space/space/models/exception_model.go new file mode 100644 index 0000000000000000000000000000000000000000..f2c026a993f950652158318977783e8563e595fb --- /dev/null +++ b/space/space/space/space/space/space/space/models/exception_model.go @@ -0,0 +1,12 @@ +package models + +type Exception struct { + Unauthorized bool `json:"unauthorized,omitempty"` + BadRequest bool `json:"bad_request,omitempty"` + DataNotFound bool `json:"data_not_found,omitempty"` + InternalServerError bool `json:"internal_server_error,omitempty"` + DataDuplicate bool `json:"data_duplicate,omitempty"` + QueryError bool `json:"query_error,omitempty"` + InvalidPasswordLength bool `json:"invalid_password_length,omitempty"` + Message string `json:"message,omitempty"` +} diff --git a/space/space/space/space/space/space/space/models/model.go b/space/space/space/space/space/space/space/models/model.go new file mode 100644 index 0000000000000000000000000000000000000000..9ce401186cb92d50737155815c1f511164b86407 --- /dev/null +++ b/space/space/space/space/space/space/space/models/model.go @@ -0,0 +1 @@ +package models diff --git a/space/space/space/space/space/space/space/models/request_model.go b/space/space/space/space/space/space/space/models/request_model.go new file mode 100644 index 0000000000000000000000000000000000000000..cfae888ec34edbdd3f5b6588050e2b382c63792d --- /dev/null +++ b/space/space/space/space/space/space/space/models/request_model.go @@ -0,0 +1,13 @@ +package models + +type LoginRequest struct { + Email string `json:"email" binding:"required"` + Password string `json:"password" binding:"required"` +} + +type RegisterRequest struct { + Name string `json:"name"` + Email string `json:"email" binding:"required,email"` + Phone int `json:"phone"` + Password string `json:"password" binding:"required"` +} diff --git a/space/space/space/space/space/space/space/models/response_model.go b/space/space/space/space/space/space/space/models/response_model.go new file mode 100644 index 0000000000000000000000000000000000000000..c45df0dbc642de90cc3537722b359a2f09a06b02 --- /dev/null +++ b/space/space/space/space/space/space/space/models/response_model.go @@ -0,0 +1,19 @@ +package models + +type SuccessResponse struct { + Status string `json:"status"` + Message string `json:"message"` + Data any `json:"data"` + MetaData any `json:"meta_data"` +} + +type ErrorResponse struct { + Status string `json:"status"` + Message string `json:"message"` + Errors Exception `json:"errors"` + MetaData any `json:"meta_data"` +} +type AuthenticatedUser struct { + Account Account `json:"account"` + Token string `json:"token"` +} diff --git a/space/space/space/space/space/space/space/repositories/account_repository.go b/space/space/space/space/space/space/space/repositories/account_repository.go new file mode 100644 index 0000000000000000000000000000000000000000..41bd903aff7bf12c9d81cbed1ad00521ae629640 --- /dev/null +++ b/space/space/space/space/space/space/space/repositories/account_repository.go @@ -0,0 +1,24 @@ +package repositories + +import ( + "api.qobiltu.id/models" +) + +func GetAccountbyEmail(email string) Repository[models.Account, models.Account] { + repo := Construct[models.Account, models.Account]( + models.Account{Email: email}, + ) + repo.Transactions( + WhereGivenConstructor[models.Account, models.Account], + Find[models.Account, models.Account], + ) + return *repo +} + +func CreateAccount(account models.Account) Repository[models.Account, models.Account] { + repo := Construct[models.Account, models.Account]( + account, + ) + Create(repo) + return *repo +} diff --git a/space/space/space/space/space/space/space/repositories/repository.go b/space/space/space/space/space/space/space/repositories/repository.go new file mode 100644 index 0000000000000000000000000000000000000000..3617e40936bf9e50e39a50285105622f080d210f --- /dev/null +++ b/space/space/space/space/space/space/space/repositories/repository.go @@ -0,0 +1,113 @@ +package repositories + +import ( + "api.qobiltu.id/config" + "gorm.io/gorm" +) + +type Repositories interface { + FindAllPaginate() + Where() + Find() + Create() + Update() + CustomQuery() + Delete() +} +type PaginationConstructor struct { + Limit int + Offset int + Filter string +} + +type CustomQueryConstructor struct { + SQL string + Values interface{} +} + +type Repository[TConstructor any, TResult any] struct { + Constructor TConstructor + Pagination PaginationConstructor + CustomQuery CustomQueryConstructor + Result TResult + Transaction *gorm.DB + RowsCount int + NoRecord bool + RowsError error +} + +func Construct[TConstructor any, TResult any](constructor ...TConstructor) *Repository[TConstructor, TResult] { + if len(constructor) == 1 { + return &Repository[TConstructor, TResult]{ + Constructor: constructor[0], + Transaction: config.DB, + } + } + return &Repository[TConstructor, TResult]{ + Constructor: constructor[0], + Transaction: config.DB.Begin(), + } +} +func (repo *Repository[T1, T2]) Transactions(transactions ...func(*Repository[T1, T2]) *gorm.DB) { + for _, tx := range transactions { + repo.Transaction = tx(repo) + if repo.RowsError != nil { + return + } + } +} +func WhereGivenConstructor[T1 any, T2 any](repo *Repository[T1, T2]) *gorm.DB { + tx := repo.Transaction.Where(&repo.Constructor) + repo.RowsCount = int(tx.RowsAffected) + repo.NoRecord = repo.RowsCount == 0 + repo.RowsError = tx.Error + return tx +} +func Find[T1 any, T2 any](repo *Repository[T1, T2]) *gorm.DB { + tx := repo.Transaction.Find(&repo.Result) + repo.RowsCount = int(tx.RowsAffected) + repo.NoRecord = repo.RowsCount == 0 + repo.RowsError = tx.Error + return tx +} + +func FinddAllPaginate[T1 any, T2 any](repo *Repository[T1, T2]) *gorm.DB { + tx := repo.Transaction.Limit(repo.Pagination.Limit).Offset(repo.Pagination.Offset).Find(&repo.Result) + repo.RowsCount = int(tx.RowsAffected) + repo.NoRecord = repo.RowsCount == 0 + repo.RowsError = tx.Error + return tx +} + +func Create[T1 any](repo *Repository[T1, T1]) *gorm.DB { + tx := repo.Transaction.Create(&repo.Constructor) + repo.RowsCount = int(tx.RowsAffected) + repo.NoRecord = repo.RowsCount == 0 + repo.RowsError = tx.Error + repo.Result = repo.Constructor + return tx +} + +func Update[T1 any](repo *Repository[T1, T1]) *gorm.DB { + tx := repo.Transaction.Save(&repo.Constructor) + repo.RowsCount = int(tx.RowsAffected) + repo.NoRecord = repo.RowsCount == 0 + repo.RowsError = tx.Error + return tx +} + +func Delete[T1 any](repo *Repository[T1, T1]) *gorm.DB { + tx := repo.Transaction.Delete(&repo.Constructor) + repo.RowsCount = int(tx.RowsAffected) + repo.NoRecord = repo.RowsCount == 0 + repo.RowsError = tx.Error + return tx +} + +func CustomQuery[T1 any, T2 any](repo *Repository[T1, T2]) *gorm.DB { + tx := repo.Transaction.Raw(repo.CustomQuery.SQL, repo.CustomQuery.Values).Scan(&repo.Result) + repo.RowsCount = int(tx.RowsAffected) + repo.NoRecord = repo.RowsCount == 0 + repo.RowsError = tx.Error + return tx +} diff --git a/space/space/space/space/space/space/space/services/login_service.go b/space/space/space/space/space/space/space/services/login_service.go new file mode 100644 index 0000000000000000000000000000000000000000..06ea6bb57e6ccdb5fa55784476af50fcda77eff1 --- /dev/null +++ b/space/space/space/space/space/space/space/services/login_service.go @@ -0,0 +1,47 @@ +package services + +import ( + "errors" + + "api.qobiltu.id/middleware" + "api.qobiltu.id/models" + "api.qobiltu.id/repositories" +) + +type LoginConstructor struct { + Email string + Password string +} + +type AuthenticationService struct { + Service[LoginConstructor, models.AuthenticatedUser] +} + +func (s *AuthenticationService) Authenticate() { + accountData := repositories.GetAccountbyEmail(s.Constructor.Email) + if accountData.NoRecord { + s.Exception.DataNotFound = true + s.Exception.Message = "there is no account with given credentials!" + return + } + if middleware.VerifyPassword(accountData.Result.Password, s.Constructor.Password) != nil { + s.Exception.Unauthorized = true + s.Exception.Message = "incorrect password!" + return + } + + token, err_tok := middleware.GenerateToken(&accountData.Result) + + if err_tok != nil { + s.Error = errors.Join(s.Error, err_tok) + } + + accountData.Result.Password = "SECRET" + s.Result = models.AuthenticatedUser{ + Account: accountData.Result, + Token: token, + } + s.Error = accountData.RowsError +} + +// LoginHandler handles user login diff --git a/space/space/space/space/space/space/space/services/register_service.go b/space/space/space/space/space/space/space/services/register_service.go new file mode 100644 index 0000000000000000000000000000000000000000..6732f99fabbd315504042de844f59841294e2025 --- /dev/null +++ b/space/space/space/space/space/space/space/services/register_service.go @@ -0,0 +1,40 @@ +package services + +import ( + "errors" + + "api.qobiltu.id/middleware" + "api.qobiltu.id/models" + "api.qobiltu.id/repositories" + uuid "github.com/satori/go.uuid" + "gorm.io/gorm" +) + +type RegisterService struct { + Service[models.Account, models.Account] +} + +func (s *RegisterService) Create() { + if len(s.Constructor.Password) < 8 { + s.Exception.InvalidPasswordLength = true + s.Exception.Message = "Password must have at least 8 characters!" + return + } + hashed_password, err_hash := middleware.HashPassword(s.Constructor.Password) + s.Error = err_hash + s.Constructor.Password = hashed_password + s.Constructor.UUID = uuid.NewV4() + accountCreated := repositories.CreateAccount(s.Constructor) + if errors.Is(accountCreated.RowsError, gorm.ErrDuplicatedKey) { + s.Exception.DataDuplicate = true + s.Exception.Message = "Account with email " + s.Constructor.Email + " already exists!" + return + } else if errors.Is(accountCreated.RowsError, gorm.ErrModelAccessibleFieldsRequired) || errors.Is(accountCreated.RowsError, gorm.ErrInvalidData) || errors.Is(accountCreated.RowsError, gorm.ErrInvalidValue) || errors.Is(accountCreated.RowsError, gorm.ErrInvalidField) { + s.Exception.BadRequest = true + s.Exception.Message = "Bad request!" + return + } + s.Error = accountCreated.RowsError + s.Result = accountCreated.Result + s.Result.Password = "SECRET" +} diff --git a/space/space/space/space/space/space/space/services/service.go b/space/space/space/space/space/space/space/services/service.go new file mode 100644 index 0000000000000000000000000000000000000000..7d56d45d633a3e7fd485bc9348527494512a0a50 --- /dev/null +++ b/space/space/space/space/space/space/space/services/service.go @@ -0,0 +1,31 @@ +package services + +import "api.qobiltu.id/models" + +type ( + Services interface { + Retrieve() + Update() + Create() + Delete() + Validate() + Authenticate() + Authorize() + } + Service[TConstructor any, TResult any] struct { + Constructor TConstructor + Result TResult + Exception models.Exception + Error error + } +) + +func Construct[TConstructor any, TResult any](constructor ...TConstructor) *Service[TConstructor, TResult] { + if len(constructor) == 1 { + return &Service[TConstructor, TResult]{} + } + + return &Service[TConstructor, TResult]{ + Constructor: constructor[0], + } +} diff --git a/space/space/space/space/space/space/space/space/README.md b/space/space/space/space/space/space/space/space/README.md index 6ebd472f68999f72453c5168cded8b9e4a1026f9..2516fae6fbcff0dc2e33c51fc780aad8ec3b97e4 100644 --- a/space/space/space/space/space/space/space/space/README.md +++ b/space/space/space/space/space/space/space/space/README.md @@ -1,8 +1,71 @@ ---- -title: Quzuu Api Dev -emoji: 🐠 -colorFrom: indigo -colorTo: gray -sdk: docker -pinned: false ---- \ No newline at end of file +# Golang API Boilerplate +This is a boilerplate for a RESTful API written in Golang. + +## Features +- [x] JWT Authentication +- [x] PostgreSQL Database +- [x] GORM ORM +- [x] Gin Framework +- [x] Docker Support +- [x] Environment Variables +- [x] Logging +- [x] Error Handling + +## Getting Started +1. Clone the repository +2. Run `go get` to download the dependencies +3. Create a file named `.env` and add the following variables: + * `DB_HOST` + * `DB_USER` + * `DB_PASSWORD` + * `DB_NAME` + * `DB_PORT` + * `SALT` + * `HOST_ADDRESS` + * `HOST_PORT` + * `LOG_PATH` +4. Run `go run main.go` to start the server +5. Open your browser and navigate to `http://localhost:HOST_PORT` + +## API Endpoints +### Authentication +- `POST /login`: Login with email and password +- `POST /register`: Register a new user +- `GET /logout`: Logout + +### Users +- `GET /users`: Get all users +- `GET /users/:id`: Get a user by ID +- `PUT /users/:id`: Update a user +- `DELETE /users/:id`: Delete a user + +## Docker Support +You can build a Docker image by running `docker build -t my-api .` and then run it with `docker run -p 8080:8080 my-api`. + +## Environment Variables +You can set the following environment variables: +- `DB_HOST` +- `DB_USER` +- `DB_PASSWORD` +- `DB_NAME` +- `DB_PORT` +- `SALT` +- `HOST_ADDRESS` +- `HOST_PORT` +- `LOG_PATH` + +## Logging +The API uses the `log` package to log errors. You can set the `LOG_PATH` environment variable to specify the log file path. + +## Error Handling +The API uses the `errors` package to handle errors. You can set the `ERROR_PATH` environment variable to specify the error file path. + +# Command Documentation + +## Update Golang Project version +> go mod edit -go 1.24 +> go mod tidy + +## Update all package +> go get -u ./... +> go mod tidy \ No newline at end of file diff --git a/space/space/space/space/space/space/space/space/config/DatabaseConfig.go b/space/space/space/space/space/space/space/space/config/DatabaseConfig.go index 7dfa116ec7152e6338c764ce5853ad21782bd26f..458388d26dd8408234352e2a835552d6a278f267 100644 --- a/space/space/space/space/space/space/space/space/config/DatabaseConfig.go +++ b/space/space/space/space/space/space/space/space/config/DatabaseConfig.go @@ -9,26 +9,10 @@ import ( "gorm.io/gorm" "gorm.io/gorm/logger" + "api.qobiltu.id/models" "github.com/joho/godotenv" - "go-dp.abdanhafidz.com/models" ) -func AutoMigrateAll(db *gorm.DB) { - // Enable logger to see SQL logs - db.Logger.LogMode(logger.Info) - - // Auto-migrate all models - err := db.AutoMigrate( - &models.Account{}, - &models.AccountDetails{}, - ) - if err != nil { - log.Fatal(err) - } - - fmt.Println("Migration completed successfully.") -} - var DB *gorm.DB var err error var Salt string @@ -58,3 +42,19 @@ func init() { // Call AutoMigrateAll to perform auto-migration AutoMigrateAll(DB) } + +func AutoMigrateAll(db *gorm.DB) { + // Enable logger to see SQL logs + db.Logger.LogMode(logger.Info) + + // Auto-migrate all models + err := db.AutoMigrate( + &models.Account{}, + &models.AccountDetails{}, + ) + if err != nil { + log.Fatal(err) + } + + fmt.Println("Migration completed successfully.") +} diff --git a/space/space/space/space/space/space/space/space/controller/LoginController.go b/space/space/space/space/space/space/space/space/controller/LoginController.go index 6d04567f7a776399c5faafc4f11dff4c0735ae9a..b411fa438a6d877515eca1d40dcb21060edd68c4 100644 --- a/space/space/space/space/space/space/space/space/controller/LoginController.go +++ b/space/space/space/space/space/space/space/space/controller/LoginController.go @@ -1,9 +1,9 @@ package controller import ( + "api.qobiltu.id/models" + "api.qobiltu.id/services" "github.com/gin-gonic/gin" - "go-dp.abdanhafidz.com/models" - "go-dp.abdanhafidz.com/services" ) func LoginController(c *gin.Context) { diff --git a/space/space/space/space/space/space/space/space/controller/RegisterController.go b/space/space/space/space/space/space/space/space/controller/RegisterController.go index e3375ba58212ddd73d2d4e29fbf67d247e89398b..3dc0f3c21343fe3279a492db3e994cbf8eef3561 100644 --- a/space/space/space/space/space/space/space/space/controller/RegisterController.go +++ b/space/space/space/space/space/space/space/space/controller/RegisterController.go @@ -1,9 +1,9 @@ package controller import ( + "api.qobiltu.id/models" + "api.qobiltu.id/services" "github.com/gin-gonic/gin" - "go-dp.abdanhafidz.com/models" - "go-dp.abdanhafidz.com/services" ) func RegisterController(c *gin.Context) { diff --git a/space/space/space/space/space/space/space/space/controller/controller.go b/space/space/space/space/space/space/space/space/controller/controller.go index fa14d0b2c6ed4e25a72839a3fad18b2d59e40af3..32a528d703f974515cc1baa4960e87ac56481629 100644 --- a/space/space/space/space/space/space/space/space/controller/controller.go +++ b/space/space/space/space/space/space/space/space/controller/controller.go @@ -1,10 +1,10 @@ package controller import ( + "api.qobiltu.id/models" + "api.qobiltu.id/services" + "api.qobiltu.id/utils" "github.com/gin-gonic/gin" - "go-dp.abdanhafidz.com/models" - "go-dp.abdanhafidz.com/services" - "go-dp.abdanhafidz.com/utils" ) type ( diff --git a/space/space/space/space/space/space/space/space/go.mod b/space/space/space/space/space/space/space/space/go.mod index f6d85920f3d50bf7a3fc013a4ee8875237b0e108..9b6fd06b10d941bc76ba173ba5f2983e9c1408d8 100644 --- a/space/space/space/space/space/space/space/space/go.mod +++ b/space/space/space/space/space/space/space/space/go.mod @@ -1,49 +1,49 @@ -module go-dp.abdanhafidz.com +module api.qobiltu.id -go 1.21.0 +go 1.24.0 require ( - github.com/dgrijalva/jwt-go v3.2.0+incompatible - github.com/gin-gonic/gin v1.9.1 + github.com/gin-gonic/gin v1.10.0 + github.com/golang-jwt/jwt/v5 v5.2.1 github.com/joho/godotenv v1.5.1 - golang.org/x/crypto v0.32.0 - gorm.io/driver/postgres v1.5.4 - gorm.io/gorm v1.25.5 + github.com/satori/go.uuid v1.2.0 + golang.org/x/crypto v0.36.0 + gorm.io/driver/postgres v1.5.11 + gorm.io/gorm v1.25.12 ) require ( - github.com/bytedance/sonic v1.10.2 // indirect - github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect - github.com/chenzhuoyu/iasm v0.9.0 // indirect + github.com/bytedance/sonic v1.13.1 // indirect + github.com/bytedance/sonic/loader v0.2.4 // indirect + github.com/cloudwego/base64x v0.1.5 // indirect github.com/gabriel-vasile/mimetype v1.4.8 // indirect - github.com/gin-contrib/sse v0.1.0 // indirect + github.com/gin-contrib/sse v1.0.0 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.25.0 // indirect - github.com/goccy/go-json v0.10.2 // indirect + github.com/goccy/go-json v0.10.5 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect - github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect - github.com/jackc/pgx/v5 v5.5.0 // indirect - github.com/jackc/puddle/v2 v2.2.1 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + github.com/jackc/pgx/v5 v5.7.2 // indirect + github.com/jackc/puddle/v2 v2.2.2 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/json-iterator/go v1.1.12 // indirect - github.com/klauspost/cpuid/v2 v2.2.6 // indirect + github.com/klauspost/cpuid/v2 v2.2.10 // indirect github.com/kr/text v0.2.0 // indirect github.com/leodido/go-urn v1.4.0 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect - github.com/pelletier/go-toml/v2 v2.1.0 // indirect + github.com/pelletier/go-toml/v2 v2.2.3 // indirect github.com/rogpeppe/go-internal v1.11.0 // indirect - github.com/satori/go.uuid v1.2.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect - github.com/ugorji/go/codec v1.2.11 // indirect - golang.org/x/arch v0.6.0 // indirect - golang.org/x/net v0.34.0 // indirect - golang.org/x/sync v0.10.0 // indirect - golang.org/x/sys v0.29.0 // indirect - golang.org/x/text v0.21.0 // indirect - google.golang.org/protobuf v1.31.0 // indirect + github.com/ugorji/go/codec v1.2.12 // indirect + golang.org/x/arch v0.15.0 // indirect + golang.org/x/net v0.37.0 // indirect + golang.org/x/sync v0.12.0 // indirect + golang.org/x/sys v0.31.0 // indirect + golang.org/x/text v0.23.0 // indirect + google.golang.org/protobuf v1.36.5 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect -) +) \ No newline at end of file diff --git a/space/space/space/space/space/space/space/space/go.sum b/space/space/space/space/space/space/space/space/go.sum index a82e88494d1f86d0560792cace9c2a61284c1d8a..6f39995754d2f66f294c1866a62d1810e8d524c5 100644 --- a/space/space/space/space/space/space/space/space/go.sum +++ b/space/space/space/space/space/space/space/space/go.sum @@ -1,51 +1,44 @@ -github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= -github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM= -github.com/bytedance/sonic v1.10.2 h1:GQebETVBxYB7JGWJtLBi07OVzWwt+8dWA00gEVW2ZFE= -github.com/bytedance/sonic v1.10.2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4= -github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= -github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= -github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0= -github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA= -github.com/chenzhuoyu/iasm v0.9.0 h1:9fhXjVzq5hUy2gkhhgHl95zG2cEAhw9OSGs8toWWAwo= -github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= +github.com/bytedance/sonic v1.13.1 h1:Jyd5CIvdFnkOWuKXr+wm4Nyk2h0yAFsr8ucJgEasO3g= +github.com/bytedance/sonic v1.13.1/go.mod h1:o68xyaF9u2gvVBuGHPlUVCy+ZfmNNO5ETf1+KgkJhz4= +github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/bytedance/sonic/loader v0.2.4 h1:ZWCw4stuXUsn1/+zQDqeE7JKP+QO47tz7QCNan80NzY= +github.com/bytedance/sonic/loader v0.2.4/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI= +github.com/cloudwego/base64x v0.1.5 h1:XPciSp1xaq2VCSt6lF0phncD4koWyULpl5bUxbfCyP4= +github.com/cloudwego/base64x v0.1.5/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= +github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= -github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= -github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= -github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM= github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8= -github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= -github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= -github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= -github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= +github.com/gin-contrib/sse v1.0.0 h1:y3bT1mUWUxDpW4JLQg/HnTqV4rozuW4tC9eFKTxYI9E= +github.com/gin-contrib/sse v1.0.0/go.mod h1:zNuFdwarAygJBht0NTKiSi3jRf6RbqeILZ9Sp6Slhe0= +github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= +github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y= github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= -github.com/go-playground/validator/v10 v10.16.0 h1:x+plE831WK4vaKHO/jpgUGsvLKIqRRkz6M78GuJAfGE= -github.com/go-playground/validator/v10 v10.16.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= github.com/go-playground/validator/v10 v10.25.0 h1:5Dh7cjvzR7BRZadnsVOzPhWsrwUr0nmsZJxEAnFLNO8= github.com/go-playground/validator/v10 v10.25.0/go.mod h1:GGzBIJMuE98Ic/kJsBXbz1x/7cByt++cQ+YOuDM5wus= -github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= -github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= -github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= +github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= +github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk= +github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= -github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= -github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= -github.com/jackc/pgx/v5 v5.5.0 h1:NxstgwndsTRy7eq9/kqYc/BZh5w2hHJV86wjvO+1xPw= -github.com/jackc/pgx/v5 v5.5.0/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA= -github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= -github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.7.2 h1:mLoDLV6sonKlvjIEsV56SkWNCnuNv531l94GaIzO+XI= +github.com/jackc/pgx/v5 v5.7.2/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ= +github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= +github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= @@ -55,15 +48,13 @@ github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwA github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= -github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc= -github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE= +github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0= github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= -github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= @@ -73,8 +64,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= -github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= -github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= +github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M= +github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= @@ -84,57 +75,44 @@ github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdh github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= -github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= -github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= -golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= -golang.org/x/arch v0.6.0 h1:S0JTfE48HbRj80+4tbvZDYsJ3tGv6BUU3XxyZ7CirAc= -golang.org/x/arch v0.6.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= -golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA= -golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g= -golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc= -golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc= -golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg= -golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ= -golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0= -golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k= -golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= -golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= -golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= +github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +golang.org/x/arch v0.15.0 h1:QtOrQd0bTUnhNVNndMpLHNWrDmYzZ2KDqSrEymqInZw= +golang.org/x/arch v0.15.0/go.mod h1:JmwW7aLIoRUKgaTzhkiEFxvcEiQGyOg9BMonBJUS7EE= +golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34= +golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc= +golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c= +golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8= +golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw= +golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= -golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= -golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= -golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= -golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= -golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= +golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik= +golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY= +golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= -google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM= +google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gorm.io/driver/postgres v1.5.4 h1:Iyrp9Meh3GmbSuyIAGyjkN+n9K+GHX9b9MqsTL4EJCo= -gorm.io/driver/postgres v1.5.4/go.mod h1:Bgo89+h0CRcdA33Y6frlaHHVuTdOf87pmyzwW9C/BH0= -gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls= -gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= -nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= -rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= +gorm.io/driver/postgres v1.5.11 h1:ubBVAfbKEUld/twyKZ0IYn9rSQh448EdelLYk9Mv314= +gorm.io/driver/postgres v1.5.11/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI= +gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8= +gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ= +nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= \ No newline at end of file diff --git a/space/space/space/space/space/space/space/space/main.go b/space/space/space/space/space/space/space/space/main.go index d89532891b559d7d8361e79a1090e77a31cafb95..676f1b4f77f3957678f711cc402e21791cf11471 100644 --- a/space/space/space/space/space/space/space/space/main.go +++ b/space/space/space/space/space/space/space/space/main.go @@ -3,8 +3,8 @@ package main import ( "fmt" - "go-dp.abdanhafidz.com/config" - "go-dp.abdanhafidz.com/router" + "api.qobiltu.id/config" + "api.qobiltu.id/router" ) func main() { diff --git a/space/space/space/space/space/space/space/space/middleware/AuthMiddleware.go b/space/space/space/space/space/space/space/space/middleware/AuthMiddleware.go index c153979ac01f108193541d23f821c288b4752f01..44178a96738817742fd4f6f7fe1ca63bbecc2d27 100644 --- a/space/space/space/space/space/space/space/space/middleware/AuthMiddleware.go +++ b/space/space/space/space/space/space/space/space/middleware/AuthMiddleware.go @@ -6,10 +6,10 @@ import ( "errors" "time" - "github.com/dgrijalva/jwt-go" + "api.qobiltu.id/config" + "api.qobiltu.id/models" "github.com/gin-gonic/gin" - "go-dp.abdanhafidz.com/config" - "go-dp.abdanhafidz.com/models" + "github.com/golang-jwt/jwt/v5" "golang.org/x/crypto/bcrypt" ) @@ -51,8 +51,8 @@ func HashPassword(password string) (string, error) { } type CustomClaims struct { - jwt.StandardClaims - IDUser int `json:"id"` + jwt.RegisteredClaims + UserID int `json:"id"` } func VerifyToken(bearer_token string) (int, string, error) { @@ -63,33 +63,34 @@ func VerifyToken(bearer_token string) (int, string, error) { if err != nil { return 0, "invalid-token", err } + + // Extract the claims claims, ok := token.Claims.(*CustomClaims) if !ok || !token.Valid { return 0, "invalid-token", err - } else if claims.StandardClaims.ExpiresAt != 0 && claims.ExpiresAt < time.Now().Unix() { + } + if claims.ExpiresAt != nil && claims.ExpiresAt.Time.Before(time.Now()) { return 0, "expired", err - } else if !ok && token.Valid { - return 0, "invalid-token", err } - return claims.IDUser, "valid", err + return claims.UserID, "valid", err } func AuthUser(c *gin.Context) { var currAccData models.AccountData if c.Request.Header["Auth-Bearer-Token"] != nil { token := c.Request.Header["Auth-Bearer-Token"] - currAccData.IdUser, currAccData.VerifyStatus, currAccData.ErrVerif = VerifyToken(token[0]) + currAccData.UserID, currAccData.VerifyStatus, currAccData.ErrVerif = VerifyToken(token[0]) // fmt.Println("Verify Status :", currAccData.verifyStatus) if currAccData.VerifyStatus == "invalid-token" || currAccData.VerifyStatus == "expired" { - currAccData.IdUser = 0 + currAccData.UserID = 0 message := "Your session is expired, Please re-Login!" SendJSON401(c, &currAccData.VerifyStatus, &message) c.Abort() return } } else { - currAccData.IdUser = 0 + currAccData.UserID = 0 currAccData.VerifyStatus = "no-token" currAccData.ErrVerif = nil message := "You have to Login First!" diff --git a/space/space/space/space/space/space/space/space/repositories/AccountRepository.go b/space/space/space/space/space/space/space/space/repositories/AccountRepository.go index f191583a0de0f8ec9291985a06d40c00161522d0..41bd903aff7bf12c9d81cbed1ad00521ae629640 100644 --- a/space/space/space/space/space/space/space/space/repositories/AccountRepository.go +++ b/space/space/space/space/space/space/space/space/repositories/AccountRepository.go @@ -1,7 +1,7 @@ package repositories import ( - "go-dp.abdanhafidz.com/models" + "api.qobiltu.id/models" ) func GetAccountbyEmail(email string) Repository[models.Account, models.Account] { diff --git a/space/space/space/space/space/space/space/space/repositories/DatabaseScoope.go b/space/space/space/space/space/space/space/space/repositories/DatabaseScoope.go index e11987b79a890dc6e84923fe7c241c6e8fa99c35..560945a97953d6c1bfef5db40b56cdab15024204 100644 --- a/space/space/space/space/space/space/space/space/repositories/DatabaseScoope.go +++ b/space/space/space/space/space/space/space/space/repositories/DatabaseScoope.go @@ -1,5 +1,5 @@ package repositories -import "go-dp.abdanhafidz.com/config" +import "api.qobiltu.id/config" var db = config.DB diff --git a/space/space/space/space/space/space/space/space/repositories/repositories.go b/space/space/space/space/space/space/space/space/repositories/repositories.go index 9d34eda9880d655f8b4200d4792fff50fc144a54..3617e40936bf9e50e39a50285105622f080d210f 100644 --- a/space/space/space/space/space/space/space/space/repositories/repositories.go +++ b/space/space/space/space/space/space/space/space/repositories/repositories.go @@ -1,7 +1,7 @@ package repositories import ( - "go-dp.abdanhafidz.com/config" + "api.qobiltu.id/config" "gorm.io/gorm" ) diff --git a/space/space/space/space/space/space/space/space/router/router.go b/space/space/space/space/space/space/space/space/router/router.go index 27ce310f782a252cab580d08873505f02ec38a03..114a87bb4eb5bd9713a0cd097c9b46f05eb761df 100644 --- a/space/space/space/space/space/space/space/space/router/router.go +++ b/space/space/space/space/space/space/space/space/router/router.go @@ -1,9 +1,9 @@ package router import ( + "api.qobiltu.id/config" + "api.qobiltu.id/controller" "github.com/gin-gonic/gin" - "go-dp.abdanhafidz.com/config" - "go-dp.abdanhafidz.com/controller" ) func StartService() { diff --git a/space/space/space/space/space/space/space/space/services/LoginService.go b/space/space/space/space/space/space/space/space/services/LoginService.go index 21778c5b70ddeded49df2a7069057abe28fb7c5d..06ea6bb57e6ccdb5fa55784476af50fcda77eff1 100644 --- a/space/space/space/space/space/space/space/space/services/LoginService.go +++ b/space/space/space/space/space/space/space/space/services/LoginService.go @@ -3,9 +3,9 @@ package services import ( "errors" - "go-dp.abdanhafidz.com/middleware" - "go-dp.abdanhafidz.com/models" - "go-dp.abdanhafidz.com/repositories" + "api.qobiltu.id/middleware" + "api.qobiltu.id/models" + "api.qobiltu.id/repositories" ) type LoginConstructor struct { diff --git a/space/space/space/space/space/space/space/space/services/RegisterService.go b/space/space/space/space/space/space/space/space/services/RegisterService.go index 106c30887c103cba810c602b4431fae9f0520d5d..6732f99fabbd315504042de844f59841294e2025 100644 --- a/space/space/space/space/space/space/space/space/services/RegisterService.go +++ b/space/space/space/space/space/space/space/space/services/RegisterService.go @@ -3,10 +3,10 @@ package services import ( "errors" + "api.qobiltu.id/middleware" + "api.qobiltu.id/models" + "api.qobiltu.id/repositories" uuid "github.com/satori/go.uuid" - "go-dp.abdanhafidz.com/middleware" - "go-dp.abdanhafidz.com/models" - "go-dp.abdanhafidz.com/repositories" "gorm.io/gorm" ) diff --git a/space/space/space/space/space/space/space/space/services/services.go b/space/space/space/space/space/space/space/space/services/services.go index e684786fdd3dc071dd085b87f27a0e7bb8becfff..7d56d45d633a3e7fd485bc9348527494512a0a50 100644 --- a/space/space/space/space/space/space/space/space/services/services.go +++ b/space/space/space/space/space/space/space/space/services/services.go @@ -1,6 +1,6 @@ package services -import "go-dp.abdanhafidz.com/models" +import "api.qobiltu.id/models" type ( Services interface { @@ -21,7 +21,7 @@ type ( ) func Construct[TConstructor any, TResult any](constructor ...TConstructor) *Service[TConstructor, TResult] { - if len(constructor) == 0 { + if len(constructor) == 1 { return &Service[TConstructor, TResult]{} } diff --git a/space/space/space/space/space/space/space/space/space/models/ExceptionModel.go b/space/space/space/space/space/space/space/space/space/models/ExceptionModel.go index b4e76203056e9c2ee6ea5b447b6d3b90842f1d88..f2c026a993f950652158318977783e8563e595fb 100644 --- a/space/space/space/space/space/space/space/space/space/models/ExceptionModel.go +++ b/space/space/space/space/space/space/space/space/space/models/ExceptionModel.go @@ -1,11 +1,12 @@ package models type Exception struct { - Unauthorized bool `json:"unauthorized,omitempty"` - BadRequest bool `json:"bad_request,omitempty"` - DataNotFound bool `json:"data_not_found,omitempty"` - InternalServerError bool `json:"internal_server_error,omitempty"` - DataDuplicate bool `json:"data_duplicate,omitempty"` - QueryError bool `json:"query_error,omitempty"` - Message string `json:"message,omitempty"` + Unauthorized bool `json:"unauthorized,omitempty"` + BadRequest bool `json:"bad_request,omitempty"` + DataNotFound bool `json:"data_not_found,omitempty"` + InternalServerError bool `json:"internal_server_error,omitempty"` + DataDuplicate bool `json:"data_duplicate,omitempty"` + QueryError bool `json:"query_error,omitempty"` + InvalidPasswordLength bool `json:"invalid_password_length,omitempty"` + Message string `json:"message,omitempty"` } diff --git a/space/space/space/space/space/space/space/space/space/services/RegisterService.go b/space/space/space/space/space/space/space/space/space/services/RegisterService.go index 93acaba8cfa9364f620c1481dec0bf913e2bbfb5..106c30887c103cba810c602b4431fae9f0520d5d 100644 --- a/space/space/space/space/space/space/space/space/space/services/RegisterService.go +++ b/space/space/space/space/space/space/space/space/space/services/RegisterService.go @@ -15,6 +15,11 @@ type RegisterService struct { } func (s *RegisterService) Create() { + if len(s.Constructor.Password) < 8 { + s.Exception.InvalidPasswordLength = true + s.Exception.Message = "Password must have at least 8 characters!" + return + } hashed_password, err_hash := middleware.HashPassword(s.Constructor.Password) s.Error = err_hash s.Constructor.Password = hashed_password diff --git a/space/space/space/space/space/space/space/space/space/space/repositories/AccountRepository.go b/space/space/space/space/space/space/space/space/space/space/repositories/AccountRepository.go index b868ad4c0353135ae0659d8fa9db3ead4f334eb8..f191583a0de0f8ec9291985a06d40c00161522d0 100644 --- a/space/space/space/space/space/space/space/space/space/space/repositories/AccountRepository.go +++ b/space/space/space/space/space/space/space/space/space/space/repositories/AccountRepository.go @@ -4,9 +4,9 @@ import ( "go-dp.abdanhafidz.com/models" ) -func GetAccountbyEmailPassword(email string, password string) Repository[models.Account, models.Account] { +func GetAccountbyEmail(email string) Repository[models.Account, models.Account] { repo := Construct[models.Account, models.Account]( - models.Account{Email: email, Password: password}, + models.Account{Email: email}, ) repo.Transactions( WhereGivenConstructor[models.Account, models.Account], diff --git a/space/space/space/space/space/space/space/space/space/space/repositories/repositories.go b/space/space/space/space/space/space/space/space/space/space/repositories/repositories.go index 7a5c2816396cbb729fc1e0df162e9d0eea6ea42b..9d34eda9880d655f8b4200d4792fff50fc144a54 100644 --- a/space/space/space/space/space/space/space/space/space/space/repositories/repositories.go +++ b/space/space/space/space/space/space/space/space/space/space/repositories/repositories.go @@ -1,8 +1,6 @@ package repositories import ( - "strconv" - "go-dp.abdanhafidz.com/config" "gorm.io/gorm" ) @@ -51,15 +49,10 @@ func Construct[TConstructor any, TResult any](constructor ...TConstructor) *Repo } } func (repo *Repository[T1, T2]) Transactions(transactions ...func(*Repository[T1, T2]) *gorm.DB) { - i := 1 for _, tx := range transactions { repo.Transaction = tx(repo) if repo.RowsError != nil { - repo.Transaction.Rollback() return - } else { - repo.Transaction.SavePoint("Save Point : " + strconv.Itoa(i)) - repo.Transaction.Commit() } } } diff --git a/space/space/space/space/space/space/space/space/space/space/services/LoginService.go b/space/space/space/space/space/space/space/space/space/space/services/LoginService.go index f1bdc1d654ff34ce16ece776c45919bd3841b38d..21778c5b70ddeded49df2a7069057abe28fb7c5d 100644 --- a/space/space/space/space/space/space/space/space/space/space/services/LoginService.go +++ b/space/space/space/space/space/space/space/space/space/space/services/LoginService.go @@ -18,7 +18,7 @@ type AuthenticationService struct { } func (s *AuthenticationService) Authenticate() { - accountData := repositories.GetAccountbyEmailPassword(s.Constructor.Email, s.Constructor.Password) + accountData := repositories.GetAccountbyEmail(s.Constructor.Email) if accountData.NoRecord { s.Exception.DataNotFound = true s.Exception.Message = "there is no account with given credentials!" diff --git a/space/space/space/space/space/space/space/space/space/space/services/RegisterService.go b/space/space/space/space/space/space/space/space/space/space/services/RegisterService.go index 8809621fac0e901ca8c058868b6318d0ad9470ca..93acaba8cfa9364f620c1481dec0bf913e2bbfb5 100644 --- a/space/space/space/space/space/space/space/space/space/space/services/RegisterService.go +++ b/space/space/space/space/space/space/space/space/space/space/services/RegisterService.go @@ -3,6 +3,7 @@ package services import ( "errors" + uuid "github.com/satori/go.uuid" "go-dp.abdanhafidz.com/middleware" "go-dp.abdanhafidz.com/models" "go-dp.abdanhafidz.com/repositories" @@ -17,6 +18,7 @@ func (s *RegisterService) Create() { hashed_password, err_hash := middleware.HashPassword(s.Constructor.Password) s.Error = err_hash s.Constructor.Password = hashed_password + s.Constructor.UUID = uuid.NewV4() accountCreated := repositories.CreateAccount(s.Constructor) if errors.Is(accountCreated.RowsError, gorm.ErrDuplicatedKey) { s.Exception.DataDuplicate = true diff --git a/space/space/space/space/space/space/space/space/space/space/space/go.mod b/space/space/space/space/space/space/space/space/space/space/space/go.mod index efe450181fc1f3927c0b6658778233ce43ac1d76..f6d85920f3d50bf7a3fc013a4ee8875237b0e108 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/go.mod +++ b/space/space/space/space/space/space/space/space/space/space/space/go.mod @@ -36,6 +36,7 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.1.0 // indirect github.com/rogpeppe/go-internal v1.11.0 // indirect + github.com/satori/go.uuid v1.2.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.11 // indirect golang.org/x/arch v0.6.0 // indirect diff --git a/space/space/space/space/space/space/space/space/space/space/space/go.sum b/space/space/space/space/space/space/space/space/space/space/space/go.sum index 79ffaec719b95b12a37e3a787b1576ef5a53db03..a82e88494d1f86d0560792cace9c2a61284c1d8a 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/go.sum +++ b/space/space/space/space/space/space/space/space/space/space/space/go.sum @@ -79,6 +79,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= +github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= +github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= diff --git a/space/space/space/space/space/space/space/space/space/space/space/models/DatabaseModel.go b/space/space/space/space/space/space/space/space/space/space/space/models/DatabaseModel.go index c8730cd31d769683f0900f12d40c1cf8ff038b8c..b2f38ee65f5094a7311928d1567e5263d87f9c7a 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/models/DatabaseModel.go +++ b/space/space/space/space/space/space/space/space/space/space/space/models/DatabaseModel.go @@ -2,14 +2,18 @@ package models import ( "time" + + uuid "github.com/satori/go.uuid" ) type Account struct { - Id uint `gorm:"primaryKey" json:"id"` - Email string `gorm:"uniqueIndex" json:"email"` - Password string `json:"password"` - CreatedAt time.Time `json:"created_at"` - DeletedAt time.Time `json:"deleted_at"` + Id uint `gorm:"primaryKey" json:"id"` + UUID uuid.UUID `gorm:"type:uuid" json:"uuid" ` + Email string `gorm:"uniqueIndex" json:"email"` + Password string `json:"password"` + IsEmailVerified bool `json:"is_email_verified"` + CreatedAt time.Time `json:"created_at"` + DeletedAt time.Time `json:"deleted_at"` } type AccountDetails struct { @@ -22,6 +26,14 @@ type AccountDetails struct { DeletedAt time.Time `json:"deleted_at"` } +type EmailVerification struct { + Id int `gorm:"primaryKey" json:"id"` + AccountId int `json:"account_id"` + UUID uuid.UUID `gorm:"type:uuid" json:"uuid" ` + CreatedAt time.Time `json:"created_at"` + ExpiredAt time.Time `json:"expired_at"` +} + // Gorm table name settings func (Account) TableName() string { return "account" } func (AccountDetails) TableName() string { return "account_details" } diff --git a/space/space/space/space/space/space/space/space/space/space/space/repositories/AccountRepository.go b/space/space/space/space/space/space/space/space/space/space/space/repositories/AccountRepository.go index c6b942973c0f0ab0a5e4204a77bd8e7482def6e1..b868ad4c0353135ae0659d8fa9db3ead4f334eb8 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/repositories/AccountRepository.go +++ b/space/space/space/space/space/space/space/space/space/space/space/repositories/AccountRepository.go @@ -1,8 +1,6 @@ package repositories import ( - "fmt" - "go-dp.abdanhafidz.com/models" ) @@ -10,7 +8,6 @@ func GetAccountbyEmailPassword(email string, password string) Repository[models. repo := Construct[models.Account, models.Account]( models.Account{Email: email, Password: password}, ) - fmt.Println(repo.Constructor) repo.Transactions( WhereGivenConstructor[models.Account, models.Account], Find[models.Account, models.Account], diff --git a/space/space/space/space/space/space/space/space/space/space/space/services/RegisterService.go b/space/space/space/space/space/space/space/space/space/space/space/services/RegisterService.go index 054093a50f74a7c8530f498afacc8ca18de63315..8809621fac0e901ca8c058868b6318d0ad9470ca 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/services/RegisterService.go +++ b/space/space/space/space/space/space/space/space/space/space/space/services/RegisterService.go @@ -14,7 +14,8 @@ type RegisterService struct { } func (s *RegisterService) Create() { - hashed_password, _ := middleware.HashPassword(s.Constructor.Password) + hashed_password, err_hash := middleware.HashPassword(s.Constructor.Password) + s.Error = err_hash s.Constructor.Password = hashed_password accountCreated := repositories.CreateAccount(s.Constructor) if errors.Is(accountCreated.RowsError, gorm.ErrDuplicatedKey) { @@ -26,7 +27,7 @@ func (s *RegisterService) Create() { s.Exception.Message = "Bad request!" return } - + s.Error = accountCreated.RowsError s.Result = accountCreated.Result s.Result.Password = "SECRET" } diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/controller/LoginController.go b/space/space/space/space/space/space/space/space/space/space/space/space/controller/LoginController.go index 8102aa46984caff0eda1d09563bdb9544c377821..6d04567f7a776399c5faafc4f11dff4c0735ae9a 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/space/controller/LoginController.go +++ b/space/space/space/space/space/space/space/space/space/space/space/space/controller/LoginController.go @@ -11,9 +11,9 @@ func LoginController(c *gin.Context) { loginController := Controller[models.LoginRequest, services.LoginConstructor, models.AuthenticatedUser]{ Service: &authentication.Service, } - loginController.RequestJSON(c) - loginController.Service.Constructor.Email = loginController.Request.Email - loginController.Service.Constructor.Password = loginController.Request.Password - authentication.Authenticate() - loginController.Response(c) + loginController.RequestJSON(c, func() { + loginController.Service.Constructor.Email = loginController.Request.Email + loginController.Service.Constructor.Password = loginController.Request.Password + authentication.Authenticate() + }) } diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/controller/RegisterController.go b/space/space/space/space/space/space/space/space/space/space/space/space/controller/RegisterController.go index 25244306baf7b5ff373c0e1c925c584740710eee..e3375ba58212ddd73d2d4e29fbf67d247e89398b 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/space/controller/RegisterController.go +++ b/space/space/space/space/space/space/space/space/space/space/space/space/controller/RegisterController.go @@ -11,9 +11,9 @@ func RegisterController(c *gin.Context) { registerController := Controller[models.RegisterRequest, models.Account, models.Account]{ Service: ®ister.Service, } - registerController.RequestJSON(c) - registerController.Service.Constructor.Password = registerController.Request.Password - registerController.Service.Constructor.Email = registerController.Request.Email - register.Create() - registerController.Response(c) + registerController.RequestJSON(c, func() { + registerController.Service.Constructor.Password = registerController.Request.Password + registerController.Service.Constructor.Email = registerController.Request.Email + register.Create() + }) } diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/controller/controller.go b/space/space/space/space/space/space/space/space/space/space/space/space/controller/controller.go index 45298c05462bb0b95532d7c35bde0e27fe76d500..fa14d0b2c6ed4e25a72839a3fad18b2d59e40af3 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/space/controller/controller.go +++ b/space/space/space/space/space/space/space/space/space/space/space/space/controller/controller.go @@ -19,7 +19,7 @@ type ( } ) -func (controller *Controller[T1, T2, T3]) RequestJSON(c *gin.Context) { +func (controller *Controller[T1, T2, T3]) RequestJSON(c *gin.Context, act func()) { cParam, _ := c.Get("accountData") if cParam != nil { controller.AccountData = cParam.(models.AccountData) @@ -28,9 +28,12 @@ func (controller *Controller[T1, T2, T3]) RequestJSON(c *gin.Context) { if errBinding != nil { utils.ResponseFAIL(c, 400, models.Exception{ BadRequest: true, - Message: "Invalid Request Type", + Message: "Invalid Request!, recheck your request, there's must be some problem about required parameter or type parameter", }) return + } else { + act() + controller.Response(c) } } func (controller *Controller[T1, T2, T3]) Response(c *gin.Context) { diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/models/DatabaseModel.go b/space/space/space/space/space/space/space/space/space/space/space/space/models/DatabaseModel.go index 355323a487ca5c8fc593a1bdea525f30081b884d..c8730cd31d769683f0900f12d40c1cf8ff038b8c 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/space/models/DatabaseModel.go +++ b/space/space/space/space/space/space/space/space/space/space/space/space/models/DatabaseModel.go @@ -6,7 +6,7 @@ import ( type Account struct { Id uint `gorm:"primaryKey" json:"id"` - Email string `gorm:"primaryKey" json:"email"` + Email string `gorm:"uniqueIndex" json:"email"` Password string `json:"password"` CreatedAt time.Time `json:"created_at"` DeletedAt time.Time `json:"deleted_at"` diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/repositories/AccountRepository.go b/space/space/space/space/space/space/space/space/space/space/space/space/repositories/AccountRepository.go index 9243b47af1fc1184b20eae52a1300010764f5bab..c6b942973c0f0ab0a5e4204a77bd8e7482def6e1 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/space/repositories/AccountRepository.go +++ b/space/space/space/space/space/space/space/space/space/space/space/space/repositories/AccountRepository.go @@ -1,6 +1,8 @@ package repositories import ( + "fmt" + "go-dp.abdanhafidz.com/models" ) @@ -8,7 +10,11 @@ func GetAccountbyEmailPassword(email string, password string) Repository[models. repo := Construct[models.Account, models.Account]( models.Account{Email: email, Password: password}, ) - Find(repo) + fmt.Println(repo.Constructor) + repo.Transactions( + WhereGivenConstructor[models.Account, models.Account], + Find[models.Account, models.Account], + ) return *repo } @@ -16,7 +22,6 @@ func CreateAccount(account models.Account) Repository[models.Account, models.Acc repo := Construct[models.Account, models.Account]( account, ) - Create(repo) return *repo } diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/repositories/repositories.go b/space/space/space/space/space/space/space/space/space/space/space/space/repositories/repositories.go index 767e3d93bb01248012ed0a89768db740be5157c9..7a5c2816396cbb729fc1e0df162e9d0eea6ea42b 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/space/repositories/repositories.go +++ b/space/space/space/space/space/space/space/space/space/space/space/space/repositories/repositories.go @@ -1,6 +1,8 @@ package repositories import ( + "strconv" + "go-dp.abdanhafidz.com/config" "gorm.io/gorm" ) @@ -56,52 +58,63 @@ func (repo *Repository[T1, T2]) Transactions(transactions ...func(*Repository[T1 repo.Transaction.Rollback() return } else { - repo.Transaction.SavePoint("Save Point : " + string(i)) + repo.Transaction.SavePoint("Save Point : " + strconv.Itoa(i)) repo.Transaction.Commit() } } } func WhereGivenConstructor[T1 any, T2 any](repo *Repository[T1, T2]) *gorm.DB { tx := repo.Transaction.Where(&repo.Constructor) - repo.RowsError = repo.Transaction.Error + repo.RowsCount = int(tx.RowsAffected) + repo.NoRecord = repo.RowsCount == 0 repo.RowsError = tx.Error return tx } func Find[T1 any, T2 any](repo *Repository[T1, T2]) *gorm.DB { tx := repo.Transaction.Find(&repo.Result) - repo.RowsError = repo.Transaction.Error + repo.RowsCount = int(tx.RowsAffected) + repo.NoRecord = repo.RowsCount == 0 repo.RowsError = tx.Error return tx } func FinddAllPaginate[T1 any, T2 any](repo *Repository[T1, T2]) *gorm.DB { tx := repo.Transaction.Limit(repo.Pagination.Limit).Offset(repo.Pagination.Offset).Find(&repo.Result) + repo.RowsCount = int(tx.RowsAffected) + repo.NoRecord = repo.RowsCount == 0 repo.RowsError = tx.Error return tx } func Create[T1 any](repo *Repository[T1, T1]) *gorm.DB { tx := repo.Transaction.Create(&repo.Constructor) - repo.Result = repo.Constructor + repo.RowsCount = int(tx.RowsAffected) + repo.NoRecord = repo.RowsCount == 0 repo.RowsError = tx.Error + repo.Result = repo.Constructor return tx } func Update[T1 any](repo *Repository[T1, T1]) *gorm.DB { tx := repo.Transaction.Save(&repo.Constructor) - repo.Result = repo.Constructor + repo.RowsCount = int(tx.RowsAffected) + repo.NoRecord = repo.RowsCount == 0 repo.RowsError = tx.Error return tx } func Delete[T1 any](repo *Repository[T1, T1]) *gorm.DB { tx := repo.Transaction.Delete(&repo.Constructor) + repo.RowsCount = int(tx.RowsAffected) + repo.NoRecord = repo.RowsCount == 0 repo.RowsError = tx.Error return tx } func CustomQuery[T1 any, T2 any](repo *Repository[T1, T2]) *gorm.DB { tx := repo.Transaction.Raw(repo.CustomQuery.SQL, repo.CustomQuery.Values).Scan(&repo.Result) + repo.RowsCount = int(tx.RowsAffected) + repo.NoRecord = repo.RowsCount == 0 repo.RowsError = tx.Error return tx } diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/services/LoginService.go b/space/space/space/space/space/space/space/space/space/space/space/space/services/LoginService.go index 2d734758bcd0ea31246376da2e71187be98d81ca..f1bdc1d654ff34ce16ece776c45919bd3841b38d 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/space/services/LoginService.go +++ b/space/space/space/space/space/space/space/space/space/space/space/space/services/LoginService.go @@ -21,10 +21,9 @@ func (s *AuthenticationService) Authenticate() { accountData := repositories.GetAccountbyEmailPassword(s.Constructor.Email, s.Constructor.Password) if accountData.NoRecord { s.Exception.DataNotFound = true - s.Exception.Message = "there is no account with given email!" + s.Exception.Message = "there is no account with given credentials!" return } - if middleware.VerifyPassword(accountData.Result.Password, s.Constructor.Password) != nil { s.Exception.Unauthorized = true s.Exception.Message = "incorrect password!" diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/services/RegisterService.go b/space/space/space/space/space/space/space/space/space/space/space/space/services/RegisterService.go index 0aca7d7de67105b79deaf19685ec9414df3f5d45..054093a50f74a7c8530f498afacc8ca18de63315 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/space/services/RegisterService.go +++ b/space/space/space/space/space/space/space/space/space/space/space/space/services/RegisterService.go @@ -2,7 +2,6 @@ package services import ( "errors" - "fmt" "go-dp.abdanhafidz.com/middleware" "go-dp.abdanhafidz.com/models" @@ -18,9 +17,6 @@ func (s *RegisterService) Create() { hashed_password, _ := middleware.HashPassword(s.Constructor.Password) s.Constructor.Password = hashed_password accountCreated := repositories.CreateAccount(s.Constructor) - - // fmt.Println("Error :", accountCreated.RowsError.Error()) - fmt.Println(errors.Is(accountCreated.RowsError, gorm.ErrDuplicatedKey)) if errors.Is(accountCreated.RowsError, gorm.ErrDuplicatedKey) { s.Exception.DataDuplicate = true s.Exception.Message = "Account with email " + s.Constructor.Email + " already exists!" diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/LoginController.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/LoginController.go index 78d94dcb74fe945b45f4491f85d9b3784a6947a1..8102aa46984caff0eda1d09563bdb9544c377821 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/LoginController.go +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/LoginController.go @@ -8,7 +8,7 @@ import ( func LoginController(c *gin.Context) { authentication := services.AuthenticationService{} - loginController := Controller[models.LoginRequest, services.LoginConstructor, models.Account]{ + loginController := Controller[models.LoginRequest, services.LoginConstructor, models.AuthenticatedUser]{ Service: &authentication.Service, } loginController.RequestJSON(c) diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/controller.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/controller.go index aba28ae8d9d64b8bb62cda08de9658163b038602..45298c05462bb0b95532d7c35bde0e27fe76d500 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/controller.go +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/controller.go @@ -41,6 +41,8 @@ func (controller *Controller[T1, T2, T3]) Response(c *gin.Context) { Message: "Internal Server Error", }) utils.LogError(controller.Service.Error) + case controller.Service.Exception.DataDuplicate: + utils.ResponseFAIL(c, 400, controller.Service.Exception) case controller.Service.Exception.Unauthorized: utils.ResponseFAIL(c, 401, controller.Service.Exception) case controller.Service.Exception.DataNotFound: diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/middleware/AuthMiddleware.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/middleware/AuthMiddleware.go index 6dc04aa18ba75ef21252e87baf23c0158cc3daea..c153979ac01f108193541d23f821c288b4752f01 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/space/space/middleware/AuthMiddleware.go +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/middleware/AuthMiddleware.go @@ -25,7 +25,7 @@ func GenerateToken(user *models.Account) (string, error) { // Set claims claims := token.Claims.(jwt.MapClaims) - claims["id"] = user.IDAccount + claims["id"] = user.Id claims["exp"] = time.Now().Add(time.Hour * 24).Unix() // Token expires in 24 hours // Sign the token with the secret key diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/models/DatabaseModel.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/models/DatabaseModel.go index f048c17a9838438555d6fd795e6c7617c2c1b729..355323a487ca5c8fc593a1bdea525f30081b884d 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/space/space/models/DatabaseModel.go +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/models/DatabaseModel.go @@ -5,14 +5,11 @@ import ( ) type Account struct { - IDAccount uint `gorm:"primaryKey" json:"id_account"` - Name string `json:"name"` - Username string `json:"username"` - Email string `json:"email"` - Password string `json:"password"` - PhoneNumber int `json:"phone_number"` - CreatedAt time.Time `json:"created_at"` - DeletedAt time.Time `json:"deleted_at"` + Id uint `gorm:"primaryKey" json:"id"` + Email string `gorm:"primaryKey" json:"email"` + Password string `json:"password"` + CreatedAt time.Time `json:"created_at"` + DeletedAt time.Time `json:"deleted_at"` } type AccountDetails struct { diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/models/ResponseModel.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/models/ResponseModel.go index 9ce401186cb92d50737155815c1f511164b86407..cd7b65b6a4ffd53ca1cc3c430abf0b256d0a4936 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/space/space/models/ResponseModel.go +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/models/ResponseModel.go @@ -1 +1,6 @@ package models + +type AuthenticatedUser struct { + Account Account `json:"account"` + Token string `json:"token"` +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/repositories/AccountRepository.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/repositories/AccountRepository.go index 79e43f0707a436c344e5cddd53187d428274bde9..9243b47af1fc1184b20eae52a1300010764f5bab 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/space/space/repositories/AccountRepository.go +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/repositories/AccountRepository.go @@ -1,24 +1,22 @@ package repositories import ( - "fmt" - "go-dp.abdanhafidz.com/models" ) -func GetAccountbyEmailPassword(username string, password string) Repository[models.Account, models.Account] { +func GetAccountbyEmailPassword(email string, password string) Repository[models.Account, models.Account] { repo := Construct[models.Account, models.Account]( - models.Account{Username: username, Password: password}, + models.Account{Email: email, Password: password}, ) Find(repo) return *repo } func CreateAccount(account models.Account) Repository[models.Account, models.Account] { - fmt.Println(account) repo := Construct[models.Account, models.Account]( account, ) + Create(repo) return *repo } diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/repositories/repositories.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/repositories/repositories.go index c82f550472331b9b8d0f5d4cdcf5aab441e5cdfa..767e3d93bb01248012ed0a89768db740be5157c9 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/space/space/repositories/repositories.go +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/repositories/repositories.go @@ -1,8 +1,6 @@ package repositories import ( - "fmt" - "go-dp.abdanhafidz.com/config" "gorm.io/gorm" ) @@ -39,7 +37,6 @@ type Repository[TConstructor any, TResult any] struct { } func Construct[TConstructor any, TResult any](constructor ...TConstructor) *Repository[TConstructor, TResult] { - fmt.Println("Len = ", len(constructor)) if len(constructor) == 1 { return &Repository[TConstructor, TResult]{ Constructor: constructor[0], @@ -51,11 +48,10 @@ func Construct[TConstructor any, TResult any](constructor ...TConstructor) *Repo Transaction: config.DB.Begin(), } } -func (repo *Repository[T1, T2]) Transactions(transactions ...func(*Repository[T1, T2])) { +func (repo *Repository[T1, T2]) Transactions(transactions ...func(*Repository[T1, T2]) *gorm.DB) { i := 1 for _, tx := range transactions { - tx(repo) - repo.RowsError = repo.Transaction.Error + repo.Transaction = tx(repo) if repo.RowsError != nil { repo.Transaction.Rollback() return @@ -65,32 +61,47 @@ func (repo *Repository[T1, T2]) Transactions(transactions ...func(*Repository[T1 } } } -func WhereGivenConstructor[T1 any, T2 any](repo *Repository[T1, T2]) { - repo.Transaction.Where(&repo.Constructor) +func WhereGivenConstructor[T1 any, T2 any](repo *Repository[T1, T2]) *gorm.DB { + tx := repo.Transaction.Where(&repo.Constructor) + repo.RowsError = repo.Transaction.Error + repo.RowsError = tx.Error + return tx } -func Find[T1 any, T2 any](repo *Repository[T1, T2]) { - repo.Transaction.Find(&repo.Result) +func Find[T1 any, T2 any](repo *Repository[T1, T2]) *gorm.DB { + tx := repo.Transaction.Find(&repo.Result) + repo.RowsError = repo.Transaction.Error + repo.RowsError = tx.Error + return tx } -func FinddAllPaginate[T1 any, T2 any](repo *Repository[T1, T2]) { - repo.Transaction.Limit(repo.Pagination.Limit).Offset(repo.Pagination.Offset).Find(&repo.Result) +func FinddAllPaginate[T1 any, T2 any](repo *Repository[T1, T2]) *gorm.DB { + tx := repo.Transaction.Limit(repo.Pagination.Limit).Offset(repo.Pagination.Offset).Find(&repo.Result) + repo.RowsError = tx.Error + return tx } -func Create[T1 any](repo *Repository[T1, T1]) { - fmt.Println(repo.Constructor) - repo.Transaction.Create(&repo.Constructor) +func Create[T1 any](repo *Repository[T1, T1]) *gorm.DB { + tx := repo.Transaction.Create(&repo.Constructor) repo.Result = repo.Constructor + repo.RowsError = tx.Error + return tx } -func Update[T1 any](repo *Repository[T1, T1]) { - repo.Transaction.Save(&repo.Constructor) +func Update[T1 any](repo *Repository[T1, T1]) *gorm.DB { + tx := repo.Transaction.Save(&repo.Constructor) repo.Result = repo.Constructor + repo.RowsError = tx.Error + return tx } -func Delete[T1 any](repo *Repository[T1, T1]) { - repo.Transaction.Delete(&repo.Constructor) +func Delete[T1 any](repo *Repository[T1, T1]) *gorm.DB { + tx := repo.Transaction.Delete(&repo.Constructor) + repo.RowsError = tx.Error + return tx } -func CustomQuery[T1 any, T2 any](repo *Repository[T1, T2]) { - repo.Transaction.Raw(repo.CustomQuery.SQL, repo.CustomQuery.Values).Scan(&repo.Result) +func CustomQuery[T1 any, T2 any](repo *Repository[T1, T2]) *gorm.DB { + tx := repo.Transaction.Raw(repo.CustomQuery.SQL, repo.CustomQuery.Values).Scan(&repo.Result) + repo.RowsError = tx.Error + return tx } diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/services/LoginService.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/services/LoginService.go index e798298cb58036f0b00cc04a919500d6b9c83c09..2d734758bcd0ea31246376da2e71187be98d81ca 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/space/space/services/LoginService.go +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/services/LoginService.go @@ -1,6 +1,8 @@ package services import ( + "errors" + "go-dp.abdanhafidz.com/middleware" "go-dp.abdanhafidz.com/models" "go-dp.abdanhafidz.com/repositories" @@ -12,7 +14,7 @@ type LoginConstructor struct { } type AuthenticationService struct { - Service[LoginConstructor, models.Account] + Service[LoginConstructor, models.AuthenticatedUser] } func (s *AuthenticationService) Authenticate() { @@ -29,7 +31,17 @@ func (s *AuthenticationService) Authenticate() { return } - s.Result = accountData.Result + token, err_tok := middleware.GenerateToken(&accountData.Result) + + if err_tok != nil { + s.Error = errors.Join(s.Error, err_tok) + } + + accountData.Result.Password = "SECRET" + s.Result = models.AuthenticatedUser{ + Account: accountData.Result, + Token: token, + } s.Error = accountData.RowsError } diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/services/RegisterService.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/services/RegisterService.go index 615a3a595a056802ecd6e130c8f0f04be2643d05..0aca7d7de67105b79deaf19685ec9414df3f5d45 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/space/space/services/RegisterService.go +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/services/RegisterService.go @@ -2,6 +2,7 @@ package services import ( "errors" + "fmt" "go-dp.abdanhafidz.com/middleware" "go-dp.abdanhafidz.com/models" @@ -17,15 +18,19 @@ func (s *RegisterService) Create() { hashed_password, _ := middleware.HashPassword(s.Constructor.Password) s.Constructor.Password = hashed_password accountCreated := repositories.CreateAccount(s.Constructor) + + // fmt.Println("Error :", accountCreated.RowsError.Error()) + fmt.Println(errors.Is(accountCreated.RowsError, gorm.ErrDuplicatedKey)) if errors.Is(accountCreated.RowsError, gorm.ErrDuplicatedKey) { s.Exception.DataDuplicate = true - s.Exception.Message = "There is account registered with given data!" + s.Exception.Message = "Account with email " + s.Constructor.Email + " already exists!" return } else if errors.Is(accountCreated.RowsError, gorm.ErrModelAccessibleFieldsRequired) || errors.Is(accountCreated.RowsError, gorm.ErrInvalidData) || errors.Is(accountCreated.RowsError, gorm.ErrInvalidValue) || errors.Is(accountCreated.RowsError, gorm.ErrInvalidField) { s.Exception.BadRequest = true s.Exception.Message = "Bad request!" return } + s.Result = accountCreated.Result s.Result.Password = "SECRET" } diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/Dockerfile b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/Dockerfile index 7d4a695141f39c703c8e6188f6a2b6eba686d624..6183c94745bf456bad5ea308482bd6c3602ad543 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/Dockerfile +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/Dockerfile @@ -21,8 +21,8 @@ RUN echo "DB_HOST=aws-0-ap-southeast-1.pooler.supabase.com" >> .env && \ echo "DB_NAME=postgres" >> .env && \ echo "HOST_ADDRESS = 0.0.0.0" >> .env && \ echo "HOST_PORT = 7860" >> .env && \ - echo "SALT=NZNZtY7dNPz8l0dWINJZLKafWaJrql1s" >> .env - echo "LOG_PATH = logs" + echo "SALT=NZNZtY7dNPz8l0dWINJZLKafWaJrql1s" >> .env && \ + echo "LOG_PATH = logs" >> .env # Build aplikasi RUN go build -o main . diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/HomeController.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/HomeController.go index bb3aae6e57284e4a46908560193aa2a32228d7e2..06cfc3d7fac0385d6aef847a186de2eae7dbb4bb 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/HomeController.go +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/HomeController.go @@ -4,6 +4,6 @@ import "github.com/gin-gonic/gin" func HomeController(c *gin.Context) { c.JSON(200, gin.H{ - "message": "Api Quzuu Backend 2024!", + "message": "Api Qobiltu 2025!", }) } diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/router/router.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/router/router.go index a225f4fa148e7c8b5551686ace69dcd3c1b17562..27ce310f782a252cab580d08873505f02ec38a03 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/router/router.go +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/router/router.go @@ -8,8 +8,9 @@ import ( func StartService() { router := gin.Default() - routerGroup := router.Group("/api") + routerGroup := router.Group("/api/v1") { + routerGroup.GET("/", controller.HomeController) routerGroup.POST("/login", controller.LoginController) routerGroup.POST("/register", controller.RegisterController) } diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/.env.example b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/.env.example new file mode 100644 index 0000000000000000000000000000000000000000..7a13c9d5665f68dc7087f208910958eb620cc5c6 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/.env.example @@ -0,0 +1,9 @@ +DB_HOST = +DB_USER = +DB_PASSWORD = +DB_PORT = +DB_NAME = +SALT = +HOST_ADDRESS = +HOST_PORT = +LOG_PATH = logs \ No newline at end of file diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/.github/workflows/main.yml b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/.github/workflows/main.yml new file mode 100644 index 0000000000000000000000000000000000000000..39a67571ac3a2e6a6e2a972b9550becd551bdded --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/.github/workflows/main.yml @@ -0,0 +1,52 @@ +name: Deploy to Development via Huggingface + +on: + push: + branches: + - main + +jobs: + deploy-to-huggingface: + runs-on: ubuntu-latest + + steps: + # Checkout repository + - name: Checkout Repository + uses: actions/checkout@v3 + + # Setup Git + - name: Setup Git for Huggingface + run: | + git config --global user.email "abdan.hafidz@gmail.com" + git config --global user.name "abdanhafidz" + + # Clone Huggingface Space Repository + - name: Clone Huggingface Space + env: + HF_TOKEN: ${{ secrets.HF_TOKEN }} + run: | + git clone https://huggingface.co/spaces/lifedebugger/api-qobiltu-dev space + + # Update Git Remote URL and Pull Latest Changes + - name: Update Remote and Pull Changes + env: + HF_TOKEN: ${{ secrets.HF_TOKEN }} + run: | + cd space + git remote set-url origin https://lifedebugger:$HF_TOKEN@huggingface.co/spaces/lifedebugger/api-qobiltu-dev + git pull origin main || echo "No changes to pull" + + # Copy Files to Huggingface Space + - name: Copy Files to Space + run: | + rsync -av --exclude='.git' ./ space/ + + # Commit and Push to Huggingface Space + - name: Commit and Push to Huggingface + env: + HF_TOKEN: ${{ secrets.HF_TOKEN }} + run: | + cd space + git add . + git commit -m "Deploy files from GitHub repository" || echo "No changes to commit" + git push origin main || echo "No changes to push" diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/.gitignore b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..deeb9479f4cb982ccd42fa0d7210b9777509e4ae --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/.gitignore @@ -0,0 +1,3 @@ +.env +vendor/ +quzuu-be.exe \ No newline at end of file diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/Dockerfile b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..7d4a695141f39c703c8e6188f6a2b6eba686d624 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/Dockerfile @@ -0,0 +1,30 @@ +# Gunakan image dasar Golang versi 1.21.6 +FROM golang:1.21.6 + +# Set working directory +WORKDIR /app + +# Copy go.mod dan go.sum +COPY go.mod go.sum ./ + +# Download dependencies +RUN go mod download + +# Copy seluruh kode +COPY . . + +# Buat file .env dengan variabel environment yang dibutuhkan +RUN echo "DB_HOST=aws-0-ap-southeast-1.pooler.supabase.com" >> .env && \ + echo "DB_USER=postgres.rdscploxoikqsevhduii" >> .env && \ + echo "DB_PASSWORD=Qobiltu12233334444" >> .env && \ + echo "DB_PORT=5432" >> .env && \ + echo "DB_NAME=postgres" >> .env && \ + echo "HOST_ADDRESS = 0.0.0.0" >> .env && \ + echo "HOST_PORT = 7860" >> .env && \ + echo "SALT=NZNZtY7dNPz8l0dWINJZLKafWaJrql1s" >> .env + echo "LOG_PATH = logs" +# Build aplikasi +RUN go build -o main . + +# Jalankan aplikasi +CMD ["./main"] diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/config/DatabaseConfig.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/config/DatabaseConfig.go new file mode 100644 index 0000000000000000000000000000000000000000..7dfa116ec7152e6338c764ce5853ad21782bd26f --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/config/DatabaseConfig.go @@ -0,0 +1,60 @@ +package config + +import ( + "fmt" + "log" + "os" + + "gorm.io/driver/postgres" + "gorm.io/gorm" + "gorm.io/gorm/logger" + + "github.com/joho/godotenv" + "go-dp.abdanhafidz.com/models" +) + +func AutoMigrateAll(db *gorm.DB) { + // Enable logger to see SQL logs + db.Logger.LogMode(logger.Info) + + // Auto-migrate all models + err := db.AutoMigrate( + &models.Account{}, + &models.AccountDetails{}, + ) + if err != nil { + log.Fatal(err) + } + + fmt.Println("Migration completed successfully.") +} + +var DB *gorm.DB +var err error +var Salt string + +func init() { + godotenv.Load() + if err != nil { + fmt.Println("Gagal membaca file .env") + return + } + os.Setenv("TZ", "Asia/Jakarta") + dbHost := os.Getenv("DB_HOST") + dbPort := os.Getenv("DB_PORT") + dbUser := os.Getenv("DB_USER") + dbPassword := os.Getenv("DB_PASSWORD") + dbName := os.Getenv("DB_NAME") + Salt := os.Getenv("SALT") + dsn := "host=" + dbHost + " user=" + dbUser + " password=" + dbPassword + " dbname=" + dbName + " port=" + dbPort + " sslmode=disable TimeZone=Asia/Jakarta" + DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{TranslateError: true}) + if err != nil { + panic(err) + } + if Salt == "" { + Salt = "D3f4u|t" + } + + // Call AutoMigrateAll to perform auto-migration + AutoMigrateAll(DB) +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/config/EnvConfig.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/config/EnvConfig.go new file mode 100644 index 0000000000000000000000000000000000000000..ae4a686c65b851a6e8d960b47a77e4c67470c615 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/config/EnvConfig.go @@ -0,0 +1,16 @@ +package config + +import "os" + +var TCP_ADDRESS string +var LOG_PATH string +var HOST_ADDRESS string +var HOST_PORT string + +func init() { + HOST_ADDRESS = os.Getenv("HOST_ADDRESS") + HOST_PORT = os.Getenv("HOST_PORT") + TCP_ADDRESS = HOST_ADDRESS + ":" + HOST_PORT + LOG_PATH = os.Getenv("LOG_PATH") + // Menampilkan nilai variabel lingkungan +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/HomeController.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/HomeController.go new file mode 100644 index 0000000000000000000000000000000000000000..bb3aae6e57284e4a46908560193aa2a32228d7e2 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/HomeController.go @@ -0,0 +1,9 @@ +package controller + +import "github.com/gin-gonic/gin" + +func HomeController(c *gin.Context) { + c.JSON(200, gin.H{ + "message": "Api Quzuu Backend 2024!", + }) +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/LoginController.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/LoginController.go new file mode 100644 index 0000000000000000000000000000000000000000..78d94dcb74fe945b45f4491f85d9b3784a6947a1 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/LoginController.go @@ -0,0 +1,19 @@ +package controller + +import ( + "github.com/gin-gonic/gin" + "go-dp.abdanhafidz.com/models" + "go-dp.abdanhafidz.com/services" +) + +func LoginController(c *gin.Context) { + authentication := services.AuthenticationService{} + loginController := Controller[models.LoginRequest, services.LoginConstructor, models.Account]{ + Service: &authentication.Service, + } + loginController.RequestJSON(c) + loginController.Service.Constructor.Email = loginController.Request.Email + loginController.Service.Constructor.Password = loginController.Request.Password + authentication.Authenticate() + loginController.Response(c) +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/RegisterController.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/RegisterController.go new file mode 100644 index 0000000000000000000000000000000000000000..25244306baf7b5ff373c0e1c925c584740710eee --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/RegisterController.go @@ -0,0 +1,19 @@ +package controller + +import ( + "github.com/gin-gonic/gin" + "go-dp.abdanhafidz.com/models" + "go-dp.abdanhafidz.com/services" +) + +func RegisterController(c *gin.Context) { + register := services.RegisterService{} + registerController := Controller[models.RegisterRequest, models.Account, models.Account]{ + Service: ®ister.Service, + } + registerController.RequestJSON(c) + registerController.Service.Constructor.Password = registerController.Request.Password + registerController.Service.Constructor.Email = registerController.Request.Email + register.Create() + registerController.Response(c) +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/controller.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/controller.go new file mode 100644 index 0000000000000000000000000000000000000000..aba28ae8d9d64b8bb62cda08de9658163b038602 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/controller/controller.go @@ -0,0 +1,53 @@ +package controller + +import ( + "github.com/gin-gonic/gin" + "go-dp.abdanhafidz.com/models" + "go-dp.abdanhafidz.com/services" + "go-dp.abdanhafidz.com/utils" +) + +type ( + Controllers interface { + RequestJSON(c *gin.Context) + Response(c *gin.Context) + } + Controller[T1 any, T2 any, T3 any] struct { + AccountData models.AccountData + Request T1 + Service *services.Service[T2, T3] + } +) + +func (controller *Controller[T1, T2, T3]) RequestJSON(c *gin.Context) { + cParam, _ := c.Get("accountData") + if cParam != nil { + controller.AccountData = cParam.(models.AccountData) + } + errBinding := c.ShouldBindJSON(&controller.Request) + if errBinding != nil { + utils.ResponseFAIL(c, 400, models.Exception{ + BadRequest: true, + Message: "Invalid Request Type", + }) + return + } +} +func (controller *Controller[T1, T2, T3]) Response(c *gin.Context) { + switch { + case controller.Service.Error != nil: + utils.ResponseFAIL(c, 500, models.Exception{ + InternalServerError: true, + Message: "Internal Server Error", + }) + utils.LogError(controller.Service.Error) + case controller.Service.Exception.Unauthorized: + utils.ResponseFAIL(c, 401, controller.Service.Exception) + case controller.Service.Exception.DataNotFound: + utils.ResponseFAIL(c, 404, controller.Service.Exception) + case controller.Service.Exception.Message != "": + utils.ResponseFAIL(c, 400, controller.Service.Exception) + default: + utils.ResponseOK(c, controller.Service.Result) + } +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/go.mod b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/go.mod new file mode 100644 index 0000000000000000000000000000000000000000..efe450181fc1f3927c0b6658778233ce43ac1d76 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/go.mod @@ -0,0 +1,48 @@ +module go-dp.abdanhafidz.com + +go 1.21.0 + +require ( + github.com/dgrijalva/jwt-go v3.2.0+incompatible + github.com/gin-gonic/gin v1.9.1 + github.com/joho/godotenv v1.5.1 + golang.org/x/crypto v0.32.0 + gorm.io/driver/postgres v1.5.4 + gorm.io/gorm v1.25.5 +) + +require ( + github.com/bytedance/sonic v1.10.2 // indirect + github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect + github.com/chenzhuoyu/iasm v0.9.0 // indirect + github.com/gabriel-vasile/mimetype v1.4.8 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.25.0 // indirect + github.com/goccy/go-json v0.10.2 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect + github.com/jackc/pgx/v5 v5.5.0 // indirect + github.com/jackc/puddle/v2 v2.2.1 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.2.6 // indirect + github.com/kr/text v0.2.0 // indirect + github.com/leodido/go-urn v1.4.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.1.0 // indirect + github.com/rogpeppe/go-internal v1.11.0 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.11 // indirect + golang.org/x/arch v0.6.0 // indirect + golang.org/x/net v0.34.0 // indirect + golang.org/x/sync v0.10.0 // indirect + golang.org/x/sys v0.29.0 // indirect + golang.org/x/text v0.21.0 // indirect + google.golang.org/protobuf v1.31.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/go.sum b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/go.sum new file mode 100644 index 0000000000000000000000000000000000000000..79ffaec719b95b12a37e3a787b1576ef5a53db03 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/go.sum @@ -0,0 +1,138 @@ +github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= +github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM= +github.com/bytedance/sonic v1.10.2 h1:GQebETVBxYB7JGWJtLBi07OVzWwt+8dWA00gEVW2ZFE= +github.com/bytedance/sonic v1.10.2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4= +github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= +github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= +github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0= +github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA= +github.com/chenzhuoyu/iasm v0.9.0 h1:9fhXjVzq5hUy2gkhhgHl95zG2cEAhw9OSGs8toWWAwo= +github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= +github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= +github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM= +github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= +github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.16.0 h1:x+plE831WK4vaKHO/jpgUGsvLKIqRRkz6M78GuJAfGE= +github.com/go-playground/validator/v10 v10.16.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= +github.com/go-playground/validator/v10 v10.25.0 h1:5Dh7cjvzR7BRZadnsVOzPhWsrwUr0nmsZJxEAnFLNO8= +github.com/go-playground/validator/v10 v10.25.0/go.mod h1:GGzBIJMuE98Ic/kJsBXbz1x/7cByt++cQ+YOuDM5wus= +github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= +github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.5.0 h1:NxstgwndsTRy7eq9/kqYc/BZh5w2hHJV86wjvO+1xPw= +github.com/jackc/pgx/v5 v5.5.0/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA= +github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= +github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc= +github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= +github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= +github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= +github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= +github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= +github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/arch v0.6.0 h1:S0JTfE48HbRj80+4tbvZDYsJ3tGv6BUU3XxyZ7CirAc= +golang.org/x/arch v0.6.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= +golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA= +golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g= +golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc= +golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc= +golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg= +golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ= +golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0= +golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k= +golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= +golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= +golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= +golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= +golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/postgres v1.5.4 h1:Iyrp9Meh3GmbSuyIAGyjkN+n9K+GHX9b9MqsTL4EJCo= +gorm.io/driver/postgres v1.5.4/go.mod h1:Bgo89+h0CRcdA33Y6frlaHHVuTdOf87pmyzwW9C/BH0= +gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls= +gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= +nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= +rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/logs/error_log.txt b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/logs/error_log.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/logs/security_log.txt b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/logs/security_log.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/main.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/main.go new file mode 100644 index 0000000000000000000000000000000000000000..d89532891b559d7d8361e79a1090e77a31cafb95 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/main.go @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" + + "go-dp.abdanhafidz.com/config" + "go-dp.abdanhafidz.com/router" +) + +func main() { + fmt.Println("Server started on ", config.TCP_ADDRESS, ", port :", config.HOST_PORT) + router.StartService() + +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/middleware/AuthMiddleware.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/middleware/AuthMiddleware.go new file mode 100644 index 0000000000000000000000000000000000000000..6dc04aa18ba75ef21252e87baf23c0158cc3daea --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/middleware/AuthMiddleware.go @@ -0,0 +1,103 @@ +// auth/auth.go + +package middleware + +import ( + "errors" + "time" + + "github.com/dgrijalva/jwt-go" + "github.com/gin-gonic/gin" + "go-dp.abdanhafidz.com/config" + "go-dp.abdanhafidz.com/models" + "golang.org/x/crypto/bcrypt" +) + +// Define a secret key for signing the JWT token +var salt = config.Salt +var secretKey = []byte(salt) + +// GenerateToken generates a JWT token for the given user +func GenerateToken(user *models.Account) (string, error) { + + // Create a new token + token := jwt.New(jwt.SigningMethodHS256) + + // Set claims + claims := token.Claims.(jwt.MapClaims) + claims["id"] = user.IDAccount + claims["exp"] = time.Now().Add(time.Hour * 24).Unix() // Token expires in 24 hours + + // Sign the token with the secret key + tokenString, err := token.SignedString(secretKey) + if err != nil { + return "", err + } + + return tokenString, nil +} + +// VerifyPassword verifies if the provided password matches the hashed password +func VerifyPassword(hashedPassword, password string) error { + err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password)) + if err != nil { + return errors.New("invalid password") + } + return nil +} +func HashPassword(password string) (string, error) { + bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14) + return string(bytes), err +} + +type CustomClaims struct { + jwt.StandardClaims + IDUser int `json:"id"` +} + +func VerifyToken(bearer_token string) (int, string, error) { + // fmt.Println(bearer_token) + token, err := jwt.ParseWithClaims(bearer_token, &CustomClaims{}, func(token *jwt.Token) (interface{}, error) { + return secretKey, nil + }) + if err != nil { + return 0, "invalid-token", err + } + claims, ok := token.Claims.(*CustomClaims) + if !ok || !token.Valid { + return 0, "invalid-token", err + } else if claims.StandardClaims.ExpiresAt != 0 && claims.ExpiresAt < time.Now().Unix() { + return 0, "expired", err + } else if !ok && token.Valid { + return 0, "invalid-token", err + } + + return claims.IDUser, "valid", err +} + +func AuthUser(c *gin.Context) { + var currAccData models.AccountData + if c.Request.Header["Auth-Bearer-Token"] != nil { + token := c.Request.Header["Auth-Bearer-Token"] + currAccData.IdUser, currAccData.VerifyStatus, currAccData.ErrVerif = VerifyToken(token[0]) + // fmt.Println("Verify Status :", currAccData.verifyStatus) + if currAccData.VerifyStatus == "invalid-token" || currAccData.VerifyStatus == "expired" { + currAccData.IdUser = 0 + message := "Your session is expired, Please re-Login!" + SendJSON401(c, &currAccData.VerifyStatus, &message) + c.Abort() + return + } + } else { + currAccData.IdUser = 0 + currAccData.VerifyStatus = "no-token" + currAccData.ErrVerif = nil + message := "You have to Login First!" + SendJSON401(c, &currAccData.VerifyStatus, &message) + c.Abort() + return + } + + c.Set("accountData", currAccData) + c.Next() +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/middleware/middleware.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/middleware/middleware.go new file mode 100644 index 0000000000000000000000000000000000000000..5b140903a637aa1d80f02382cb8dbb821c5c01bf --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/middleware/middleware.go @@ -0,0 +1,31 @@ +package middleware + +import ( + "math" + "time" + + "gorm.io/gorm" +) + +func RecordCheck(rows *gorm.DB) (string, error) { + count := rows.RowsAffected + err := rows.Error + // fmt.Println(rows) + // fmt.Println(count) + if count == 0 { + return "no-record", err + } else if err != nil { + return "query-error", err + } else { + return "ok", err + } +} + +func DiffTime(t1 time.Time, t2 time.Time) (int, int, int) { + hs := t1.Sub(t2).Hours() + hs, mf := math.Modf(hs) + ms := mf * 60 + ms, sf := math.Modf(ms) + ss := sf * 60 + return int(hs), int(ms), int(ss) +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/middleware/responseMiddleware.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/middleware/responseMiddleware.go new file mode 100644 index 0000000000000000000000000000000000000000..55f3385077839115eadb8dace294229f4b4c3df2 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/middleware/responseMiddleware.go @@ -0,0 +1,39 @@ +package middleware + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +// SendJSON200 sends a JSON response with HTTP status code 200 +func SendJSON200(c *gin.Context, data interface{}) { + c.JSON(http.StatusOK, gin.H{"status": "success", "data": data}) +} + +// SendJSON400 sends a JSON response with HTTP status code 400 +func SendJSON400(c *gin.Context, error_status *string, message *string) { + c.JSON(http.StatusBadRequest, gin.H{"status": "error", "error-status": error_status, "message": message}) +} + +// SendJSON401 sends a JSON response with HTTP status code 401 +func SendJSON401(c *gin.Context, error_status *string, message *string) { + c.JSON(http.StatusUnauthorized, gin.H{"status": "error", "error-status": error_status, "message": message}) +} + +// SendJSON403 sends a JSON response with HTTP status code 403 +func SendJSON403(c *gin.Context, message *string) { + c.JSON(http.StatusForbidden, gin.H{"status": "error", "message": message}) +} + +// SendJSON404 sends a JSON response with HTTP status code 404 +func SendJSON404(c *gin.Context, message *string) { + c.JSON(http.StatusNotFound, gin.H{"status": "error", "message": message}) +} + +// SendJSON500 sends a JSON response with HTTP status code 500 +func SendJSON500(c *gin.Context, error_status *string, message *string) { + c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "error-status": error_status, "message": message}) +} + +// JSONResponseMiddleware is a middleware that provides functions for sending JSON responses diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/models/AuthModel.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/models/AuthModel.go new file mode 100644 index 0000000000000000000000000000000000000000..5f7c52925aee7c529ef737ae0feb88ee3cb3dbc7 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/models/AuthModel.go @@ -0,0 +1,7 @@ +package models + +type AccountData struct { + IdUser int + VerifyStatus string + ErrVerif error +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/models/DatabaseModel.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/models/DatabaseModel.go new file mode 100644 index 0000000000000000000000000000000000000000..f048c17a9838438555d6fd795e6c7617c2c1b729 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/models/DatabaseModel.go @@ -0,0 +1,30 @@ +package models + +import ( + "time" +) + +type Account struct { + IDAccount uint `gorm:"primaryKey" json:"id_account"` + Name string `json:"name"` + Username string `json:"username"` + Email string `json:"email"` + Password string `json:"password"` + PhoneNumber int `json:"phone_number"` + CreatedAt time.Time `json:"created_at"` + DeletedAt time.Time `json:"deleted_at"` +} + +type AccountDetails struct { + IDDetail uint `gorm:"primaryKey" json:"id_detail"` + IDAccount uint `json:"id_account"` + Province string `json:"province"` + City string `json:"city"` + Institution string `json:"institution"` + UpdatedAt time.Time `json:"updated_at"` + DeletedAt time.Time `json:"deleted_at"` +} + +// Gorm table name settings +func (Account) TableName() string { return "account" } +func (AccountDetails) TableName() string { return "account_details" } diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/models/ExceptionModel.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/models/ExceptionModel.go new file mode 100644 index 0000000000000000000000000000000000000000..b4e76203056e9c2ee6ea5b447b6d3b90842f1d88 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/models/ExceptionModel.go @@ -0,0 +1,11 @@ +package models + +type Exception struct { + Unauthorized bool `json:"unauthorized,omitempty"` + BadRequest bool `json:"bad_request,omitempty"` + DataNotFound bool `json:"data_not_found,omitempty"` + InternalServerError bool `json:"internal_server_error,omitempty"` + DataDuplicate bool `json:"data_duplicate,omitempty"` + QueryError bool `json:"query_error,omitempty"` + Message string `json:"message,omitempty"` +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/models/RequestModel.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/models/RequestModel.go new file mode 100644 index 0000000000000000000000000000000000000000..cfae888ec34edbdd3f5b6588050e2b382c63792d --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/models/RequestModel.go @@ -0,0 +1,13 @@ +package models + +type LoginRequest struct { + Email string `json:"email" binding:"required"` + Password string `json:"password" binding:"required"` +} + +type RegisterRequest struct { + Name string `json:"name"` + Email string `json:"email" binding:"required,email"` + Phone int `json:"phone"` + Password string `json:"password" binding:"required"` +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/models/ResponseModel.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/models/ResponseModel.go new file mode 100644 index 0000000000000000000000000000000000000000..9ce401186cb92d50737155815c1f511164b86407 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/models/ResponseModel.go @@ -0,0 +1 @@ +package models diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/repositories/AccountRepository.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/repositories/AccountRepository.go new file mode 100644 index 0000000000000000000000000000000000000000..79e43f0707a436c344e5cddd53187d428274bde9 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/repositories/AccountRepository.go @@ -0,0 +1,24 @@ +package repositories + +import ( + "fmt" + + "go-dp.abdanhafidz.com/models" +) + +func GetAccountbyEmailPassword(username string, password string) Repository[models.Account, models.Account] { + repo := Construct[models.Account, models.Account]( + models.Account{Username: username, Password: password}, + ) + Find(repo) + return *repo +} + +func CreateAccount(account models.Account) Repository[models.Account, models.Account] { + fmt.Println(account) + repo := Construct[models.Account, models.Account]( + account, + ) + Create(repo) + return *repo +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/repositories/DatabaseScoope.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/repositories/DatabaseScoope.go new file mode 100644 index 0000000000000000000000000000000000000000..e11987b79a890dc6e84923fe7c241c6e8fa99c35 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/repositories/DatabaseScoope.go @@ -0,0 +1,5 @@ +package repositories + +import "go-dp.abdanhafidz.com/config" + +var db = config.DB diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/repositories/repositories.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/repositories/repositories.go new file mode 100644 index 0000000000000000000000000000000000000000..c82f550472331b9b8d0f5d4cdcf5aab441e5cdfa --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/repositories/repositories.go @@ -0,0 +1,96 @@ +package repositories + +import ( + "fmt" + + "go-dp.abdanhafidz.com/config" + "gorm.io/gorm" +) + +type Repositories interface { + FindAllPaginate() + Where() + Find() + Create() + Update() + CustomQuery() + Delete() +} +type PaginationConstructor struct { + Limit int + Offset int + Filter string +} + +type CustomQueryConstructor struct { + SQL string + Values interface{} +} + +type Repository[TConstructor any, TResult any] struct { + Constructor TConstructor + Pagination PaginationConstructor + CustomQuery CustomQueryConstructor + Result TResult + Transaction *gorm.DB + RowsCount int + NoRecord bool + RowsError error +} + +func Construct[TConstructor any, TResult any](constructor ...TConstructor) *Repository[TConstructor, TResult] { + fmt.Println("Len = ", len(constructor)) + if len(constructor) == 1 { + return &Repository[TConstructor, TResult]{ + Constructor: constructor[0], + Transaction: config.DB, + } + } + return &Repository[TConstructor, TResult]{ + Constructor: constructor[0], + Transaction: config.DB.Begin(), + } +} +func (repo *Repository[T1, T2]) Transactions(transactions ...func(*Repository[T1, T2])) { + i := 1 + for _, tx := range transactions { + tx(repo) + repo.RowsError = repo.Transaction.Error + if repo.RowsError != nil { + repo.Transaction.Rollback() + return + } else { + repo.Transaction.SavePoint("Save Point : " + string(i)) + repo.Transaction.Commit() + } + } +} +func WhereGivenConstructor[T1 any, T2 any](repo *Repository[T1, T2]) { + repo.Transaction.Where(&repo.Constructor) +} +func Find[T1 any, T2 any](repo *Repository[T1, T2]) { + repo.Transaction.Find(&repo.Result) +} + +func FinddAllPaginate[T1 any, T2 any](repo *Repository[T1, T2]) { + repo.Transaction.Limit(repo.Pagination.Limit).Offset(repo.Pagination.Offset).Find(&repo.Result) +} + +func Create[T1 any](repo *Repository[T1, T1]) { + fmt.Println(repo.Constructor) + repo.Transaction.Create(&repo.Constructor) + repo.Result = repo.Constructor +} + +func Update[T1 any](repo *Repository[T1, T1]) { + repo.Transaction.Save(&repo.Constructor) + repo.Result = repo.Constructor +} + +func Delete[T1 any](repo *Repository[T1, T1]) { + repo.Transaction.Delete(&repo.Constructor) +} + +func CustomQuery[T1 any, T2 any](repo *Repository[T1, T2]) { + repo.Transaction.Raw(repo.CustomQuery.SQL, repo.CustomQuery.Values).Scan(&repo.Result) +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/router/router.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/router/router.go new file mode 100644 index 0000000000000000000000000000000000000000..a225f4fa148e7c8b5551686ace69dcd3c1b17562 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/router/router.go @@ -0,0 +1,17 @@ +package router + +import ( + "github.com/gin-gonic/gin" + "go-dp.abdanhafidz.com/config" + "go-dp.abdanhafidz.com/controller" +) + +func StartService() { + router := gin.Default() + routerGroup := router.Group("/api") + { + routerGroup.POST("/login", controller.LoginController) + routerGroup.POST("/register", controller.RegisterController) + } + router.Run(config.TCP_ADDRESS) +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/services/LoginService.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/services/LoginService.go new file mode 100644 index 0000000000000000000000000000000000000000..e798298cb58036f0b00cc04a919500d6b9c83c09 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/services/LoginService.go @@ -0,0 +1,36 @@ +package services + +import ( + "go-dp.abdanhafidz.com/middleware" + "go-dp.abdanhafidz.com/models" + "go-dp.abdanhafidz.com/repositories" +) + +type LoginConstructor struct { + Email string + Password string +} + +type AuthenticationService struct { + Service[LoginConstructor, models.Account] +} + +func (s *AuthenticationService) Authenticate() { + accountData := repositories.GetAccountbyEmailPassword(s.Constructor.Email, s.Constructor.Password) + if accountData.NoRecord { + s.Exception.DataNotFound = true + s.Exception.Message = "there is no account with given email!" + return + } + + if middleware.VerifyPassword(accountData.Result.Password, s.Constructor.Password) != nil { + s.Exception.Unauthorized = true + s.Exception.Message = "incorrect password!" + return + } + + s.Result = accountData.Result + s.Error = accountData.RowsError +} + +// LoginHandler handles user login diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/services/RegisterService.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/services/RegisterService.go new file mode 100644 index 0000000000000000000000000000000000000000..615a3a595a056802ecd6e130c8f0f04be2643d05 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/services/RegisterService.go @@ -0,0 +1,31 @@ +package services + +import ( + "errors" + + "go-dp.abdanhafidz.com/middleware" + "go-dp.abdanhafidz.com/models" + "go-dp.abdanhafidz.com/repositories" + "gorm.io/gorm" +) + +type RegisterService struct { + Service[models.Account, models.Account] +} + +func (s *RegisterService) Create() { + hashed_password, _ := middleware.HashPassword(s.Constructor.Password) + s.Constructor.Password = hashed_password + accountCreated := repositories.CreateAccount(s.Constructor) + if errors.Is(accountCreated.RowsError, gorm.ErrDuplicatedKey) { + s.Exception.DataDuplicate = true + s.Exception.Message = "There is account registered with given data!" + return + } else if errors.Is(accountCreated.RowsError, gorm.ErrModelAccessibleFieldsRequired) || errors.Is(accountCreated.RowsError, gorm.ErrInvalidData) || errors.Is(accountCreated.RowsError, gorm.ErrInvalidValue) || errors.Is(accountCreated.RowsError, gorm.ErrInvalidField) { + s.Exception.BadRequest = true + s.Exception.Message = "Bad request!" + return + } + s.Result = accountCreated.Result + s.Result.Password = "SECRET" +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/services/services.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/services/services.go new file mode 100644 index 0000000000000000000000000000000000000000..e684786fdd3dc071dd085b87f27a0e7bb8becfff --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/services/services.go @@ -0,0 +1,31 @@ +package services + +import "go-dp.abdanhafidz.com/models" + +type ( + Services interface { + Retrieve() + Update() + Create() + Delete() + Validate() + Authenticate() + Authorize() + } + Service[TConstructor any, TResult any] struct { + Constructor TConstructor + Result TResult + Exception models.Exception + Error error + } +) + +func Construct[TConstructor any, TResult any](constructor ...TConstructor) *Service[TConstructor, TResult] { + if len(constructor) == 0 { + return &Service[TConstructor, TResult]{} + } + + return &Service[TConstructor, TResult]{ + Constructor: constructor[0], + } +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/.gitattributes b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/.gitattributes new file mode 100644 index 0000000000000000000000000000000000000000..a6344aac8c09253b3b630fb776ae94478aa0275b --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/.gitattributes @@ -0,0 +1,35 @@ +*.7z filter=lfs diff=lfs merge=lfs -text +*.arrow filter=lfs diff=lfs merge=lfs -text +*.bin filter=lfs diff=lfs merge=lfs -text +*.bz2 filter=lfs diff=lfs merge=lfs -text +*.ckpt filter=lfs diff=lfs merge=lfs -text +*.ftz filter=lfs diff=lfs merge=lfs -text +*.gz filter=lfs diff=lfs merge=lfs -text +*.h5 filter=lfs diff=lfs merge=lfs -text +*.joblib filter=lfs diff=lfs merge=lfs -text +*.lfs.* filter=lfs diff=lfs merge=lfs -text +*.mlmodel filter=lfs diff=lfs merge=lfs -text +*.model filter=lfs diff=lfs merge=lfs -text +*.msgpack filter=lfs diff=lfs merge=lfs -text +*.npy filter=lfs diff=lfs merge=lfs -text +*.npz filter=lfs diff=lfs merge=lfs -text +*.onnx filter=lfs diff=lfs merge=lfs -text +*.ot filter=lfs diff=lfs merge=lfs -text +*.parquet filter=lfs diff=lfs merge=lfs -text +*.pb filter=lfs diff=lfs merge=lfs -text +*.pickle filter=lfs diff=lfs merge=lfs -text +*.pkl filter=lfs diff=lfs merge=lfs -text +*.pt filter=lfs diff=lfs merge=lfs -text +*.pth filter=lfs diff=lfs merge=lfs -text +*.rar filter=lfs diff=lfs merge=lfs -text +*.safetensors filter=lfs diff=lfs merge=lfs -text +saved_model/**/* filter=lfs diff=lfs merge=lfs -text +*.tar.* filter=lfs diff=lfs merge=lfs -text +*.tar filter=lfs diff=lfs merge=lfs -text +*.tflite filter=lfs diff=lfs merge=lfs -text +*.tgz filter=lfs diff=lfs merge=lfs -text +*.wasm filter=lfs diff=lfs merge=lfs -text +*.xz filter=lfs diff=lfs merge=lfs -text +*.zip filter=lfs diff=lfs merge=lfs -text +*.zst filter=lfs diff=lfs merge=lfs -text +*tfevents* filter=lfs diff=lfs merge=lfs -text diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/APIResponse.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/APIResponse.go new file mode 100644 index 0000000000000000000000000000000000000000..7a3e7df219e76ae94bc17ef748c6dfe7f8afeb93 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/APIResponse.go @@ -0,0 +1,39 @@ +package utils + +import ( + "net/http" + "reflect" + + "github.com/gin-gonic/gin" + "go-dp.abdanhafidz.com/models" + "go-dp.abdanhafidz.com/services" +) + +func ResponseOK(c *gin.Context, data any) { + c.JSON(http.StatusOK, gin.H{"message": "Request Success!", "data": data, "status": "success"}) +} + +func ResponseFAIL(c *gin.Context, status int, exception models.Exception) { + message := exception.Message + exception.Message = "" + c.AbortWithStatusJSON(status, gin.H{"status": "error", "message": message, "error": exception}) + return +} + +func SendResponse(c *gin.Context, data services.Service[any, any]) { + if reflect.ValueOf(data.Exception).IsNil() { + ResponseOK(c, data) + } else { + if data.Exception.Unauthorized { + ResponseFAIL(c, 401, data.Exception) + } else if data.Exception.BadRequest { + ResponseFAIL(c, 400, data.Exception) + } else if data.Exception.DataNotFound { + ResponseFAIL(c, 404, data.Exception) + } else if data.Exception.InternalServerError { + ResponseFAIL(c, 500, data.Exception) + } else { + ResponseFAIL(c, 403, data.Exception) + } + } +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/Exception.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/Exception.go new file mode 100644 index 0000000000000000000000000000000000000000..0b273b83d7e7cb140e2b9df6801f161f0ff08be4 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/Exception.go @@ -0,0 +1 @@ +package utils diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/Helper.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/Helper.go new file mode 100644 index 0000000000000000000000000000000000000000..3e36fc57268d71ef58e1511b038f8c56e7f8928c --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/Helper.go @@ -0,0 +1,11 @@ +package utils + +import ( + "github.com/gin-gonic/gin" + "go-dp.abdanhafidz.com/models" +) + +func GetAccount(c *gin.Context) models.AccountData { + cParam, _ := c.Get("accountData") + return cParam.(models.AccountData) +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/Logger.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/Logger.go new file mode 100644 index 0000000000000000000000000000000000000000..98d4a9447588e08458738263844c45eb0d6ae98b --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/Logger.go @@ -0,0 +1,19 @@ +package utils + +import ( + "log" + "os" + + "go-dp.abdanhafidz.com/config" +) + +func LogError(errorLogged error) { + file, err := os.OpenFile(config.LOG_PATH+"error_log.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) + if err != nil { + log.Fatal(err) + } + + log.SetOutput(file) + + log.Println(errorLogged) +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/utils.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/utils.go new file mode 100644 index 0000000000000000000000000000000000000000..480645d434c01f7152d6747171e5e3ea53969533 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/utils.go @@ -0,0 +1,9 @@ +package utils + +func ternaryMessage(condition bool, valueIfTrue string, valueIfFalse string) string { + if condition { + return valueIfTrue + } else { + return valueIfFalse + } +} diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/APIResponse.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/APIResponse.go index 7a3e7df219e76ae94bc17ef748c6dfe7f8afeb93..da8a2be04c3d8ef4a28939d5c40cdb3574efb650 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/APIResponse.go +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/APIResponse.go @@ -11,6 +11,7 @@ import ( func ResponseOK(c *gin.Context, data any) { c.JSON(http.StatusOK, gin.H{"message": "Request Success!", "data": data, "status": "success"}) + return } func ResponseFAIL(c *gin.Context, status int, exception models.Exception) { diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/APIResponse.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/APIResponse.go index da8a2be04c3d8ef4a28939d5c40cdb3574efb650..68eb789f1daf3ed57cfe6ea600c948f2ee0be9cc 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/APIResponse.go +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/APIResponse.go @@ -18,6 +18,7 @@ func ResponseFAIL(c *gin.Context, status int, exception models.Exception) { message := exception.Message exception.Message = "" c.AbortWithStatusJSON(status, gin.H{"status": "error", "message": message, "error": exception}) + c. return } diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/APIResponse.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/APIResponse.go index 68eb789f1daf3ed57cfe6ea600c948f2ee0be9cc..da8a2be04c3d8ef4a28939d5c40cdb3574efb650 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/APIResponse.go +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/APIResponse.go @@ -18,7 +18,6 @@ func ResponseFAIL(c *gin.Context, status int, exception models.Exception) { message := exception.Message exception.Message = "" c.AbortWithStatusJSON(status, gin.H{"status": "error", "message": message, "error": exception}) - c. return } diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/Logger.go b/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/Logger.go index 98d4a9447588e08458738263844c45eb0d6ae98b..6473e869e33fbe10150a80de942cfcb9f46a4749 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/Logger.go +++ b/space/space/space/space/space/space/space/space/space/space/space/space/space/utils/Logger.go @@ -15,5 +15,5 @@ func LogError(errorLogged error) { log.SetOutput(file) - log.Println(errorLogged) + log.Println("Error Log :", errorLogged) } diff --git a/space/space/space/space/space/space/space/space/space/space/space/space/utils/Logger.go b/space/space/space/space/space/space/space/space/space/space/space/space/utils/Logger.go index 6473e869e33fbe10150a80de942cfcb9f46a4749..2bb3f755429b22e6c3d62cdf95ac630010eacae6 100644 --- a/space/space/space/space/space/space/space/space/space/space/space/space/utils/Logger.go +++ b/space/space/space/space/space/space/space/space/space/space/space/space/utils/Logger.go @@ -1,6 +1,7 @@ package utils import ( + "fmt" "log" "os" @@ -8,6 +9,7 @@ import ( ) func LogError(errorLogged error) { + fmt.Println("There is an error!") file, err := os.OpenFile(config.LOG_PATH+"error_log.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) if err != nil { log.Fatal(err) diff --git a/space/space/space/space/space/space/space/space/utils/APIResponse.go b/space/space/space/space/space/space/space/space/utils/APIResponse.go index da8a2be04c3d8ef4a28939d5c40cdb3574efb650..455109a156badaed17549222ed98a6b0d268b98a 100644 --- a/space/space/space/space/space/space/space/space/utils/APIResponse.go +++ b/space/space/space/space/space/space/space/space/utils/APIResponse.go @@ -4,9 +4,9 @@ import ( "net/http" "reflect" + "api.qobiltu.id/models" + "api.qobiltu.id/services" "github.com/gin-gonic/gin" - "go-dp.abdanhafidz.com/models" - "go-dp.abdanhafidz.com/services" ) func ResponseOK(c *gin.Context, data any) { diff --git a/space/space/space/space/space/space/space/space/utils/Helper.go b/space/space/space/space/space/space/space/space/utils/Helper.go index 3e36fc57268d71ef58e1511b038f8c56e7f8928c..4742c7fa5c357435cb9bdfb86e48828670a9792b 100644 --- a/space/space/space/space/space/space/space/space/utils/Helper.go +++ b/space/space/space/space/space/space/space/space/utils/Helper.go @@ -1,8 +1,8 @@ package utils import ( + "api.qobiltu.id/models" "github.com/gin-gonic/gin" - "go-dp.abdanhafidz.com/models" ) func GetAccount(c *gin.Context) models.AccountData { diff --git a/space/space/space/space/space/space/space/space/utils/Logger.go b/space/space/space/space/space/space/space/space/utils/Logger.go index 2bb3f755429b22e6c3d62cdf95ac630010eacae6..d0f8ec5600ab6a524e594198900de51e76b460a8 100644 --- a/space/space/space/space/space/space/space/space/utils/Logger.go +++ b/space/space/space/space/space/space/space/space/utils/Logger.go @@ -5,7 +5,7 @@ import ( "log" "os" - "go-dp.abdanhafidz.com/config" + "api.qobiltu.id/config" ) func LogError(errorLogged error) { diff --git a/space/space/space/space/space/space/space/utils/api_response.go b/space/space/space/space/space/space/space/utils/api_response.go new file mode 100644 index 0000000000000000000000000000000000000000..aae210ee39978d67c5412c665a041f45685f0c13 --- /dev/null +++ b/space/space/space/space/space/space/space/utils/api_response.go @@ -0,0 +1,52 @@ +package utils + +import ( + "net/http" + "reflect" + + "api.qobiltu.id/models" + "api.qobiltu.id/services" + "github.com/gin-gonic/gin" +) + +func ResponseOK(c *gin.Context, data any) { + res := models.SuccessResponse{ + Status: "success", + Message: "Data retrieved successfully!", + Data: data, + MetaData: c.Request.Body, + } + c.JSON(http.StatusOK, res) + return +} + +func ResponseFAIL(c *gin.Context, status int, exception models.Exception) { + message := exception.Message + exception.Message = "" + res := models.ErrorResponse{ + Status: "error", + Message: message, + Errors: exception, + MetaData: c.Request.Body, + } + c.AbortWithStatusJSON(status, res) + return +} + +func SendResponse(c *gin.Context, data services.Service[any, any]) { + if reflect.ValueOf(data.Exception).IsNil() { + ResponseOK(c, data) + } else { + if data.Exception.Unauthorized { + ResponseFAIL(c, 401, data.Exception) + } else if data.Exception.BadRequest { + ResponseFAIL(c, 400, data.Exception) + } else if data.Exception.DataNotFound { + ResponseFAIL(c, 404, data.Exception) + } else if data.Exception.InternalServerError { + ResponseFAIL(c, 500, data.Exception) + } else { + ResponseFAIL(c, 403, data.Exception) + } + } +} diff --git a/space/space/space/space/space/space/space/utils/util.go b/space/space/space/space/space/space/space/utils/util.go new file mode 100644 index 0000000000000000000000000000000000000000..480645d434c01f7152d6747171e5e3ea53969533 --- /dev/null +++ b/space/space/space/space/space/space/space/utils/util.go @@ -0,0 +1,9 @@ +package utils + +func ternaryMessage(condition bool, valueIfTrue string, valueIfFalse string) string { + if condition { + return valueIfTrue + } else { + return valueIfFalse + } +}