From 36540c11a83bfa27d78f7417f2b5dd09972342ee Mon Sep 17 00:00:00 2001 From: nikhilnarayanan623 Date: Sun, 30 Jul 2023 13:55:37 +0530 Subject: [PATCH] backup commit --- .github/workflows/main.yml | 0 .gitignore | 0 Dockerfile | 0 cmd/api/main.go | 8 +++++--- docker-compose.yml | 0 ecommerce-kubernet.yaml | 0 go.mod | 0 go.sum | 0 makefile | 3 +++ pkg/api/handler/admin.go | 0 pkg/api/handler/auth.go | 5 ++++- pkg/api/handler/auth_google.go | 10 ++++++---- pkg/api/handler/auth_test.go | 9 +++++++-- pkg/api/handler/cart.go | 0 pkg/api/handler/coupon.go | 0 pkg/api/handler/interfaces/admin.go | 0 pkg/api/handler/interfaces/auth.go | 0 pkg/api/handler/interfaces/cart.go | 0 pkg/api/handler/interfaces/coupon.go | 0 pkg/api/handler/interfaces/order.go | 0 pkg/api/handler/interfaces/payment.go | 0 pkg/api/handler/interfaces/product.go | 0 pkg/api/handler/interfaces/user.go | 0 pkg/api/handler/main_test.go | 0 pkg/api/handler/message.go | 0 pkg/api/handler/offer.go | 0 pkg/api/handler/order.go | 0 pkg/api/handler/payment.go | 0 pkg/api/handler/payment_razorpay.go | 0 pkg/api/handler/payment_stripe.go | 0 pkg/api/handler/product.go | 0 pkg/api/handler/request/auth.go | 0 pkg/api/handler/request/common.go | 0 pkg/api/handler/request/coupon.go | 0 pkg/api/handler/request/offer.go | 0 pkg/api/handler/request/order.go | 0 pkg/api/handler/request/pagination.go | 0 pkg/api/handler/request/payment.go | 0 pkg/api/handler/request/product.go | 0 pkg/api/handler/request/request_params.go | 0 pkg/api/handler/request/user.go | 0 pkg/api/handler/response/admin.go | 0 pkg/api/handler/response/auth.go | 0 pkg/api/handler/response/coupons.go | 0 pkg/api/handler/response/order.go | 0 pkg/api/handler/response/product.go | 0 pkg/api/handler/response/response.go | 0 pkg/api/handler/response/user.go | 0 pkg/api/handler/user.go | 5 +---- pkg/api/handler/wallet.go | 0 pkg/api/middleware/auth.go | 8 ++++---- pkg/api/middleware/auth_test.go | 0 pkg/api/middleware/main_test.go | 0 pkg/api/middleware/trime_spaces.go | 6 +++--- pkg/api/routes/admin.go | 0 pkg/api/routes/user.go | 0 pkg/api/server.go | 4 ++-- pkg/config/config.go | 11 ++++++++--- pkg/db/connection.go | 9 +++++++-- pkg/db/trigger.go | 20 ++++++++++---------- pkg/di/wire.go | 2 +- pkg/di/wire_gen.go | 2 +- pkg/domain/admin.go | 0 pkg/domain/auth.go | 0 pkg/domain/coupons.go | 0 pkg/domain/order.go | 0 pkg/domain/product.go | 0 pkg/domain/user.go | 3 ++- pkg/mock/mockrepo/auth_mock.go | 0 pkg/mock/mockrepo/user_mock.go | 0 pkg/mock/mockservice/token_mock.go | 0 pkg/mock/mockusecase/auth_mock.go | 0 pkg/repository/admin.go | 0 pkg/repository/auth.go | 0 pkg/repository/auth_test.go | 0 pkg/repository/cart.go | 0 pkg/repository/coupon.go | 0 pkg/repository/interfaces/admin.go | 0 pkg/repository/interfaces/auth.go | 0 pkg/repository/interfaces/cart.go | 0 pkg/repository/interfaces/coupon.go | 0 pkg/repository/interfaces/order.go | 0 pkg/repository/interfaces/payment.go | 0 pkg/repository/interfaces/product.go | 0 pkg/repository/interfaces/user.go | 0 pkg/repository/offer.go | 0 pkg/repository/order.go | 0 pkg/repository/payment.go | 0 pkg/repository/product.go | 0 pkg/repository/user.go | 7 ++++--- pkg/repository/user_test.go | 0 pkg/repository/wallet.go | 0 pkg/service/otp/otp.go | 2 +- pkg/service/otp/twilio.go | 0 pkg/service/token/jwt.go | 0 pkg/service/token/jwt_test.go | 0 pkg/service/token/token.go | 0 pkg/usecase/admin.go | 0 pkg/usecase/auth.go | 3 +++ pkg/usecase/auth_test.go | 0 pkg/usecase/cart.go | 0 pkg/usecase/coupon.go | 1 + pkg/usecase/errors.go | 0 pkg/usecase/interfaces/admin.go | 0 pkg/usecase/interfaces/auth.go | 0 pkg/usecase/interfaces/cart.go | 0 pkg/usecase/interfaces/coupon.go | 0 pkg/usecase/interfaces/order.go | 0 pkg/usecase/interfaces/payment.go | 0 pkg/usecase/interfaces/product.go | 0 pkg/usecase/interfaces/user.go | 0 pkg/usecase/offer.go | 0 pkg/usecase/order.go | 0 pkg/usecase/payment.go | 0 pkg/usecase/product.go | 0 pkg/usecase/user.go | 0 pkg/usecase/wallet.go | 0 pkg/utils/compare.go | 0 pkg/utils/error.go | 0 pkg/utils/helper_functions.go | 5 +++-- pkg/utils/hsash.go | 0 pkg/utils/log.go | 0 pkg/utils/unique.go | 0 postgres-kubernet.yaml | 0 readme.md | 0 views/goauth.html | 0 views/paymentForm.html | 0 127 files changed, 76 insertions(+), 47 deletions(-) mode change 100755 => 100644 .github/workflows/main.yml mode change 100755 => 100644 .gitignore mode change 100755 => 100644 Dockerfile mode change 100755 => 100644 cmd/api/main.go mode change 100755 => 100644 docker-compose.yml mode change 100755 => 100644 ecommerce-kubernet.yaml mode change 100755 => 100644 go.mod mode change 100755 => 100644 go.sum mode change 100755 => 100644 makefile mode change 100755 => 100644 pkg/api/handler/admin.go mode change 100755 => 100644 pkg/api/handler/auth.go mode change 100755 => 100644 pkg/api/handler/auth_google.go mode change 100755 => 100644 pkg/api/handler/auth_test.go mode change 100755 => 100644 pkg/api/handler/cart.go mode change 100755 => 100644 pkg/api/handler/coupon.go mode change 100755 => 100644 pkg/api/handler/interfaces/admin.go mode change 100755 => 100644 pkg/api/handler/interfaces/auth.go mode change 100755 => 100644 pkg/api/handler/interfaces/cart.go mode change 100755 => 100644 pkg/api/handler/interfaces/coupon.go mode change 100755 => 100644 pkg/api/handler/interfaces/order.go mode change 100755 => 100644 pkg/api/handler/interfaces/payment.go mode change 100755 => 100644 pkg/api/handler/interfaces/product.go mode change 100755 => 100644 pkg/api/handler/interfaces/user.go mode change 100755 => 100644 pkg/api/handler/main_test.go mode change 100755 => 100644 pkg/api/handler/message.go mode change 100755 => 100644 pkg/api/handler/offer.go mode change 100755 => 100644 pkg/api/handler/order.go mode change 100755 => 100644 pkg/api/handler/payment.go mode change 100755 => 100644 pkg/api/handler/payment_razorpay.go mode change 100755 => 100644 pkg/api/handler/payment_stripe.go mode change 100755 => 100644 pkg/api/handler/product.go mode change 100755 => 100644 pkg/api/handler/request/auth.go mode change 100755 => 100644 pkg/api/handler/request/common.go mode change 100755 => 100644 pkg/api/handler/request/coupon.go mode change 100755 => 100644 pkg/api/handler/request/offer.go mode change 100755 => 100644 pkg/api/handler/request/order.go mode change 100755 => 100644 pkg/api/handler/request/pagination.go mode change 100755 => 100644 pkg/api/handler/request/payment.go mode change 100755 => 100644 pkg/api/handler/request/product.go mode change 100755 => 100644 pkg/api/handler/request/request_params.go mode change 100755 => 100644 pkg/api/handler/request/user.go mode change 100755 => 100644 pkg/api/handler/response/admin.go mode change 100755 => 100644 pkg/api/handler/response/auth.go mode change 100755 => 100644 pkg/api/handler/response/coupons.go mode change 100755 => 100644 pkg/api/handler/response/order.go mode change 100755 => 100644 pkg/api/handler/response/product.go mode change 100755 => 100644 pkg/api/handler/response/response.go mode change 100755 => 100644 pkg/api/handler/response/user.go mode change 100755 => 100644 pkg/api/handler/user.go mode change 100755 => 100644 pkg/api/handler/wallet.go mode change 100755 => 100644 pkg/api/middleware/auth.go mode change 100755 => 100644 pkg/api/middleware/auth_test.go mode change 100755 => 100644 pkg/api/middleware/main_test.go mode change 100755 => 100644 pkg/api/routes/admin.go mode change 100755 => 100644 pkg/api/routes/user.go mode change 100755 => 100644 pkg/api/server.go mode change 100755 => 100644 pkg/config/config.go mode change 100755 => 100644 pkg/db/connection.go mode change 100755 => 100644 pkg/db/trigger.go mode change 100755 => 100644 pkg/di/wire.go mode change 100755 => 100644 pkg/di/wire_gen.go mode change 100755 => 100644 pkg/domain/admin.go mode change 100755 => 100644 pkg/domain/auth.go mode change 100755 => 100644 pkg/domain/coupons.go mode change 100755 => 100644 pkg/domain/order.go mode change 100755 => 100644 pkg/domain/product.go mode change 100755 => 100644 pkg/domain/user.go mode change 100755 => 100644 pkg/mock/mockrepo/auth_mock.go mode change 100755 => 100644 pkg/mock/mockrepo/user_mock.go mode change 100755 => 100644 pkg/mock/mockservice/token_mock.go mode change 100755 => 100644 pkg/mock/mockusecase/auth_mock.go mode change 100755 => 100644 pkg/repository/admin.go mode change 100755 => 100644 pkg/repository/auth.go mode change 100755 => 100644 pkg/repository/auth_test.go mode change 100755 => 100644 pkg/repository/cart.go mode change 100755 => 100644 pkg/repository/coupon.go mode change 100755 => 100644 pkg/repository/interfaces/admin.go mode change 100755 => 100644 pkg/repository/interfaces/auth.go mode change 100755 => 100644 pkg/repository/interfaces/cart.go mode change 100755 => 100644 pkg/repository/interfaces/coupon.go mode change 100755 => 100644 pkg/repository/interfaces/order.go mode change 100755 => 100644 pkg/repository/interfaces/payment.go mode change 100755 => 100644 pkg/repository/interfaces/product.go mode change 100755 => 100644 pkg/repository/interfaces/user.go mode change 100755 => 100644 pkg/repository/offer.go mode change 100755 => 100644 pkg/repository/order.go mode change 100755 => 100644 pkg/repository/payment.go mode change 100755 => 100644 pkg/repository/product.go mode change 100755 => 100644 pkg/repository/user.go mode change 100755 => 100644 pkg/repository/user_test.go mode change 100755 => 100644 pkg/repository/wallet.go mode change 100755 => 100644 pkg/service/otp/otp.go mode change 100755 => 100644 pkg/service/otp/twilio.go mode change 100755 => 100644 pkg/service/token/jwt.go mode change 100755 => 100644 pkg/service/token/jwt_test.go mode change 100755 => 100644 pkg/service/token/token.go mode change 100755 => 100644 pkg/usecase/admin.go mode change 100755 => 100644 pkg/usecase/auth.go mode change 100755 => 100644 pkg/usecase/auth_test.go mode change 100755 => 100644 pkg/usecase/cart.go mode change 100755 => 100644 pkg/usecase/coupon.go mode change 100755 => 100644 pkg/usecase/errors.go mode change 100755 => 100644 pkg/usecase/interfaces/admin.go mode change 100755 => 100644 pkg/usecase/interfaces/auth.go mode change 100755 => 100644 pkg/usecase/interfaces/cart.go mode change 100755 => 100644 pkg/usecase/interfaces/coupon.go mode change 100755 => 100644 pkg/usecase/interfaces/order.go mode change 100755 => 100644 pkg/usecase/interfaces/payment.go mode change 100755 => 100644 pkg/usecase/interfaces/product.go mode change 100755 => 100644 pkg/usecase/interfaces/user.go mode change 100755 => 100644 pkg/usecase/offer.go mode change 100755 => 100644 pkg/usecase/order.go mode change 100755 => 100644 pkg/usecase/payment.go mode change 100755 => 100644 pkg/usecase/product.go mode change 100755 => 100644 pkg/usecase/user.go mode change 100755 => 100644 pkg/usecase/wallet.go mode change 100755 => 100644 pkg/utils/compare.go mode change 100755 => 100644 pkg/utils/error.go mode change 100755 => 100644 pkg/utils/helper_functions.go mode change 100755 => 100644 pkg/utils/hsash.go mode change 100755 => 100644 pkg/utils/log.go mode change 100755 => 100644 pkg/utils/unique.go mode change 100755 => 100644 postgres-kubernet.yaml mode change 100755 => 100644 readme.md mode change 100755 => 100644 views/goauth.html mode change 100755 => 100644 views/paymentForm.html diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml old mode 100755 new mode 100644 diff --git a/.gitignore b/.gitignore old mode 100755 new mode 100644 diff --git a/Dockerfile b/Dockerfile old mode 100755 new mode 100644 diff --git a/cmd/api/main.go b/cmd/api/main.go old mode 100755 new mode 100644 index 534f375..060c7bb --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -12,13 +12,15 @@ func main() { cfg, err := config.LoadConfig() if err != nil { - log.Fatal("Error to load the config") + log.Fatal("Error to load the config: ", err) } server, err := di.InitializeApi(cfg) if err != nil { - log.Fatal("Failed to initialize the api") + log.Fatal("Failed to initialize the api: ", err) } - server.Start() + if server.Start(); err != nil { + log.Fatal("failed to start server: ", err) + } } diff --git a/docker-compose.yml b/docker-compose.yml old mode 100755 new mode 100644 diff --git a/ecommerce-kubernet.yaml b/ecommerce-kubernet.yaml old mode 100755 new mode 100644 diff --git a/go.mod b/go.mod old mode 100755 new mode 100644 diff --git a/go.sum b/go.sum old mode 100755 new mode 100644 diff --git a/makefile b/makefile old mode 100755 new mode 100644 index 9f96957..ed7f26d --- a/makefile +++ b/makefile @@ -46,6 +46,9 @@ swagger: ## install swagger and its dependencies for generate swagger using swag swag: ## Generate swagger docs swag init -g pkg/api/server.go -o ./cmd/api/docs +check: ## To check the code standard violations and errors + golangci-lint run + mockgen: # Generate mock files for the test mockgen -source=pkg/repository/interfaces/auth.go -destination=pkg/mock/mockrepo/auth_mock.go -package=mockrepo mockgen -source=pkg/repository/interfaces/user.go -destination=pkg/mock/mockrepo/user_mock.go -package=mockrepo diff --git a/pkg/api/handler/admin.go b/pkg/api/handler/admin.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/auth.go b/pkg/api/handler/auth.go old mode 100755 new mode 100644 index d04ebb2..698d1ca --- a/pkg/api/handler/auth.go +++ b/pkg/api/handler/auth.go @@ -189,7 +189,10 @@ func (c *AuthHandler) UserSignUp(ctx *gin.Context) { } var user domain.User - copier.Copy(&user, body) + if err := copier.Copy(&user, body); err != nil { + response.ErrorResponse(ctx, http.StatusInternalServerError, "failed to copy details", err, nil) + return + } err := c.authUseCase.UserSignUp(ctx, user) diff --git a/pkg/api/handler/auth_google.go b/pkg/api/handler/auth_google.go old mode 100755 new mode 100644 index 167944a..8900584 --- a/pkg/api/handler/auth_google.go +++ b/pkg/api/handler/auth_google.go @@ -4,7 +4,6 @@ import ( "net/http" "github.com/gin-gonic/gin" - "github.com/jinzhu/copier" "github.com/markbates/goth" "github.com/markbates/goth/gothic" "github.com/markbates/goth/providers/google" @@ -68,9 +67,12 @@ func (c *AuthHandler) UserGoogleAuthCallBack(ctx *gin.Context) { return } - var user domain.User - - copier.Copy(&user, &googleUser) + user := domain.User{ + FirstName: googleUser.FirstName, + LastName: googleUser.LastName, + Email: googleUser.Email, + GoogleImage: googleUser.AvatarURL, + } userID, err := c.authUseCase.GoogleLogin(ctx, user) if err != nil { diff --git a/pkg/api/handler/auth_test.go b/pkg/api/handler/auth_test.go old mode 100755 new mode 100644 index 871e435..32e5db0 --- a/pkg/api/handler/auth_test.go +++ b/pkg/api/handler/auth_test.go @@ -206,7 +206,8 @@ func TestUserRenewRefreshToken(t *testing.T) { assert.NoError(t, err) var tokenResponse response.TokenResponse - json.Unmarshal(jsonData, &tokenResponse) + err = json.Unmarshal(jsonData, &tokenResponse) + assert.NoError(t, err) assert.Equal(t, "generated_access_token", tokenResponse.AccessToken) }, @@ -245,7 +246,11 @@ func TestUserRenewRefreshToken(t *testing.T) { } func getResponseStructFromResponseBody(responseBody *bytes.Buffer) (responseStruct response.Response, err error) { + data, err := io.ReadAll(responseBody) - json.Unmarshal(data, &responseStruct) + if err := json.Unmarshal(data, &responseStruct); err != nil { + return response.Response{}, nil + } + return } diff --git a/pkg/api/handler/cart.go b/pkg/api/handler/cart.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/coupon.go b/pkg/api/handler/coupon.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/interfaces/admin.go b/pkg/api/handler/interfaces/admin.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/interfaces/auth.go b/pkg/api/handler/interfaces/auth.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/interfaces/cart.go b/pkg/api/handler/interfaces/cart.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/interfaces/coupon.go b/pkg/api/handler/interfaces/coupon.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/interfaces/order.go b/pkg/api/handler/interfaces/order.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/interfaces/payment.go b/pkg/api/handler/interfaces/payment.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/interfaces/product.go b/pkg/api/handler/interfaces/product.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/interfaces/user.go b/pkg/api/handler/interfaces/user.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/main_test.go b/pkg/api/handler/main_test.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/message.go b/pkg/api/handler/message.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/offer.go b/pkg/api/handler/offer.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/order.go b/pkg/api/handler/order.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/payment.go b/pkg/api/handler/payment.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/payment_razorpay.go b/pkg/api/handler/payment_razorpay.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/payment_stripe.go b/pkg/api/handler/payment_stripe.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/product.go b/pkg/api/handler/product.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/request/auth.go b/pkg/api/handler/request/auth.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/request/common.go b/pkg/api/handler/request/common.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/request/coupon.go b/pkg/api/handler/request/coupon.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/request/offer.go b/pkg/api/handler/request/offer.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/request/order.go b/pkg/api/handler/request/order.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/request/pagination.go b/pkg/api/handler/request/pagination.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/request/payment.go b/pkg/api/handler/request/payment.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/request/product.go b/pkg/api/handler/request/product.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/request/request_params.go b/pkg/api/handler/request/request_params.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/request/user.go b/pkg/api/handler/request/user.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/response/admin.go b/pkg/api/handler/response/admin.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/response/auth.go b/pkg/api/handler/response/auth.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/response/coupons.go b/pkg/api/handler/response/coupons.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/response/order.go b/pkg/api/handler/response/order.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/response/product.go b/pkg/api/handler/response/product.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/response/response.go b/pkg/api/handler/response/response.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/response/user.go b/pkg/api/handler/response/user.go old mode 100755 new mode 100644 diff --git a/pkg/api/handler/user.go b/pkg/api/handler/user.go old mode 100755 new mode 100644 index 916ca9a..de1d346 --- a/pkg/api/handler/user.go +++ b/pkg/api/handler/user.go @@ -91,10 +91,7 @@ func (u *UserHandler) GetProfile(ctx *gin.Context) { return } - var data response.User - copier.Copy(&data, &user) - - response.SuccessResponse(ctx, http.StatusOK, "Successfully retrieved user details", data) + response.SuccessResponse(ctx, http.StatusOK, "Successfully retrieved user details", user) } // UpdateProfile godoc diff --git a/pkg/api/handler/wallet.go b/pkg/api/handler/wallet.go old mode 100755 new mode 100644 diff --git a/pkg/api/middleware/auth.go b/pkg/api/middleware/auth.go old mode 100755 new mode 100644 index ed4d14d..4d24050 --- a/pkg/api/middleware/auth.go +++ b/pkg/api/middleware/auth.go @@ -9,10 +9,10 @@ import ( "github.com/nikhilnarayanan623/ecommerce-gin-clean-arch/pkg/service/token" ) -const ( - authorizationHeaderKey string = "authorization" - authorizationType string = "bearer" -) +// const ( +// authorizationHeaderKey string = "authorization" +// authorizationType string = "bearer" +// ) // Get User Auth middleware func (c *middleware) AuthenticateUser() gin.HandlerFunc { diff --git a/pkg/api/middleware/auth_test.go b/pkg/api/middleware/auth_test.go old mode 100755 new mode 100644 diff --git a/pkg/api/middleware/main_test.go b/pkg/api/middleware/main_test.go old mode 100755 new mode 100644 diff --git a/pkg/api/middleware/trime_spaces.go b/pkg/api/middleware/trime_spaces.go index 9f9fa69..10c3799 100644 --- a/pkg/api/middleware/trime_spaces.go +++ b/pkg/api/middleware/trime_spaces.go @@ -3,7 +3,7 @@ package middleware import ( "bytes" "encoding/json" - "io/ioutil" + "io" "net/http" "strings" @@ -16,7 +16,7 @@ func (c *middleware) TrimSpaces() gin.HandlerFunc { ctx.Request.Body = http.MaxBytesReader(ctx.Writer, ctx.Request.Body, int64(32<<20)) - bodyBytes, err := ioutil.ReadAll(ctx.Request.Body) + bodyBytes, err := io.ReadAll(ctx.Request.Body) if err != nil { ctx.Abort() response.ErrorResponse(ctx, http.StatusBadRequest, "failed to ready request body", err, nil) @@ -25,7 +25,7 @@ func (c *middleware) TrimSpaces() gin.HandlerFunc { bodyBytes = trimSpacesInJson(bodyBytes) - ctx.Request.Body = ioutil.NopCloser(bytes.NewReader(bodyBytes)) + ctx.Request.Body = io.NopCloser(bytes.NewReader(bodyBytes)) } } diff --git a/pkg/api/routes/admin.go b/pkg/api/routes/admin.go old mode 100755 new mode 100644 diff --git a/pkg/api/routes/user.go b/pkg/api/routes/user.go old mode 100755 new mode 100644 diff --git a/pkg/api/server.go b/pkg/api/server.go old mode 100755 new mode 100644 index 3f0cc53..f2f7020 --- a/pkg/api/server.go +++ b/pkg/api/server.go @@ -49,7 +49,7 @@ func NewServerHTTP(authHandler handlerInterface.AuthHandler, middleware middlewa return &ServerHTTP{Engine: engine} } -func (s *ServerHTTP) Start() { +func (s *ServerHTTP) Start() error { - s.Engine.Run(":8000") + return s.Engine.Run(":8000") } diff --git a/pkg/config/config.go b/pkg/config/config.go old mode 100755 new mode 100644 index 38d3fed..25b24ec --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -35,7 +35,7 @@ type Config struct { GoauthCallbackUrl string `mapstructure:"GOAUTH_CALL_BACK_URL"` } -// to hold all names of env variables +// name of envs and used to read from system envs var envsNames = []string{ "ADMIN_EMAIL", "ADMIN_USER_NAME", "ADMIN_PASSWORD", "DB_HOST", "DB_NAME", "DB_USER", "DB_PASSWORD", "DB_PORT", // database @@ -50,11 +50,16 @@ var config Config func LoadConfig() (Config, error) { - // set-up viper + // read from .env file viper.AddConfigPath("./") viper.SetConfigFile(".env") - viper.ReadInConfig() + err := viper.ReadInConfig() + // if there is no error means found .env file and read from it + if err == nil { + return config, nil + } + // if there is error then read envs from system environments for _, env := range envsNames { if err := viper.BindEnv(env); err != nil { return config, err diff --git a/pkg/db/connection.go b/pkg/db/connection.go old mode 100755 new mode 100644 index 29a67cf..2ef5ffe --- a/pkg/db/connection.go +++ b/pkg/db/connection.go @@ -11,7 +11,7 @@ import ( ) // func to connect data base using config(database config) and return address of a new instnce of gorm DB -func ConnectDatbase(cfg config.Config) (*gorm.DB, error) { +func ConnectDatabase(cfg config.Config) (*gorm.DB, error) { dsn := fmt.Sprintf("host=%s user=%s dbname=%s port=%s password=%s", cfg.DBHost, cfg.DBUser, cfg.DBName, cfg.DBPort, cfg.DBPassword) @@ -24,7 +24,7 @@ func ConnectDatbase(cfg config.Config) (*gorm.DB, error) { } // migrate the database tables - db.AutoMigrate( + err = db.AutoMigrate( //auth domain.RefreshSession{}, @@ -74,6 +74,11 @@ func ConnectDatbase(cfg config.Config) (*gorm.DB, error) { domain.Transaction{}, ) + if err != nil { + log.Printf("failed to migrate database models") + return nil, err + } + // setup the triggers if err := SetUpDBTriggers(db); err != nil { log.Printf("failed to setup database triggers") diff --git a/pkg/db/trigger.go b/pkg/db/trigger.go old mode 100755 new mode 100644 index 68ceeda..aaf6803 --- a/pkg/db/trigger.go +++ b/pkg/db/trigger.go @@ -11,43 +11,43 @@ func SetUpDBTriggers(db *gorm.DB) error { // first execute the trigger funtion if db.Exec(cartTotalPriceUpdateSqlFunc).Error != nil { - return errors.New("faild to execute cart_total_Pirce update trigger fun()") + return errors.New("failed to execute cart_total_price update trigger fun()") } // create trigger for calling the update funciton if db.Exec(cartTotalPriceTriggerExec).Error != nil { - return errors.New("faild to create trigger for update total price on cart") + return errors.New("failed to create trigger for update total price on cart") } // update product_item qty on order time if db.Exec(orderProductUpdateOnPlaceOrder).Error != nil { - return errors.New("faild to excute orderProductUpdate() trigger function") + return errors.New("failed to execute orderProductUpdate() trigger function") } if db.Exec(orderProductUpdateOnPlaceOrderTriggerExec).Error != nil { - return errors.New("faild to create orderProductUpdateTriggerExec trigger") + return errors.New("failed to create orderProductUpdateTriggerExec trigger") } //update product_item qty on order returned if db.Exec(orderReturnProductUpdate).Error != nil { - return errors.New("faild to create orderReturnProductUpdate() trigger function") + return errors.New("failed to create orderReturnProductUpdate() trigger function") } if db.Exec(orderStatusFindFunc).Error != nil { - return errors.New("faild to create orderStatusFindFunc function for return order_status") + return errors.New("failed to create orderStatusFindFunc function for return order_status") } if db.Exec(orderReturnProductUpdateExec).Error != nil { - return errors.New("faild to create orderReturnProductUpdateExec trigger") + return errors.New("failed to create orderReturnProductUpdateExec trigger") } - log.Printf("successfully trggers updated for databse") + log.Printf("successfully triggers updated for database") return nil } var ( // function which return total price calculation on cart when product_item added or remove delete cart - // in here cheking first it delete any row from cart_item then take its cart_id an find all cart_items with this id and calculate total price and update it + // in here checking first it delete any row from cart_item then take its cart_id an find all cart_items with this id and calculate total price and update it // cart with this cart_id // any other like update or inset then take the cart_id and calculate the total price update on cart cartTotalPriceUpdateSqlFunc = `CREATE OR REPLACE FUNCTION update_cart_total_price() @@ -81,7 +81,7 @@ var ( AFTER INSERT OR UPDATE OR DELETE ON cart_items FOR EACH ROW EXECUTE FUNCTION update_cart_total_price();` - //for updating product_item quntity when order place + //for updating product_item quantity when order place orderProductUpdateOnPlaceOrder = `CREATE OR REPLACE FUNCTION update_product_quantity() RETURNS TRIGGER AS $$ BEGIN diff --git a/pkg/di/wire.go b/pkg/di/wire.go old mode 100755 new mode 100644 index c5eb9bf..476a1b2 --- a/pkg/di/wire.go +++ b/pkg/di/wire.go @@ -18,7 +18,7 @@ import ( func InitializeApi(cfg config.Config) (*http.ServerHTTP, error) { - wire.Build(db.ConnectDatbase, + wire.Build(db.ConnectDatabase, //external token.NewTokenService, otp.NewOtpAuth, diff --git a/pkg/di/wire_gen.go b/pkg/di/wire_gen.go old mode 100755 new mode 100644 index 9ece3d3..db9625b --- a/pkg/di/wire_gen.go +++ b/pkg/di/wire_gen.go @@ -21,7 +21,7 @@ import ( // Injectors from wire.go: func InitializeApi(cfg config.Config) (*http.ServerHTTP, error) { - gormDB, err := db.ConnectDatbase(cfg) + gormDB, err := db.ConnectDatabase(cfg) if err != nil { return nil, err } diff --git a/pkg/domain/admin.go b/pkg/domain/admin.go old mode 100755 new mode 100644 diff --git a/pkg/domain/auth.go b/pkg/domain/auth.go old mode 100755 new mode 100644 diff --git a/pkg/domain/coupons.go b/pkg/domain/coupons.go old mode 100755 new mode 100644 diff --git a/pkg/domain/order.go b/pkg/domain/order.go old mode 100755 new mode 100644 diff --git a/pkg/domain/product.go b/pkg/domain/product.go old mode 100755 new mode 100644 diff --git a/pkg/domain/user.go b/pkg/domain/user.go old mode 100755 new mode 100644 index f5eca7a..96cb9a2 --- a/pkg/domain/user.go +++ b/pkg/domain/user.go @@ -4,9 +4,10 @@ import "time" type User struct { ID uint `json:"id" gorm:"primaryKey;unique"` + Age uint `json:"age" binding:"required,numeric"` + GoogleImage string `json:"google_profile_image"` FirstName string `json:"first_name" gorm:"not null" binding:"required,min=2,max=50"` LastName string `json:"last_name" gorm:"not null" binding:"required,min=1,max=50"` - Age uint `json:"age" binding:"required,numeric"` UserName string `json:"user_name" gorm:"not null;unique" binding:"required,min=3,max=15"` Email string `json:"email" gorm:"unique;not null" binding:"required,email"` Phone string `json:"phone" gorm:"unique" binding:"required,min=10,max=10"` diff --git a/pkg/mock/mockrepo/auth_mock.go b/pkg/mock/mockrepo/auth_mock.go old mode 100755 new mode 100644 diff --git a/pkg/mock/mockrepo/user_mock.go b/pkg/mock/mockrepo/user_mock.go old mode 100755 new mode 100644 diff --git a/pkg/mock/mockservice/token_mock.go b/pkg/mock/mockservice/token_mock.go old mode 100755 new mode 100644 diff --git a/pkg/mock/mockusecase/auth_mock.go b/pkg/mock/mockusecase/auth_mock.go old mode 100755 new mode 100644 diff --git a/pkg/repository/admin.go b/pkg/repository/admin.go old mode 100755 new mode 100644 diff --git a/pkg/repository/auth.go b/pkg/repository/auth.go old mode 100755 new mode 100644 diff --git a/pkg/repository/auth_test.go b/pkg/repository/auth_test.go old mode 100755 new mode 100644 diff --git a/pkg/repository/cart.go b/pkg/repository/cart.go old mode 100755 new mode 100644 diff --git a/pkg/repository/coupon.go b/pkg/repository/coupon.go old mode 100755 new mode 100644 diff --git a/pkg/repository/interfaces/admin.go b/pkg/repository/interfaces/admin.go old mode 100755 new mode 100644 diff --git a/pkg/repository/interfaces/auth.go b/pkg/repository/interfaces/auth.go old mode 100755 new mode 100644 diff --git a/pkg/repository/interfaces/cart.go b/pkg/repository/interfaces/cart.go old mode 100755 new mode 100644 diff --git a/pkg/repository/interfaces/coupon.go b/pkg/repository/interfaces/coupon.go old mode 100755 new mode 100644 diff --git a/pkg/repository/interfaces/order.go b/pkg/repository/interfaces/order.go old mode 100755 new mode 100644 diff --git a/pkg/repository/interfaces/payment.go b/pkg/repository/interfaces/payment.go old mode 100755 new mode 100644 diff --git a/pkg/repository/interfaces/product.go b/pkg/repository/interfaces/product.go old mode 100755 new mode 100644 diff --git a/pkg/repository/interfaces/user.go b/pkg/repository/interfaces/user.go old mode 100755 new mode 100644 diff --git a/pkg/repository/offer.go b/pkg/repository/offer.go old mode 100755 new mode 100644 diff --git a/pkg/repository/order.go b/pkg/repository/order.go old mode 100755 new mode 100644 diff --git a/pkg/repository/payment.go b/pkg/repository/payment.go old mode 100755 new mode 100644 diff --git a/pkg/repository/product.go b/pkg/repository/product.go old mode 100755 new mode 100644 diff --git a/pkg/repository/user.go b/pkg/repository/user.go old mode 100755 new mode 100644 index 4e85a4b..235b5a9 --- a/pkg/repository/user.go +++ b/pkg/repository/user.go @@ -63,12 +63,13 @@ func (c *userDatabase) FindUserByUserNameEmailOrPhoneNotID(ctx context.Context, func (c *userDatabase) SaveUser(ctx context.Context, user domain.User) (userID uint, err error) { //save the user details - query := `INSERT INTO users (user_name, first_name, last_name, age, email, phone, password,created_at) - VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING id` + query := `INSERT INTO users (user_name, first_name, + last_name, age, email, phone, password, google_image, created_at) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9 ) RETURNING id` createdAt := time.Now() err = c.DB.Raw(query, user.UserName, user.FirstName, user.LastName, - user.Age, user.Email, user.Phone, user.Password, createdAt).Scan(&user).Error + user.Age, user.Email, user.Phone, user.Password, user.GoogleImage, createdAt).Scan(&user).Error return userID, err } diff --git a/pkg/repository/user_test.go b/pkg/repository/user_test.go old mode 100755 new mode 100644 diff --git a/pkg/repository/wallet.go b/pkg/repository/wallet.go old mode 100755 new mode 100644 diff --git a/pkg/service/otp/otp.go b/pkg/service/otp/otp.go old mode 100755 new mode 100644 index 99f29ac..1d5932a --- a/pkg/service/otp/otp.go +++ b/pkg/service/otp/otp.go @@ -2,5 +2,5 @@ package otp type OtpAuth interface { SentOtp(phoneNumber string) (string, error) - VerifyOtp(phoneNumber string, code string) (valid bool,err error) + VerifyOtp(phoneNumber string, code string) (valid bool, err error) } diff --git a/pkg/service/otp/twilio.go b/pkg/service/otp/twilio.go old mode 100755 new mode 100644 diff --git a/pkg/service/token/jwt.go b/pkg/service/token/jwt.go old mode 100755 new mode 100644 diff --git a/pkg/service/token/jwt_test.go b/pkg/service/token/jwt_test.go old mode 100755 new mode 100644 diff --git a/pkg/service/token/token.go b/pkg/service/token/token.go old mode 100755 new mode 100644 diff --git a/pkg/usecase/admin.go b/pkg/usecase/admin.go old mode 100755 new mode 100644 diff --git a/pkg/usecase/auth.go b/pkg/usecase/auth.go old mode 100755 new mode 100644 index d931b19..c06b614 --- a/pkg/usecase/auth.go +++ b/pkg/usecase/auth.go @@ -281,11 +281,14 @@ func (c *authUseCase) GoogleLogin(ctx context.Context, user domain.User) (userID if err != nil { return userID, fmt.Errorf("failed to get user details with given email \nerror:%v", err.Error()) } + if existUser.ID != 0 { return existUser.ID, nil } + // create a random user name for user based on user name user.UserName = utils.GenerateRandomUserName(user.FirstName) + userID, err = c.userRepo.SaveUser(ctx, user) if err != nil { return userID, fmt.Errorf("failed to save user details \nerror:%v", err.Error()) diff --git a/pkg/usecase/auth_test.go b/pkg/usecase/auth_test.go old mode 100755 new mode 100644 diff --git a/pkg/usecase/cart.go b/pkg/usecase/cart.go old mode 100755 new mode 100644 diff --git a/pkg/usecase/coupon.go b/pkg/usecase/coupon.go old mode 100755 new mode 100644 index a12994c..e322c33 --- a/pkg/usecase/coupon.go +++ b/pkg/usecase/coupon.go @@ -177,3 +177,4 @@ func (c *couponUseCase) ApplyCouponToCart(ctx context.Context, userID uint, coup log.Printf("successfully updated the cart price with dicount price %d", discountAmount) return discountAmount, nil } + \ No newline at end of file diff --git a/pkg/usecase/errors.go b/pkg/usecase/errors.go old mode 100755 new mode 100644 diff --git a/pkg/usecase/interfaces/admin.go b/pkg/usecase/interfaces/admin.go old mode 100755 new mode 100644 diff --git a/pkg/usecase/interfaces/auth.go b/pkg/usecase/interfaces/auth.go old mode 100755 new mode 100644 diff --git a/pkg/usecase/interfaces/cart.go b/pkg/usecase/interfaces/cart.go old mode 100755 new mode 100644 diff --git a/pkg/usecase/interfaces/coupon.go b/pkg/usecase/interfaces/coupon.go old mode 100755 new mode 100644 diff --git a/pkg/usecase/interfaces/order.go b/pkg/usecase/interfaces/order.go old mode 100755 new mode 100644 diff --git a/pkg/usecase/interfaces/payment.go b/pkg/usecase/interfaces/payment.go old mode 100755 new mode 100644 diff --git a/pkg/usecase/interfaces/product.go b/pkg/usecase/interfaces/product.go old mode 100755 new mode 100644 diff --git a/pkg/usecase/interfaces/user.go b/pkg/usecase/interfaces/user.go old mode 100755 new mode 100644 diff --git a/pkg/usecase/offer.go b/pkg/usecase/offer.go old mode 100755 new mode 100644 diff --git a/pkg/usecase/order.go b/pkg/usecase/order.go old mode 100755 new mode 100644 diff --git a/pkg/usecase/payment.go b/pkg/usecase/payment.go old mode 100755 new mode 100644 diff --git a/pkg/usecase/product.go b/pkg/usecase/product.go old mode 100755 new mode 100644 diff --git a/pkg/usecase/user.go b/pkg/usecase/user.go old mode 100755 new mode 100644 diff --git a/pkg/usecase/wallet.go b/pkg/usecase/wallet.go old mode 100755 new mode 100644 diff --git a/pkg/utils/compare.go b/pkg/utils/compare.go old mode 100755 new mode 100644 diff --git a/pkg/utils/error.go b/pkg/utils/error.go old mode 100755 new mode 100644 diff --git a/pkg/utils/helper_functions.go b/pkg/utils/helper_functions.go old mode 100755 new mode 100644 index 47fcb5e..da60c5c --- a/pkg/utils/helper_functions.go +++ b/pkg/utils/helper_functions.go @@ -29,10 +29,11 @@ func GenerateRandomUserName(FirstName string) string { suffix := make([]byte, 4) numbers := "1234567890" - rand.Seed(time.Now().UnixMilli()) + seed := time.Now().UnixNano() + rng := rand.New(rand.NewSource(seed)) for i := range suffix { - suffix[i] = numbers[rand.Intn(10)] + suffix[i] = numbers[rng.Intn(10)] } userName := (FirstName + string(suffix)) diff --git a/pkg/utils/hsash.go b/pkg/utils/hsash.go old mode 100755 new mode 100644 diff --git a/pkg/utils/log.go b/pkg/utils/log.go old mode 100755 new mode 100644 diff --git a/pkg/utils/unique.go b/pkg/utils/unique.go old mode 100755 new mode 100644 diff --git a/postgres-kubernet.yaml b/postgres-kubernet.yaml old mode 100755 new mode 100644 diff --git a/readme.md b/readme.md old mode 100755 new mode 100644 diff --git a/views/goauth.html b/views/goauth.html old mode 100755 new mode 100644 diff --git a/views/paymentForm.html b/views/paymentForm.html old mode 100755 new mode 100644