diff --git a/.env.example b/.env.example new file mode 100644 index 0000000000000000000000000000000000000000..7a13c9d5665f68dc7087f208910958eb620cc5c6 --- /dev/null +++ b/.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/.github/workflows/main_huggingface.yml b/.github/workflows/main_huggingface.yml new file mode 100644 index 0000000000000000000000000000000000000000..3e3fb006baef2027a7a34f264a7a79e6a0d191fb --- /dev/null +++ b/.github/workflows/main_huggingface.yml @@ -0,0 +1,52 @@ +name: Deploy to 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/pweb-api-ets 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/pweb-api-ets + 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/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..475c354fc4960d1f5bf6dd9a4818bd0799a4e84e --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.env +vendor/ +quzuu-be.exe +.qodo +.error +logs/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..76f071e49b7df9ec2bdde67ec77a09b1346b94a8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,29 @@ +# Gunakan image dasar Golang versi 1.21.6 +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 . . +# 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.ykxexvvsqhgsonpzmivq" >> .env && \ + echo "DB_PASSWORD=Pweb@558585" >> .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=OkeGASOKEGASTAMBAHDUASORANGG45" >> .env + +# Build aplikasi +RUN go build -o main . + +# Jalankan aplikasi +CMD ["./main"] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..79da4b69760d1658f4dbc42604187316b368d857 --- /dev/null +++ b/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/README.md b/README.md index a3b40241fb96df26883b45d911c9e190d00d4d13..217c7edc441d0d0d5e4f6901059085069ad03dc2 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ ---- -title: Pweb Api Ets -emoji: 🐢 -colorFrom: green -colorTo: green -sdk: docker -pinned: false ---- - -Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference +--- +title: Pweb Api +emoji: 📉 +colorFrom: green +colorTo: pink +sdk: docker +pinned: false +--- + + diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000000000000000000000000000000000000..55487887878907689a66b9e5897895013b1b708f --- /dev/null +++ b/config/config.go @@ -0,0 +1,34 @@ +package config + +import ( + "os" + "strconv" + + "github.com/joho/godotenv" +) + +var TCP_ADDRESS string +var LOG_PATH string + +var HOST_ADDRESS string +var HOST_PORT string +var EMAIL_VERIFICATION_DURATION int + +var SMTP_SENDER_EMAIL string +var SMTP_SENDER_PASSWORD string +var SMTP_HOST string +var SMTP_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") + EMAIL_VERIFICATION_DURATION, _ = strconv.Atoi(os.Getenv("EMAIL_VERIFICATION_DURATION")) + SMTP_SENDER_EMAIL = os.Getenv("SMTP_SENDER_EMAIL") + SMTP_SENDER_PASSWORD = os.Getenv("SMTP_SENDER_PASSWORD") + SMTP_HOST = os.Getenv("SMTP_HOST") + SMTP_PORT = os.Getenv("SMTP_PORT") + // Menampilkan nilai variabel lingkungan +} diff --git a/config/database_connection_config.go b/config/database_connection_config.go new file mode 100644 index 0000000000000000000000000000000000000000..f4057a37141c9975b8b7006cc0167e28db15adc5 --- /dev/null +++ b/config/database_connection_config.go @@ -0,0 +1,61 @@ +package config + +import ( + "fmt" + "log" + "os" + + "gorm.io/driver/postgres" + "gorm.io/gorm" + "gorm.io/gorm/logger" + + "github.com/joho/godotenv" + "pweb-api.abdanhafidz.com/models" +) + +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/controller/auth/auth_change_password_controller.go b/controller/auth/auth_change_password_controller.go new file mode 100644 index 0000000000000000000000000000000000000000..17a0df832612e924e66f022e40341bce940fec1d --- /dev/null +++ b/controller/auth/auth_change_password_controller.go @@ -0,0 +1,21 @@ +package auth + +import ( + "github.com/gin-gonic/gin" + "pweb-api.abdanhafidz.com/controller" + "pweb-api.abdanhafidz.com/models" + "pweb-api.abdanhafidz.com/services" +) + +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..5c240866d0be398ae11c01f3c70d4dc2acf59fb7 --- /dev/null +++ b/controller/auth/auth_login_controller.go @@ -0,0 +1,20 @@ +package auth + +import ( + "github.com/gin-gonic/gin" + "pweb-api.abdanhafidz.com/controller" + "pweb-api.abdanhafidz.com/models" + "pweb-api.abdanhafidz.com/services" +) + +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_register_controller.go b/controller/auth/auth_register_controller.go new file mode 100644 index 0000000000000000000000000000000000000000..11516596b08131ab6781ca9091d4ab5a32885c4b --- /dev/null +++ b/controller/auth/auth_register_controller.go @@ -0,0 +1,20 @@ +package auth + +import ( + "github.com/gin-gonic/gin" + "pweb-api.abdanhafidz.com/controller" + "pweb-api.abdanhafidz.com/models" + "pweb-api.abdanhafidz.com/services" +) + +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/controller.go b/controller/controller.go new file mode 100644 index 0000000000000000000000000000000000000000..127db8926bc937f200064b4c7650706facbd0d2d --- /dev/null +++ b/controller/controller.go @@ -0,0 +1,65 @@ +package controller + +import ( + "github.com/gin-gonic/gin" + "pweb-api.abdanhafidz.com/models" + "pweb-api.abdanhafidz.com/services" + "pweb-api.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]) 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 { + controller.AccountData = cParam.(models.AccountData) + } + errBinding := c.ShouldBindJSON(&controller.Request) + if errBinding != nil { + utils.ResponseFAIL(c, 400, models.Exception{ + BadRequest: true, + 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) { + switch { + case controller.Service.Error != nil: + utils.LogError(controller.Service.Error) + utils.ResponseFAIL(c, 500, models.Exception{ + InternalServerError: true, + Message: "Internal Server 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: + 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/controller/home_controller.go b/controller/home_controller.go new file mode 100644 index 0000000000000000000000000000000000000000..77e770d3520bdb80be8a80c51fa686d4487e266b --- /dev/null +++ b/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": "PWEB API 2025 by Abdan Hafidz!", + }) +} diff --git a/controller/user/user_delete_controller.go b/controller/user/user_delete_controller.go new file mode 100644 index 0000000000000000000000000000000000000000..7c3892f015f5b1432a309fa1d13407905e28290e --- /dev/null +++ b/controller/user/user_delete_controller.go @@ -0,0 +1,22 @@ +package user + +import ( + "strconv" + + "github.com/gin-gonic/gin" + "pweb-api.abdanhafidz.com/controller" + "pweb-api.abdanhafidz.com/models" + "pweb-api.abdanhafidz.com/services" +) + +func Delete(c *gin.Context) { + userList := services.UserListService{} + userListController := controller.Controller[any, models.Account, []models.UserProfileResponse]{ + Service: &userList.Service, + } + userListController.HeaderParse(c, func() { + id_user, _ := strconv.Atoi(c.Param("id_user")) + userListController.Service.Constructor.Id = uint(id_user) + userList.Delete() + }) +} diff --git a/controller/user/user_list_controller.go b/controller/user/user_list_controller.go new file mode 100644 index 0000000000000000000000000000000000000000..72c1bbacaa2d80c12196f3a6635fa2fc1ddb3aa1 --- /dev/null +++ b/controller/user/user_list_controller.go @@ -0,0 +1,19 @@ +package user + +import ( + "github.com/gin-gonic/gin" + "pweb-api.abdanhafidz.com/controller" + "pweb-api.abdanhafidz.com/models" + "pweb-api.abdanhafidz.com/services" +) + +func List(c *gin.Context) { + userList := services.UserListService{} + userListController := controller.Controller[any, models.Account, []models.UserProfileResponse]{ + Service: &userList.Service, + } + userListController.HeaderParse(c, func() { + userList.Retrieve() + userListController.Response(c) + }) +} diff --git a/controller/user/user_profile_controller.go b/controller/user/user_profile_controller.go new file mode 100644 index 0000000000000000000000000000000000000000..5c461b2b36f45d12ade20116dd922d9b840c3fe8 --- /dev/null +++ b/controller/user/user_profile_controller.go @@ -0,0 +1,21 @@ +package user + +import ( + "github.com/gin-gonic/gin" + "pweb-api.abdanhafidz.com/controller" + "pweb-api.abdanhafidz.com/models" + "pweb-api.abdanhafidz.com/services" +) + +func Profile(c *gin.Context) { + userProfile := services.UserProfileService{} + userProfileController := controller.Controller[any, models.AccountDetails, models.UserProfileResponse]{ + Service: &userProfile.Service, + } + userProfileController.HeaderParse(c, func() { + userProfileController.Service.Constructor.AccountId = uint(userProfileController.AccountData.UserID) + userProfile.Retrieve() + userProfileController.Response(c) + }, + ) +} diff --git a/controller/user/user_update_profile_controller.go b/controller/user/user_update_profile_controller.go new file mode 100644 index 0000000000000000000000000000000000000000..e8510abc15bf3627d5144d3f36853385893a953b --- /dev/null +++ b/controller/user/user_update_profile_controller.go @@ -0,0 +1,25 @@ +package user + +import ( + "github.com/gin-gonic/gin" + "pweb-api.abdanhafidz.com/controller" + "pweb-api.abdanhafidz.com/models" + "pweb-api.abdanhafidz.com/services" +) + +func UpdateProfile(c *gin.Context) { + userProfile := services.UserProfileService{} + userUpdateProfileController := controller.Controller[models.AccountDetails, models.AccountDetails, models.UserProfileResponse]{ + 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 new file mode 100644 index 0000000000000000000000000000000000000000..e92615382d436eb3b3d06544e6acd038e2d7a80c --- /dev/null +++ b/go.mod @@ -0,0 +1,67 @@ +module pweb-api.abdanhafidz.com + +go 1.24.0 + +require ( + github.com/gin-gonic/gin v1.10.0 + github.com/golang-jwt/jwt/v5 v5.2.1 + github.com/gosimple/slug v1.15.0 + github.com/joho/godotenv v1.5.1 + github.com/satori/go.uuid v1.2.0 + golang.org/x/crypto v0.36.0 + google.golang.org/api v0.228.0 + gorm.io/driver/postgres v1.5.11 + gorm.io/gorm v1.25.12 +) + +require ( + cloud.google.com/go/auth v0.15.0 // indirect + cloud.google.com/go/auth/oauth2adapt v0.2.8 // indirect + cloud.google.com/go/compute/metadata v0.6.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/felixge/httpsnoop v1.0.4 // indirect + github.com/gabriel-vasile/mimetype v1.4.8 // indirect + github.com/gin-contrib/sse v1.0.0 // indirect + github.com/go-logr/logr v1.4.2 // indirect + github.com/go-logr/stdr v1.2.2 // 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.5 // indirect + github.com/google/s2a-go v0.1.9 // indirect + github.com/googleapis/enterprise-certificate-proxy v0.3.6 // indirect + github.com/googleapis/gax-go/v2 v2.14.1 // indirect + github.com/gosimple/unidecode v1.0.1 // indirect + github.com/jackc/pgpassfile v1.0.0 // 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.10 // 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.2.3 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.12 // indirect + go.opentelemetry.io/auto/sdk v1.1.0 // indirect + go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.59.0 // indirect + go.opentelemetry.io/otel v1.34.0 // indirect + go.opentelemetry.io/otel/metric v1.34.0 // indirect + go.opentelemetry.io/otel/trace v1.34.0 // indirect + golang.org/x/arch v0.15.0 // indirect + golang.org/x/net v0.37.0 // indirect + golang.org/x/oauth2 v0.28.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/genproto/googleapis/rpc v0.0.0-20250313205543-e70fdf4c4cb4 // indirect + google.golang.org/grpc v1.71.0 // indirect + google.golang.org/protobuf v1.36.6 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000000000000000000000000000000000000..4fac69c64291b273a4a6b4d6887c80b0f43f7bc3 --- /dev/null +++ b/go.sum @@ -0,0 +1,168 @@ +cloud.google.com/go/auth v0.15.0 h1:Ly0u4aA5vG/fsSsxu98qCQBemXtAtJf+95z9HK+cxps= +cloud.google.com/go/auth v0.15.0/go.mod h1:WJDGqZ1o9E9wKIL+IwStfyn/+s59zl4Bi+1KQNVXLZ8= +cloud.google.com/go/auth/oauth2adapt v0.2.8 h1:keo8NaayQZ6wimpNSmW5OPc283g65QNIiLpZnkHRbnc= +cloud.google.com/go/auth/oauth2adapt v0.2.8/go.mod h1:XQ9y31RkqZCcwJWNSx2Xvric3RrU88hAYYbjDWYDL+c= +cloud.google.com/go/compute/metadata v0.6.0 h1:A6hENjEsCDtC1k8byVsgwvVcioamEHvZ4j01OwKxG9I= +cloud.google.com/go/compute/metadata v0.6.0/go.mod h1:FjyFAW1MW0C203CEOMDTu3Dk1FlqW3Rga40jzHL4hfg= +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/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/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= +github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= +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= +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-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= +github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +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.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.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/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= +github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/s2a-go v0.1.9 h1:LGD7gtMgezd8a/Xak7mEWL0PjoTQFvpRudN895yqKW0= +github.com/google/s2a-go v0.1.9/go.mod h1:YA0Ei2ZQL3acow2O62kdp9UlnvMmU7kA6Eutn0dXayM= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/googleapis/enterprise-certificate-proxy v0.3.6 h1:GW/XbdyBFQ8Qe+YAmFU9uHLo7OnF5tL52HFAgMmyrf4= +github.com/googleapis/enterprise-certificate-proxy v0.3.6/go.mod h1:MkHOF77EYAE7qfSuSS9PU6g4Nt4e11cnsDUowfwewLA= +github.com/googleapis/gax-go/v2 v2.14.1 h1:hb0FFeiPaQskmvakKu5EbCbpntQn48jyHuvrkurSS/Q= +github.com/googleapis/gax-go/v2 v2.14.1/go.mod h1:Hb/NubMaVM88SrNkvl8X/o8XWwDJEPqouaLeN2IUxoA= +github.com/gosimple/slug v1.15.0 h1:wRZHsRrRcs6b0XnxMUBM6WK1U1Vg5B0R7VkIf1Xzobo= +github.com/gosimple/slug v1.15.0/go.mod h1:UiRaFH+GEilHstLUmcBgWcI42viBN7mAb818JrYOeFQ= +github.com/gosimple/unidecode v1.0.1 h1:hZzFTMMqSswvf0LBJZCZgThIZrpDHFXux9KeGmn6T/o= +github.com/gosimple/unidecode v1.0.1/go.mod h1:CP0Cr1Y1kogOtx0bJblKzsVWrqYaqfNOnHzpgWw4Awc= +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-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= +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.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.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +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.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.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.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII= +github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o= +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= +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.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.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= +github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA= +go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A= +go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.59.0 h1:rgMkmiGfix9vFJDcDi1PK8WEQP4FLQwLDfhp5ZLpFeE= +go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.59.0/go.mod h1:ijPqXp5P6IRRByFVVg9DY8P5HkxkHE5ARIa+86aXPf4= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.59.0 h1:CV7UdSGJt/Ao6Gp4CXckLxVRRsRgDHoI8XjbL3PDl8s= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.59.0/go.mod h1:FRmFuRJfag1IZ2dPkHnEoSFVgTVPUd2qf5Vi69hLb8I= +go.opentelemetry.io/otel v1.34.0 h1:zRLXxLCgL1WyKsPVrgbSdMN4c0FMkDAskSTQP+0hdUY= +go.opentelemetry.io/otel v1.34.0/go.mod h1:OWFPOQ+h4G8xpyjgqo4SxJYdDQ/qmRH+wivy7zzx9oI= +go.opentelemetry.io/otel/metric v1.34.0 h1:+eTR3U0MyfWjRDhmFMxe2SsW64QrZ84AOhvqS7Y+PoQ= +go.opentelemetry.io/otel/metric v1.34.0/go.mod h1:CEDrp0fy2D0MvkXE+dPV7cMi8tWZwX3dmaIhwPOaqHE= +go.opentelemetry.io/otel/sdk v1.34.0 h1:95zS4k/2GOy069d321O8jWgYsW3MzVV+KuSPKp7Wr1A= +go.opentelemetry.io/otel/sdk v1.34.0/go.mod h1:0e/pNiaMAqaykJGKbi+tSjWfNNHMTxoC9qANsCzbyxU= +go.opentelemetry.io/otel/sdk/metric v1.34.0 h1:5CeK9ujjbFVL5c1PhLuStg1wxA7vQv7ce1EK0Gyvahk= +go.opentelemetry.io/otel/sdk/metric v1.34.0/go.mod h1:jQ/r8Ze28zRKoNRdkjCZxfs6YvBTG1+YIqyFVFYec5w= +go.opentelemetry.io/otel/trace v1.34.0 h1:+ouXS2V8Rd4hp4580a8q23bg0azF2nI8cqLYnC8mh/k= +go.opentelemetry.io/otel/trace v1.34.0/go.mod h1:Svm7lSjQD7kG7KJ/MUHPVXSDGz2OX4h0M2jHBhmSfRE= +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/oauth2 v0.28.0 h1:CrgCKl8PPAVtLnU3c+EDw6x11699EWlsDeWNWKdIOkc= +golang.org/x/oauth2 v0.28.0/go.mod h1:onh5ek6nERTohokkhCD/y2cV4Do3fxFHFuAejCkRWT8= +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.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/time v0.11.0 h1:/bpjEDfN9tkoN/ryeYHnv5hcMlc8ncjMcM4XBk5NWV0= +golang.org/x/time v0.11.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg= +google.golang.org/api v0.228.0 h1:X2DJ/uoWGnY5obVjewbp8icSL5U4FzuCfy9OjbLSnLs= +google.golang.org/api v0.228.0/go.mod h1:wNvRS1Pbe8r4+IfBIniV8fwCpGwTrYa+kMUDiC5z5a4= +google.golang.org/genproto/googleapis/rpc v0.0.0-20250313205543-e70fdf4c4cb4 h1:iK2jbkWL86DXjEx0qiHcRE9dE4/Ahua5k6V8OWFb//c= +google.golang.org/genproto/googleapis/rpc v0.0.0-20250313205543-e70fdf4c4cb4/go.mod h1:LuRYeWDFV6WOn90g357N17oMCaxpgCnbi/44qJvDn2I= +google.golang.org/grpc v1.71.0 h1:kF77BGdPTQ4/JZWMlb9VpJ5pa25aqvVqogsxNHHdeBg= +google.golang.org/grpc v1.71.0/go.mod h1:H0GRtasmQOh9LkFoCPDu3ZrwUtD1YGE+b2vYBYd/8Ec= +google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY= +google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY= +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.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= diff --git a/main.go b/main.go new file mode 100644 index 0000000000000000000000000000000000000000..8bc7b6d50d0c12a1c2dcfc2674c41d542186b11d --- /dev/null +++ b/main.go @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" + + "pweb-api.abdanhafidz.com/config" + "pweb-api.abdanhafidz.com/router" +) + +func main() { + fmt.Println("Server started on ", config.TCP_ADDRESS, ", port :", config.HOST_PORT) + router.StartService() + +} diff --git a/middleware/authentication_middleware.go b/middleware/authentication_middleware.go new file mode 100644 index 0000000000000000000000000000000000000000..fafb445a9e05173890844f2e13f8155b512316f5 --- /dev/null +++ b/middleware/authentication_middleware.go @@ -0,0 +1,65 @@ +// auth/auth.go + +package middleware + +import ( + "github.com/gin-gonic/gin" + "pweb-api.abdanhafidz.com/models" + "pweb-api.abdanhafidz.com/services" + "pweb-api.abdanhafidz.com/utils" +) + +func AuthUser(c *gin.Context) { + var currAccData models.AccountData + if c.Request.Header["Authorization"] != nil { + token := c.Request.Header["Authorization"] + + _, currAccData.UserID, currAccData.VerifyStatus, currAccData.ErrVerif = services.VerifyToken(token[0]) + + 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!"}) + c.Abort() + return + } else { + c.Set("accountData", currAccData) + c.Next() + } + } else { + currAccData.UserID = 0 + currAccData.VerifyStatus = "no-token" + currAccData.ErrVerif = nil + utils.ResponseFAIL(c, 401, models.Exception{Unauthorized: true, Message: "You have to login first!"}) + c.Abort() + return + } + +} + +func AuthAdmin(c *gin.Context) { + var currAccData models.AccountData + + if c.Request.Header["Authorization"] != nil { + token := c.Request.Header["Authorization"] + + currAccData.Role, currAccData.UserID, currAccData.VerifyStatus, currAccData.ErrVerif = services.VerifyToken(token[0]) + + 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!"}) + c.Abort() + return + } else { + c.Set("accountData", currAccData) + c.Next() + } + } else { + currAccData.UserID = 0 + currAccData.VerifyStatus = "no-token" + currAccData.ErrVerif = nil + utils.ResponseFAIL(c, 401, models.Exception{Unauthorized: true, Message: "You have to login first!"}) + c.Abort() + return + } + +} diff --git a/middleware/middleware.go b/middleware/middleware.go new file mode 100644 index 0000000000000000000000000000000000000000..5b140903a637aa1d80f02382cb8dbb821c5c01bf --- /dev/null +++ b/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/middleware/response_middleware.go b/middleware/response_middleware.go new file mode 100644 index 0000000000000000000000000000000000000000..d52391cd3607e6ec2697e6a5813b348d49ff5ee2 --- /dev/null +++ b/middleware/response_middleware.go @@ -0,0 +1,45 @@ +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}) + 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/models/authentication_payload_model.go b/models/authentication_payload_model.go new file mode 100644 index 0000000000000000000000000000000000000000..5428a589eb63600ae11414741311e1b84ab9ffc5 --- /dev/null +++ b/models/authentication_payload_model.go @@ -0,0 +1,8 @@ +package models + +type AccountData struct { + UserID uint + VerifyStatus string + Role string + ErrVerif error +} diff --git a/models/custom_claim.go b/models/custom_claim.go new file mode 100644 index 0000000000000000000000000000000000000000..aca49241031e3cb7e96649591e8772ccc2e4c067 --- /dev/null +++ b/models/custom_claim.go @@ -0,0 +1,9 @@ +package models + +import "github.com/golang-jwt/jwt/v5" + +type CustomClaims struct { + jwt.RegisteredClaims + UserID uint `json:"id"` + Role string `json:"role"` +} diff --git a/models/database_orm_model.go b/models/database_orm_model.go new file mode 100644 index 0000000000000000000000000000000000000000..b83b673ba8132e3cea05d61b5dbe2035dc0d9a89 --- /dev/null +++ b/models/database_orm_model.go @@ -0,0 +1,35 @@ +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"` + Role string `json:"role"` + IsDetailCompleted bool `json:"is_detail_completed"` + CreatedAt time.Time `json:"created_at"` + DeletedAt *time.Time `json:"deleted_at" gorm:"default:null"` +} + +type AccountDetails struct { + ID uint `gorm:"primaryKey" json:"id"` + AccountId uint `json:"account_id"` + InitialName string `json:"initial_name"` + FullName *string `json:"full_name"` + Address *string `json:"address"` + Gender bool `json:"gender"` + University *string `json:"university"` + DateOfBirth *time.Time `json:"date_of_birth"` + PlaceOfBirth *string `json:"place_of_birth"` + PhoneNumber *string `json:"phone_number"` +} + +// Gorm table name settings +func (Account) TableName() string { return "account" } +func (AccountDetails) TableName() string { return "account_details" } diff --git a/models/exception_model.go b/models/exception_model.go new file mode 100644 index 0000000000000000000000000000000000000000..f2c026a993f950652158318977783e8563e595fb --- /dev/null +++ b/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/models/model.go b/models/model.go new file mode 100644 index 0000000000000000000000000000000000000000..9ce401186cb92d50737155815c1f511164b86407 --- /dev/null +++ b/models/model.go @@ -0,0 +1 @@ +package models diff --git a/models/request_model.go b/models/request_model.go new file mode 100644 index 0000000000000000000000000000000000000000..80e5e088cf171bff6841265d01caf14510b7ac53 --- /dev/null +++ b/models/request_model.go @@ -0,0 +1,42 @@ +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"` +} + +type ChangePasswordRequest struct { + OldPassword string `json:"old_password" binding:"required" ` + NewPassword string `json:"new_password" binding:"required" ` +} + +type CreateVerifyEmailRequest struct { + Token uint `json:"token" binding:"required"` +} + +type OptionsRequest struct { + OptionName string `json:"option_name" binding:"required"` + OptionValue []string `json:"option_values" binding:"required"` +} + +type ExternalAuthRequest struct { + OauthID string `json:"oauth_id" binding:"required"` + OauthProvider string `json:"oauth_provider" binding:"required"` + IsAgreeTerms bool `json:"is_agree_terms"` + IsSexualDisease bool `json:"is_sexual_disease"` +} + +type ForgotPasswordRequest struct { + Email string `json:"email" binding:"required,email"` +} +type ValidateForgotPasswordRequest struct { + Token uint `json:"token" binding:"required"` + NewPassword string `json:"new_password"` +} diff --git a/models/response_model.go b/models/response_model.go new file mode 100644 index 0000000000000000000000000000000000000000..14adbf0edfc55fa4bb2d079eba54bb57abf91591 --- /dev/null +++ b/models/response_model.go @@ -0,0 +1,25 @@ +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"` +} + +type UserProfileResponse struct { + Account Account `json:"account"` + Details AccountDetails `json:"details"` +} diff --git a/repositories/account_repository.go b/repositories/account_repository.go new file mode 100644 index 0000000000000000000000000000000000000000..78cd8bf448654e50a77d6c5f9bd4d85586662a33 --- /dev/null +++ b/repositories/account_repository.go @@ -0,0 +1,97 @@ +package repositories + +import ( + "pweb-api.abdanhafidz.com/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 GetAllAccount() Repository[models.Account, []models.Account] { + repo := Construct[models.Account, []models.Account]( + models.Account{}, + ) + repo.Transactions( + Find[models.Account, []models.Account], + ) + return *repo +} +func GetAccountById(AccountId uint) Repository[models.Account, models.Account] { + repo := Construct[models.Account, models.Account]( + models.Account{Id: AccountId}, + ) + repo.Transactions( + WhereGivenConstructor[models.Account, models.Account], + Find[models.Account, models.Account], + ) + return *repo +} + +func UpdateAccount(account models.Account) Repository[models.Account, models.Account] { + repo := Construct[models.Account, models.Account]( + account, + ) + repo.Transaction.Save(&repo.Constructor) + repo.Result = repo.Constructor + return *repo +} + +func GetDetailAccountById(AccountId uint) Repository[models.AccountDetails, models.AccountDetails] { + repo := Construct[models.AccountDetails, models.AccountDetails]( + models.AccountDetails{AccountId: 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, + ) + Create(repo) + return *repo +} + +func CreateAccountDetails(accountDetails models.AccountDetails) Repository[models.AccountDetails, models.AccountDetails] { + repo := Construct[models.AccountDetails, models.AccountDetails]( + accountDetails, + ) + Create(repo) + return *repo +} + +func UpdateAccountDetails(accountDetails models.AccountDetails) Repository[models.AccountDetails, models.AccountDetails] { + repo := Construct[models.AccountDetails, models.AccountDetails]( + models.AccountDetails{AccountId: accountDetails.AccountId}, + ) + repo.Transaction.Where("account_id = ?", accountDetails.AccountId).First(&repo.Constructor) + accountDetails.ID = repo.Constructor.ID + // fmt.Println(repo.Constructor) + // fmt.Println(accountDetails) + repo.Transaction.Updates(accountDetails) + repo.Result = accountDetails + return *repo +} + +func DeleteAccountbyId(accountId uint) Repository[models.Account, models.Account] { + repo := Construct[models.Account, models.Account]( + models.Account{Id: accountId}, + ) + repo.Transactions( + Delete[models.Account], + ) + return *repo +} diff --git a/repositories/repository.go b/repositories/repository.go new file mode 100644 index 0000000000000000000000000000000000000000..573ca531412cc34e631acabf4fd43b34f23227e8 --- /dev/null +++ b/repositories/repository.go @@ -0,0 +1,117 @@ +package repositories + +import ( + "gorm.io/gorm" + "pweb-api.abdanhafidz.com/config" +) + +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.Updates(&repo.Constructor) + repo.RowsCount = int(tx.RowsAffected) + repo.NoRecord = repo.RowsCount == 0 + repo.RowsError = tx.Error + repo.Result = repo.Constructor + 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/router/auth_route.go b/router/auth_route.go new file mode 100644 index 0000000000000000000000000000000000000000..7ab6521214fc7e5016ad417e548c6c8678e6912a --- /dev/null +++ b/router/auth_route.go @@ -0,0 +1,15 @@ +package router + +import ( + "github.com/gin-gonic/gin" + AuthController "pweb-api.abdanhafidz.com/controller/auth" +) + +func AuthRoute(router *gin.Engine) { + routerGroup := router.Group("/api/v1/auth") + { + routerGroup.POST("/login", AuthController.Login) + routerGroup.POST("/register", AuthController.Register) + + } +} diff --git a/router/router.go b/router/router.go new file mode 100644 index 0000000000000000000000000000000000000000..6c943c0663a0e0ae1506a4bbe919351180cbb429 --- /dev/null +++ b/router/router.go @@ -0,0 +1,21 @@ +package router + +import ( + "log" + + "github.com/gin-gonic/gin" + "pweb-api.abdanhafidz.com/config" + "pweb-api.abdanhafidz.com/controller" +) + +func StartService() { + router := gin.Default() + router.GET("/", controller.HomeController) + + AuthRoute(router) + UserRoute(router) + err := router.Run(config.TCP_ADDRESS) + if err != nil { + log.Fatalf("Failed to run server: %v", err) + } +} diff --git a/router/user_route.go b/router/user_route.go new file mode 100644 index 0000000000000000000000000000000000000000..605428bc19edc7c635bd61b0d4f5ee81a6ebafc5 --- /dev/null +++ b/router/user_route.go @@ -0,0 +1,17 @@ +package router + +import ( + "github.com/gin-gonic/gin" + UserController "pweb-api.abdanhafidz.com/controller/user" + "pweb-api.abdanhafidz.com/middleware" +) + +func UserRoute(router *gin.Engine) { + routerGroup := router.Group("/api/v1/user") + { + routerGroup.GET("/me", middleware.AuthUser, UserController.Profile) + routerGroup.GET("/list", middleware.AuthUser, UserController.List) + routerGroup.DELETE("/:id_user", middleware.AuthAdmin, UserController.Delete) + routerGroup.PUT("/me", middleware.AuthUser, UserController.UpdateProfile) + } +} diff --git a/services/authentication_service.go b/services/authentication_service.go new file mode 100644 index 0000000000000000000000000000000000000000..52c300ab9cbc2b388501d79fe142a82d12762247 --- /dev/null +++ b/services/authentication_service.go @@ -0,0 +1,72 @@ +// meme +package services + +import ( + "errors" + + "pweb-api.abdanhafidz.com/models" + "pweb-api.abdanhafidz.com/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) + return + } + + 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 + } + if VerifyPassword(accountData.Result.Password, oldPassword) != nil { + s.Exception.Unauthorized = true + s.Exception.Message = "incorrect old password!" + return + } + hashed_password, _ := HashPassword(newPassword) + accountData.Result.Password = hashed_password + 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/services/jwt_service.go b/services/jwt_service.go new file mode 100644 index 0000000000000000000000000000000000000000..4718fe448fdf0adb89325922f142fc2943db1007 --- /dev/null +++ b/services/jwt_service.go @@ -0,0 +1,81 @@ +package services + +import ( + "errors" + "strings" + "time" + + "github.com/golang-jwt/jwt/v5" + "golang.org/x/crypto/bcrypt" + "pweb-api.abdanhafidz.com/config" + "pweb-api.abdanhafidz.com/models" +) + +var salt = config.Salt +var secretKey = []byte(salt) + +func GenerateToken(user *models.Account) (string, error) { + claims := models.CustomClaims{ + UserID: user.Id, + Role: user.Role, + RegisteredClaims: jwt.RegisteredClaims{ + ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour * 24)), // Token berlaku 24 jam + IssuedAt: jwt.NewNumericDate(time.Now()), + Issuer: "abdanhafidz.com", + }, + } + + // 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 +} + +func VerifyToken(bearerToken string) (string, uint, string, error) { + // fmt.Println("bearerToken :", bearerToken) + + 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 + }) + + if err != nil { + 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 claims.Role, claims.UserID, "valid", err +} + +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/services/register_service.go b/services/register_service.go new file mode 100644 index 0000000000000000000000000000000000000000..38410038df6a32db16854f5de1c3872894be2752 --- /dev/null +++ b/services/register_service.go @@ -0,0 +1,46 @@ +package services + +import ( + "errors" + + uuid "github.com/satori/go.uuid" + "gorm.io/gorm" + "pweb-api.abdanhafidz.com/models" + "pweb-api.abdanhafidz.com/repositories" +) + +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 := 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 + } + 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/services/service.go b/services/service.go new file mode 100644 index 0000000000000000000000000000000000000000..0c14296c2857732dac4cffae6631cb7d7a6269ea --- /dev/null +++ b/services/service.go @@ -0,0 +1,42 @@ +package services + +import ( + "time" + + "pweb-api.abdanhafidz.com/models" +) + +type ( + Services interface { + Retrieve() + Update() + Create() + Delete() + Validate() + Authenticate() + Authorize() + } + IService interface { + Implements() + } + 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], + } +} + +func CalculateDueTime(duration time.Duration) time.Time { + return time.Now().Add(duration) +} diff --git a/services/user_list_service.go b/services/user_list_service.go new file mode 100644 index 0000000000000000000000000000000000000000..ff5b3fbbc40642412bea5b4e1b9fbb76519cdefb --- /dev/null +++ b/services/user_list_service.go @@ -0,0 +1,43 @@ +package services + +import ( + "errors" + + "pweb-api.abdanhafidz.com/models" + "pweb-api.abdanhafidz.com/repositories" +) + +type UserListService struct { + Service[models.Account, []models.UserProfileResponse] +} + +func (s *UserListService) Retrieve() { + userListRepo := repositories.GetAllAccount() + s.Error = userListRepo.RowsError + for _, user := range userListRepo.Result { + if user.Role != "admin" { + userDetailsRepo := repositories.GetDetailAccountById(user.Id) + s.Error = errors.Join(userDetailsRepo.RowsError) + if s.Error != nil { + return + } + s.Result = append(s.Result, models.UserProfileResponse{ + Account: user, + Details: userDetailsRepo.Result, + }) + } + } + return +} + +func (s *UserListService) Delete() { + deleteUserRepo := repositories.DeleteAccountbyId(s.Constructor.Id) + if deleteUserRepo.NoRecord { + s.Exception.DataNotFound = true + s.Exception.Message = "user not found" + return + } + s.Error = deleteUserRepo.RowsError + s.Retrieve() + return +} diff --git a/services/user_profile_service.go b/services/user_profile_service.go new file mode 100644 index 0000000000000000000000000000000000000000..7f4d8fc213d172ffe4791dd356f9bad21ee097a7 --- /dev/null +++ b/services/user_profile_service.go @@ -0,0 +1,97 @@ +package services + +import ( + "regexp" + "strings" + + "pweb-api.abdanhafidz.com/models" + "pweb-api.abdanhafidz.com/repositories" +) + +type UserProfileService struct { + Service[models.AccountDetails, models.UserProfileResponse] +} + +// SanitizePhoneNumber membersihkan dan menormalkan nomor telepon ke format +62 +func SanitizePhoneNumber(input string) string { + // Hilangkan semua spasi dan strip + input = strings.ReplaceAll(input, " ", "") + input = strings.ReplaceAll(input, "-", "") + input = strings.ReplaceAll(input, "(", "") + input = strings.ReplaceAll(input, ")", "") + + // Hilangkan semua karakter non-digit kecuali + + re := regexp.MustCompile(`[^0-9\+]`) + input = re.ReplaceAllString(input, "") + + // Handle nomor diawali 0 (contoh: 0812...) menjadi +62812... + if strings.HasPrefix(input, "0") { + input = "+62" + input[1:] + } + + // Handle jika diawali dengan 62 tanpa + (contoh: 62812...) + if strings.HasPrefix(input, "62") && !strings.HasPrefix(input, "+62") { + input = "+" + input + } + + // Handle jika tidak ada awalan +62 sama sekali (contoh: 8123456789) + if !strings.HasPrefix(input, "+62") { + if strings.HasPrefix(input, "8") { + input = "+62" + input + } + } + + return input +} +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 = models.UserProfileResponse{ + Account: repositories.GetAccountById(s.Constructor.AccountId).Result, + Details: userProfile.Result, + } +} +func (s *UserProfileService) Retrieve() { + userProfile := repositories.GetDetailAccountById(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 = models.UserProfileResponse{ + Account: repositories.GetAccountById(s.Constructor.AccountId).Result, + Details: userProfile.Result, + } + s.Result.Account.Password = "SECRET" +} + +func (s *UserProfileService) Update() { + if s.Constructor.PhoneNumber != nil { + phoneNumber := *s.Constructor.PhoneNumber + *s.Constructor.PhoneNumber = SanitizePhoneNumber(phoneNumber) + } + 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 + } + account := repositories.GetAccountById(s.Constructor.AccountId) + account.Result.IsDetailCompleted = (userProfile.Result.InitialName != "" && + userProfile.Result.FullName != nil && + userProfile.Result.PhoneNumber != nil && + userProfile.Result.University != nil) + repositories.UpdateAccount(account.Result) + s.Result = models.UserProfileResponse{ + Account: account.Result, + Details: userProfile.Result, + } + s.Result.Account.Password = "SECRET" +} diff --git a/space/.gitattributes b/space/.gitattributes new file mode 100644 index 0000000000000000000000000000000000000000..a6344aac8c09253b3b630fb776ae94478aa0275b --- /dev/null +++ b/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/README.md b/space/README.md new file mode 100644 index 0000000000000000000000000000000000000000..217c7edc441d0d0d5e4f6901059085069ad03dc2 --- /dev/null +++ b/space/README.md @@ -0,0 +1,10 @@ +--- +title: Pweb Api +emoji: 📉 +colorFrom: green +colorTo: pink +sdk: docker +pinned: false +--- + + diff --git a/utils/Logger.go b/utils/Logger.go new file mode 100644 index 0000000000000000000000000000000000000000..dc9094902596693c899439c959a9494a94e38747 --- /dev/null +++ b/utils/Logger.go @@ -0,0 +1,28 @@ +package utils + +import ( + "log" + "os" + + "pweb-api.abdanhafidz.com/config" +) + +func LogError(errorLogged error) { + log.Println("Error Log :", errorLogged) + + _, err := os.Stat(config.LOG_PATH + "/error_log.txt") + if os.IsNotExist(err) { + _, err = os.Create(config.LOG_PATH + "/error_log.txt") + if err != nil { + log.Fatalf("Gagal buka file log: %v", err) + } + } + + file, err := os.OpenFile(config.LOG_PATH+"/error_log.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) + + if err != nil { + log.Fatalf("Gagal buka file log: %v", err) + } + + log.SetOutput(file) +} diff --git a/utils/api_response.go b/utils/api_response.go new file mode 100644 index 0000000000000000000000000000000000000000..8d74d76e7cb6791b38dc5d9d45a115b156093be6 --- /dev/null +++ b/utils/api_response.go @@ -0,0 +1,53 @@ +package utils + +import ( + "net/http" + "reflect" + + "pweb-api.abdanhafidz.com/models" + "pweb-api.abdanhafidz.com/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/utils/helper.go b/utils/helper.go new file mode 100644 index 0000000000000000000000000000000000000000..f1b05dde110733f819142efac2d1ad025830b592 --- /dev/null +++ b/utils/helper.go @@ -0,0 +1,11 @@ +package utils + +import ( + "github.com/gin-gonic/gin" + "pweb-api.abdanhafidz.com/models" +) + +func GetAccount(c *gin.Context) models.AccountData { + cParam, _ := c.Get("accountData") + return cParam.(models.AccountData) +} diff --git a/utils/util.go b/utils/util.go new file mode 100644 index 0000000000000000000000000000000000000000..480645d434c01f7152d6747171e5e3ea53969533 --- /dev/null +++ b/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 + } +} diff --git a/views/index.html b/views/index.html new file mode 100644 index 0000000000000000000000000000000000000000..da253a53c2d32fa41a1212067d72c380c7e79806 --- /dev/null +++ b/views/index.html @@ -0,0 +1,61 @@ + + + + + + Welcome to Abdan Hafidz Portal + + + + + + + + + +
+
+ +
+
+ + +
+
+
+

Welcome to Abdan Hafidz Portal

+

+ Your personal dashboard for managing your profile and settings. +

+
+ Login + Register +
+
+
+
+ + + + + + + \ No newline at end of file diff --git a/views/login.html b/views/login.html new file mode 100644 index 0000000000000000000000000000000000000000..a016caa195f7316db50b621d8dd613a13ab22f38 --- /dev/null +++ b/views/login.html @@ -0,0 +1,77 @@ + + + + + + Login - Abdan Hafidz Portal + + + + + + + + + +
+
+ +
+
+ + +
+
+
+

Login to Your Account

+ +
+
+ + +
+
+ + +
+ + +
+
+
+
+ + + + + + + \ No newline at end of file diff --git a/views/profile.html b/views/profile.html new file mode 100644 index 0000000000000000000000000000000000000000..de4b450d2947e68c39fe6ad6cdb1beced06581e2 --- /dev/null +++ b/views/profile.html @@ -0,0 +1,108 @@ + + + + + + My Profile - Abdan Hafidz Portal + + + + + + + + + +
+
+ +
+
+ + +
+
+
+
+
U
+
+

User Profile

+

email@example.com

+
+
+ +
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+
+ + +
+
+
+
+
+ + + + + + + \ No newline at end of file diff --git a/views/register.html b/views/register.html new file mode 100644 index 0000000000000000000000000000000000000000..638dcbaf5327c4089a4a867ff49f12c008a5665f --- /dev/null +++ b/views/register.html @@ -0,0 +1,88 @@ + + + + + + Register - Abdan Hafidz Portal + + + + + + + + + +
+
+ +
+
+ + +
+
+
+

Create an Account

+ +
+
+ + +
+
+ + +
+
+ + +
+ + +
+
+
+
+ + + + + + + \ No newline at end of file diff --git a/views/scripts/scripts.js b/views/scripts/scripts.js new file mode 100644 index 0000000000000000000000000000000000000000..223bd2bfdd469d9bc9c4cc274421b03b5f21b134 --- /dev/null +++ b/views/scripts/scripts.js @@ -0,0 +1,270 @@ +// Configuration +const API_BASE_URL = 'https://lifedebugger-pweb-api.hf.space/api/v1'; +const TOKEN_COOKIE_NAME = 'auth_token'; + +// Utility Functions +function setCookie(name, value, days) { + let expires = ''; + if (days) { + const date = new Date(); + date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); + expires = '; expires=' + date.toUTCString(); + } + document.cookie = name + '=' + (value || '') + expires + '; path=/'; +} + +function getCookie(name) { + const nameEQ = name + '='; + const ca = document.cookie.split(';'); + for (let i = 0; i < ca.length; i++) { + let c = ca[i]; + while (c.charAt(0) === ' ') c = c.substring(1, c.length); + if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length); + } + return null; +} + +function eraseCookie(name) { + document.cookie = name + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;'; +} + +function showLoading() { + $('#loadingIndicator').show(); +} + +function hideLoading() { + $('#loadingIndicator').hide(); +} + +function showAlert(elementId, type, message) { + const alertElement = $(`#${elementId}`); + alertElement.attr('class', `alert alert-${type}`); + alertElement.html(message); + alertElement.show(); + + // Auto hide after 5 seconds + setTimeout(() => { + alertElement.hide(); + }, 5000); +} + +function isLoggedIn() { + return !!getCookie(TOKEN_COOKIE_NAME); +} + +function updateNavLinks() { + const navLinks = $('#navLinks'); + navLinks.empty(); + + if (isLoggedIn()) { + navLinks.append('Profile'); + navLinks.append('Logout'); + } else { + navLinks.append('Login'); + navLinks.append('Register'); + } + + // Attach event listeners to nav links + $('#navLogout').on('click', function(e) { + e.preventDefault(); + logout(); + }); +} + +function setAuthToken(token) { + setCookie(TOKEN_COOKIE_NAME, token, 7); // Store token for 7 days +} + +function getAuthToken() { + return getCookie(TOKEN_COOKIE_NAME); +} + +function getAuthHeader() { + const token = getAuthToken(); + return token ? { 'Authorization': `Bearer ${token}` } : {}; +} + +// Auth Functions +function login(email, password) { + showLoading(); + + $.ajax({ + url: `${API_BASE_URL}/auth/login`, + type: 'POST', + contentType: 'application/json', + data: JSON.stringify({ + email: email, + password: password + }), + success: function(response) { + if (response.status === 'success') { + setAuthToken(response.data.token); + showAlert('loginAlert', 'success', 'Login successful! Redirecting to your profile...'); + setTimeout(() => { + window.location.href = 'profile.html'; + }, 1000); + } else { + showAlert('loginAlert', 'danger', 'Login failed. Please check your credentials.'); + } + }, + error: function(xhr) { + const message = xhr.responseJSON ? xhr.responseJSON.message : 'An error occurred during login.'; + showAlert('loginAlert', 'danger', message); + }, + complete: function() { + hideLoading(); + } + }); +} + +function register(email, password) { + showLoading(); + + $.ajax({ + url: `${API_BASE_URL}/auth/register`, + type: 'POST', + contentType: 'application/json', + data: JSON.stringify({ + email: email, + password: password + }), + success: function(response) { + if (response.status === 'success') { + showAlert('registerAlert', 'success', 'Registration successful! Redirecting to login page...'); + setTimeout(() => { + window.location.href = 'login.html'; + }, 2000); + } else { + showAlert('registerAlert', 'danger', 'Registration failed. Please try again.'); + } + }, + error: function(xhr) { + const message = xhr.responseJSON ? xhr.responseJSON.message : 'An error occurred during registration.'; + showAlert('registerAlert', 'danger', message); + }, + complete: function() { + hideLoading(); + } + }); +} + +function logout() { + eraseCookie(TOKEN_COOKIE_NAME); + window.location.href = 'login.html'; +} + +function fetchUserProfile() { + showLoading(); + + $.ajax({ + url: `${API_BASE_URL}/user/me`, + type: 'GET', + headers: getAuthHeader(), + success: function(response) { + if (response.status === 'success') { + const userData = response.data; + + // Update profile email + $('#profileEmail').text(userData.account.email); + + // Update form fields if details exist + if (userData.details) { + $('#profileFullName').val(userData.details.full_name || ''); + $('#profileInitialName').val(userData.details.initial_name || ''); + $('#profileUniversity').val(userData.details.university || ''); + $('#profilePhone').val(userData.details.phone_number || ''); + + // Update profile name and initials + if (userData.details.full_name) { + $('#profileName').text(userData.details.full_name); + } else { + $('#profileName').text('User Profile'); + } + + if (userData.details.initial_name) { + $('#profileInitials').text(userData.details.initial_name.substring(0, 2).toUpperCase()); + } else if (userData.details.full_name) { + const nameParts = userData.details.full_name.split(' '); + if (nameParts.length > 1) { + $('#profileInitials').text((nameParts[0][0] + nameParts[1][0]).toUpperCase()); + } else { + $('#profileInitials').text(nameParts[0][0].toUpperCase()); + } + } else { + $('#profileInitials').text(userData.account.email[0].toUpperCase()); + } + } else { + $('#profileInitials').text(userData.account.email[0].toUpperCase()); + } + } else { + showAlert('profileAlert', 'danger', 'Failed to load profile data.'); + } + }, + error: function(xhr) { + if (xhr.status === 401) { + eraseCookie(TOKEN_COOKIE_NAME); + window.location.href = 'login.html'; + showAlert('loginAlert', 'danger', 'Session expired. Please login again.'); + } else { + const message = xhr.responseJSON ? xhr.responseJSON.message : 'An error occurred while fetching profile data.'; + showAlert('profileAlert', 'danger', message); + } + }, + complete: function() { + hideLoading(); + } + }); +} + +function updateUserProfile(profileData) { + showLoading(); + + $.ajax({ + url: `${API_BASE_URL}/user/me`, + type: 'PUT', + headers: getAuthHeader(), + contentType: 'application/json', + data: JSON.stringify({ + full_name: profileData.fullName, + initial_name: profileData.initialName, + university: profileData.university, + phone_number: profileData.phoneNumber + }), + success: function(response) { + if (response.status === 'success') { + showAlert('profileAlert', 'success', 'Profile updated successfully!'); + fetchUserProfile(); // Refresh profile data + } else { + showAlert('profileAlert', 'danger', 'Failed to update profile data.'); + } + }, + error: function(xhr) { + if (xhr.status === 401) { + eraseCookie(TOKEN_COOKIE_NAME); + window.location.href = 'login.html'; + showAlert('loginAlert', 'danger', 'Session expired. Please login again.'); + } else { + const message = xhr.responseJSON ? xhr.responseJSON.message : 'An error occurred while updating profile data.'; + showAlert('profileAlert', 'danger', message); + } + }, + complete: function() { + hideLoading(); + } + }); +} + +// Check Authentication State +$(document).ready(function() { + updateNavLinks(); + + // Handle logo click + $('.logo').on('click', function(e) { + e.preventDefault(); + if (isLoggedIn()) { + window.location.href = 'profile.html'; + } else { + window.location.href = 'login.html'; + } + }); +}); \ No newline at end of file diff --git a/views/style/styles.css b/views/style/styles.css new file mode 100644 index 0000000000000000000000000000000000000000..4ef03e6a1c0145d7ba03bb7c7efd9885446498ad --- /dev/null +++ b/views/style/styles.css @@ -0,0 +1,291 @@ +:root { + --primary-color: #4361ee; + --secondary-color: #3f37c9; + --accent-color: #4895ef; + --light-color: #f8f9fa; + --dark-color: #212529; + --success-color: #4cc9f0; + --danger-color: #e5383b; + --warning-color: #ffba08; +} + +* { + margin: 0; + padding: 0; + box-sizing: border-box; + font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; +} + +body { + background-color: #f5f7fa; + color: var(--dark-color); + min-height: 100vh; + display: flex; + flex-direction: column; +} + +.container { + max-width: 1200px; + margin: 0 auto; + padding: 0 20px; +} + +header { + background-color: white; + box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); + padding: 1rem 0; +} + +nav { + display: flex; + justify-content: space-between; + align-items: center; +} + +.logo { + font-size: 1.5rem; + font-weight: bold; + color: var(--primary-color); + cursor: pointer; +} + +.nav-links { + display: flex; + gap: 20px; +} + +.nav-links a { + text-decoration: none; + color: var(--dark-color); + font-weight: 500; + transition: color 0.3s; +} + +.nav-links a:hover { + color: var(--primary-color); +} + +.btn { + display: inline-block; + padding: 0.6rem 1.5rem; + background-color: var(--primary-color); + color: white; + border: none; + border-radius: 4px; + cursor: pointer; + font-size: 1rem; + font-weight: 500; + transition: all 0.3s ease; + text-decoration: none; +} + +.btn:hover { + background-color: var(--secondary-color); + transform: translateY(-2px); +} + +.btn-outline { + background-color: transparent; + border: 2px solid var(--primary-color); + color: var(--primary-color); +} + +.btn-outline:hover { + background-color: var(--primary-color); + color: white; +} + +.btn-danger { + background-color: var(--danger-color); +} + +.btn-danger:hover { + background-color: #c1121f; +} + +.auth-section { + flex: 1; + display: flex; + align-items: center; + justify-content: center; + padding: 2rem 0; +} + +.auth-container { + width: 100%; + max-width: 450px; + background-color: white; + padding: 2.5rem; + border-radius: 10px; + box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1); +} + +.auth-title { + font-size: 1.75rem; + font-weight: 600; + color: var(--primary-color); + margin-bottom: 1.5rem; + text-align: center; +} + +.form-group { + margin-bottom: 1.5rem; +} + +.form-group label { + display: block; + margin-bottom: 0.5rem; + font-weight: 500; +} + +.form-control { + width: 100%; + padding: 0.8rem; + border: 1px solid #ddd; + border-radius: 4px; + font-size: 1rem; + transition: border-color 0.3s; +} + +.form-control:focus { + outline: none; + border-color: var(--primary-color); + box-shadow: 0 0 0 3px rgba(67, 97, 238, 0.1); +} + +.form-footer { + text-align: center; + margin-top: 1.5rem; +} + +.form-footer a { + color: var(--primary-color); + text-decoration: none; +} + +.form-footer a:hover { + text-decoration: underline; +} + +.alert { + padding: 0.75rem 1rem; + margin-bottom: 1rem; + border-radius: 4px; + font-weight: 500; +} + +.alert-danger { + background-color: #f8d7da; + color: #721c24; + border: 1px solid #f5c6cb; +} + +.alert-success { + background-color: #d4edda; + color: #155724; + border: 1px solid #c3e6cb; +} + +/* Profile Page Styles */ +.profile-container { + max-width: 800px; + margin: 2rem auto; + background-color: white; + border-radius: 10px; + box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1); + padding: 2rem; +} + +.profile-header { + display: flex; + align-items: center; + margin-bottom: 2rem; + padding-bottom: 1rem; + border-bottom: 1px solid #eee; +} + +.profile-avatar { + width: 80px; + height: 80px; + border-radius: 50%; + background-color: var(--accent-color); + color: white; + display: flex; + align-items: center; + justify-content: center; + font-size: 2rem; + font-weight: bold; + margin-right: 1.5rem; +} + +.profile-title h1 { + font-size: 1.75rem; + margin-bottom: 0.25rem; + color: var(--dark-color); +} + +.profile-email { + color: #6c757d; +} + +.profile-form { + display: grid; + grid-template-columns: 1fr 1fr; + gap: 1.5rem; +} + +.profile-actions { + margin-top: 2rem; + display: flex; + justify-content: flex-end; + gap: 1rem; +} + +footer { + background-color: white; + padding: 1.5rem 0; + border-top: 1px solid #eee; + margin-top: auto; +} + +footer p { + text-align: center; + color: #6c757d; +} + +.loading { + position: fixed; + top: 0; + left: 0; + width: 100%; + height: 100%; + background-color: rgba(255, 255, 255, 0.8); + display: flex; + align-items: center; + justify-content: center; + z-index: 1000; +} + +.loading-spinner { + width: 50px; + height: 50px; + border: 5px solid #f3f3f3; + border-top: 5px solid var(--primary-color); + border-radius: 50%; + animation: spin 1s linear infinite; +} + +@keyframes spin { + 0% { transform: rotate(0deg); } + 100% { transform: rotate(360deg); } +} + +@media (max-width: 768px) { + .profile-form { + grid-template-columns: 1fr; + } + + .auth-container { + padding: 1.5rem; + } +} \ No newline at end of file diff --git a/views/user-portal.zip b/views/user-portal.zip new file mode 100644 index 0000000000000000000000000000000000000000..d0205f30376fbdb77d616b060c14e7c2b079c76c --- /dev/null +++ b/views/user-portal.zip @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5f55c7ed6afbd6b7e71ac6e942b28eacf367222229263632e84f41e359a27748 +size 9026