Compare commits
32
Commits
+4
-30
@@ -1,36 +1,6 @@
|
|||||||
# RUST_ENV : Mode environment aplikasi (misal: development, production)
|
|
||||||
# RUST_LOG : Level logging aplikasi Rust (misal: debug, info, warn, error)
|
|
||||||
# PORT : Port yang digunakan backend service
|
|
||||||
# SURREALDB_URL : URL koneksi ke SurrealDB (gunakan ws:// untuk WebSocket)
|
|
||||||
# SURREALDB_USERNAME : Username untuk SurrealDB
|
|
||||||
# SURREALDB_PASSWORD : Password untuk SurrealDB
|
|
||||||
# SURREALDB_NAMESPACE : Namespace SurrealDB yang digunakan
|
|
||||||
# SURREALDB_DBNAME : Nama database SurrealDB
|
|
||||||
# ACCESS_TOKEN_SECRET : Secret key untuk JWT access token (ganti dengan nilai rahasia)
|
|
||||||
# REFRESH_TOKEN_SECRET : Secret key untuk JWT refresh token (ganti dengan nilai rahasia)
|
|
||||||
# SMTP_EMAIL : Email yang digunakan untuk mengirim email (SMTP)
|
|
||||||
# SMTP_PASSWORD : Password email SMTP
|
|
||||||
# SMTP_NAME : Nama pengirim email (misal: nama aplikasi)
|
|
||||||
# SMTP_HOST : Host SMTP server (misal: smtp.gmail.com)
|
|
||||||
# REDISDB_URL : URL/host Redis database
|
|
||||||
# FE_URL : URL frontend aplikasi (misal: http://localhost)
|
|
||||||
# MINIO_ENDPOINT : Endpoint MinIO object storage
|
|
||||||
# MINIO_BUCKET_NAME : Nama bucket default MinIO
|
|
||||||
# MINIO_ACCESS_KEY : Access key MinIO atau MINIO_ROOT_USER
|
|
||||||
# MINIO_SECRET_KEY : Secret key MinIO atau MINIO_ROOT_PASSWORD
|
|
||||||
# MINIO_ROOT_USER : Username root MinIO (default: minioadmin)
|
|
||||||
# MINIO_ROOT_PASSWORD : Password root MinIO (default: minioadmin)
|
|
||||||
# GOOGLE_CLIENT_ID : Client ID OAuth Google (untuk login Google)
|
|
||||||
# GOOGLE_CLIENT_SECRET : Client Secret OAuth Google
|
|
||||||
|
|
||||||
RUST_ENV=development
|
RUST_ENV=development
|
||||||
RUST_LOG=debug
|
RUST_LOG=debug
|
||||||
PORT=4099
|
PORT=4099
|
||||||
SURREALDB_URL=ws://localhost:8000/rpc
|
|
||||||
SURREALDB_USERNAME=root
|
|
||||||
SURREALDB_PASSWORD=root
|
|
||||||
SURREALDB_NAMESPACE=test
|
|
||||||
SURREALDB_DBNAME=test
|
|
||||||
ACCESS_TOKEN_SECRET=your-access-token-secret-key-here
|
ACCESS_TOKEN_SECRET=your-access-token-secret-key-here
|
||||||
REFRESH_TOKEN_SECRET=your-refresh-token-secret-key-here
|
REFRESH_TOKEN_SECRET=your-refresh-token-secret-key-here
|
||||||
SMTP_EMAIL=your-email@example.com
|
SMTP_EMAIL=your-email@example.com
|
||||||
@@ -57,3 +27,7 @@ SSLMODE=require
|
|||||||
RETRY_ATTEMPTS=3
|
RETRY_ATTEMPTS=3
|
||||||
RETRY_DELAY=1
|
RETRY_DELAY=1
|
||||||
GOOGLE_REDIRECT_URL=http://localhost:8000/api/v1/auth/google/callback
|
GOOGLE_REDIRECT_URL=http://localhost:8000/api/v1/auth/google/callback
|
||||||
|
|
||||||
|
CDN_URL=https://cdn.asepharyana.tech
|
||||||
|
CORS_ALLOWED_ORIGINS=http://localhost:3000,https://gacha.imphnen.dev,https://imphnen.dev,https://dimentorin.imphnen.dev
|
||||||
|
|
||||||
|
|||||||
@@ -1,65 +0,0 @@
|
|||||||
name: Deploy to Ancikri
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- develop
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build-and-deploy:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: Checkout code
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
|
|
||||||
- name: Set up Rust
|
|
||||||
uses: actions-rs/toolchain@v1
|
|
||||||
with:
|
|
||||||
toolchain: stable
|
|
||||||
override: true
|
|
||||||
|
|
||||||
- name: Build the project
|
|
||||||
run: cargo build --release
|
|
||||||
|
|
||||||
- name: Stop service on VPS before upload
|
|
||||||
uses: appleboy/ssh-action@v0.1.7
|
|
||||||
with:
|
|
||||||
host: ${{ secrets.VPS_ANCIKRI_IP }}
|
|
||||||
username: ${{ secrets.VPS_ANCIKRI_USER }}
|
|
||||||
key: ${{ secrets.VPS_ANCIKRI_SSH_KEY }}
|
|
||||||
port: ${{ secrets.VPS_ANCIKRI_PORT }}
|
|
||||||
script: |
|
|
||||||
set -e
|
|
||||||
echo "Stopping the service before uploading the binary"
|
|
||||||
sudo systemctl stop imphnen-backend-service
|
|
||||||
|
|
||||||
- name: Upload artifact to VPS
|
|
||||||
uses: appleboy/scp-action@v0.1.7
|
|
||||||
with:
|
|
||||||
host: ${{ secrets.VPS_ANCIKRI_IP }}
|
|
||||||
username: ${{ secrets.VPS_ANCIKRI_USER }}
|
|
||||||
key: ${{ secrets.VPS_ANCIKRI_SSH_KEY }}
|
|
||||||
port: ${{ secrets.VPS_ANCIKRI_PORT }}
|
|
||||||
source: ./target/release/*
|
|
||||||
target: /opt/imphnen-backend-service/imphnen-backend-service
|
|
||||||
rm: true
|
|
||||||
overwrite: true
|
|
||||||
|
|
||||||
- name: Deploy to server
|
|
||||||
uses: appleboy/ssh-action@v0.1.7
|
|
||||||
with:
|
|
||||||
host: ${{ secrets.VPS_ANCIKRI_IP }}
|
|
||||||
username: ${{ secrets.VPS_ANCIKRI_USER }}
|
|
||||||
key: ${{ secrets.VPS_ANCIKRI_SSH_KEY }}
|
|
||||||
port: ${{ secrets.VPS_ANCIKRI_PORT }}
|
|
||||||
script: |
|
|
||||||
set -e
|
|
||||||
|
|
||||||
echo "Restarting the service"
|
|
||||||
|
|
||||||
sudo systemctl daemon-reload
|
|
||||||
|
|
||||||
sudo systemctl restart imphnen-backend-service
|
|
||||||
|
|
||||||
echo "Deployment completed successfully"
|
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
name: Nix Build & Deploy
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: ['develop']
|
||||||
|
pull_request:
|
||||||
|
branches: ['develop']
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Install Nix
|
||||||
|
uses: DeterminateSystems/nix-installer-action@main
|
||||||
|
|
||||||
|
- name: Setup Cachix
|
||||||
|
uses: cachix/cachix-action@v15
|
||||||
|
with:
|
||||||
|
name: msdqn
|
||||||
|
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
|
||||||
|
|
||||||
|
- name: Build
|
||||||
|
run: nix build .#default -o result
|
||||||
|
|
||||||
|
- name: Push to Cachix
|
||||||
|
if: github.event_name == 'push' && github.ref == 'refs/heads/develop'
|
||||||
|
run: cachix push msdqn result
|
||||||
|
|
||||||
|
deploy:
|
||||||
|
needs: build
|
||||||
|
if: (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && github.ref == 'refs/heads/develop'
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Install Nix
|
||||||
|
uses: DeterminateSystems/nix-installer-action@main
|
||||||
|
|
||||||
|
- name: Setup SSH
|
||||||
|
env:
|
||||||
|
INFRA_DEPLOY_KEY: ${{ secrets.INFRA_DEPLOY_KEY }}
|
||||||
|
run: |
|
||||||
|
mkdir -p ~/.ssh
|
||||||
|
echo "${INFRA_DEPLOY_KEY}" > ~/.ssh/deploy_key
|
||||||
|
chmod 600 ~/.ssh/deploy_key
|
||||||
|
ssh-keyscan github.com >> ~/.ssh/known_hosts 2>/dev/null
|
||||||
|
ssh-keyscan 167.235.70.37 >> ~/.ssh/known_hosts 2>/dev/null
|
||||||
|
|
||||||
|
- name: Update infra flake.lock
|
||||||
|
run: |
|
||||||
|
export GIT_SSH_COMMAND="ssh -i ~/.ssh/deploy_key -o IdentitiesOnly=yes"
|
||||||
|
git clone git@github.com:IMPHNEN/imphnen-infrastructure.git /tmp/infra
|
||||||
|
cd /tmp/infra
|
||||||
|
nix flake update imphnen-backend
|
||||||
|
if git diff --quiet flake.lock; then
|
||||||
|
echo "flake.lock unchanged, skipping"
|
||||||
|
else
|
||||||
|
git config user.name "github-actions[bot]"
|
||||||
|
git config user.email "github-actions[bot]@users.noreply.github.com"
|
||||||
|
git add flake.lock
|
||||||
|
git commit -m "chore: update imphnen-backend-service to ${GITHUB_SHA::7}"
|
||||||
|
git push
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Deploy to server
|
||||||
|
run: |
|
||||||
|
ssh -i ~/.ssh/deploy_key \
|
||||||
|
-o ConnectTimeout=30 \
|
||||||
|
-o StrictHostKeyChecking=accept-new \
|
||||||
|
-o ServerAliveInterval=30 \
|
||||||
|
-o ServerAliveCountMax=40 \
|
||||||
|
root@167.235.70.37 \
|
||||||
|
'nixos-rebuild switch --flake github:IMPHNEN/imphnen-infrastructure#hetzner --refresh 2>&1 | tail -50'
|
||||||
|
|
||||||
|
- name: Cleanup
|
||||||
|
if: always()
|
||||||
|
run: rm -f ~/.ssh/deploy_key
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
name: Rust
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches: ["develop"]
|
|
||||||
pull_request:
|
|
||||||
branches: ["develop"]
|
|
||||||
|
|
||||||
env:
|
|
||||||
CARGO_TERM_COLOR: always
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
- name: Build
|
|
||||||
run: cargo build --verbose
|
|
||||||
Generated
+792
-125
File diff suppressed because it is too large
Load Diff
+28
-15
@@ -1,18 +1,20 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
resolver = "2"
|
resolver = "2"
|
||||||
members = [
|
members = [
|
||||||
"tests",
|
"imphnen-entities",
|
||||||
"imphnen-entities", # Most basic - core data structures
|
"imphnen-macros",
|
||||||
"imphnen-macros", # Macros
|
"imphnen-libs",
|
||||||
"imphnen-libs", # Depends on entities
|
"imphnen-storage",
|
||||||
"imphnen-utils", # Depends on libs and entities
|
"imphnen-email",
|
||||||
"imphnen-middleware",# Utility for permissions
|
"imphnen-utils",
|
||||||
"imphnen-iam", # Core auth service, depends on libs, utils, entities
|
"imphnen-middleware",
|
||||||
"imphnen-cms", # Content management, depends on core services
|
"imphnen-iam",
|
||||||
"imphnen-gacha", # Game mechanics, depends on core services
|
"imphnen-cms",
|
||||||
"imphnen-dimentorin",# Learning platform, depends on core services
|
"imphnen-gacha",
|
||||||
"imphnen-gateway", # API gateway, depends on all services
|
"imphnen-dimentorin",
|
||||||
"imphnen-backend", # Main application, depends on all services
|
"imphnen-hackathon",
|
||||||
|
"imphnen-gateway",
|
||||||
|
"imphnen-backend",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@@ -28,7 +30,7 @@ tokio = { version = "1.47.1", features = ["full"] }
|
|||||||
argon2 = { version = "0.5.3", features = ["password-hash"] }
|
argon2 = { version = "0.5.3", features = ["password-hash"] }
|
||||||
jsonwebtoken = "9.3.1"
|
jsonwebtoken = "9.3.1"
|
||||||
chrono = "0.4.41"
|
chrono = "0.4.41"
|
||||||
utoipa = { version = "5.4.0", features = ["axum_extras"] }
|
utoipa = { version = "5.4.0", features = ["axum_extras", "uuid", "chrono"] }
|
||||||
utoipa-swagger-ui = { version = "9.0.2", features = ["axum"] }
|
utoipa-swagger-ui = { version = "9.0.2", features = ["axum"] }
|
||||||
lettre = { version = "0.11.18", features = ["tokio1-native-tls"] }
|
lettre = { version = "0.11.18", features = ["tokio1-native-tls"] }
|
||||||
thiserror = "2.0.14"
|
thiserror = "2.0.14"
|
||||||
@@ -37,7 +39,12 @@ rand = { version = "0.9.2", features = ["std", "alloc"] }
|
|||||||
rand_distr = "0.5.1"
|
rand_distr = "0.5.1"
|
||||||
tower-http = { version = "0.6.6", features = ["cors", "trace"] }
|
tower-http = { version = "0.6.6", features = ["cors", "trace"] }
|
||||||
http-body-util = "0.1.3"
|
http-body-util = "0.1.3"
|
||||||
validator = { version = "0.20.0", features = ["derive"] }
|
zod-rs = { version = "0.4", features = ["macros"] }
|
||||||
|
zod-rs-util = "0.4"
|
||||||
|
paginator-rs = "0.2"
|
||||||
|
paginator-utils = "0.2"
|
||||||
|
paginator-sea-orm = { version = "0.2", features = ["sqlx-postgres", "runtime-tokio"] }
|
||||||
|
paginator-axum = "0.2"
|
||||||
lazy_static = "1.5.0"
|
lazy_static = "1.5.0"
|
||||||
regex = "1.11.1"
|
regex = "1.11.1"
|
||||||
axum-test = "17.3.0"
|
axum-test = "17.3.0"
|
||||||
@@ -59,6 +66,7 @@ hyper = "1.6.0"
|
|||||||
hyper-util = "0.1.16"
|
hyper-util = "0.1.16"
|
||||||
minio = "0.3.0"
|
minio = "0.3.0"
|
||||||
sea-orm = { version = "1.1", features = ["sqlx-postgres", "runtime-tokio-native-tls", "macros", "with-chrono", "uuid"] }
|
sea-orm = { version = "1.1", features = ["sqlx-postgres", "runtime-tokio-native-tls", "macros", "with-chrono", "uuid"] }
|
||||||
|
sqlx = { version = "0.8", features = ["postgres", "runtime-tokio-native-tls", "uuid", "chrono", "json", "macros"] }
|
||||||
num_cpus = "1.16.0"
|
num_cpus = "1.16.0"
|
||||||
|
|
||||||
|
|
||||||
@@ -71,7 +79,6 @@ tokio-tungstenite = "0.27"
|
|||||||
url = "2.5"
|
url = "2.5"
|
||||||
futures-util = "0.3"
|
futures-util = "0.3"
|
||||||
http = "1.3"
|
http = "1.3"
|
||||||
tests = { path = "./tests" }
|
|
||||||
imphnen-iam = { path = "./imphnen-iam" }
|
imphnen-iam = { path = "./imphnen-iam" }
|
||||||
imphnen-cms = { path = "./imphnen-cms" }
|
imphnen-cms = { path = "./imphnen-cms" }
|
||||||
imphnen-libs = { path = "./imphnen-libs" }
|
imphnen-libs = { path = "./imphnen-libs" }
|
||||||
@@ -83,6 +90,12 @@ imphnen-entities = { path = "./imphnen-entities" }
|
|||||||
imphnen-dimentorin = { path = "./imphnen-dimentorin" }
|
imphnen-dimentorin = { path = "./imphnen-dimentorin" }
|
||||||
imphnen-middleware = { path = "./imphnen-middleware" }
|
imphnen-middleware = { path = "./imphnen-middleware" }
|
||||||
imphnen-macros = { path = "./imphnen-macros" }
|
imphnen-macros = { path = "./imphnen-macros" }
|
||||||
|
imphnen-hackathon = { path = "./imphnen-hackathon" }
|
||||||
|
imphnen-storage = { path = "./imphnen-storage" }
|
||||||
|
imphnen-email = { path = "./imphnen-email" }
|
||||||
|
bcrypt = "0.15"
|
||||||
|
image = { version = "0.25", features = ["png", "jpeg"] }
|
||||||
|
qrcode = { version = "0.14", default-features = false, features = ["image"] }
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
lto = "fat"
|
lto = "fat"
|
||||||
|
|||||||
+20
-12
@@ -11,30 +11,38 @@ WORKDIR /app
|
|||||||
|
|
||||||
COPY Cargo.toml Cargo.lock ./
|
COPY Cargo.toml Cargo.lock ./
|
||||||
|
|
||||||
RUN mkdir -p imphnen-backend/src imphnen-cms/src imphnen-dimentorin/src \
|
RUN mkdir -p \
|
||||||
imphnen-entities/src imphnen-gacha/src imphnen-gateway/src \
|
imphnen-backend/src \
|
||||||
imphnen-iam/src imphnen-libs/src imphnen-middleware/src \
|
imphnen-cms/src \
|
||||||
imphnen-utils/src tests/src && \
|
imphnen-dimentorin/src \
|
||||||
|
imphnen-email/src \
|
||||||
|
imphnen-entities/src \
|
||||||
|
imphnen-gacha/src \
|
||||||
|
imphnen-gateway/src \
|
||||||
|
imphnen-hackathon/src \
|
||||||
|
imphnen-iam/src \
|
||||||
|
imphnen-libs/src \
|
||||||
|
imphnen-macros/src \
|
||||||
|
imphnen-middleware/src \
|
||||||
|
imphnen-storage/src \
|
||||||
|
imphnen-utils/src && \
|
||||||
echo "fn main() {}" > imphnen-backend/src/main.rs && \
|
echo "fn main() {}" > imphnen-backend/src/main.rs && \
|
||||||
find . -name "src" -type d -exec sh -c 'echo "// dummy" > "$1/lib.rs"' _ {} \;
|
find . -name "src" -type d -exec sh -c 'touch "$1/lib.rs"' _ {} \;
|
||||||
|
|
||||||
RUN echo '[package]\nname = "tests"\nversion = "0.1.0"\nedition = "2021"' > tests/Cargo.toml
|
|
||||||
|
|
||||||
|
|
||||||
RUN echo -e '[package]\nname = "tests"\nversion = "0.1.0"\nedition = "2021"' > tests/Cargo.toml
|
|
||||||
|
|
||||||
|
|
||||||
COPY imphnen-backend ./imphnen-backend
|
COPY imphnen-backend ./imphnen-backend
|
||||||
COPY imphnen-cms ./imphnen-cms
|
COPY imphnen-cms ./imphnen-cms
|
||||||
COPY imphnen-dimentorin ./imphnen-dimentorin
|
COPY imphnen-dimentorin ./imphnen-dimentorin
|
||||||
|
COPY imphnen-email ./imphnen-email
|
||||||
COPY imphnen-entities ./imphnen-entities
|
COPY imphnen-entities ./imphnen-entities
|
||||||
COPY imphnen-gacha ./imphnen-gacha
|
COPY imphnen-gacha ./imphnen-gacha
|
||||||
COPY imphnen-gateway ./imphnen-gateway
|
COPY imphnen-gateway ./imphnen-gateway
|
||||||
|
COPY imphnen-hackathon ./imphnen-hackathon
|
||||||
COPY imphnen-iam ./imphnen-iam
|
COPY imphnen-iam ./imphnen-iam
|
||||||
COPY imphnen-libs ./imphnen-libs
|
COPY imphnen-libs ./imphnen-libs
|
||||||
|
COPY imphnen-macros ./imphnen-macros
|
||||||
COPY imphnen-middleware ./imphnen-middleware
|
COPY imphnen-middleware ./imphnen-middleware
|
||||||
|
COPY imphnen-storage ./imphnen-storage
|
||||||
COPY imphnen-utils ./imphnen-utils
|
COPY imphnen-utils ./imphnen-utils
|
||||||
COPY tests ./tests
|
|
||||||
|
|
||||||
RUN RUSTFLAGS="-C target-cpu=generic -C opt-level=s -C panic=abort -C codegen-units=1 -C strip=symbols" \
|
RUN RUSTFLAGS="-C target-cpu=generic -C opt-level=s -C panic=abort -C codegen-units=1 -C strip=symbols" \
|
||||||
cargo build -p imphnen-backend --release && \
|
cargo build -p imphnen-backend --release && \
|
||||||
|
|||||||
@@ -1,35 +0,0 @@
|
|||||||
@echo off
|
|
||||||
setlocal
|
|
||||||
|
|
||||||
:: Cek apakah file .env ada
|
|
||||||
if not exist ".env" (
|
|
||||||
echo File .env tidak ditemukan di direktori saat ini.
|
|
||||||
exit /b 1
|
|
||||||
)
|
|
||||||
|
|
||||||
echo Memuat variabel dari .env...
|
|
||||||
|
|
||||||
:: Baca file .env baris per baris
|
|
||||||
for /f "tokens=*" %%a in ('type ".env" ^| findstr /v "^$" ^| findstr /v "^#"') do (
|
|
||||||
echo.%%a | findstr "=" >nul && (
|
|
||||||
for /f "tokens=1,2 delims==" %%b in ("%%a") do (
|
|
||||||
set "key=%%b"
|
|
||||||
set "value=%%c"
|
|
||||||
:: Trim whitespace
|
|
||||||
call :trimValue key value
|
|
||||||
echo Set variabel: %%b=%%c
|
|
||||||
setx %%b %%c >nul
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
echo.
|
|
||||||
echo Semua variabel telah dimuat.
|
|
||||||
endlocal
|
|
||||||
goto :eof
|
|
||||||
|
|
||||||
:: Fungsi trim (sederhana)
|
|
||||||
:trimValue
|
|
||||||
set "%1=%[%1]%"
|
|
||||||
set "%2=%[%2]%"
|
|
||||||
goto :eof
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
function Set-TempEnvFromDotEnv {
|
|
||||||
param (
|
|
||||||
[string]$envFilePath
|
|
||||||
)
|
|
||||||
|
|
||||||
if (-Not (Test-Path $envFilePath)) {
|
|
||||||
Write-Error "The .env file at path '$envFilePath' does not exist."
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
$envContent = Get-Content $envFilePath
|
|
||||||
|
|
||||||
foreach ($line in $envContent) {
|
|
||||||
$trimmedLine = $line.Trim()
|
|
||||||
|
|
||||||
if (-Not [string]::IsNullOrWhiteSpace($trimmedLine) -and -Not $trimmedLine.StartsWith("#")) {
|
|
||||||
$keyValue = $trimmedLine -split "=", 2
|
|
||||||
if ($keyValue.Length -eq 2) {
|
|
||||||
$key = $keyValue[0].Trim()
|
|
||||||
$value = $keyValue[1].Trim()
|
|
||||||
[System.Environment]::SetEnvironmentVariable($key, $value, [System.EnvironmentVariableTarget]::Process)
|
|
||||||
Write-Host "Set temporary environment variable: $key=$value"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Write-Host "All environment variables from '$envFilePath' have been set temporarily."
|
|
||||||
}
|
|
||||||
|
|
||||||
Set-TempEnvFromDotEnv -envFilePath ".env"
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
set_temp_env_from_dotenv() {
|
|
||||||
local env_file_path="$1"
|
|
||||||
|
|
||||||
if [[ ! -f "$env_file_path" ]]; then
|
|
||||||
echo "Error: The .env file at path '$env_file_path' does not exist."
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
while IFS= read -r line || [[ -n "$line" ]]; do
|
|
||||||
trimmed_line=$(echo "$line" | xargs)
|
|
||||||
|
|
||||||
if [[ -n "$trimmed_line" && ! "$trimmed_line" =~ ^# ]]; then
|
|
||||||
key=$(echo "$trimmed_line" | cut -d '=' -f 1 | xargs)
|
|
||||||
value=$(echo "$trimmed_line" | cut -d '=' -f 2- | xargs)
|
|
||||||
export "$key=$value"
|
|
||||||
echo "Set temporary environment variable: $key=$value"
|
|
||||||
fi
|
|
||||||
done < "$env_file_path"
|
|
||||||
|
|
||||||
echo "All environment variables from '$env_file_path' have been set temporarily."
|
|
||||||
}
|
|
||||||
|
|
||||||
set_temp_env_from_dotenv ".env"
|
|
||||||
+22
-12
@@ -1,15 +1,25 @@
|
|||||||
{pkgs ? import <nixpkgs> {}}: let
|
{ pkgs ? import <nixpkgs> { } }:
|
||||||
manifest = (pkgs.lib.importTOML ./Cargo.toml).package;
|
let
|
||||||
rustDeps = pkgs.callPackage ./Cargo.nix {};
|
swaggerUi = pkgs.fetchurl {
|
||||||
packageEntry = rustDeps.workspaceMembers.${manifest.name};
|
url = "https://github.com/swagger-api/swagger-ui/archive/refs/tags/v5.17.14.zip";
|
||||||
deps = packageEntry.build.cargoDeps or null;
|
hash = "sha256-SBJE0IEgl7Efuu73n3HZQrFxYX+cn5UU5jrL4T5xzNw=";
|
||||||
|
};
|
||||||
in
|
in
|
||||||
pkgs.rustPlatform.buildRustPackage {
|
pkgs.rustPlatform.buildRustPackage {
|
||||||
pname = manifest.name;
|
pname = "imphnen-backend";
|
||||||
version = manifest.version;
|
version = (pkgs.lib.importTOML ./imphnen-backend/Cargo.toml).package.version;
|
||||||
cargoDeps = deps;
|
|
||||||
src = pkgs.lib.cleanSource ./.;
|
src = pkgs.lib.cleanSource ./.;
|
||||||
cargoLock.lockFile = ./Cargo.lock;
|
cargoLock.lockFile = ./Cargo.lock;
|
||||||
nativeBuildInputs = [pkgs.openssl pkgs.pkg-config];
|
cargoBuildFlags = [
|
||||||
buildInputs = [pkgs.openssl];
|
"--package"
|
||||||
}
|
"imphnen-backend"
|
||||||
|
"--bin"
|
||||||
|
"api"
|
||||||
|
];
|
||||||
|
nativeBuildInputs = [ pkgs.pkg-config ];
|
||||||
|
buildInputs = [ pkgs.openssl ];
|
||||||
|
preBuild = ''
|
||||||
|
export SWAGGER_UI_DOWNLOAD_URL="file://${swaggerUi}"
|
||||||
|
'';
|
||||||
|
doCheck = false;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,49 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
# Script deploy semua binary hasil build Rust (untuk Linux)
|
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
REMOTE_USER="asephs"
|
|
||||||
REMOTE_HOST="70.153.9.42"
|
|
||||||
REMOTE_PATH="/home/asephs/imphnen-backend-service"
|
|
||||||
|
|
||||||
# Build release binary
|
|
||||||
echo "🔧 Building Rust project..."
|
|
||||||
cargo build --release
|
|
||||||
|
|
||||||
# Filter hanya file executable tanpa ekstensi .exe, .rlib, atau .d
|
|
||||||
BINARIES=$(find target/release -maxdepth 1 -type f ! -name "*.exe" ! -name "*.rlib" ! -name "*.d")
|
|
||||||
|
|
||||||
if [ -z "$BINARIES" ]; then
|
|
||||||
echo "❌ Tidak ada binary Linux (.exe/.rlib/.d diabaikan)"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Upload semua binary yang valid ke server
|
|
||||||
echo "🚀 Mengirim binary ke server..."
|
|
||||||
rsync -avz --compress-level=9 --progress \
|
|
||||||
$BINARIES \
|
|
||||||
$REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH/target/release/
|
|
||||||
|
|
||||||
# Jalankan ulang service utama di VPS
|
|
||||||
echo "♻️ Restart service utama di server..."
|
|
||||||
ssh $REMOTE_USER@$REMOTE_HOST << 'EOF'
|
|
||||||
set -e
|
|
||||||
cd /home/asephs/imphnen-backend-service
|
|
||||||
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
|
|
||||||
if [ -f ~/.bashrc ]; then
|
|
||||||
source ~/.bashrc
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Pastikan semua binary bisa dieksekusi
|
|
||||||
chmod +x target/release/*
|
|
||||||
|
|
||||||
# Restart service utama (misalnya api.d)
|
|
||||||
if pm2 list | grep -q api; then
|
|
||||||
pm2 restart api --update-env
|
|
||||||
else
|
|
||||||
pm2 start target/release/api.d --name api
|
|
||||||
fi
|
|
||||||
EOF
|
|
||||||
|
|
||||||
echo "✅ Deploy semua binary Linux selesai ke $REMOTE_HOST:$REMOTE_PATH"
|
|
||||||
+9
-8
@@ -1,19 +1,20 @@
|
|||||||
{pkgs, ...}: let
|
{ pkgs, ... }:
|
||||||
|
let
|
||||||
baseImage = pkgs.ociTools.pullImage {
|
baseImage = pkgs.ociTools.pullImage {
|
||||||
imageName = "ubuntu";
|
imageName = "ubuntu";
|
||||||
tag = "latest";
|
tag = "latest";
|
||||||
};
|
};
|
||||||
in
|
in
|
||||||
pkgs.dockerTools.buildImage {
|
pkgs.dockerTools.buildImage {
|
||||||
name = "imphnen-cms-api";
|
name = "imphnen-backend-service";
|
||||||
|
|
||||||
fromImage = baseImage;
|
fromImage = baseImage;
|
||||||
|
|
||||||
copyToRoot = pkgs.buildEnv {
|
copyToRoot = pkgs.buildEnv {
|
||||||
name = "imphnen-cms-api";
|
name = "imphnen-backend-service";
|
||||||
paths = [
|
paths = [
|
||||||
(pkgs.stdenv.mkDerivation {
|
(pkgs.stdenv.mkDerivation {
|
||||||
name = "imphnen-cms-api";
|
name = "imphnen-backend-service";
|
||||||
src = ./src;
|
src = ./src;
|
||||||
|
|
||||||
buildInputs = [
|
buildInputs = [
|
||||||
@@ -29,14 +30,14 @@ in
|
|||||||
|
|
||||||
installPhase = ''
|
installPhase = ''
|
||||||
mkdir -p $out/bin
|
mkdir -p $out/bin
|
||||||
cp target/release/najm-course-api $out/bin/
|
cp target/release/imphnen-backend-service $out/bin/
|
||||||
'';
|
'';
|
||||||
})
|
})
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
config = {
|
config = {
|
||||||
Cmd = ["/bin/imphnen-cms-api"];
|
Cmd = [ "/bin/imphnen-backend-service" ];
|
||||||
WorkingDir = "/bin";
|
WorkingDir = "/bin";
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Generated
+3
-3
@@ -2,11 +2,11 @@
|
|||||||
"nodes": {
|
"nodes": {
|
||||||
"nixpkgs": {
|
"nixpkgs": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1739020877,
|
"lastModified": 1775036866,
|
||||||
"narHash": "sha256-mIvECo/NNdJJ/bXjNqIh8yeoSjVLAuDuTUzAo7dzs8Y=",
|
"narHash": "sha256-ZojAnPuCdy657PbTq5V0Y+AHKhZAIwSIT2cb8UgAz/U=",
|
||||||
"owner": "nixos",
|
"owner": "nixos",
|
||||||
"repo": "nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"rev": "a79cfe0ebd24952b580b1cf08cd906354996d547",
|
"rev": "6201e203d09599479a3b3450ed24fa81537ebc4e",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
|
|||||||
@@ -5,28 +5,43 @@
|
|||||||
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
|
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
|
||||||
};
|
};
|
||||||
|
|
||||||
outputs = {
|
outputs =
|
||||||
|
{
|
||||||
self,
|
self,
|
||||||
nixpkgs,
|
nixpkgs,
|
||||||
}: let
|
}:
|
||||||
supportedSystems = ["x86_64-linux" "x86_64-darwin" "aarch64-darwin" "aarch64-linux"];
|
let
|
||||||
pkgsFor = system:
|
supportedSystems = [
|
||||||
|
"x86_64-linux"
|
||||||
|
"x86_64-darwin"
|
||||||
|
"aarch64-darwin"
|
||||||
|
"aarch64-linux"
|
||||||
|
];
|
||||||
|
pkgsFor =
|
||||||
|
system:
|
||||||
import nixpkgs {
|
import nixpkgs {
|
||||||
inherit system;
|
inherit system;
|
||||||
config = {
|
config.allowUnfree = true;
|
||||||
allowUnfree = true;
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
|
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
|
||||||
in {
|
in
|
||||||
|
{
|
||||||
packages = forAllSystems (system: {
|
packages = forAllSystems (system: {
|
||||||
default = (pkgsFor system).callPackage ./default.nix {};
|
default = (pkgsFor system).callPackage ./default.nix { };
|
||||||
});
|
});
|
||||||
|
|
||||||
|
overlays.default = final: _prev: {
|
||||||
|
imphnen-backend = final.callPackage ./default.nix { };
|
||||||
|
};
|
||||||
|
|
||||||
|
nixosModules.backend = ./nixos-module.nix;
|
||||||
|
|
||||||
devShells = forAllSystems (system: {
|
devShells = forAllSystems (system: {
|
||||||
default = (pkgsFor system).callPackage ./shell.nix {};
|
default = (pkgsFor system).callPackage ./shell.nix { };
|
||||||
});
|
});
|
||||||
|
|
||||||
dockerImages = forAllSystems (system: {
|
dockerImages = forAllSystems (system: {
|
||||||
tryOutApi = (pkgsFor system).callPackage ./docker.nix {};
|
tryOutApi = (pkgsFor system).callPackage ./docker.nix { };
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "imphnen-backend"
|
name = "imphnen-backend"
|
||||||
version = "0.1.0"
|
version = "0.3.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
@@ -63,7 +63,6 @@ serde_json.workspace = true
|
|||||||
utoipa.workspace = true
|
utoipa.workspace = true
|
||||||
lazy_static.workspace = true
|
lazy_static.workspace = true
|
||||||
regex.workspace = true
|
regex.workspace = true
|
||||||
validator.workspace = true
|
|
||||||
axum-test.workspace = true
|
axum-test.workspace = true
|
||||||
rand.workspace = true
|
rand.workspace = true
|
||||||
tokio.workspace = true
|
tokio.workspace = true
|
||||||
|
|||||||
@@ -1,13 +1,9 @@
|
|||||||
// API entry point using PostgreSQL (SurrealDB migration complete)
|
|
||||||
// This file has been updated to use SeaORM with PostgreSQL instead of SurrealDB
|
|
||||||
use imphnen_gateway::gateway_service;
|
use imphnen_gateway::gateway_service;
|
||||||
use imphnen_libs::axum_init;
|
use imphnen_libs::axum_init;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
axum_init(|postgres_db| async {
|
axum_init(|postgres_db| async {
|
||||||
// Gateway service now uses PostgreSQL exclusively (SeaORM)
|
|
||||||
// SurrealDB dependencies have been completely removed
|
|
||||||
gateway_service(postgres_db).await
|
gateway_service(postgres_db).await
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
|||||||
@@ -1,21 +1,21 @@
|
|||||||
#![allow(clippy::all)]
|
#![allow(clippy::all)]
|
||||||
|
|
||||||
use imphnen_libs::postgres::{PostgresConfig, PostgresConnection};
|
use imphnen_libs::postgres::{PostgresConfig, PostgresConnection};
|
||||||
use sea_orm::{Statement, ConnectionTrait};
|
use sea_orm::{ConnectionTrait, Statement};
|
||||||
use std::error::Error;
|
|
||||||
use std::env;
|
use std::env;
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
let args: Vec<String> = env::args().collect();
|
let args: Vec<String> = env::args().collect();
|
||||||
// New default behavior: execute by default; use --dry-run to preview only.
|
let dry_run = args
|
||||||
let dry_run = args.iter().any(|s| s == "--dry-run" || s == "--no-exec" || s == "--dry");
|
.iter()
|
||||||
|
.any(|s| s == "--dry-run" || s == "--no-exec" || s == "--dry");
|
||||||
let force = args.iter().any(|s| s == "--force" || s == "-f");
|
let force = args.iter().any(|s| s == "--force" || s == "-f");
|
||||||
|
|
||||||
println!("🔎 Clear DB script - WARNING: This will remove data from tables\n");
|
println!("🔎 Clear DB script - WARNING: This will remove data from tables\n");
|
||||||
println!("Note: script now runs by default (no --yes required). To preview without executing, use --dry-run.\n");
|
println!("Note: script now runs by default (no --yes required). To preview without executing, use --dry-run.\n");
|
||||||
|
|
||||||
// List of tables to truncate (order doesn't matter with CASCADE)
|
|
||||||
let tables = vec![
|
let tables = vec![
|
||||||
"gacha_claims",
|
"gacha_claims",
|
||||||
"gacha_rolls",
|
"gacha_rolls",
|
||||||
@@ -37,7 +37,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
let pg_conn = PostgresConnection::new(postgres_config).await?;
|
let pg_conn = PostgresConnection::new(postgres_config).await?;
|
||||||
let db = &pg_conn.conn;
|
let db = &pg_conn.conn;
|
||||||
|
|
||||||
// Filter tables that actually exist in the database
|
|
||||||
let mut existing_tables: Vec<&str> = vec![];
|
let mut existing_tables: Vec<&str> = vec![];
|
||||||
for t in tables.iter() {
|
for t in tables.iter() {
|
||||||
let check_sql = format!(
|
let check_sql = format!(
|
||||||
@@ -65,8 +64,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
|
|
||||||
println!("The script will run the following SQL (on the DB configured by env vars):\n\n{}", truncate_sql);
|
println!("The script will run the following SQL (on the DB configured by env vars):\n\n{}", truncate_sql);
|
||||||
|
|
||||||
// Prevent accidental execution in production without explicit force flag
|
let env_name = imphnen_libs::ENV.rust_env.clone();
|
||||||
let env_name = std::env::var("RUST_ENV").unwrap_or_else(|_| "development".to_string());
|
|
||||||
if env_name == "production" && !force {
|
if env_name == "production" && !force {
|
||||||
println!("Security: RUST_ENV=production; the script will NOT run without --force. Use --force to override.");
|
println!("Security: RUST_ENV=production; the script will NOT run without --force. Use --force to override.");
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
#![allow(clippy::all)]
|
#![allow(clippy::all)]
|
||||||
|
|
||||||
use sea_orm::{ConnectionTrait, Database, Schema, DbBackend, EntityTrait};
|
|
||||||
use imphnen_libs::postgres::PostgresConfig;
|
|
||||||
use imphnen_entities::seaorm::{auth, common, gacha};
|
use imphnen_entities::seaorm::{auth, common, gacha};
|
||||||
|
use imphnen_libs::postgres::PostgresConfig;
|
||||||
use sea_orm::sea_query::Table;
|
use sea_orm::sea_query::Table;
|
||||||
|
use sea_orm::{ConnectionTrait, Database, DbBackend, EntityTrait, Schema};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
@@ -15,24 +15,39 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
|
|
||||||
println!(" Database connected. Creating/updating tables...");
|
println!(" Database connected. Creating/updating tables...");
|
||||||
|
|
||||||
// Dropping and recreating tables to ensure schema is up-to-date
|
|
||||||
// This is safer for development/testing environments to prevent schema drift.
|
|
||||||
drop_and_create_table(&db, builder, "app_roles", auth::roles::Entity).await?;
|
drop_and_create_table(&db, builder, "app_roles", auth::roles::Entity).await?;
|
||||||
drop_and_create_table(&db, builder, "app_permissions", auth::permissions::Entity).await?;
|
drop_and_create_table(&db, builder, "app_permissions", auth::permissions::Entity)
|
||||||
|
.await?;
|
||||||
drop_and_create_table(&db, builder, "app_users", auth::users::Entity).await?;
|
drop_and_create_table(&db, builder, "app_users", auth::users::Entity).await?;
|
||||||
drop_and_create_table(&db, builder, "app_roles_permissions", auth::roles_permissions::Entity).await?;
|
drop_and_create_table(
|
||||||
|
&db,
|
||||||
|
builder,
|
||||||
|
"app_roles_permissions",
|
||||||
|
auth::roles_permissions::Entity,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
drop_and_create_table(&db, builder, "app_mentors", auth::mentors::Entity).await?;
|
drop_and_create_table(&db, builder, "app_mentors", auth::mentors::Entity).await?;
|
||||||
drop_and_create_table(&db, builder, "app_sessions", auth::sessions::Entity).await?;
|
drop_and_create_table(&db, builder, "app_sessions", auth::sessions::Entity)
|
||||||
|
.await?;
|
||||||
|
drop_and_create_table(&db, builder, "app_articles", common::articles::Entity)
|
||||||
|
.await?;
|
||||||
|
|
||||||
drop_and_create_table(&db, builder, "events", common::events::Entity).await?;
|
drop_and_create_table(&db, builder, "events", common::events::Entity).await?;
|
||||||
drop_and_create_table(&db, builder, "testimonials", common::testimonials::Entity).await?;
|
drop_and_create_table(&db, builder, "testimonials", common::testimonials::Entity)
|
||||||
drop_and_create_table(&db, builder, "audit_logs", common::audit_log::Entity).await?;
|
.await?;
|
||||||
drop_and_create_table(&db, builder, "rate_limits", common::rate_limit::Entity).await?;
|
drop_and_create_table(&db, builder, "audit_logs", common::audit_log::Entity)
|
||||||
|
.await?;
|
||||||
|
drop_and_create_table(&db, builder, "rate_limits", common::rate_limit::Entity)
|
||||||
|
.await?;
|
||||||
|
|
||||||
drop_and_create_table(&db, builder, "gacha_credits", gacha::gacha_credits::Entity).await?;
|
drop_and_create_table(&db, builder, "gacha_credits", gacha::gacha_credits::Entity)
|
||||||
drop_and_create_table(&db, builder, "gacha_items", gacha::gacha_items::Entity).await?;
|
.await?;
|
||||||
drop_and_create_table(&db, builder, "gacha_rolls", gacha::gacha_rolls::Entity).await?;
|
drop_and_create_table(&db, builder, "gacha_items", gacha::gacha_items::Entity)
|
||||||
drop_and_create_table(&db, builder, "gacha_claims", gacha::gacha_claims::Entity).await?;
|
.await?;
|
||||||
|
drop_and_create_table(&db, builder, "gacha_rolls", gacha::gacha_rolls::Entity)
|
||||||
|
.await?;
|
||||||
|
drop_and_create_table(&db, builder, "gacha_claims", gacha::gacha_claims::Entity)
|
||||||
|
.await?;
|
||||||
|
|
||||||
println!("✅ Schema creation completed.");
|
println!("✅ Schema creation completed.");
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -43,22 +58,20 @@ async fn drop_and_create_table<E>(
|
|||||||
builder: DbBackend,
|
builder: DbBackend,
|
||||||
name: &str,
|
name: &str,
|
||||||
entity: E,
|
entity: E,
|
||||||
) -> Result<(), Box<dyn std::error::Error>> // Return Result
|
) -> Result<(), Box<dyn std::error::Error>>
|
||||||
where
|
where
|
||||||
E: EntityTrait,
|
E: EntityTrait,
|
||||||
{
|
{
|
||||||
let schema = Schema::new(builder);
|
let schema = Schema::new(builder);
|
||||||
|
|
||||||
// Drop table if it exists
|
let drop_stmt = Table::drop().table(entity).if_exists().cascade().to_owned();
|
||||||
let drop_stmt = Table::drop().table(entity).if_exists().cascade().to_owned(); // Added .cascade()
|
db.execute(builder.build(&drop_stmt)).await?;
|
||||||
db.execute(builder.build(&drop_stmt)).await?; // Propagate error
|
|
||||||
println!(" Dropped table if exists: {}", name);
|
println!(" Dropped table if exists: {}", name);
|
||||||
|
|
||||||
// Create table
|
|
||||||
let mut create_stmt = schema.create_table_from_entity(entity);
|
let mut create_stmt = schema.create_table_from_entity(entity);
|
||||||
create_stmt.if_not_exists();
|
create_stmt.if_not_exists();
|
||||||
|
|
||||||
db.execute(builder.build(&create_stmt)).await?; // Propagate error
|
db.execute(builder.build(&create_stmt)).await?;
|
||||||
println!(" ✅ Created table: {}", name);
|
println!(" ✅ Created table: {}", name);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ fn main() {
|
|||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
}
|
}
|
||||||
let sub = args[1].clone();
|
let sub = args[1].clone();
|
||||||
// Use sub as both sub and user_id
|
|
||||||
match encode_access_token(sub.clone(), sub.clone()) {
|
match encode_access_token(sub.clone(), sub.clone()) {
|
||||||
Ok(token) => println!("{}", token),
|
Ok(token) => println!("{}", token),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
#![allow(clippy::all)]
|
||||||
|
use chrono::Utc;
|
||||||
|
use imphnen_entities::seaorm::common::articles::{
|
||||||
|
ActiveModel as ArticleActiveModel, Entity as ArticlesEntity,
|
||||||
|
};
|
||||||
|
use imphnen_libs::postgres::PostgresConfig;
|
||||||
|
use sea_orm::{ActiveModelTrait, ActiveValue, Database};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let config = PostgresConfig::from_env()?;
|
||||||
|
let db = Database::connect(&config.database_url).await?;
|
||||||
|
|
||||||
|
let articles = vec![
|
||||||
|
("Cara Memulai Karier di UI/UX Design", "cara-memulai-karier-ui-ux-design", "UI/UX & Design", "Panduan lengkap untuk masuk ke dunia UI/UX design, dari skill yang dibutuhkan hingga portofolio.", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. UI/UX design adalah bidang yang menjanjikan. Artikel ini membahas langkah awal memulai karier sebagai UI/UX designer, tools yang wajib dikuasai seperti Figma, serta cara membangun portofolio yang menarik bagi perekrut."),
|
||||||
|
("Belajar Rust: Panduan Pemula 2026", "belajar-rust-panduan-pemula-2026", "Software/Web Dev", "Bahasa pemrograman Rust sedang naik daun. Pelajari konsep ownership dan borrow checker.", "Rust adalah bahasa pemrograman yang fokus pada performa dan keamanan memori. Dalam artikel ini kita membahas ownership, borrowing, dan cara setup environment Rust di Linux dan Windows, serta contoh project sederhana."),
|
||||||
|
("Mengenal Machine Learning untuk Data Analyst", "mengenal-machine-learning-data-analyst", "Data & AI", "Peran Data Analyst berevolusi dengan hadirnya machine learning. Simak panduannya.", "Machine learning membuka peluang besar bagi data analyst. Artikel ini menjelaskan perbedaan data analysis dan machine learning, serta roadmap belajar dari Python, pandas, sampai scikit-learn."),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (title, slug, category, excerpt, content) in articles {
|
||||||
|
let am = ArticleActiveModel {
|
||||||
|
id: ActiveValue::Set(Uuid::new_v4()),
|
||||||
|
title: ActiveValue::Set(title.to_string()),
|
||||||
|
slug: ActiveValue::Set(slug.to_string()),
|
||||||
|
category: ActiveValue::Set(category.to_string()),
|
||||||
|
excerpt: ActiveValue::Set(excerpt.to_string()),
|
||||||
|
content: ActiveValue::Set(content.to_string()),
|
||||||
|
cover_url: ActiveValue::Set(None),
|
||||||
|
author_name: ActiveValue::Set(Some("IMPHNEN Editorial".to_string())),
|
||||||
|
is_published: ActiveValue::Set(true),
|
||||||
|
created_at: ActiveValue::Set(Utc::now()),
|
||||||
|
updated_at: ActiveValue::Set(Utc::now()),
|
||||||
|
};
|
||||||
|
am.insert(&db).await?;
|
||||||
|
println!("✅ Inserted article: {}", slug);
|
||||||
|
}
|
||||||
|
println!("🟢 All articles seeded");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -1,11 +1,15 @@
|
|||||||
#![allow(clippy::all)]
|
#![allow(clippy::all)]
|
||||||
|
|
||||||
use std::error::Error;
|
use chrono::Utc;
|
||||||
|
use imphnen_entities::seaorm::common::events::{
|
||||||
|
ActiveModel as EventsActiveModel, Entity as EventEntity,
|
||||||
|
};
|
||||||
use imphnen_libs::postgres::{PostgresConfig, PostgresConnection};
|
use imphnen_libs::postgres::{PostgresConfig, PostgresConnection};
|
||||||
use imphnen_entities::seaorm::common::events::{ActiveModel as EventsActiveModel, Entity as EventEntity};
|
use sea_orm::{
|
||||||
use sea_orm::{ActiveValue::Set, ActiveModelTrait, EntityTrait, ColumnTrait, QueryFilter};
|
ActiveModelTrait, ActiveValue::Set, ColumnTrait, EntityTrait, QueryFilter,
|
||||||
|
};
|
||||||
|
use std::error::Error;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use chrono::Utc; // Removed NaiveDateTime as it was unused
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
@@ -54,7 +58,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
"2025-09-20T13:00:00Z",
|
"2025-09-20T13:00:00Z",
|
||||||
"2025-09-22T15:00:00Z",
|
"2025-09-22T15:00:00Z",
|
||||||
),
|
),
|
||||||
// Additional Events
|
|
||||||
(
|
(
|
||||||
"Rust Programming Bootcamp",
|
"Rust Programming Bootcamp",
|
||||||
"Intensive 3-day bootcamp to master Rust fundamentals and advanced concepts.",
|
"Intensive 3-day bootcamp to master Rust fundamentals and advanced concepts.",
|
||||||
@@ -154,18 +157,20 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
price,
|
price,
|
||||||
location,
|
location,
|
||||||
is_online,
|
is_online,
|
||||||
start_date_str, // Renamed to avoid conflict
|
start_date_str,
|
||||||
end_date_str, // Renamed to avoid conflict
|
end_date_str,
|
||||||
) in events
|
) in events
|
||||||
{
|
{
|
||||||
// Check if event already exists by name
|
let existing = EventEntity::find()
|
||||||
let existing = EventEntity::find().filter(<EventEntity as EntityTrait>::Column::Name.eq(name)).one(db).await?;
|
.filter(<EventEntity as EntityTrait>::Column::Name.eq(name))
|
||||||
|
.one(db)
|
||||||
|
.await?;
|
||||||
if existing.is_some() {
|
if existing.is_some() {
|
||||||
println!("ℹ️ Skipping (already exists): {name}");
|
println!("ℹ️ Skipping (already exists): {name}");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let uuid = Uuid::new_v4(); // Generate a Uuid
|
let uuid = Uuid::new_v4();
|
||||||
let mut event_model: EventsActiveModel = Default::default();
|
let mut event_model: EventsActiveModel = Default::default();
|
||||||
event_model.id = Set(uuid);
|
event_model.id = Set(uuid);
|
||||||
event_model.name = Set(name.to_string());
|
event_model.name = Set(name.to_string());
|
||||||
@@ -174,12 +179,17 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
event_model.price = Set(price);
|
event_model.price = Set(price);
|
||||||
event_model.is_online = Set(is_online);
|
event_model.is_online = Set(is_online);
|
||||||
event_model.location = Set(location.clone());
|
event_model.location = Set(location.clone());
|
||||||
event_model.start_date = Set(chrono::DateTime::parse_from_rfc3339(start_date_str)?.with_timezone(&chrono::Utc));
|
event_model.start_date = Set(
|
||||||
event_model.end_date = Set(chrono::DateTime::parse_from_rfc3339(end_date_str)?.with_timezone(&chrono::Utc));
|
chrono::DateTime::parse_from_rfc3339(start_date_str)?
|
||||||
event_model.is_deleted = Set(false); // Explicitly set is_deleted
|
.with_timezone(&chrono::Utc),
|
||||||
event_model.created_at = Set(Utc::now()); // Explicitly set created_at
|
);
|
||||||
event_model.updated_at = Set(Utc::now()); // Explicitly set updated_at
|
event_model.end_date = Set(
|
||||||
|
chrono::DateTime::parse_from_rfc3339(end_date_str)?
|
||||||
|
.with_timezone(&chrono::Utc),
|
||||||
|
);
|
||||||
|
event_model.is_deleted = Set(false);
|
||||||
|
event_model.created_at = Set(Utc::now());
|
||||||
|
event_model.updated_at = Set(Utc::now());
|
||||||
|
|
||||||
event_model.insert(db).await?;
|
event_model.insert(db).await?;
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
#![allow(clippy::all)]
|
#![allow(clippy::all)]
|
||||||
|
|
||||||
use std::error::Error;
|
|
||||||
use imphnen_libs::postgres::{PostgresConfig, PostgresConnection};
|
|
||||||
use imphnen_entities::seaorm::gacha::gacha_items::ActiveModel as GachaItemActiveModel;
|
use imphnen_entities::seaorm::gacha::gacha_items::ActiveModel as GachaItemActiveModel;
|
||||||
use imphnen_entities::seaorm::gacha::gacha_rolls::ActiveModel as GachaRollActiveModel;
|
use imphnen_entities::seaorm::gacha::gacha_rolls::ActiveModel as GachaRollActiveModel;
|
||||||
|
use imphnen_libs::postgres::{PostgresConfig, PostgresConnection};
|
||||||
use sea_orm::ActiveModelTrait;
|
use sea_orm::ActiveModelTrait;
|
||||||
use sea_orm::ActiveValue::Set;
|
use sea_orm::ActiveValue::Set;
|
||||||
use uuid::Uuid;
|
|
||||||
use sea_orm::ConnectionTrait;
|
use sea_orm::ConnectionTrait;
|
||||||
|
use std::error::Error;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
@@ -15,18 +15,25 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
let pg_conn = PostgresConnection::new(config).await?;
|
let pg_conn = PostgresConnection::new(config).await?;
|
||||||
let db = &pg_conn.conn;
|
let db = &pg_conn.conn;
|
||||||
|
|
||||||
// Check if gacha item already exists
|
let check_item_sql =
|
||||||
let check_item_sql = "SELECT id FROM app_gacha_items WHERE item_code = 'ITEM_TEST_1' LIMIT 1";
|
"SELECT id FROM app_gacha_items WHERE item_code = 'ITEM_TEST_1' LIMIT 1";
|
||||||
let item_result = pg_conn.query_one(sea_orm::Statement::from_string(db.get_database_backend(), check_item_sql)).await?;
|
let item_result = pg_conn
|
||||||
|
.query_one(sea_orm::Statement::from_string(
|
||||||
|
db.get_database_backend(),
|
||||||
|
check_item_sql,
|
||||||
|
))
|
||||||
|
.await?;
|
||||||
let gacha_item_uuid = if let Some(ref row) = item_result {
|
let gacha_item_uuid = if let Some(ref row) = item_result {
|
||||||
// Item exists, get its ID
|
|
||||||
row.try_get("", "id")?
|
row.try_get("", "id")?
|
||||||
} else {
|
} else {
|
||||||
// Item doesn't exist, create it
|
let _ = pg_conn
|
||||||
// Note: We can't easily delete by a fixed ID since it's a UUID, but the insert will fail if there's a conflict
|
.execute(sea_orm::Statement::from_string(
|
||||||
let _ = pg_conn.execute(sea_orm::Statement::from_string(db.get_database_backend(), "DELETE FROM app_gacha_items WHERE item_code = 'ITEM_TEST_1'".to_string())).await.ok();
|
db.get_database_backend(),
|
||||||
|
"DELETE FROM app_gacha_items WHERE item_code = 'ITEM_TEST_1'".to_string(),
|
||||||
|
))
|
||||||
|
.await
|
||||||
|
.ok();
|
||||||
|
|
||||||
// Create gacha item via SeaORM
|
|
||||||
let new_uuid = Uuid::new_v4();
|
let new_uuid = Uuid::new_v4();
|
||||||
let mut item_model: GachaItemActiveModel = Default::default();
|
let mut item_model: GachaItemActiveModel = Default::default();
|
||||||
item_model.id = Set(new_uuid);
|
item_model.id = Set(new_uuid);
|
||||||
@@ -47,7 +54,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
new_uuid
|
new_uuid
|
||||||
};
|
};
|
||||||
|
|
||||||
// Always try to insert the roll, relying on the database constraints to prevent duplicates if needed
|
|
||||||
let gacha_roll_id = Uuid::new_v4();
|
let gacha_roll_id = Uuid::new_v4();
|
||||||
let mut roll_model: GachaRollActiveModel = Default::default();
|
let mut roll_model: GachaRollActiveModel = Default::default();
|
||||||
roll_model.id = Set(gacha_roll_id);
|
roll_model.id = Set(gacha_roll_id);
|
||||||
|
|||||||
@@ -1,13 +1,18 @@
|
|||||||
#![allow(clippy::all)]
|
#![allow(clippy::all)]
|
||||||
|
|
||||||
|
use imphnen_entities::seaorm::auth::mentors::ActiveModel as MentorsActiveModel;
|
||||||
|
use imphnen_entities::seaorm::auth::roles::{
|
||||||
|
Column as RoleColumn, Entity as RoleEntity,
|
||||||
|
};
|
||||||
|
use imphnen_entities::seaorm::auth::users::ActiveModel as UsersActiveModel;
|
||||||
use imphnen_libs::hash_password;
|
use imphnen_libs::hash_password;
|
||||||
|
use imphnen_libs::postgres::{PostgresConfig, PostgresConnection};
|
||||||
|
use sea_orm::{
|
||||||
|
ActiveModelTrait, ActiveValue::Set, ColumnTrait, ConnectionTrait, EntityTrait,
|
||||||
|
QueryFilter,
|
||||||
|
};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use imphnen_libs::postgres::{PostgresConfig, PostgresConnection};
|
|
||||||
use imphnen_entities::seaorm::auth::users::ActiveModel as UsersActiveModel;
|
|
||||||
use imphnen_entities::seaorm::auth::mentors::ActiveModel as MentorsActiveModel;
|
|
||||||
use imphnen_entities::seaorm::auth::roles::{Entity as RoleEntity, Column as RoleColumn};
|
|
||||||
use sea_orm::{ActiveModelTrait, ConnectionTrait, ActiveValue::Set, EntityTrait, QueryFilter, ColumnTrait};
|
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
@@ -16,17 +21,28 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
let pg_conn = PostgresConnection::new(config).await?;
|
let pg_conn = PostgresConnection::new(config).await?;
|
||||||
let db = &pg_conn.conn;
|
let db = &pg_conn.conn;
|
||||||
|
|
||||||
let _ = pg_conn.execute(sea_orm::Statement::from_string(db.get_database_backend(), "DELETE FROM app_mentors WHERE id = 'e6f78d23-83bf-5c2b-bcd4-001345678901'".to_string())).await.ok();
|
let _ = pg_conn
|
||||||
let _ = pg_conn.execute(sea_orm::Statement::from_string(db.get_database_backend(), "DELETE FROM app_users WHERE email = 'mentor@example.com'".to_string())).await.ok();
|
.execute(sea_orm::Statement::from_string(
|
||||||
|
db.get_database_backend(),
|
||||||
|
"DELETE FROM app_mentors WHERE id = 'e6f78d23-83bf-5c2b-bcd4-001345678901'"
|
||||||
|
.to_string(),
|
||||||
|
))
|
||||||
|
.await
|
||||||
|
.ok();
|
||||||
|
let _ = pg_conn
|
||||||
|
.execute(sea_orm::Statement::from_string(
|
||||||
|
db.get_database_backend(),
|
||||||
|
"DELETE FROM app_users WHERE email = 'mentor@example.com'".to_string(),
|
||||||
|
))
|
||||||
|
.await
|
||||||
|
.ok();
|
||||||
|
|
||||||
// Find Mentor role
|
|
||||||
let role = RoleEntity::find()
|
let role = RoleEntity::find()
|
||||||
.filter(RoleColumn::Name.eq("Mentor"))
|
.filter(RoleColumn::Name.eq("Mentor"))
|
||||||
.one(db)
|
.one(db)
|
||||||
.await?
|
.await?
|
||||||
.ok_or("Role 'Mentor' not found")?;
|
.ok_or("Role 'Mentor' not found")?;
|
||||||
|
|
||||||
// Insert user with Mentor role
|
|
||||||
let user_id = Uuid::new_v4();
|
let user_id = Uuid::new_v4();
|
||||||
let mut user_model: UsersActiveModel = Default::default();
|
let mut user_model: UsersActiveModel = Default::default();
|
||||||
user_model.id = Set(user_id);
|
user_model.id = Set(user_id);
|
||||||
@@ -43,21 +59,23 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
user_model.updated_at = Set(chrono::Utc::now());
|
user_model.updated_at = Set(chrono::Utc::now());
|
||||||
user_model.insert(db).await?;
|
user_model.insert(db).await?;
|
||||||
|
|
||||||
// Insert mentor
|
|
||||||
let mentor_id = Uuid::new_v4();
|
let mentor_id = Uuid::new_v4();
|
||||||
let mut mentor_model: MentorsActiveModel = Default::default();
|
let mut mentor_model: MentorsActiveModel = Default::default();
|
||||||
mentor_model.id = Set(mentor_id);
|
mentor_model.id = Set(mentor_id);
|
||||||
mentor_model.user_id = Set(user_id);
|
mentor_model.user_id = Set(user_id);
|
||||||
mentor_model.industries = Set(Some(json!( ["Software", "Education"] )));
|
mentor_model.industries = Set(Some(json!(["Software", "Education"])));
|
||||||
mentor_model.expertise = Set(Some(json!( ["Rust", "Microservices"] )));
|
mentor_model.expertise = Set(Some(json!(["Rust", "Microservices"])));
|
||||||
mentor_model.languages = Set(Some(json!( ["Indonesian", "English"] )));
|
mentor_model.languages = Set(Some(json!(["Indonesian", "English"])));
|
||||||
mentor_model.current_company = Set(Some("PT Contoh".to_string()));
|
mentor_model.current_company = Set(Some("PT Contoh".to_string()));
|
||||||
mentor_model.current_role = Set(Some("Senior Backend Engineer".to_string()));
|
mentor_model.current_role = Set(Some("Senior Backend Engineer".to_string()));
|
||||||
mentor_model.years_of_experience = Set(Some(5));
|
mentor_model.years_of_experience = Set(Some(5));
|
||||||
mentor_model.topics_of_interest = Set(Some(json!( ["Rust Programming", "Backend Development"] )));
|
mentor_model.topics_of_interest =
|
||||||
|
Set(Some(json!(["Rust Programming", "Backend Development"])));
|
||||||
mentor_model.preferred_mentee_level = Set(Some("beginner".to_string()));
|
mentor_model.preferred_mentee_level = Set(Some("beginner".to_string()));
|
||||||
mentor_model.preferred_mentoring_formats = Set(Some(json!( ["online", "offline"] )));
|
mentor_model.preferred_mentoring_formats = Set(Some(json!(["online", "offline"])));
|
||||||
mentor_model.availability_commitment = Set(Some("2 jam per minggu untuk mentoring online dan offline".to_string()));
|
mentor_model.availability_commitment = Set(Some(
|
||||||
|
"2 jam per minggu untuk mentoring online dan offline".to_string(),
|
||||||
|
));
|
||||||
mentor_model.mentoring_rate = Set(Some(100000.0));
|
mentor_model.mentoring_rate = Set(Some(100000.0));
|
||||||
mentor_model.status = Set(Some("verified".to_string()));
|
mentor_model.status = Set(Some("verified".to_string()));
|
||||||
mentor_model.is_deleted = Set(false);
|
mentor_model.is_deleted = Set(false);
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
#![allow(clippy::all)]
|
#![allow(clippy::all)]
|
||||||
|
|
||||||
use imphnen_iam::PermissionsEnum;
|
use chrono::Utc;
|
||||||
use std::error::Error;
|
|
||||||
use imphnen_libs::postgres::{PostgresConfig, PostgresConnection};
|
|
||||||
use imphnen_entities::seaorm::auth::permissions::ActiveModel as PermissionActiveModel;
|
use imphnen_entities::seaorm::auth::permissions::ActiveModel as PermissionActiveModel;
|
||||||
use imphnen_entities::seaorm::auth::permissions::Entity as PermissionEntity;
|
use imphnen_entities::seaorm::auth::permissions::Entity as PermissionEntity;
|
||||||
|
use imphnen_iam::PermissionsEnum;
|
||||||
|
use imphnen_libs::postgres::{PostgresConfig, PostgresConnection};
|
||||||
|
use sea_orm::ActiveModelTrait;
|
||||||
use sea_orm::ActiveValue::Set;
|
use sea_orm::ActiveValue::Set;
|
||||||
use sea_orm::{ActiveModelTrait};
|
use std::error::Error;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use chrono::Utc;
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
@@ -54,17 +54,15 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
PermissionsEnum::DeleteMentors,
|
PermissionsEnum::DeleteMentors,
|
||||||
PermissionsEnum::Administrator,
|
PermissionsEnum::Administrator,
|
||||||
] {
|
] {
|
||||||
// permission.id() returns a string, try parse to uuid
|
let parsed_id =
|
||||||
let parsed_id = Uuid::parse_str(&permission.id()).unwrap_or_else(|_| Uuid::new_v4());
|
Uuid::parse_str(&permission.id()).unwrap_or_else(|_| Uuid::new_v4());
|
||||||
|
|
||||||
// Check if permission already exists
|
|
||||||
let existing = PermissionEntity::find_by_id(parsed_id).one(db).await?;
|
let existing = PermissionEntity::find_by_id(parsed_id).one(db).await?;
|
||||||
if existing.is_some() {
|
if existing.is_some() {
|
||||||
println!("ℹ️ Skipping (already exists): {permission}");
|
println!("ℹ️ Skipping (already exists): {permission}");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert permission using active model
|
|
||||||
let mut perm_model: PermissionActiveModel = Default::default();
|
let mut perm_model: PermissionActiveModel = Default::default();
|
||||||
perm_model.id = Set(parsed_id);
|
perm_model.id = Set(parsed_id);
|
||||||
perm_model.name = Set(permission.to_string());
|
perm_model.name = Set(permission.to_string());
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
use std::error::Error;
|
use chrono::Utc;
|
||||||
|
use imphnen_entities::seaorm::auth::roles::{Entity as RoleEntity, RoleBuilder};
|
||||||
use imphnen_libs::postgres::{PostgresConfig, PostgresConnection};
|
use imphnen_libs::postgres::{PostgresConfig, PostgresConnection};
|
||||||
use imphnen_entities::seaorm::auth::roles::{RoleBuilder, Entity as RoleEntity};
|
|
||||||
use sea_orm::{ActiveModelTrait, ActiveValue::Set, EntityTrait};
|
use sea_orm::{ActiveModelTrait, ActiveValue::Set, EntityTrait};
|
||||||
|
use std::error::Error;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use chrono::Utc; // Added chrono
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
@@ -50,19 +50,15 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
),
|
),
|
||||||
];
|
];
|
||||||
|
|
||||||
for (id, name, _created_at_str, _updated_at_str) in roles { // Renamed to avoid conflict
|
for (id, name, _created_at_str, _updated_at_str) in roles {
|
||||||
let uuid = Uuid::parse_str(id).unwrap_or_else(|_| Uuid::new_v4());
|
let uuid = Uuid::parse_str(id).unwrap_or_else(|_| Uuid::new_v4());
|
||||||
|
|
||||||
// Check if role already exists
|
|
||||||
let existing = RoleEntity::find_by_id(uuid).one(db).await?;
|
let existing = RoleEntity::find_by_id(uuid).one(db).await?;
|
||||||
if existing.is_some() {
|
if existing.is_some() {
|
||||||
println!("ℹ️ Skipping (already exists): {name}");
|
println!("ℹ️ Skipping (already exists): {name}");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete existing by id to avoid duplicates (original logic, replaced by existence check)
|
|
||||||
// let _ = pg_conn.execute(sea_orm::Statement::from_string(db.get_database_backend(), format!("DELETE FROM app_roles WHERE id = '{}'", uuid))).await.ok();
|
|
||||||
|
|
||||||
let role_model = RoleBuilder::new()
|
let role_model = RoleBuilder::new()
|
||||||
.name(name.to_string())
|
.name(name.to_string())
|
||||||
.description("System generated role".to_string())
|
.description("System generated role".to_string())
|
||||||
@@ -71,9 +67,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
.build()?;
|
.build()?;
|
||||||
let mut role_model = role_model;
|
let mut role_model = role_model;
|
||||||
role_model.id = Set(uuid);
|
role_model.id = Set(uuid);
|
||||||
role_model.is_system_role = Set(true); // Set the missing field
|
role_model.is_system_role = Set(true);
|
||||||
role_model.created_at = Set(Utc::now()); // Set created_at
|
role_model.created_at = Set(Utc::now());
|
||||||
role_model.updated_at = Set(Utc::now()); // Set updated_at
|
role_model.updated_at = Set(Utc::now());
|
||||||
|
|
||||||
role_model.insert(db).await?;
|
role_model.insert(db).await?;
|
||||||
println!("✅ Inserted role: {name}");
|
println!("✅ Inserted role: {name}");
|
||||||
|
|||||||
@@ -1,35 +1,32 @@
|
|||||||
use imphnen_iam::PermissionsEnum;
|
|
||||||
use std::error::Error;
|
|
||||||
use imphnen_libs::postgres::{PostgresConfig, PostgresConnection};
|
|
||||||
use imphnen_entities::seaorm::auth::roles::Entity as RolesEntity;
|
|
||||||
use imphnen_entities::seaorm::auth::roles::ActiveModel as RoleActiveModel;
|
use imphnen_entities::seaorm::auth::roles::ActiveModel as RoleActiveModel;
|
||||||
|
use imphnen_entities::seaorm::auth::roles::Entity as RolesEntity;
|
||||||
|
use imphnen_iam::PermissionsEnum;
|
||||||
|
use imphnen_libs::postgres::{PostgresConfig, PostgresConnection};
|
||||||
|
use sea_orm::ActiveModelTrait;
|
||||||
use sea_orm::ActiveValue::Set;
|
use sea_orm::ActiveValue::Set;
|
||||||
use sea_orm::EntityTrait;
|
use sea_orm::EntityTrait;
|
||||||
use sea_orm::ActiveModelTrait;
|
|
||||||
use uuid::Uuid;
|
|
||||||
use serde_json::Value as JsonValue;
|
use serde_json::Value as JsonValue;
|
||||||
|
use std::error::Error;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
let config = PostgresConfig::from_env()?;
|
let config = PostgresConfig::from_env()?;
|
||||||
let pg_conn = PostgresConnection::new(config).await?;
|
let pg_conn = PostgresConnection::new(config).await?;
|
||||||
let db = &pg_conn.conn;
|
let db = &pg_conn.conn;
|
||||||
// Ensure indexes are present if needed (placeholders) - we don't modify schema here
|
println!(
|
||||||
|
"✅ Index 'user_email_index' defined on table 'users' for column 'email'."
|
||||||
println!("✅ Index 'user_email_index' defined on table 'users' for column 'email'.");
|
);
|
||||||
|
|
||||||
let roles_permissions = vec![
|
let roles_permissions = vec![
|
||||||
(
|
(
|
||||||
"f6b03f25-e416-4893-ac88-caaa690afb07",
|
"f6b03f25-e416-4893-ac88-caaa690afb07",
|
||||||
vec![
|
vec![PermissionsEnum::Administrator],
|
||||||
// Only Administrator permission - grants access to everything
|
|
||||||
PermissionsEnum::Administrator,
|
|
||||||
],
|
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"3b9f8c4e-6a2d-4f8a-9a12-2d6f8b3c4e5a",
|
"3b9f8c4e-6a2d-4f8a-9a12-2d6f8b3c4e5a",
|
||||||
vec![
|
vec![
|
||||||
PermissionsEnum::ReadListUsers, // Added ReadListUsers permission
|
PermissionsEnum::ReadListUsers,
|
||||||
PermissionsEnum::ReadOwnMentorProfile,
|
PermissionsEnum::ReadOwnMentorProfile,
|
||||||
PermissionsEnum::UpdateOwnMentorProfile,
|
PermissionsEnum::UpdateOwnMentorProfile,
|
||||||
PermissionsEnum::ReadOwnMentorStatus,
|
PermissionsEnum::ReadOwnMentorStatus,
|
||||||
@@ -64,7 +61,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
(
|
(
|
||||||
"50133429-f4b1-4249-9f97-7b86e6ee9d86",
|
"50133429-f4b1-4249-9f97-7b86e6ee9d86",
|
||||||
vec![
|
vec![
|
||||||
// Staff should be able to list roles and permissions in tests
|
|
||||||
PermissionsEnum::ReadListRoles,
|
PermissionsEnum::ReadListRoles,
|
||||||
PermissionsEnum::ReadListPermissions,
|
PermissionsEnum::ReadListPermissions,
|
||||||
PermissionsEnum::ReadListUsers,
|
PermissionsEnum::ReadListUsers,
|
||||||
@@ -91,12 +87,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
|
|
||||||
for (role_id, permissions) in roles_permissions {
|
for (role_id, permissions) in roles_permissions {
|
||||||
let role_uuid = Uuid::parse_str(role_id).unwrap_or_else(|_| Uuid::new_v4());
|
let role_uuid = Uuid::parse_str(role_id).unwrap_or_else(|_| Uuid::new_v4());
|
||||||
// Map permissions enum to JSON array of permission ids
|
|
||||||
let json_permissions = JsonValue::Array(
|
let json_permissions = JsonValue::Array(
|
||||||
permissions.iter().map(|p| JsonValue::String(p.id())).collect()
|
permissions
|
||||||
|
.iter()
|
||||||
|
.map(|p| JsonValue::String(p.id()))
|
||||||
|
.collect(),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Find role and update permissions
|
|
||||||
if let Some(role_model) = RolesEntity::find_by_id(role_uuid).one(db).await? {
|
if let Some(role_model) = RolesEntity::find_by_id(role_uuid).one(db).await? {
|
||||||
let mut am: RoleActiveModel = role_model.into();
|
let mut am: RoleActiveModel = role_model.into();
|
||||||
am.permissions = Set(Some(json_permissions));
|
am.permissions = Set(Some(json_permissions));
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
#![allow(clippy::all)]
|
#![allow(clippy::all)]
|
||||||
|
|
||||||
use std::error::Error;
|
use chrono::Utc;
|
||||||
use imphnen_libs::postgres::{PostgresConfig, PostgresConnection};
|
use imphnen_entities::seaorm::auth::mentors::ActiveModel as MentorsActiveModel;
|
||||||
use imphnen_entities::seaorm::common::events::ActiveModel as EventsActiveModel;
|
use imphnen_entities::seaorm::common::events::ActiveModel as EventsActiveModel;
|
||||||
use imphnen_entities::seaorm::common::testimonials::ActiveModel as TestimonialsActiveModel;
|
use imphnen_entities::seaorm::common::testimonials::ActiveModel as TestimonialsActiveModel;
|
||||||
use imphnen_entities::seaorm::auth::mentors::ActiveModel as MentorsActiveModel;
|
use imphnen_libs::postgres::{PostgresConfig, PostgresConnection};
|
||||||
use sea_orm::ActiveValue::Set;
|
|
||||||
use sea_orm::ActiveModelTrait;
|
use sea_orm::ActiveModelTrait;
|
||||||
use uuid::Uuid;
|
use sea_orm::ActiveValue::Set;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use chrono::Utc;
|
use std::error::Error;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
@@ -17,7 +17,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
let pg_conn = PostgresConnection::new(config).await?;
|
let pg_conn = PostgresConnection::new(config).await?;
|
||||||
let db = &pg_conn.conn;
|
let db = &pg_conn.conn;
|
||||||
|
|
||||||
// Seed Events - handle existing data
|
|
||||||
let uuid = Uuid::new_v4().to_string();
|
let uuid = Uuid::new_v4().to_string();
|
||||||
let mut event_model: EventsActiveModel = Default::default();
|
let mut event_model: EventsActiveModel = Default::default();
|
||||||
event_model.id = Set(Uuid::parse_str(&uuid)?);
|
event_model.id = Set(Uuid::parse_str(&uuid)?);
|
||||||
@@ -32,43 +31,46 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
event_model.is_deleted = Set(false);
|
event_model.is_deleted = Set(false);
|
||||||
match event_model.insert(db).await {
|
match event_model.insert(db).await {
|
||||||
Ok(_) => println!("✅ Inserted test event"),
|
Ok(_) => println!("✅ Inserted test event"),
|
||||||
Err(_) => println!("⚠️ Test event already exists or could not be inserted, skipping"),
|
Err(_) => {
|
||||||
|
println!("⚠️ Test event already exists or could not be inserted, skipping")
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Seed Testimonials - handle existing data
|
|
||||||
let mut testimonial_model: TestimonialsActiveModel = Default::default();
|
let mut testimonial_model: TestimonialsActiveModel = Default::default();
|
||||||
testimonial_model.id = Set(Uuid::parse_str("00000000-0000-0000-0000-000000000001")?);
|
testimonial_model.id =
|
||||||
testimonial_model.user_id = Set(Uuid::parse_str("c3b1d6a8-8d4f-4b36-b789-2e532ec7a7b2")?);
|
Set(Uuid::parse_str("00000000-0000-0000-0000-000000000001")?);
|
||||||
|
testimonial_model.user_id =
|
||||||
|
Set(Uuid::parse_str("c3b1d6a8-8d4f-4b36-b789-2e532ec7a7b2")?);
|
||||||
testimonial_model.role = Set("Student".to_string());
|
testimonial_model.role = Set("Student".to_string());
|
||||||
testimonial_model.content = Set("This is a great platform!".to_string());
|
testimonial_model.content = Set("This is a great platform!".to_string());
|
||||||
testimonial_model.is_deleted = Set(false);
|
testimonial_model.is_deleted = Set(false);
|
||||||
match testimonial_model.insert(db).await {
|
match testimonial_model.insert(db).await {
|
||||||
Ok(_) => println!("✅ Inserted test testimonial"),
|
Ok(_) => println!("✅ Inserted test testimonial"),
|
||||||
Err(_) => println!("⚠️ Test testimonial already exists or could not be inserted, skipping"),
|
Err(_) => println!(
|
||||||
|
"⚠️ Test testimonial already exists or could not be inserted, skipping"
|
||||||
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Seed Mentor - handle existing data
|
|
||||||
let mentor_id = Uuid::new_v4();
|
let mentor_id = Uuid::new_v4();
|
||||||
let mut mentor_model: MentorsActiveModel = Default::default();
|
let mut mentor_model: MentorsActiveModel = Default::default();
|
||||||
mentor_model.id = Set(mentor_id);
|
mentor_model.id = Set(mentor_id);
|
||||||
// Use the admin user ID instead of a random one
|
mentor_model.user_id =
|
||||||
mentor_model.user_id = Set(Uuid::parse_str("c3b1d6a8-8d4f-4b36-b789-2e532ec7a7b2")?);
|
Set(Uuid::parse_str("c3b1d6a8-8d4f-4b36-b789-2e532ec7a7b2")?);
|
||||||
mentor_model.industries = Set(Some(json!( ["Technology", "Education"] )));
|
mentor_model.industries = Set(Some(json!(["Technology", "Education"])));
|
||||||
mentor_model.expertise = Set(Some(json!( ["Software Development"] )));
|
mentor_model.expertise = Set(Some(json!(["Software Development"])));
|
||||||
mentor_model.languages = Set(Some(json!( ["English", "Indonesian"] )));
|
mentor_model.languages = Set(Some(json!(["English", "Indonesian"])));
|
||||||
mentor_model.current_company = Set(Some("Tech Corp".to_string()));
|
mentor_model.current_company = Set(Some("Tech Corp".to_string()));
|
||||||
mentor_model.current_role = Set(Some("Senior Engineer".to_string()));
|
mentor_model.current_role = Set(Some("Senior Engineer".to_string()));
|
||||||
mentor_model.years_of_experience = Set(Some(5));
|
mentor_model.years_of_experience = Set(Some(5));
|
||||||
mentor_model.topics_of_interest = Set(Some(json!( ["Rust", "Web Development"] )));
|
mentor_model.topics_of_interest = Set(Some(json!(["Rust", "Web Development"])));
|
||||||
mentor_model.preferred_mentee_level = Set(Some("Beginner".to_string()));
|
mentor_model.preferred_mentee_level = Set(Some("Beginner".to_string()));
|
||||||
mentor_model.preferred_mentoring_formats = Set(Some(json!( ["1:1", "Group"] )));
|
mentor_model.preferred_mentoring_formats = Set(Some(json!(["1:1", "Group"])));
|
||||||
mentor_model.availability_commitment = Set(Some("Weekly".to_string()));
|
mentor_model.availability_commitment = Set(Some("Weekly".to_string()));
|
||||||
mentor_model.mentoring_rate = Set(Some(100.0));
|
mentor_model.mentoring_rate = Set(Some(100.0));
|
||||||
mentor_model.status = Set(Some("active".to_string()));
|
mentor_model.status = Set(Some("active".to_string()));
|
||||||
mentor_model.is_deleted = Set(false);
|
mentor_model.is_deleted = Set(false);
|
||||||
mentor_model.created_at = Set(chrono::Utc::now());
|
mentor_model.created_at = Set(chrono::Utc::now());
|
||||||
mentor_model.updated_at = Set(chrono::Utc::now());
|
mentor_model.updated_at = Set(chrono::Utc::now());
|
||||||
// Create mentor record via SeaORM active model
|
|
||||||
mentor_model.insert(db).await?;
|
mentor_model.insert(db).await?;
|
||||||
println!("✅ Inserted test mentor via SeaORM");
|
println!("✅ Inserted test mentor via SeaORM");
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
#![allow(clippy::all)]
|
#![allow(clippy::all)]
|
||||||
|
|
||||||
|
use imphnen_entities::seaorm::auth::users::ActiveModel as UsersActiveModel;
|
||||||
|
use imphnen_entities::seaorm::auth::users::Entity as UserEntity;
|
||||||
use imphnen_libs::hash_password;
|
use imphnen_libs::hash_password;
|
||||||
use imphnen_libs::postgres::{PostgresConfig, PostgresConnection};
|
use imphnen_libs::postgres::{PostgresConfig, PostgresConnection};
|
||||||
use imphnen_entities::seaorm::auth::users::Entity as UserEntity; // Added for dynamic role lookup
|
|
||||||
use imphnen_entities::seaorm::auth::users::ActiveModel as UsersActiveModel;
|
|
||||||
|
|
||||||
use sea_orm::{ActiveModelTrait, ActiveValue::Set, EntityTrait, IntoActiveModel};
|
|
||||||
use uuid::Uuid;
|
|
||||||
use std::error::Error;
|
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
|
use sea_orm::{ActiveModelTrait, ActiveValue::Set, EntityTrait, IntoActiveModel};
|
||||||
|
use std::error::Error;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
@@ -39,7 +39,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
"665a3cfc-ea5f-4bcd-8769-4a6d8d1451d4",
|
"665a3cfc-ea5f-4bcd-8769-4a6d8d1451d4",
|
||||||
"testuser1@example.com",
|
"testuser1@example.com",
|
||||||
"Test User 1",
|
"Test User 1",
|
||||||
"5713cb37-dc02-4e87-8048-d7a41d352059", // Fixed UUID
|
"5713cb37-dc02-4e87-8048-d7a41d352059",
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"3972c139-a450-416c-93b0-c42539dc780f",
|
"3972c139-a450-416c-93b0-c42539dc780f",
|
||||||
@@ -51,9 +51,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
"b426c0a9-0efb-4e26-b078-4f18767255f3",
|
"b426c0a9-0efb-4e26-b078-4f18767255f3",
|
||||||
"testuser3@example.com",
|
"testuser3@example.com",
|
||||||
"Test User 3",
|
"Test User 3",
|
||||||
"5713cb37-dc02-4e87-8048-d7a41d352059", // Fixed UUID
|
"5713cb37-dc02-4e87-8048-d7a41d352059",
|
||||||
),
|
),
|
||||||
// Additional Users for Volume and Variety
|
|
||||||
(
|
(
|
||||||
"11111111-1111-1111-1111-111111111111",
|
"11111111-1111-1111-1111-111111111111",
|
||||||
"user4@example.com",
|
"user4@example.com",
|
||||||
@@ -70,13 +69,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
"33333333-3333-3333-3333-333333333333",
|
"33333333-3333-3333-3333-333333333333",
|
||||||
"mentor2@example.com",
|
"mentor2@example.com",
|
||||||
"Mentor Two",
|
"Mentor Two",
|
||||||
"3b9f8c4e-6a2d-4f8a-9a12-2d6f8b3c4e5a", // Mentor Role
|
"3b9f8c4e-6a2d-4f8a-9a12-2d6f8b3c4e5a",
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"44444444-4444-4444-4444-444444444444",
|
"44444444-4444-4444-4444-444444444444",
|
||||||
"staff2@example.com",
|
"staff2@example.com",
|
||||||
"Staff Two",
|
"Staff Two",
|
||||||
"50133429-f4b1-4249-9f97-7b86e6ee9d86", // Staff Role
|
"50133429-f4b1-4249-9f97-7b86e6ee9d86",
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"55555555-5555-5555-5555-555555555555",
|
"55555555-5555-5555-5555-555555555555",
|
||||||
@@ -110,21 +109,25 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
),
|
),
|
||||||
];
|
];
|
||||||
|
|
||||||
for (id, email, fullname, role_id_str) in users { // role_id_str directly contains UUID
|
for (id, email, fullname, role_id_str) in users {
|
||||||
let role_uuid = Some(Uuid::parse_str(role_id_str)
|
let role_uuid = Some(
|
||||||
.map_err(|e| format!("Invalid UUID for role: {role_id_str} - {e}"))?);
|
Uuid::parse_str(role_id_str)
|
||||||
|
.map_err(|e| format!("Invalid UUID for role: {role_id_str} - {e}"))?,
|
||||||
|
);
|
||||||
|
|
||||||
// Build SeaORM ActiveModel for users
|
let uid = Uuid::parse_str(id)?;
|
||||||
let uid = Uuid::parse_str(id)?; // Should always be valid UUID strings from test data
|
|
||||||
|
|
||||||
let names: Vec<&str> = fullname.split_whitespace().collect();
|
let names: Vec<&str> = fullname.split_whitespace().collect();
|
||||||
let first_name = names.first().map(|s| s.to_string());
|
let first_name = names.first().map(|s| s.to_string());
|
||||||
let last_name = if names.len() > 1 { Some(names[1..].join(" ")) } else { None };
|
let last_name = if names.len() > 1 {
|
||||||
|
Some(names[1..].join(" "))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
let password = "password";
|
let password = "password";
|
||||||
let hashed = hash_password(password).unwrap();
|
let hashed = hash_password(password).unwrap();
|
||||||
|
|
||||||
// Explicit Upsert Logic
|
|
||||||
let existing_user = UserEntity::find_by_id(uid).one(db).await?;
|
let existing_user = UserEntity::find_by_id(uid).one(db).await?;
|
||||||
let is_update = existing_user.is_some();
|
let is_update = existing_user.is_some();
|
||||||
|
|
||||||
|
|||||||
@@ -1,28 +1,34 @@
|
|||||||
//! PostgreSQL Connection Test Program
|
|
||||||
//! This program tests the PostgreSQL integration with SeaORM
|
|
||||||
|
|
||||||
use std::sync::Arc;
|
|
||||||
use imphnen_libs::postgres::{PostgresConfig, PostgresConnection, PostgresError};
|
|
||||||
use imphnen_entities::seaorm::auth::users::{Entity as UsersEntity, Model as UserModel};
|
|
||||||
use imphnen_entities::seaorm::auth::roles::{Entity as RolesEntity, Model as RoleModel};
|
|
||||||
use sea_orm::{EntityTrait, ActiveModelTrait, Set, TransactionTrait, DbErr, PaginatorTrait};
|
|
||||||
use uuid::Uuid;
|
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
|
use imphnen_entities::seaorm::auth::roles::{
|
||||||
|
Entity as RolesEntity, Model as RoleModel,
|
||||||
|
};
|
||||||
|
use imphnen_entities::seaorm::auth::users::{
|
||||||
|
Entity as UsersEntity, Model as UserModel,
|
||||||
|
};
|
||||||
|
use imphnen_libs::postgres::{PostgresConfig, PostgresConnection, PostgresError};
|
||||||
|
use sea_orm::{
|
||||||
|
ActiveModelTrait, DbErr, EntityTrait, PaginatorTrait, Set, TransactionTrait,
|
||||||
|
};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
println!("🚀 Starting PostgreSQL Connection Test");
|
println!("🚀 Starting PostgreSQL Connection Test");
|
||||||
println!("=====================================");
|
println!("=====================================");
|
||||||
|
|
||||||
// Load configuration from environment
|
|
||||||
let config = PostgresConfig::from_env()?;
|
let config = PostgresConfig::from_env()?;
|
||||||
println!("✅ Configuration loaded successfully");
|
println!("✅ Configuration loaded successfully");
|
||||||
println!(" Database URL: {}", config.database_url.replace("postgres://", "postgres://****:****@"));
|
println!(
|
||||||
|
" Database URL: {}",
|
||||||
|
config
|
||||||
|
.database_url
|
||||||
|
.replace("postgres://", "postgres://****:****@")
|
||||||
|
);
|
||||||
println!(" Pool size: {}", config.pool_size);
|
println!(" Pool size: {}", config.pool_size);
|
||||||
println!(" Connect timeout: {}s", config.connect_timeout);
|
println!(" Connect timeout: {}s", config.connect_timeout);
|
||||||
println!(" Retry attempts: {}", config.retry_attempts);
|
println!(" Retry attempts: {}", config.retry_attempts);
|
||||||
|
|
||||||
// Test connection
|
|
||||||
println!("\n🔌 Testing PostgreSQL connection...");
|
println!("\n🔌 Testing PostgreSQL connection...");
|
||||||
match test_connection(config).await {
|
match test_connection(config).await {
|
||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
@@ -37,33 +43,27 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn test_connection(config: PostgresConfig) -> Result<(), PostgresError> {
|
async fn test_connection(config: PostgresConfig) -> Result<(), PostgresError> {
|
||||||
// Create connection
|
|
||||||
println!(" Creating PostgreSQL connection...");
|
println!(" Creating PostgreSQL connection...");
|
||||||
let postgres_conn = PostgresConnection::new(config).await?;
|
let postgres_conn = PostgresConnection::new(config).await?;
|
||||||
let connection = Arc::new(postgres_conn);
|
let connection = Arc::new(postgres_conn);
|
||||||
println!(" ✅ Connection established successfully");
|
println!(" ✅ Connection established successfully");
|
||||||
|
|
||||||
// Test basic connectivity
|
|
||||||
println!(" Testing basic connectivity...");
|
println!(" Testing basic connectivity...");
|
||||||
test_basic_connectivity(&connection).await?;
|
test_basic_connectivity(&connection).await?;
|
||||||
println!(" ✅ Basic connectivity test passed");
|
println!(" ✅ Basic connectivity test passed");
|
||||||
|
|
||||||
// Test table existence
|
|
||||||
println!(" Testing table existence...");
|
println!(" Testing table existence...");
|
||||||
test_table_existence(&connection).await?;
|
test_table_existence(&connection).await?;
|
||||||
println!(" ✅ Table existence test passed");
|
println!(" ✅ Table existence test passed");
|
||||||
|
|
||||||
// Test CRUD operations
|
|
||||||
println!(" Testing CRUD operations...");
|
println!(" Testing CRUD operations...");
|
||||||
test_crud_operations(&connection).await?;
|
test_crud_operations(&connection).await?;
|
||||||
println!(" ✅ CRUD operations test passed");
|
println!(" ✅ CRUD operations test passed");
|
||||||
|
|
||||||
// Test transaction support
|
|
||||||
println!(" Testing transaction support...");
|
println!(" Testing transaction support...");
|
||||||
test_transactions(&connection).await?;
|
test_transactions(&connection).await?;
|
||||||
println!(" ✅ Transaction support test passed");
|
println!(" ✅ Transaction support test passed");
|
||||||
|
|
||||||
// Test error handling
|
|
||||||
println!(" Testing error handling...");
|
println!(" Testing error handling...");
|
||||||
test_error_handling(&connection).await?;
|
test_error_handling(&connection).await?;
|
||||||
println!(" ✅ Error handling test passed");
|
println!(" ✅ Error handling test passed");
|
||||||
@@ -71,38 +71,45 @@ async fn test_connection(config: PostgresConfig) -> Result<(), PostgresError> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn test_basic_connectivity(connection: &Arc<PostgresConnection>) -> Result<(), PostgresError> {
|
async fn test_basic_connectivity(
|
||||||
// Execute a simple query
|
connection: &Arc<PostgresConnection>,
|
||||||
|
) -> Result<(), PostgresError> {
|
||||||
let statement = sea_orm::Statement::from_string(
|
let statement = sea_orm::Statement::from_string(
|
||||||
connection.get_database_backend(),
|
connection.get_database_backend(),
|
||||||
"SELECT 1 as test_value, current_timestamp as current_time".to_string()
|
"SELECT 1 as test_value, current_timestamp as current_time".to_string(),
|
||||||
);
|
);
|
||||||
|
|
||||||
let result = connection.query_one(statement).await?
|
let result = connection.query_one(statement).await?.ok_or_else(|| {
|
||||||
.ok_or_else(|| PostgresError::ConnectionError(sea_orm::DbErr::Custom("No results returned".to_string())))?;
|
PostgresError::ConnectionError(sea_orm::DbErr::Custom(
|
||||||
|
"No results returned".to_string(),
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
|
||||||
// Verify we got expected results
|
|
||||||
let test_value: Option<i32> = result.try_get("", "test_value").ok();
|
let test_value: Option<i32> = result.try_get("", "test_value").ok();
|
||||||
let current_time: Option<String> = result.try_get("", "current_time").ok();
|
let current_time: Option<String> = result.try_get("", "current_time").ok();
|
||||||
|
|
||||||
if test_value != Some(1) {
|
if test_value != Some(1) {
|
||||||
return Err(PostgresError::ConnectionError(sea_orm::DbErr::Custom(
|
return Err(PostgresError::ConnectionError(sea_orm::DbErr::Custom(
|
||||||
format!("Expected test_value=1, got {:?}", test_value)
|
format!("Expected test_value=1, got {:?}", test_value),
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
if current_time.is_none() {
|
if current_time.is_none() {
|
||||||
return Err(PostgresError::ConnectionError(sea_orm::DbErr::Custom(
|
return Err(PostgresError::ConnectionError(sea_orm::DbErr::Custom(
|
||||||
"Expected current_time to be set".to_string()
|
"Expected current_time to be set".to_string(),
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
println!(" 📝 Query result: test_value={:?}, current_time={:?}", test_value, current_time);
|
println!(
|
||||||
|
" 📝 Query result: test_value={:?}, current_time={:?}",
|
||||||
|
test_value, current_time
|
||||||
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn test_table_existence(connection: &Arc<PostgresConnection>) -> Result<(), PostgresError> {
|
async fn test_table_existence(
|
||||||
// Test if our tables exist
|
connection: &Arc<PostgresConnection>,
|
||||||
|
) -> Result<(), PostgresError> {
|
||||||
use sea_orm::EntityTrait;
|
use sea_orm::EntityTrait;
|
||||||
|
|
||||||
println!(" 📋 Checking users table...");
|
println!(" 📋 Checking users table...");
|
||||||
@@ -110,22 +117,29 @@ async fn test_table_existence(connection: &Arc<PostgresConnection>) -> Result<()
|
|||||||
.count(&connection.conn)
|
.count(&connection.conn)
|
||||||
.await
|
.await
|
||||||
.map_err(PostgresError::ConnectionError)?;
|
.map_err(PostgresError::ConnectionError)?;
|
||||||
println!(" 📊 Users table accessible, current count: {}", user_count);
|
println!(
|
||||||
|
" 📊 Users table accessible, current count: {}",
|
||||||
|
user_count
|
||||||
|
);
|
||||||
|
|
||||||
println!(" 📋 Checking roles table...");
|
println!(" 📋 Checking roles table...");
|
||||||
let role_count = RolesEntity::find()
|
let role_count = RolesEntity::find()
|
||||||
.count(&connection.conn)
|
.count(&connection.conn)
|
||||||
.await
|
.await
|
||||||
.map_err(PostgresError::ConnectionError)?;
|
.map_err(PostgresError::ConnectionError)?;
|
||||||
println!(" 📊 Roles table accessible, current count: {}", role_count);
|
println!(
|
||||||
|
" 📊 Roles table accessible, current count: {}",
|
||||||
|
role_count
|
||||||
|
);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn test_crud_operations(connection: &Arc<PostgresConnection>) -> Result<(), PostgresError> {
|
async fn test_crud_operations(
|
||||||
|
connection: &Arc<PostgresConnection>,
|
||||||
|
) -> Result<(), PostgresError> {
|
||||||
use sea_orm::{ActiveModelTrait, Set};
|
use sea_orm::{ActiveModelTrait, Set};
|
||||||
|
|
||||||
// Create test user
|
|
||||||
println!(" ➕ Creating test user...");
|
println!(" ➕ Creating test user...");
|
||||||
let test_user_id = Uuid::new_v4();
|
let test_user_id = Uuid::new_v4();
|
||||||
let now = Utc::now();
|
let now = Utc::now();
|
||||||
@@ -147,35 +161,45 @@ async fn test_crud_operations(connection: &Arc<PostgresConnection>) -> Result<()
|
|||||||
deleted_at: Set(None),
|
deleted_at: Set(None),
|
||||||
};
|
};
|
||||||
|
|
||||||
let created_user = user_model.insert(&connection.conn)
|
let created_user = user_model
|
||||||
|
.insert(&connection.conn)
|
||||||
.await
|
.await
|
||||||
.map_err(PostgresError::ConnectionError)?;
|
.map_err(PostgresError::ConnectionError)?;
|
||||||
|
|
||||||
println!(" ✅ Created user with ID: {}", created_user.id);
|
println!(" ✅ Created user with ID: {}", created_user.id);
|
||||||
|
|
||||||
// Read user
|
|
||||||
println!(" 🔍 Reading test user...");
|
println!(" 🔍 Reading test user...");
|
||||||
let found_user = UsersEntity::find_by_id(test_user_id)
|
let found_user = UsersEntity::find_by_id(test_user_id)
|
||||||
.one(&connection.conn)
|
.one(&connection.conn)
|
||||||
.await
|
.await
|
||||||
.map_err(PostgresError::ConnectionError)?
|
.map_err(PostgresError::ConnectionError)?
|
||||||
.ok_or_else(|| PostgresError::ConnectionError(sea_orm::DbErr::Custom("User not found after creation".to_string())))?;
|
.ok_or_else(|| {
|
||||||
|
PostgresError::ConnectionError(sea_orm::DbErr::Custom(
|
||||||
|
"User not found after creation".to_string(),
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
|
||||||
println!(" ✅ Found user: {} ({})", found_user.username, found_user.email);
|
println!(
|
||||||
|
" ✅ Found user: {} ({})",
|
||||||
|
found_user.username, found_user.email
|
||||||
|
);
|
||||||
|
|
||||||
// Update user
|
|
||||||
println!(" ✏️ Updating test user...");
|
println!(" ✏️ Updating test user...");
|
||||||
let mut update_model: imphnen_entities::seaorm::auth::users::ActiveModel = found_user.into();
|
let mut update_model: imphnen_entities::seaorm::auth::users::ActiveModel =
|
||||||
|
found_user.into();
|
||||||
update_model.first_name = Set(Some("Updated".to_string()));
|
update_model.first_name = Set(Some("Updated".to_string()));
|
||||||
update_model.updated_at = Set(Utc::now());
|
update_model.updated_at = Set(Utc::now());
|
||||||
|
|
||||||
let updated_user = update_model.update(&connection.conn)
|
let updated_user = update_model
|
||||||
|
.update(&connection.conn)
|
||||||
.await
|
.await
|
||||||
.map_err(PostgresError::ConnectionError)?;
|
.map_err(PostgresError::ConnectionError)?;
|
||||||
|
|
||||||
println!(" ✅ Updated user first name to: {:?}", updated_user.first_name);
|
println!(
|
||||||
|
" ✅ Updated user first name to: {:?}",
|
||||||
|
updated_user.first_name
|
||||||
|
);
|
||||||
|
|
||||||
// Delete user
|
|
||||||
println!(" 🗑️ Deleting test user...");
|
println!(" 🗑️ Deleting test user...");
|
||||||
UsersEntity::delete_by_id(updated_user.id)
|
UsersEntity::delete_by_id(updated_user.id)
|
||||||
.exec(&connection.conn)
|
.exec(&connection.conn)
|
||||||
@@ -187,13 +211,15 @@ async fn test_crud_operations(connection: &Arc<PostgresConnection>) -> Result<()
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn test_transactions(connection: &Arc<PostgresConnection>) -> Result<(), PostgresError> {
|
async fn test_transactions(
|
||||||
|
connection: &Arc<PostgresConnection>,
|
||||||
|
) -> Result<(), PostgresError> {
|
||||||
println!(" 💰 Testing transaction support...");
|
println!(" 💰 Testing transaction support...");
|
||||||
|
|
||||||
// Test transaction with rollback
|
let transaction_result = connection
|
||||||
let transaction_result = connection.conn.transaction(|txn| {
|
.conn
|
||||||
|
.transaction(|txn| {
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
// Create a test user within transaction
|
|
||||||
let test_user_id = Uuid::new_v4();
|
let test_user_id = Uuid::new_v4();
|
||||||
let now = Utc::now();
|
let now = Utc::now();
|
||||||
|
|
||||||
@@ -214,31 +240,34 @@ async fn test_transactions(connection: &Arc<PostgresConnection>) -> Result<(), P
|
|||||||
deleted_at: Set(None),
|
deleted_at: Set(None),
|
||||||
};
|
};
|
||||||
|
|
||||||
let _created_user = user_model.insert(txn)
|
let _created_user = user_model.insert(txn).await?;
|
||||||
.await?;
|
|
||||||
|
|
||||||
// Simulate an error to trigger rollback (return a sea_orm::DbErr so the TransactionError matches)
|
|
||||||
Err::<(), DbErr>(DbErr::Custom("Simulated transaction failure".to_string()))
|
Err::<(), DbErr>(DbErr::Custom("Simulated transaction failure".to_string()))
|
||||||
})
|
})
|
||||||
}).await;
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
// Transaction should fail and rollback
|
|
||||||
match transaction_result {
|
match transaction_result {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let e_text = format!("{:?}", e);
|
let e_text = format!("{:?}", e);
|
||||||
if e_text.contains("Simulated transaction failure") {
|
if e_text.contains("Simulated transaction failure") {
|
||||||
println!(" ✅ Transaction failed as expected, rollback successful");
|
println!(" ✅ Transaction failed as expected, rollback successful");
|
||||||
} else {
|
} else {
|
||||||
return Err(PostgresError::OperationFailed(format!("Unexpected transaction result: {}", e_text)));
|
return Err(PostgresError::OperationFailed(format!(
|
||||||
|
"Unexpected transaction result: {}",
|
||||||
|
e_text
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
return Err(PostgresError::OperationFailed("Unexpected transaction result: transaction unexpectedly succeeded".to_string()));
|
return Err(PostgresError::OperationFailed(
|
||||||
|
"Unexpected transaction result: transaction unexpectedly succeeded"
|
||||||
|
.to_string(),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify user was not created (due to rollback)
|
let user_exists = UsersEntity::find_by_id(Uuid::nil())
|
||||||
let user_exists = UsersEntity::find_by_id(Uuid::nil()) // Use nil UUID as we don't know the actual ID
|
|
||||||
.one(&connection.conn)
|
.one(&connection.conn)
|
||||||
.await
|
.await
|
||||||
.map_err(PostgresError::ConnectionError)?
|
.map_err(PostgresError::ConnectionError)?
|
||||||
@@ -253,27 +282,29 @@ async fn test_transactions(connection: &Arc<PostgresConnection>) -> Result<(), P
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn test_error_handling(connection: &Arc<PostgresConnection>) -> Result<(), PostgresError> {
|
async fn test_error_handling(
|
||||||
|
connection: &Arc<PostgresConnection>,
|
||||||
|
) -> Result<(), PostgresError> {
|
||||||
println!(" ⚠️ Testing error handling...");
|
println!(" ⚠️ Testing error handling...");
|
||||||
|
|
||||||
// Test invalid UUID
|
|
||||||
println!(" 🔍 Testing invalid UUID handling...");
|
println!(" 🔍 Testing invalid UUID handling...");
|
||||||
let invalid_uuid = Uuid::nil(); // This should exist or be handled gracefully
|
let invalid_uuid = Uuid::nil();
|
||||||
|
|
||||||
match UsersEntity::find_by_id(invalid_uuid)
|
match UsersEntity::find_by_id(invalid_uuid)
|
||||||
.one(&connection.conn)
|
.one(&connection.conn)
|
||||||
.await
|
.await
|
||||||
.map_err(PostgresError::ConnectionError)?
|
.map_err(PostgresError::ConnectionError)?
|
||||||
{
|
{
|
||||||
Some(_) => println!(" ✅ Found user with nil UUID (expected in some cases)"),
|
Some(_) => {
|
||||||
|
println!(" ✅ Found user with nil UUID (expected in some cases)")
|
||||||
|
}
|
||||||
None => println!(" ✅ No user found with nil UUID (expected)"),
|
None => println!(" ✅ No user found with nil UUID (expected)"),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test invalid query
|
|
||||||
println!(" 🔍 Testing invalid query handling...");
|
println!(" 🔍 Testing invalid query handling...");
|
||||||
let invalid_statement = sea_orm::Statement::from_string(
|
let invalid_statement = sea_orm::Statement::from_string(
|
||||||
connection.get_database_backend(),
|
connection.get_database_backend(),
|
||||||
"SELECT * FROM non_existent_table".to_string()
|
"SELECT * FROM non_existent_table".to_string(),
|
||||||
);
|
);
|
||||||
|
|
||||||
match connection.execute(invalid_statement).await {
|
match connection.execute(invalid_statement).await {
|
||||||
@@ -284,14 +315,13 @@ async fn test_error_handling(connection: &Arc<PostgresConnection>) -> Result<(),
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Additional utility functions for comprehensive testing
|
|
||||||
pub mod test_utils {
|
pub mod test_utils {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
/// Create a test PostgreSQL configuration
|
|
||||||
pub fn create_test_config() -> PostgresConfig {
|
pub fn create_test_config() -> PostgresConfig {
|
||||||
PostgresConfig {
|
PostgresConfig {
|
||||||
database_url: "postgres://postgres:postgres@localhost:5432/imphnen_test".to_string(),
|
database_url: "postgres://postgres:postgres@localhost:5432/imphnen_test"
|
||||||
|
.to_string(),
|
||||||
pool_size: 5,
|
pool_size: 5,
|
||||||
connect_timeout: 10,
|
connect_timeout: 10,
|
||||||
idle_timeout: 30,
|
idle_timeout: 30,
|
||||||
@@ -301,7 +331,6 @@ pub mod test_utils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a test user model
|
|
||||||
pub fn create_test_user_model() -> UserModel {
|
pub fn create_test_user_model() -> UserModel {
|
||||||
UserModel {
|
UserModel {
|
||||||
id: Uuid::new_v4(),
|
id: Uuid::new_v4(),
|
||||||
@@ -321,7 +350,6 @@ pub mod test_utils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a test role model
|
|
||||||
pub fn create_test_role_model() -> RoleModel {
|
pub fn create_test_role_model() -> RoleModel {
|
||||||
RoleModel {
|
RoleModel {
|
||||||
id: Uuid::new_v4(),
|
id: Uuid::new_v4(),
|
||||||
@@ -355,7 +383,6 @@ mod tests {
|
|||||||
assert!(!user.email.is_empty());
|
assert!(!user.email.is_empty());
|
||||||
assert!(!user.username.is_empty());
|
assert!(!user.username.is_empty());
|
||||||
assert!(user.is_active);
|
assert!(user.is_active);
|
||||||
// is_admin field removed; instead, check role-based permission or is_active
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@@ -5,9 +5,6 @@ use imphnen_libs::axum_init;
|
|||||||
async fn main() {
|
async fn main() {
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
let _ = axum_init(|postgres_conn| async {
|
let _ =
|
||||||
// PostgreSQL is now the primary database - SurrealDB has been completely removed
|
axum_init(|postgres_conn| async { gateway_service(postgres_conn).await }).await;
|
||||||
gateway_service(postgres_conn).await
|
|
||||||
})
|
|
||||||
.await;
|
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-2
@@ -1,9 +1,10 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "imphnen-cms"
|
name = "imphnen-cms"
|
||||||
version = "0.1.0"
|
version = "0.3.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
async-trait.workspace = true
|
||||||
imphnen-iam.workspace = true
|
imphnen-iam.workspace = true
|
||||||
imphnen-libs.workspace = true
|
imphnen-libs.workspace = true
|
||||||
imphnen-utils.workspace = true
|
imphnen-utils.workspace = true
|
||||||
@@ -14,7 +15,8 @@ serde_json.workspace = true
|
|||||||
utoipa.workspace = true
|
utoipa.workspace = true
|
||||||
lazy_static.workspace = true
|
lazy_static.workspace = true
|
||||||
regex.workspace = true
|
regex.workspace = true
|
||||||
validator.workspace = true
|
zod-rs.workspace = true
|
||||||
|
zod-rs-util.workspace = true
|
||||||
axum-test.workspace = true
|
axum-test.workspace = true
|
||||||
rand.workspace = true
|
rand.workspace = true
|
||||||
tokio.workspace = true
|
tokio.workspace = true
|
||||||
@@ -27,5 +29,13 @@ tracing.workspace = true
|
|||||||
sea-orm.workspace = true
|
sea-orm.workspace = true
|
||||||
uuid.workspace = true
|
uuid.workspace = true
|
||||||
|
|
||||||
|
paginator-rs.workspace = true
|
||||||
|
paginator-utils.workspace = true
|
||||||
|
paginator-sea-orm.workspace = true
|
||||||
|
paginator-axum.workspace = true
|
||||||
|
sqlx.workspace = true
|
||||||
|
image.workspace = true
|
||||||
|
qrcode.workspace = true
|
||||||
|
|
||||||
[package.metadata.validator.regex]
|
[package.metadata.validator.regex]
|
||||||
VALID_URL_REGEX = "^https?://"
|
VALID_URL_REGEX = "^https?://"
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
use crate::events::domain::{EventEntity, EventRepository, EventService};
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use imphnen_utils::AppError;
|
||||||
|
use paginator_rs::PaginationParams;
|
||||||
|
use paginator_utils::PaginatorResponse;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
pub struct EventServiceImpl {
|
||||||
|
repo: Arc<dyn EventRepository>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EventServiceImpl {
|
||||||
|
pub fn new(repo: Arc<dyn EventRepository>) -> Self {
|
||||||
|
Self { repo }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl EventService for EventServiceImpl {
|
||||||
|
async fn list(
|
||||||
|
&self,
|
||||||
|
params: PaginationParams,
|
||||||
|
) -> Result<PaginatorResponse<EventEntity>, AppError> {
|
||||||
|
self.repo.find_all(params).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get(&self, id: Uuid) -> Result<EventEntity, AppError> {
|
||||||
|
self.repo.find_by_id(id).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn create(&self, entity: EventEntity) -> Result<(), AppError> {
|
||||||
|
self.repo.create(entity).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update(&self, entity: EventEntity) -> Result<(), AppError> {
|
||||||
|
self.repo.update(entity).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn delete(&self, id: Uuid) -> Result<(), AppError> {
|
||||||
|
self.repo.delete(id).await
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
pub mod event_service;
|
||||||
|
|
||||||
|
pub use event_service::EventServiceImpl;
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct EventEntity {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub name: String,
|
||||||
|
pub description: String,
|
||||||
|
pub detail_link: String,
|
||||||
|
pub price: f64,
|
||||||
|
pub is_online: bool,
|
||||||
|
pub is_deleted: bool,
|
||||||
|
pub location: Option<String>,
|
||||||
|
pub start_date: DateTime<Utc>,
|
||||||
|
pub end_date: DateTime<Utc>,
|
||||||
|
pub created_at: DateTime<Utc>,
|
||||||
|
pub updated_at: DateTime<Utc>,
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
pub mod event;
|
||||||
|
pub mod repository;
|
||||||
|
pub mod service;
|
||||||
|
|
||||||
|
pub use event::EventEntity;
|
||||||
|
pub use repository::EventRepository;
|
||||||
|
pub use service::EventService;
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
use super::event::EventEntity;
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use imphnen_utils::AppError;
|
||||||
|
use paginator_rs::PaginationParams;
|
||||||
|
use paginator_utils::PaginatorResponse;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait EventRepository: Send + Sync {
|
||||||
|
async fn find_all(
|
||||||
|
&self,
|
||||||
|
params: PaginationParams,
|
||||||
|
) -> Result<PaginatorResponse<EventEntity>, AppError>;
|
||||||
|
async fn find_by_id(&self, id: Uuid) -> Result<EventEntity, AppError>;
|
||||||
|
async fn create(&self, entity: EventEntity) -> Result<(), AppError>;
|
||||||
|
async fn update(&self, entity: EventEntity) -> Result<(), AppError>;
|
||||||
|
async fn delete(&self, id: Uuid) -> Result<(), AppError>;
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
use super::event::EventEntity;
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use imphnen_utils::AppError;
|
||||||
|
use paginator_rs::PaginationParams;
|
||||||
|
use paginator_utils::PaginatorResponse;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait EventService: Send + Sync {
|
||||||
|
async fn list(
|
||||||
|
&self,
|
||||||
|
params: PaginationParams,
|
||||||
|
) -> Result<PaginatorResponse<EventEntity>, AppError>;
|
||||||
|
async fn get(&self, id: Uuid) -> Result<EventEntity, AppError>;
|
||||||
|
async fn create(&self, entity: EventEntity) -> Result<(), AppError>;
|
||||||
|
async fn update(&self, entity: EventEntity) -> Result<(), AppError>;
|
||||||
|
async fn delete(&self, id: Uuid) -> Result<(), AppError>;
|
||||||
|
}
|
||||||
@@ -0,0 +1,131 @@
|
|||||||
|
use crate::events::domain::event::EventEntity;
|
||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use imphnen_libs::ZodValidate;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use utoipa::ToSchema;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct EventsCreateRequestDto {
|
||||||
|
pub name: String,
|
||||||
|
pub description: String,
|
||||||
|
pub detail_link: String,
|
||||||
|
pub price: f64,
|
||||||
|
#[schema(example = "2025-09-20T13:00:00Z", value_type = String)]
|
||||||
|
pub end_date: DateTime<Utc>,
|
||||||
|
#[schema(example = "2025-09-20T13:00:00Z", value_type = String)]
|
||||||
|
pub start_date: DateTime<Utc>,
|
||||||
|
pub location: Option<String>,
|
||||||
|
pub is_online: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ZodValidate for EventsCreateRequestDto {
|
||||||
|
fn zod_validate(value: &serde_json::Value) -> Result<Self, String> {
|
||||||
|
serde_json::from_value(value.clone()).map_err(|e| e.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<EventsCreateRequestDto> for EventEntity {
|
||||||
|
fn from(dto: EventsCreateRequestDto) -> Self {
|
||||||
|
EventEntity {
|
||||||
|
id: Uuid::new_v4(),
|
||||||
|
name: dto.name,
|
||||||
|
description: dto.description,
|
||||||
|
detail_link: dto.detail_link,
|
||||||
|
price: dto.price,
|
||||||
|
is_online: dto.is_online,
|
||||||
|
is_deleted: false,
|
||||||
|
location: dto.location,
|
||||||
|
start_date: dto.start_date,
|
||||||
|
end_date: dto.end_date,
|
||||||
|
created_at: chrono::Utc::now(),
|
||||||
|
updated_at: chrono::Utc::now(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct EventsUpdateRequestDto {
|
||||||
|
pub name: String,
|
||||||
|
#[schema(example = "2025-09-20T13:00:00Z", value_type = String)]
|
||||||
|
pub end_date: DateTime<Utc>,
|
||||||
|
#[schema(example = "2025-09-20T13:00:00Z", value_type = String)]
|
||||||
|
pub start_date: DateTime<Utc>,
|
||||||
|
pub price: f64,
|
||||||
|
pub is_online: bool,
|
||||||
|
pub description: String,
|
||||||
|
pub detail_link: String,
|
||||||
|
pub location: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ZodValidate for EventsUpdateRequestDto {
|
||||||
|
fn zod_validate(value: &serde_json::Value) -> Result<Self, String> {
|
||||||
|
serde_json::from_value(value.clone()).map_err(|e| e.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct EventsListItemDto {
|
||||||
|
pub id: String,
|
||||||
|
pub name: String,
|
||||||
|
pub description: String,
|
||||||
|
pub detail_link: String,
|
||||||
|
pub price: f64,
|
||||||
|
pub is_online: bool,
|
||||||
|
pub start_date: String,
|
||||||
|
pub end_date: String,
|
||||||
|
pub created_at: String,
|
||||||
|
pub location: Option<String>,
|
||||||
|
pub is_deleted: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<EventEntity> for EventsListItemDto {
|
||||||
|
fn from(e: EventEntity) -> Self {
|
||||||
|
EventsListItemDto {
|
||||||
|
id: e.id.to_string(),
|
||||||
|
name: e.name,
|
||||||
|
description: e.description,
|
||||||
|
detail_link: e.detail_link,
|
||||||
|
price: e.price,
|
||||||
|
is_online: e.is_online,
|
||||||
|
start_date: e.start_date.to_rfc3339(),
|
||||||
|
end_date: e.end_date.to_rfc3339(),
|
||||||
|
created_at: e.created_at.to_rfc3339(),
|
||||||
|
location: e.location,
|
||||||
|
is_deleted: e.is_deleted,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct EventsDetailItemDto {
|
||||||
|
pub id: String,
|
||||||
|
pub name: String,
|
||||||
|
pub description: String,
|
||||||
|
pub detail_link: String,
|
||||||
|
pub price: f64,
|
||||||
|
pub is_online: bool,
|
||||||
|
pub start_date: String,
|
||||||
|
pub end_date: String,
|
||||||
|
pub created_at: String,
|
||||||
|
pub updated_at: String,
|
||||||
|
pub location: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<EventEntity> for EventsDetailItemDto {
|
||||||
|
fn from(e: EventEntity) -> Self {
|
||||||
|
EventsDetailItemDto {
|
||||||
|
id: e.id.to_string(),
|
||||||
|
name: e.name,
|
||||||
|
description: e.description,
|
||||||
|
detail_link: e.detail_link,
|
||||||
|
price: e.price,
|
||||||
|
is_online: e.is_online,
|
||||||
|
start_date: e.start_date.to_rfc3339(),
|
||||||
|
end_date: e.end_date.to_rfc3339(),
|
||||||
|
created_at: e.created_at.to_rfc3339(),
|
||||||
|
updated_at: e.updated_at.to_rfc3339(),
|
||||||
|
location: e.location,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,180 @@
|
|||||||
|
use super::dto::{
|
||||||
|
EventsCreateRequestDto, EventsDetailItemDto, EventsListItemDto,
|
||||||
|
EventsUpdateRequestDto,
|
||||||
|
};
|
||||||
|
use crate::events::domain::EventService;
|
||||||
|
use axum::{
|
||||||
|
Extension,
|
||||||
|
extract::Path,
|
||||||
|
http::HeaderMap,
|
||||||
|
response::{IntoResponse, Response},
|
||||||
|
};
|
||||||
|
use imphnen_entities::ResponseSuccessDto;
|
||||||
|
use imphnen_iam::{PermissionsEnum, require_permissions};
|
||||||
|
use imphnen_libs::{AppState, ValidatedJson};
|
||||||
|
use imphnen_utils::AppError;
|
||||||
|
use imphnen_utils::{ApiMessage, ApiPaginated, ApiSuccess};
|
||||||
|
use paginator_axum::PaginationQuery;
|
||||||
|
use paginator_utils::PaginatorResponse;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/v1/landing/cms/events",
|
||||||
|
params(
|
||||||
|
("page" = Option<i64>, Query, description = "Page number"),
|
||||||
|
("per_page" = Option<i64>, Query, description = "Items per page"),
|
||||||
|
("search" = Option<String>, Query, description = "Search keyword"),
|
||||||
|
("sort_by" = Option<String>, Query, description = "Sort by field"),
|
||||||
|
("order" = Option<String>, Query, description = "Order ASC or DESC"),
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "[PUBLIC] Get event list")
|
||||||
|
),
|
||||||
|
tag = "Events"
|
||||||
|
)]
|
||||||
|
pub async fn get_event_list(
|
||||||
|
Extension(service): Extension<Arc<dyn EventService>>,
|
||||||
|
PaginationQuery(params): PaginationQuery,
|
||||||
|
) -> Response {
|
||||||
|
match service.list(params).await {
|
||||||
|
Ok(result) => {
|
||||||
|
let mapped = PaginatorResponse {
|
||||||
|
data: result
|
||||||
|
.data
|
||||||
|
.into_iter()
|
||||||
|
.map(EventsListItemDto::from)
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
|
meta: result.meta,
|
||||||
|
};
|
||||||
|
ApiPaginated(mapped).into_response()
|
||||||
|
}
|
||||||
|
Err(e) => ApiMessage::new(axum::http::StatusCode::BAD_REQUEST, e.to_string())
|
||||||
|
.into_response(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/v1/landing/cms/events/detail/{id}",
|
||||||
|
params(
|
||||||
|
("id" = String, Path, description = "Event ID")
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "[PUBLIC] Get event by ID", body = ResponseSuccessDto<EventsDetailItemDto>)
|
||||||
|
),
|
||||||
|
tag = "Events"
|
||||||
|
)]
|
||||||
|
pub async fn get_event_by_id(
|
||||||
|
Extension(service): Extension<Arc<dyn EventService>>,
|
||||||
|
Path(id): Path<String>,
|
||||||
|
) -> Response {
|
||||||
|
let uuid = match Uuid::parse_str(&id) {
|
||||||
|
Ok(u) => u,
|
||||||
|
Err(e) => {
|
||||||
|
return ApiMessage::new(
|
||||||
|
axum::http::StatusCode::BAD_REQUEST,
|
||||||
|
format!("Invalid UUID: {e}"),
|
||||||
|
)
|
||||||
|
.into_response();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
match service.get(uuid).await {
|
||||||
|
Ok(event) => ApiSuccess(EventsDetailItemDto::from(event)).into_response(),
|
||||||
|
Err(e) => ApiMessage::new(axum::http::StatusCode::NOT_FOUND, e.to_string())
|
||||||
|
.into_response(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
security(("Bearer" = [])),
|
||||||
|
path = "/v1/landing/cms/events/create",
|
||||||
|
request_body = EventsCreateRequestDto,
|
||||||
|
responses(
|
||||||
|
(status = 201, description = "[ADMIN] Create new event")
|
||||||
|
),
|
||||||
|
tag = "Events"
|
||||||
|
)]
|
||||||
|
pub async fn post_create_event(
|
||||||
|
headers: HeaderMap,
|
||||||
|
Extension(state): Extension<AppState>,
|
||||||
|
Extension(service): Extension<Arc<dyn EventService>>,
|
||||||
|
ValidatedJson(payload): ValidatedJson<EventsCreateRequestDto>,
|
||||||
|
) -> Result<impl IntoResponse, AppError> {
|
||||||
|
require_permissions!(headers, state, [PermissionsEnum::Administrator], {
|
||||||
|
let entity = payload.into();
|
||||||
|
service.create(entity).await?;
|
||||||
|
Ok(ApiMessage::created("Event created"))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
patch,
|
||||||
|
security(("Bearer" = [])),
|
||||||
|
path = "/v1/landing/cms/events/update/{id}",
|
||||||
|
params(
|
||||||
|
("id" = String, Path, description = "Event ID")
|
||||||
|
),
|
||||||
|
request_body = EventsUpdateRequestDto,
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "[ADMIN] Update event")
|
||||||
|
),
|
||||||
|
tag = "Events"
|
||||||
|
)]
|
||||||
|
pub async fn patch_update_event(
|
||||||
|
headers: HeaderMap,
|
||||||
|
Extension(state): Extension<AppState>,
|
||||||
|
Extension(service): Extension<Arc<dyn EventService>>,
|
||||||
|
Path(id): Path<String>,
|
||||||
|
ValidatedJson(payload): ValidatedJson<EventsUpdateRequestDto>,
|
||||||
|
) -> Result<impl IntoResponse, AppError> {
|
||||||
|
require_permissions!(headers, state, [PermissionsEnum::Administrator], {
|
||||||
|
let uuid = Uuid::parse_str(&id)
|
||||||
|
.map_err(|e| AppError::BadRequestError(format!("Invalid UUID: {e}")))?;
|
||||||
|
let existing = service.get(uuid).await?;
|
||||||
|
let entity = crate::events::domain::EventEntity {
|
||||||
|
id: existing.id,
|
||||||
|
name: payload.name,
|
||||||
|
description: payload.description,
|
||||||
|
detail_link: payload.detail_link,
|
||||||
|
price: payload.price,
|
||||||
|
is_online: payload.is_online,
|
||||||
|
location: payload.location,
|
||||||
|
start_date: payload.start_date,
|
||||||
|
end_date: payload.end_date,
|
||||||
|
is_deleted: existing.is_deleted,
|
||||||
|
created_at: existing.created_at,
|
||||||
|
updated_at: chrono::Utc::now(),
|
||||||
|
};
|
||||||
|
service.update(entity).await?;
|
||||||
|
Ok(ApiMessage::ok("Event updated"))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
delete,
|
||||||
|
security(("Bearer" = [])),
|
||||||
|
path = "/v1/landing/cms/events/delete/{id}",
|
||||||
|
params(
|
||||||
|
("id" = String, Path, description = "Event ID")
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "[ADMIN] Soft delete event")
|
||||||
|
),
|
||||||
|
tag = "Events"
|
||||||
|
)]
|
||||||
|
pub async fn delete_event(
|
||||||
|
headers: HeaderMap,
|
||||||
|
Extension(state): Extension<AppState>,
|
||||||
|
Extension(service): Extension<Arc<dyn EventService>>,
|
||||||
|
Path(id): Path<String>,
|
||||||
|
) -> Result<impl IntoResponse, AppError> {
|
||||||
|
require_permissions!(headers, state, [PermissionsEnum::Administrator], {
|
||||||
|
let uuid = Uuid::parse_str(&id)
|
||||||
|
.map_err(|e| AppError::BadRequestError(format!("Invalid UUID: {e}")))?;
|
||||||
|
service.delete(uuid).await?;
|
||||||
|
Ok(ApiMessage::ok("Event deleted"))
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
pub mod dto;
|
||||||
|
pub mod handlers;
|
||||||
|
pub mod routes;
|
||||||
|
|
||||||
|
pub use routes::{events_protected_routes, events_public_routes};
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
use super::handlers::{
|
||||||
|
delete_event, get_event_by_id, get_event_list, patch_update_event,
|
||||||
|
post_create_event,
|
||||||
|
};
|
||||||
|
use crate::events::application::EventServiceImpl;
|
||||||
|
use crate::events::domain::EventService;
|
||||||
|
use crate::events::infrastructure::persistence::PostgresEventRepository;
|
||||||
|
use axum::{
|
||||||
|
Extension, Router,
|
||||||
|
routing::{delete, get, patch, post},
|
||||||
|
};
|
||||||
|
use sea_orm::DatabaseConnection;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
fn build_service(db: DatabaseConnection) -> Arc<dyn EventService> {
|
||||||
|
let repo = Arc::new(PostgresEventRepository::new(db));
|
||||||
|
Arc::new(EventServiceImpl::new(repo))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn events_public_routes(db: DatabaseConnection) -> Router {
|
||||||
|
let service = build_service(db);
|
||||||
|
Router::new()
|
||||||
|
.route("/events", get(get_event_list))
|
||||||
|
.route("/events/detail/{id}", get(get_event_by_id))
|
||||||
|
.layer(Extension(service))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn events_protected_routes(db: DatabaseConnection) -> Router {
|
||||||
|
let service = build_service(db);
|
||||||
|
Router::new()
|
||||||
|
.route("/events/create", post(post_create_event))
|
||||||
|
.route("/events/update/{id}", patch(patch_update_event))
|
||||||
|
.route("/events/delete/{id}", delete(delete_event))
|
||||||
|
.layer(Extension(service))
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
pub mod http;
|
||||||
|
pub mod persistence;
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
pub mod postgres_event_repository;
|
||||||
|
|
||||||
|
pub use postgres_event_repository::PostgresEventRepository;
|
||||||
@@ -0,0 +1,161 @@
|
|||||||
|
use crate::events::domain::{event::EventEntity, repository::EventRepository};
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use imphnen_entities::seaorm::common::events::{
|
||||||
|
ActiveModel as EventsActiveModel, Column as EventsColumn, Entity as EventsEntity,
|
||||||
|
Model as EventsModel,
|
||||||
|
};
|
||||||
|
use imphnen_utils::AppError;
|
||||||
|
use paginator_rs::{PaginationParams, SortDirection};
|
||||||
|
use paginator_utils::{PaginatorResponse, PaginatorResponseMeta};
|
||||||
|
use sea_orm::prelude::*;
|
||||||
|
use sea_orm::{ActiveValue, Order, PaginatorTrait, QueryOrder};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
fn to_entity(model: EventsModel) -> EventEntity {
|
||||||
|
EventEntity {
|
||||||
|
id: model.id,
|
||||||
|
name: model.name,
|
||||||
|
description: model.description,
|
||||||
|
detail_link: model.detail_link,
|
||||||
|
price: model.price,
|
||||||
|
is_online: model.is_online,
|
||||||
|
is_deleted: model.is_deleted,
|
||||||
|
location: model.location,
|
||||||
|
start_date: model.start_date,
|
||||||
|
end_date: model.end_date,
|
||||||
|
created_at: model.created_at,
|
||||||
|
updated_at: model.updated_at,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct PostgresEventRepository {
|
||||||
|
db: Arc<DatabaseConnection>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PostgresEventRepository {
|
||||||
|
pub fn new(db: DatabaseConnection) -> Self {
|
||||||
|
Self { db: Arc::new(db) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl EventRepository for PostgresEventRepository {
|
||||||
|
async fn find_all(
|
||||||
|
&self,
|
||||||
|
params: PaginationParams,
|
||||||
|
) -> Result<PaginatorResponse<EventEntity>, AppError> {
|
||||||
|
let page = params.page.max(1);
|
||||||
|
let per_page = params.per_page.clamp(1, 100);
|
||||||
|
|
||||||
|
let mut query = EventsEntity::find().filter(EventsColumn::IsDeleted.eq(false));
|
||||||
|
|
||||||
|
if let Some(ref search) = params.search {
|
||||||
|
query = query.filter(EventsColumn::Name.contains(&search.query));
|
||||||
|
}
|
||||||
|
|
||||||
|
query = match params.sort_by.as_deref() {
|
||||||
|
Some("name") => match params.sort_direction {
|
||||||
|
Some(SortDirection::Desc) => query.order_by(EventsColumn::Name, Order::Desc),
|
||||||
|
_ => query.order_by(EventsColumn::Name, Order::Asc),
|
||||||
|
},
|
||||||
|
_ => match params.sort_direction {
|
||||||
|
Some(SortDirection::Asc) => {
|
||||||
|
query.order_by(EventsColumn::CreatedAt, Order::Asc)
|
||||||
|
}
|
||||||
|
_ => query.order_by(EventsColumn::CreatedAt, Order::Desc),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
let paginator = query.paginate(self.db.as_ref(), per_page as u64);
|
||||||
|
let total = paginator
|
||||||
|
.num_items()
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||||
|
let events = paginator
|
||||||
|
.fetch_page((page - 1) as u64)
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||||
|
|
||||||
|
let data = events.into_iter().map(to_entity).collect();
|
||||||
|
let meta = PaginatorResponseMeta::new(page, per_page, total as u32);
|
||||||
|
Ok(PaginatorResponse { data, meta })
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn find_by_id(&self, id: Uuid) -> Result<EventEntity, AppError> {
|
||||||
|
let event = EventsEntity::find_by_id(id)
|
||||||
|
.filter(EventsColumn::IsDeleted.eq(false))
|
||||||
|
.one(self.db.as_ref())
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?
|
||||||
|
.ok_or_else(|| AppError::NotFoundError("Event not found".to_string()))?;
|
||||||
|
|
||||||
|
Ok(to_entity(event))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn create(&self, entity: EventEntity) -> Result<(), AppError> {
|
||||||
|
let active_model = EventsActiveModel {
|
||||||
|
id: ActiveValue::Set(entity.id),
|
||||||
|
name: ActiveValue::Set(entity.name),
|
||||||
|
description: ActiveValue::Set(entity.description),
|
||||||
|
detail_link: ActiveValue::Set(entity.detail_link),
|
||||||
|
price: ActiveValue::Set(entity.price),
|
||||||
|
is_online: ActiveValue::Set(entity.is_online),
|
||||||
|
is_deleted: ActiveValue::Set(false),
|
||||||
|
location: ActiveValue::Set(entity.location),
|
||||||
|
start_date: ActiveValue::Set(entity.start_date),
|
||||||
|
end_date: ActiveValue::Set(entity.end_date),
|
||||||
|
created_at: ActiveValue::Set(chrono::Utc::now()),
|
||||||
|
updated_at: ActiveValue::Set(chrono::Utc::now()),
|
||||||
|
};
|
||||||
|
|
||||||
|
EventsEntity::insert(active_model)
|
||||||
|
.exec(self.db.as_ref())
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update(&self, entity: EventEntity) -> Result<(), AppError> {
|
||||||
|
let mut active_model: EventsActiveModel = EventsEntity::find_by_id(entity.id)
|
||||||
|
.one(self.db.as_ref())
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?
|
||||||
|
.ok_or_else(|| AppError::NotFoundError("Event not found".to_string()))?
|
||||||
|
.into();
|
||||||
|
|
||||||
|
active_model.name = ActiveValue::Set(entity.name);
|
||||||
|
active_model.description = ActiveValue::Set(entity.description);
|
||||||
|
active_model.detail_link = ActiveValue::Set(entity.detail_link);
|
||||||
|
active_model.price = ActiveValue::Set(entity.price);
|
||||||
|
active_model.is_online = ActiveValue::Set(entity.is_online);
|
||||||
|
active_model.location = ActiveValue::Set(entity.location);
|
||||||
|
active_model.start_date = ActiveValue::Set(entity.start_date);
|
||||||
|
active_model.end_date = ActiveValue::Set(entity.end_date);
|
||||||
|
active_model.updated_at = ActiveValue::Set(chrono::Utc::now());
|
||||||
|
|
||||||
|
active_model
|
||||||
|
.update(self.db.as_ref())
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn delete(&self, id: Uuid) -> Result<(), AppError> {
|
||||||
|
let mut active_model: EventsActiveModel = EventsEntity::find_by_id(id)
|
||||||
|
.one(self.db.as_ref())
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?
|
||||||
|
.ok_or_else(|| AppError::NotFoundError("Event not found".to_string()))?
|
||||||
|
.into();
|
||||||
|
|
||||||
|
active_model.is_deleted = ActiveValue::Set(true);
|
||||||
|
active_model.updated_at = ActiveValue::Set(chrono::Utc::now());
|
||||||
|
active_model
|
||||||
|
.update(self.db.as_ref())
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
pub mod application;
|
||||||
|
pub mod domain;
|
||||||
|
pub mod infrastructure;
|
||||||
|
|
||||||
|
pub use infrastructure::http::{events_protected_routes, events_public_routes};
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
pub mod v1;
|
pub mod events;
|
||||||
|
pub mod roadmap;
|
||||||
|
pub mod testimonials;
|
||||||
|
pub mod qr;
|
||||||
|
|
||||||
pub use v1::landing;
|
pub use events::{events_protected_routes, events_public_routes};
|
||||||
pub use v1::landing::events;
|
pub use roadmap::{roadmap_protected_routes, roadmap_public_routes};
|
||||||
pub use v1::landing::testimonials;
|
pub use testimonials::{testimonials_protected_routes, testimonials_public_routes};
|
||||||
pub use v1::landing::events::events_public_routes;
|
pub use qr::qr_router;
|
||||||
pub use v1::landing::events::events_protected_routes;
|
|
||||||
pub use v1::landing::testimonials::testimonials_public_routes;
|
|
||||||
pub use v1::landing::testimonials::testimonials_protected_routes;
|
|
||||||
|
|||||||
@@ -0,0 +1,101 @@
|
|||||||
|
use async_trait::async_trait;
|
||||||
|
use image::{DynamicImage, GenericImageView, ImageFormat, imageops};
|
||||||
|
use imphnen_utils::errors::AppError;
|
||||||
|
use qrcode::QrCode;
|
||||||
|
use std::io::Cursor;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::qr::campaigns::domain::{
|
||||||
|
entity::{CampaignEntity, CreateCampaignInput},
|
||||||
|
repository::CampaignRepository,
|
||||||
|
service::QrCampaignService,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct QrCampaignServiceImpl {
|
||||||
|
repo: Arc<dyn CampaignRepository>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl QrCampaignServiceImpl {
|
||||||
|
pub fn new(repo: Arc<dyn CampaignRepository>) -> Self {
|
||||||
|
Self { repo }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl QrCampaignService for QrCampaignServiceImpl {
|
||||||
|
async fn create(
|
||||||
|
&self,
|
||||||
|
name: String,
|
||||||
|
url: String,
|
||||||
|
created_by: Uuid,
|
||||||
|
) -> Result<CampaignEntity, AppError> {
|
||||||
|
let qr = QrCode::new(url.as_bytes())
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||||
|
let qr_img = qr
|
||||||
|
.render::<image::Luma<u8>>()
|
||||||
|
.min_dimensions(256, 256)
|
||||||
|
.build();
|
||||||
|
let mut qr_bytes = Vec::new();
|
||||||
|
DynamicImage::ImageLuma8(qr_img)
|
||||||
|
.write_to(&mut Cursor::new(&mut qr_bytes), ImageFormat::Png)
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||||
|
|
||||||
|
let input = CreateCampaignInput {
|
||||||
|
name,
|
||||||
|
url,
|
||||||
|
created_by,
|
||||||
|
qr_code_data: qr_bytes,
|
||||||
|
};
|
||||||
|
self.repo.create(input).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn list_all(&self) -> Result<Vec<CampaignEntity>, AppError> {
|
||||||
|
self.repo.find_all().await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_active_qr_data(&self) -> Result<Option<Vec<u8>>, AppError> {
|
||||||
|
self.repo.find_active_qr_data().await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn set_active(&self, id: Uuid) -> Result<CampaignEntity, AppError> {
|
||||||
|
self.repo.set_active(id).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn delete(&self, id: Uuid) -> Result<(), AppError> {
|
||||||
|
self.repo.delete(id).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn process_image(&self, image_bytes: Vec<u8>) -> Result<Vec<u8>, AppError> {
|
||||||
|
let qr_data = self
|
||||||
|
.repo
|
||||||
|
.find_active_qr_data()
|
||||||
|
.await?
|
||||||
|
.ok_or_else(|| AppError::NotFoundError("No active campaign".to_string()))?;
|
||||||
|
|
||||||
|
let img = image::load_from_memory(&image_bytes)
|
||||||
|
.map_err(|_| AppError::BadRequestError("Invalid image format".to_string()))?;
|
||||||
|
|
||||||
|
let qr_img = image::load_from_memory(&qr_data).map_err(|_| {
|
||||||
|
AppError::InternalServerError("Failed to load QR data".to_string())
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let (w, h) = img.dimensions();
|
||||||
|
let qr_size = (std::cmp::min(w, h) / 5).max(100);
|
||||||
|
|
||||||
|
let qr_resized =
|
||||||
|
qr_img.resize_exact(qr_size, qr_size, imageops::FilterType::Nearest);
|
||||||
|
|
||||||
|
let mut output = img.to_rgba8();
|
||||||
|
let x = (w - qr_size - 10) as i64;
|
||||||
|
let y = (h - qr_size - 10) as i64;
|
||||||
|
imageops::overlay(&mut output, &qr_resized.to_rgba8(), x, y);
|
||||||
|
|
||||||
|
let mut out_bytes = Vec::new();
|
||||||
|
DynamicImage::ImageRgba8(output)
|
||||||
|
.write_to(&mut Cursor::new(&mut out_bytes), ImageFormat::Png)
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||||
|
|
||||||
|
Ok(out_bytes)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
pub mod campaign_service;
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
|
pub struct CampaignEntity {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub name: String,
|
||||||
|
pub url: String,
|
||||||
|
pub is_active: bool,
|
||||||
|
pub created_by: Uuid,
|
||||||
|
pub expires_at: DateTime<Utc>,
|
||||||
|
pub created_at: Option<DateTime<Utc>>,
|
||||||
|
pub updated_at: Option<DateTime<Utc>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct CreateCampaignInput {
|
||||||
|
pub name: String,
|
||||||
|
pub url: String,
|
||||||
|
pub created_by: Uuid,
|
||||||
|
pub qr_code_data: Vec<u8>,
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
pub mod entity;
|
||||||
|
pub mod repository;
|
||||||
|
pub mod service;
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
use async_trait::async_trait;
|
||||||
|
use imphnen_utils::errors::AppError;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::entity::{CampaignEntity, CreateCampaignInput};
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait CampaignRepository: Send + Sync {
|
||||||
|
async fn create(
|
||||||
|
&self,
|
||||||
|
input: CreateCampaignInput,
|
||||||
|
) -> Result<CampaignEntity, AppError>;
|
||||||
|
async fn find_all(&self) -> Result<Vec<CampaignEntity>, AppError>;
|
||||||
|
async fn find_active_qr_data(&self) -> Result<Option<Vec<u8>>, AppError>;
|
||||||
|
async fn set_active(&self, id: Uuid) -> Result<CampaignEntity, AppError>;
|
||||||
|
async fn delete(&self, id: Uuid) -> Result<(), AppError>;
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
use async_trait::async_trait;
|
||||||
|
use imphnen_utils::errors::AppError;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::entity::CampaignEntity;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait QrCampaignService: Send + Sync {
|
||||||
|
async fn create(
|
||||||
|
&self,
|
||||||
|
name: String,
|
||||||
|
url: String,
|
||||||
|
created_by: Uuid,
|
||||||
|
) -> Result<CampaignEntity, AppError>;
|
||||||
|
async fn list_all(&self) -> Result<Vec<CampaignEntity>, AppError>;
|
||||||
|
async fn get_active_qr_data(&self) -> Result<Option<Vec<u8>>, AppError>;
|
||||||
|
async fn set_active(&self, id: Uuid) -> Result<CampaignEntity, AppError>;
|
||||||
|
async fn delete(&self, id: Uuid) -> Result<(), AppError>;
|
||||||
|
async fn process_image(&self, image_bytes: Vec<u8>) -> Result<Vec<u8>, AppError>;
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use utoipa::ToSchema;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, ToSchema)]
|
||||||
|
pub struct CreateCampaignRequest {
|
||||||
|
pub name: String,
|
||||||
|
pub url: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, ToSchema)]
|
||||||
|
pub struct CampaignResponse {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub name: String,
|
||||||
|
pub url: String,
|
||||||
|
pub is_active: bool,
|
||||||
|
pub created_by: Uuid,
|
||||||
|
pub expires_at: DateTime<Utc>,
|
||||||
|
pub created_at: Option<DateTime<Utc>>,
|
||||||
|
pub updated_at: Option<DateTime<Utc>>,
|
||||||
|
}
|
||||||
@@ -0,0 +1,204 @@
|
|||||||
|
use axum::{
|
||||||
|
Extension, Json,
|
||||||
|
extract::{Multipart, Path},
|
||||||
|
response::{IntoResponse, Response},
|
||||||
|
};
|
||||||
|
use imphnen_utils::{errors::AppError, response_format::ApiSuccess};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::qr::{
|
||||||
|
campaigns::{
|
||||||
|
domain::service::QrCampaignService,
|
||||||
|
infrastructure::http::dto::CreateCampaignRequest,
|
||||||
|
},
|
||||||
|
middleware::qr_auth::QrAuthUser,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = "/v1/qr/campaigns",
|
||||||
|
request_body = CreateCampaignRequest,
|
||||||
|
responses(
|
||||||
|
(status = 201, description = "Create a QR campaign",
|
||||||
|
example = json!({
|
||||||
|
"data": {
|
||||||
|
"id": "e5f6a7b8-c9d0-1234-efab-345678901234",
|
||||||
|
"name": "Imphnen Hackathon 2025",
|
||||||
|
"url": "https://imphnen.dev/register",
|
||||||
|
"is_active": false,
|
||||||
|
"created_by": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
|
||||||
|
"expires_at": "2025-12-31T23:59:59Z",
|
||||||
|
"created_at": "2025-01-01T00:00:00Z",
|
||||||
|
"updated_at": "2025-01-01T00:00:00Z"
|
||||||
|
},
|
||||||
|
"version": "0.3.0"
|
||||||
|
})),
|
||||||
|
(status = 401, description = "Unauthorized"),
|
||||||
|
(status = 403, description = "Forbidden - admin only")
|
||||||
|
),
|
||||||
|
tag = "QR - Campaigns",
|
||||||
|
security(("Bearer" = []))
|
||||||
|
)]
|
||||||
|
pub async fn create_campaign_handler(
|
||||||
|
Extension(service): Extension<Arc<dyn QrCampaignService>>,
|
||||||
|
Extension(auth_user): Extension<QrAuthUser>,
|
||||||
|
Json(body): Json<CreateCampaignRequest>,
|
||||||
|
) -> Result<Response, AppError> {
|
||||||
|
if auth_user.role != "admin" {
|
||||||
|
return Err(AppError::ForbiddenError(
|
||||||
|
"Admin access required".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
let campaign = service
|
||||||
|
.create(body.name, body.url, auth_user.user_id)
|
||||||
|
.await?;
|
||||||
|
Ok(imphnen_utils::response_format::ApiCreated(campaign).into_response())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/v1/qr/campaigns",
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Admin: list all QR campaigns",
|
||||||
|
example = json!({
|
||||||
|
"data": [
|
||||||
|
{
|
||||||
|
"id": "e5f6a7b8-c9d0-1234-efab-345678901234",
|
||||||
|
"name": "Imphnen Hackathon 2025",
|
||||||
|
"url": "https://imphnen.dev/register",
|
||||||
|
"is_active": true,
|
||||||
|
"created_by": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
|
||||||
|
"expires_at": "2025-12-31T23:59:59Z",
|
||||||
|
"created_at": "2025-01-01T00:00:00Z",
|
||||||
|
"updated_at": "2025-01-05T00:00:00Z"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"version": "0.3.0"
|
||||||
|
})),
|
||||||
|
(status = 401, description = "Unauthorized"),
|
||||||
|
(status = 403, description = "Forbidden - admin only")
|
||||||
|
),
|
||||||
|
tag = "QR - Campaigns",
|
||||||
|
security(("Bearer" = []))
|
||||||
|
)]
|
||||||
|
pub async fn list_campaigns_handler(
|
||||||
|
Extension(service): Extension<Arc<dyn QrCampaignService>>,
|
||||||
|
Extension(auth_user): Extension<QrAuthUser>,
|
||||||
|
) -> Result<Response, AppError> {
|
||||||
|
if auth_user.role != "admin" {
|
||||||
|
return Err(AppError::ForbiddenError(
|
||||||
|
"Admin access required".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
let campaigns = service.list_all().await?;
|
||||||
|
Ok(ApiSuccess(campaigns).into_response())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
put,
|
||||||
|
path = "/v1/qr/campaigns/{id}/activate",
|
||||||
|
params(("id" = Uuid, Path, description = "Campaign ID")),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Admin: activate a campaign (deactivates all others)",
|
||||||
|
example = json!({
|
||||||
|
"data": {
|
||||||
|
"id": "e5f6a7b8-c9d0-1234-efab-345678901234",
|
||||||
|
"name": "Imphnen Hackathon 2025",
|
||||||
|
"url": "https://imphnen.dev/register",
|
||||||
|
"is_active": true,
|
||||||
|
"created_by": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
|
||||||
|
"expires_at": "2025-12-31T23:59:59Z",
|
||||||
|
"created_at": "2025-01-01T00:00:00Z",
|
||||||
|
"updated_at": "2025-01-05T10:00:00Z"
|
||||||
|
},
|
||||||
|
"version": "0.3.0"
|
||||||
|
})),
|
||||||
|
(status = 401, description = "Unauthorized"),
|
||||||
|
(status = 403, description = "Forbidden - admin only")
|
||||||
|
),
|
||||||
|
tag = "QR - Campaigns",
|
||||||
|
security(("Bearer" = []))
|
||||||
|
)]
|
||||||
|
pub async fn activate_campaign_handler(
|
||||||
|
Extension(service): Extension<Arc<dyn QrCampaignService>>,
|
||||||
|
Extension(auth_user): Extension<QrAuthUser>,
|
||||||
|
Path(id): Path<Uuid>,
|
||||||
|
) -> Result<Response, AppError> {
|
||||||
|
if auth_user.role != "admin" {
|
||||||
|
return Err(AppError::ForbiddenError(
|
||||||
|
"Admin access required".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
let campaign = service.set_active(id).await?;
|
||||||
|
Ok(ApiSuccess(campaign).into_response())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
delete,
|
||||||
|
path = "/v1/qr/campaigns/{id}",
|
||||||
|
params(("id" = Uuid, Path, description = "Campaign ID")),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Admin: delete a campaign",
|
||||||
|
example = json!({"message": "Campaign deleted successfully", "version": "0.3.0"})),
|
||||||
|
(status = 401, description = "Unauthorized"),
|
||||||
|
(status = 403, description = "Forbidden - admin only")
|
||||||
|
),
|
||||||
|
tag = "QR - Campaigns",
|
||||||
|
security(("Bearer" = []))
|
||||||
|
)]
|
||||||
|
pub async fn delete_campaign_handler(
|
||||||
|
Extension(service): Extension<Arc<dyn QrCampaignService>>,
|
||||||
|
Extension(auth_user): Extension<QrAuthUser>,
|
||||||
|
Path(id): Path<Uuid>,
|
||||||
|
) -> Result<Response, AppError> {
|
||||||
|
if auth_user.role != "admin" {
|
||||||
|
return Err(AppError::ForbiddenError(
|
||||||
|
"Admin access required".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
service.delete(id).await?;
|
||||||
|
Ok(
|
||||||
|
imphnen_utils::response_format::ApiMessage::ok("Campaign deleted successfully")
|
||||||
|
.into_response(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = "/v1/qr/campaigns/process-image",
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Process QR code image — send multipart/form-data with field 'file'. Returns PNG image bytes.",
|
||||||
|
content_type = "image/png"),
|
||||||
|
(status = 400, description = "No file provided or invalid image"),
|
||||||
|
(status = 401, description = "Unauthorized")
|
||||||
|
),
|
||||||
|
tag = "QR - Campaigns",
|
||||||
|
security(("Bearer" = []))
|
||||||
|
)]
|
||||||
|
pub async fn process_image_handler(
|
||||||
|
Extension(service): Extension<Arc<dyn QrCampaignService>>,
|
||||||
|
Extension(_auth_user): Extension<QrAuthUser>,
|
||||||
|
mut multipart: Multipart,
|
||||||
|
) -> Result<Response, AppError> {
|
||||||
|
let mut image_bytes = Vec::new();
|
||||||
|
while let Some(field) = multipart
|
||||||
|
.next_field()
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::BadRequestError(e.to_string()))?
|
||||||
|
{
|
||||||
|
if field.name() == Some("file") {
|
||||||
|
image_bytes = field
|
||||||
|
.bytes()
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::BadRequestError(e.to_string()))?
|
||||||
|
.to_vec();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if image_bytes.is_empty() {
|
||||||
|
return Err(AppError::BadRequestError("No file provided".to_string()));
|
||||||
|
}
|
||||||
|
let png_bytes = service.process_image(image_bytes).await?;
|
||||||
|
Ok(([(axum::http::header::CONTENT_TYPE, "image/png")], png_bytes).into_response())
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
pub mod dto;
|
||||||
|
pub mod handlers;
|
||||||
|
pub mod routes;
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
use axum::{
|
||||||
|
Extension, Router,
|
||||||
|
middleware::from_fn,
|
||||||
|
routing::{delete, post, put},
|
||||||
|
};
|
||||||
|
use sqlx::PgPool;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use crate::qr::{
|
||||||
|
campaigns::{
|
||||||
|
application::campaign_service::QrCampaignServiceImpl,
|
||||||
|
domain::{repository::CampaignRepository, service::QrCampaignService},
|
||||||
|
infrastructure::{
|
||||||
|
http::handlers::{
|
||||||
|
activate_campaign_handler, create_campaign_handler, delete_campaign_handler,
|
||||||
|
list_campaigns_handler, process_image_handler,
|
||||||
|
},
|
||||||
|
persistence::postgres_campaign_repository::PostgresCampaignRepository,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
middleware::qr_auth::qr_auth_middleware,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn qr_campaigns_routes(pool: Arc<PgPool>) -> Router {
|
||||||
|
let repo: Arc<dyn CampaignRepository> =
|
||||||
|
Arc::new(PostgresCampaignRepository::new(pool.clone()));
|
||||||
|
let service: Arc<dyn QrCampaignService> =
|
||||||
|
Arc::new(QrCampaignServiceImpl::new(repo));
|
||||||
|
|
||||||
|
Router::new()
|
||||||
|
.route(
|
||||||
|
"/campaigns",
|
||||||
|
post(create_campaign_handler).get(list_campaigns_handler),
|
||||||
|
)
|
||||||
|
.route("/campaigns/{id}/activate", put(activate_campaign_handler))
|
||||||
|
.route("/campaigns/{id}", delete(delete_campaign_handler))
|
||||||
|
.route("/campaigns/process-image", post(process_image_handler))
|
||||||
|
.layer(Extension(service))
|
||||||
|
.layer(from_fn(qr_auth_middleware))
|
||||||
|
.layer(Extension(pool))
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
pub mod http;
|
||||||
|
pub mod persistence;
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
pub mod postgres_campaign_repository;
|
||||||
+147
@@ -0,0 +1,147 @@
|
|||||||
|
use async_trait::async_trait;
|
||||||
|
use imphnen_utils::errors::AppError;
|
||||||
|
use sqlx::FromRow;
|
||||||
|
use sqlx::PgPool;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::qr::campaigns::domain::{
|
||||||
|
entity::{CampaignEntity, CreateCampaignInput},
|
||||||
|
repository::CampaignRepository,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(FromRow)]
|
||||||
|
struct CampaignRow {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub name: String,
|
||||||
|
pub url: String,
|
||||||
|
pub is_active: bool,
|
||||||
|
pub created_by: Uuid,
|
||||||
|
pub expires_at: chrono::DateTime<chrono::Utc>,
|
||||||
|
pub created_at: Option<chrono::DateTime<chrono::Utc>>,
|
||||||
|
pub updated_at: Option<chrono::DateTime<chrono::Utc>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<CampaignRow> for CampaignEntity {
|
||||||
|
fn from(row: CampaignRow) -> Self {
|
||||||
|
CampaignEntity {
|
||||||
|
id: row.id,
|
||||||
|
name: row.name,
|
||||||
|
url: row.url,
|
||||||
|
is_active: row.is_active,
|
||||||
|
created_by: row.created_by,
|
||||||
|
expires_at: row.expires_at,
|
||||||
|
created_at: row.created_at,
|
||||||
|
updated_at: row.updated_at,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct PostgresCampaignRepository {
|
||||||
|
pool: Arc<PgPool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PostgresCampaignRepository {
|
||||||
|
pub fn new(pool: Arc<PgPool>) -> Self {
|
||||||
|
Self { pool }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl CampaignRepository for PostgresCampaignRepository {
|
||||||
|
async fn create(
|
||||||
|
&self,
|
||||||
|
input: CreateCampaignInput,
|
||||||
|
) -> Result<CampaignEntity, AppError> {
|
||||||
|
let mut tx = self
|
||||||
|
.pool
|
||||||
|
.begin()
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||||
|
|
||||||
|
sqlx::query("UPDATE qr_campaigns SET is_active = false, updated_at = NOW()")
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||||
|
|
||||||
|
let id = Uuid::new_v4();
|
||||||
|
let campaign = sqlx::query_as::<_, CampaignRow>(
|
||||||
|
"INSERT INTO qr_campaigns (id, name, url, qr_code_data, is_active, created_by, expires_at) \
|
||||||
|
VALUES ($1, $2, $3, $4, true, $5, NOW() + INTERVAL '30 days') \
|
||||||
|
RETURNING id, name, url, is_active, created_by, expires_at, created_at, updated_at",
|
||||||
|
)
|
||||||
|
.bind(id)
|
||||||
|
.bind(&input.name)
|
||||||
|
.bind(&input.url)
|
||||||
|
.bind(&input.qr_code_data)
|
||||||
|
.bind(input.created_by)
|
||||||
|
.fetch_one(&mut *tx)
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||||
|
|
||||||
|
tx.commit()
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||||
|
|
||||||
|
Ok(campaign.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn find_all(&self) -> Result<Vec<CampaignEntity>, AppError> {
|
||||||
|
sqlx::query_as::<_, CampaignRow>(
|
||||||
|
"SELECT id, name, url, is_active, created_by, expires_at, created_at, updated_at \
|
||||||
|
FROM qr_campaigns ORDER BY created_at DESC",
|
||||||
|
)
|
||||||
|
.fetch_all(self.pool.as_ref())
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))
|
||||||
|
.map(|rows| rows.into_iter().map(Into::into).collect())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn find_active_qr_data(&self) -> Result<Option<Vec<u8>>, AppError> {
|
||||||
|
let row = sqlx::query_as::<_, (Vec<u8>,)>(
|
||||||
|
"SELECT qr_code_data FROM qr_campaigns WHERE is_active = true LIMIT 1",
|
||||||
|
)
|
||||||
|
.fetch_optional(self.pool.as_ref())
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||||
|
|
||||||
|
Ok(row.map(|r| r.0))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn set_active(&self, id: Uuid) -> Result<CampaignEntity, AppError> {
|
||||||
|
let mut tx = self
|
||||||
|
.pool
|
||||||
|
.begin()
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||||
|
|
||||||
|
sqlx::query("UPDATE qr_campaigns SET is_active = false, updated_at = NOW()")
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||||
|
|
||||||
|
let campaign = sqlx::query_as::<_, CampaignRow>(
|
||||||
|
"UPDATE qr_campaigns SET is_active = true, updated_at = NOW() WHERE id = $1 \
|
||||||
|
RETURNING id, name, url, is_active, created_by, expires_at, created_at, updated_at",
|
||||||
|
)
|
||||||
|
.bind(id)
|
||||||
|
.fetch_one(&mut *tx)
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||||
|
|
||||||
|
tx.commit()
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||||
|
|
||||||
|
Ok(campaign.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn delete(&self, id: Uuid) -> Result<(), AppError> {
|
||||||
|
sqlx::query("DELETE FROM qr_campaigns WHERE id = $1")
|
||||||
|
.bind(id)
|
||||||
|
.execute(self.pool.as_ref())
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
pub mod application;
|
||||||
|
pub mod domain;
|
||||||
|
pub mod infrastructure;
|
||||||
|
pub use infrastructure::http::routes::qr_campaigns_routes;
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
pub mod qr_auth;
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
use axum::http::StatusCode;
|
||||||
|
use axum::{
|
||||||
|
body::Body,
|
||||||
|
extract::Request,
|
||||||
|
middleware::Next,
|
||||||
|
response::{IntoResponse, Response},
|
||||||
|
};
|
||||||
|
use imphnen_libs::decode_access_token;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use sqlx::PgPool;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct QrAuthUser {
|
||||||
|
pub user_id: Uuid,
|
||||||
|
pub role: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn qr_auth_middleware(
|
||||||
|
axum::Extension(pool): axum::Extension<Arc<PgPool>>,
|
||||||
|
mut request: Request<Body>,
|
||||||
|
next: Next,
|
||||||
|
) -> Result<Response, Response> {
|
||||||
|
let auth_header = request
|
||||||
|
.headers()
|
||||||
|
.get("Authorization")
|
||||||
|
.and_then(|h| h.to_str().ok())
|
||||||
|
.ok_or_else(|| {
|
||||||
|
(StatusCode::UNAUTHORIZED, "Missing Authorization header").into_response()
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let token = auth_header.strip_prefix("Bearer ").ok_or_else(|| {
|
||||||
|
(
|
||||||
|
StatusCode::UNAUTHORIZED,
|
||||||
|
"Invalid Authorization header format",
|
||||||
|
)
|
||||||
|
.into_response()
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let token_data = decode_access_token(token).map_err(|_| {
|
||||||
|
(StatusCode::UNAUTHORIZED, "Invalid or expired token").into_response()
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let user_id = Uuid::parse_str(&token_data.claims.user_id).map_err(|_| {
|
||||||
|
(StatusCode::UNAUTHORIZED, "Invalid user ID in token").into_response()
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let _ = sqlx::query(
|
||||||
|
"INSERT INTO qr_users (id, email, name, role, provider) VALUES ($1, $2, $2, 'user', 'external') ON CONFLICT (id) DO NOTHING"
|
||||||
|
)
|
||||||
|
.bind(user_id)
|
||||||
|
.bind(&token_data.claims.sub)
|
||||||
|
.execute(pool.as_ref())
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let role: String = sqlx::query_scalar("SELECT role FROM qr_users WHERE id = $1")
|
||||||
|
.bind(user_id)
|
||||||
|
.fetch_optional(pool.as_ref())
|
||||||
|
.await
|
||||||
|
.ok()
|
||||||
|
.flatten()
|
||||||
|
.unwrap_or_else(|| "user".to_string());
|
||||||
|
|
||||||
|
request
|
||||||
|
.extensions_mut()
|
||||||
|
.insert(QrAuthUser { user_id, role });
|
||||||
|
Ok(next.run(request).await)
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
pub mod campaigns;
|
||||||
|
pub mod middleware;
|
||||||
|
pub mod users;
|
||||||
|
|
||||||
|
use axum::Router;
|
||||||
|
use sea_orm::DatabaseConnection;
|
||||||
|
use sqlx::PgPool;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
pub fn qr_router(db: DatabaseConnection) -> Router {
|
||||||
|
let pool: Arc<PgPool> = Arc::new(db.get_postgres_connection_pool().clone());
|
||||||
|
Router::new()
|
||||||
|
.merge(users::infrastructure::http::routes::qr_users_routes(pool.clone()))
|
||||||
|
.merge(campaigns::infrastructure::http::routes::qr_campaigns_routes(pool))
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
pub mod user_service;
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
use async_trait::async_trait;
|
||||||
|
use imphnen_utils::errors::AppError;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::qr::users::domain::{
|
||||||
|
entity::{UpdateUserInput, UserEntity},
|
||||||
|
repository::UserRepository,
|
||||||
|
service::QrUserService,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct QrUserServiceImpl {
|
||||||
|
repo: Arc<dyn UserRepository>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl QrUserServiceImpl {
|
||||||
|
pub fn new(repo: Arc<dyn UserRepository>) -> Self {
|
||||||
|
Self { repo }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl QrUserService for QrUserServiceImpl {
|
||||||
|
async fn get_profile(&self, user_id: Uuid) -> Result<UserEntity, AppError> {
|
||||||
|
self
|
||||||
|
.repo
|
||||||
|
.find_by_id(user_id)
|
||||||
|
.await?
|
||||||
|
.ok_or_else(|| AppError::NotFoundError("User not found".to_string()))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update_profile(
|
||||||
|
&self,
|
||||||
|
user_id: Uuid,
|
||||||
|
input: UpdateUserInput,
|
||||||
|
) -> Result<UserEntity, AppError> {
|
||||||
|
if let Some(ref email) = input.email
|
||||||
|
&& email.trim().is_empty()
|
||||||
|
{
|
||||||
|
return Err(AppError::ValidationError(
|
||||||
|
"Email cannot be empty".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
self.repo.update(user_id, input).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn list_all(&self) -> Result<Vec<UserEntity>, AppError> {
|
||||||
|
self.repo.find_all().await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update_role(
|
||||||
|
&self,
|
||||||
|
id: Uuid,
|
||||||
|
role: String,
|
||||||
|
) -> Result<UserEntity, AppError> {
|
||||||
|
self.repo.update_role(id, role).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn delete(&self, id: Uuid) -> Result<(), AppError> {
|
||||||
|
self.repo.delete(id).await
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
|
pub struct UserEntity {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub email: String,
|
||||||
|
pub name: String,
|
||||||
|
pub role: String,
|
||||||
|
pub provider: String,
|
||||||
|
pub created_at: Option<DateTime<Utc>>,
|
||||||
|
pub updated_at: Option<DateTime<Utc>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct UpdateUserInput {
|
||||||
|
pub name: Option<String>,
|
||||||
|
pub email: Option<String>,
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
pub mod entity;
|
||||||
|
pub mod repository;
|
||||||
|
pub mod service;
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
use async_trait::async_trait;
|
||||||
|
use imphnen_utils::errors::AppError;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::entity::{UpdateUserInput, UserEntity};
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait UserRepository: Send + Sync {
|
||||||
|
async fn find_by_id(&self, id: Uuid) -> Result<Option<UserEntity>, AppError>;
|
||||||
|
async fn find_all(&self) -> Result<Vec<UserEntity>, AppError>;
|
||||||
|
async fn update(
|
||||||
|
&self,
|
||||||
|
id: Uuid,
|
||||||
|
input: UpdateUserInput,
|
||||||
|
) -> Result<UserEntity, AppError>;
|
||||||
|
async fn update_role(
|
||||||
|
&self,
|
||||||
|
id: Uuid,
|
||||||
|
role: String,
|
||||||
|
) -> Result<UserEntity, AppError>;
|
||||||
|
async fn delete(&self, id: Uuid) -> Result<(), AppError>;
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
use async_trait::async_trait;
|
||||||
|
use imphnen_utils::errors::AppError;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::entity::{UpdateUserInput, UserEntity};
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait QrUserService: Send + Sync {
|
||||||
|
async fn get_profile(&self, user_id: Uuid) -> Result<UserEntity, AppError>;
|
||||||
|
async fn update_profile(
|
||||||
|
&self,
|
||||||
|
user_id: Uuid,
|
||||||
|
input: UpdateUserInput,
|
||||||
|
) -> Result<UserEntity, AppError>;
|
||||||
|
async fn list_all(&self) -> Result<Vec<UserEntity>, AppError>;
|
||||||
|
async fn update_role(
|
||||||
|
&self,
|
||||||
|
id: Uuid,
|
||||||
|
role: String,
|
||||||
|
) -> Result<UserEntity, AppError>;
|
||||||
|
async fn delete(&self, id: Uuid) -> Result<(), AppError>;
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use utoipa::ToSchema;
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, ToSchema)]
|
||||||
|
pub struct UpdateProfileRequest {
|
||||||
|
pub name: Option<String>,
|
||||||
|
pub email: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, ToSchema)]
|
||||||
|
pub struct UpdateRoleRequest {
|
||||||
|
pub role: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, ToSchema)]
|
||||||
|
pub struct UserResponse {
|
||||||
|
pub id: String,
|
||||||
|
pub email: String,
|
||||||
|
pub name: String,
|
||||||
|
pub role: String,
|
||||||
|
pub provider: String,
|
||||||
|
}
|
||||||
@@ -0,0 +1,199 @@
|
|||||||
|
use axum::{
|
||||||
|
Extension, Json,
|
||||||
|
extract::Path,
|
||||||
|
response::{IntoResponse, Response},
|
||||||
|
};
|
||||||
|
use imphnen_utils::{errors::AppError, response_format::ApiSuccess};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::qr::{
|
||||||
|
middleware::qr_auth::QrAuthUser,
|
||||||
|
users::{
|
||||||
|
domain::{entity::UpdateUserInput, service::QrUserService},
|
||||||
|
infrastructure::http::dto::{UpdateProfileRequest, UpdateRoleRequest},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/v1/qr/users/me",
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Get my QR user profile",
|
||||||
|
example = json!({
|
||||||
|
"data": {
|
||||||
|
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
|
||||||
|
"email": "user@example.com",
|
||||||
|
"name": "Budi Santoso",
|
||||||
|
"role": "user",
|
||||||
|
"provider": "google",
|
||||||
|
"created_at": "2025-01-01T00:00:00Z",
|
||||||
|
"updated_at": "2025-01-01T00:00:00Z"
|
||||||
|
},
|
||||||
|
"version": "0.3.0"
|
||||||
|
})),
|
||||||
|
(status = 401, description = "Unauthorized")
|
||||||
|
),
|
||||||
|
tag = "QR - Users",
|
||||||
|
security(("Bearer" = []))
|
||||||
|
)]
|
||||||
|
pub async fn get_me_handler(
|
||||||
|
Extension(service): Extension<Arc<dyn QrUserService>>,
|
||||||
|
Extension(auth_user): Extension<QrAuthUser>,
|
||||||
|
) -> Result<Response, AppError> {
|
||||||
|
let user = service.get_profile(auth_user.user_id).await?;
|
||||||
|
Ok(ApiSuccess(user).into_response())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
put,
|
||||||
|
path = "/v1/qr/users/me",
|
||||||
|
request_body = UpdateProfileRequest,
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Update my QR user profile",
|
||||||
|
example = json!({
|
||||||
|
"data": {
|
||||||
|
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
|
||||||
|
"email": "updated@example.com",
|
||||||
|
"name": "Budi Santoso Updated",
|
||||||
|
"role": "user",
|
||||||
|
"provider": "google",
|
||||||
|
"created_at": "2025-01-01T00:00:00Z",
|
||||||
|
"updated_at": "2025-01-15T00:00:00Z"
|
||||||
|
},
|
||||||
|
"version": "0.3.0"
|
||||||
|
})),
|
||||||
|
(status = 401, description = "Unauthorized")
|
||||||
|
),
|
||||||
|
tag = "QR - Users",
|
||||||
|
security(("Bearer" = []))
|
||||||
|
)]
|
||||||
|
pub async fn update_me_handler(
|
||||||
|
Extension(service): Extension<Arc<dyn QrUserService>>,
|
||||||
|
Extension(auth_user): Extension<QrAuthUser>,
|
||||||
|
Json(body): Json<UpdateProfileRequest>,
|
||||||
|
) -> Result<Response, AppError> {
|
||||||
|
let input = UpdateUserInput {
|
||||||
|
name: body.name,
|
||||||
|
email: body.email,
|
||||||
|
};
|
||||||
|
let user = service.update_profile(auth_user.user_id, input).await?;
|
||||||
|
Ok(ApiSuccess(user).into_response())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/v1/qr/users",
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Admin: list all QR users",
|
||||||
|
example = json!({
|
||||||
|
"data": [
|
||||||
|
{
|
||||||
|
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
|
||||||
|
"email": "user@example.com",
|
||||||
|
"name": "Budi Santoso",
|
||||||
|
"role": "user",
|
||||||
|
"provider": "google",
|
||||||
|
"created_at": "2025-01-01T00:00:00Z",
|
||||||
|
"updated_at": "2025-01-01T00:00:00Z"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "4gb96g75-6828-5673-c4gd-3d074g77bgb7",
|
||||||
|
"email": "admin@example.com",
|
||||||
|
"name": "Admin User",
|
||||||
|
"role": "admin",
|
||||||
|
"provider": "google",
|
||||||
|
"created_at": "2024-12-01T00:00:00Z",
|
||||||
|
"updated_at": "2024-12-01T00:00:00Z"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"version": "0.3.0"
|
||||||
|
})),
|
||||||
|
(status = 401, description = "Unauthorized"),
|
||||||
|
(status = 403, description = "Forbidden - admin only")
|
||||||
|
),
|
||||||
|
tag = "QR - Users",
|
||||||
|
security(("Bearer" = []))
|
||||||
|
)]
|
||||||
|
pub async fn list_users_handler(
|
||||||
|
Extension(service): Extension<Arc<dyn QrUserService>>,
|
||||||
|
Extension(auth_user): Extension<QrAuthUser>,
|
||||||
|
) -> Result<Response, AppError> {
|
||||||
|
if auth_user.role != "admin" {
|
||||||
|
return Err(AppError::ForbiddenError(
|
||||||
|
"Admin access required".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
let users = service.list_all().await?;
|
||||||
|
Ok(ApiSuccess(users).into_response())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
put,
|
||||||
|
path = "/v1/qr/users/{id}/role",
|
||||||
|
params(("id" = Uuid, Path, description = "User ID")),
|
||||||
|
request_body = UpdateRoleRequest,
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Admin: update user role",
|
||||||
|
example = json!({
|
||||||
|
"data": {
|
||||||
|
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
|
||||||
|
"email": "user@example.com",
|
||||||
|
"name": "Budi Santoso",
|
||||||
|
"role": "admin",
|
||||||
|
"provider": "google",
|
||||||
|
"created_at": "2025-01-01T00:00:00Z",
|
||||||
|
"updated_at": "2025-01-20T00:00:00Z"
|
||||||
|
},
|
||||||
|
"version": "0.3.0"
|
||||||
|
})),
|
||||||
|
(status = 401, description = "Unauthorized"),
|
||||||
|
(status = 403, description = "Forbidden - admin only")
|
||||||
|
),
|
||||||
|
tag = "QR - Users",
|
||||||
|
security(("Bearer" = []))
|
||||||
|
)]
|
||||||
|
pub async fn update_role_handler(
|
||||||
|
Extension(service): Extension<Arc<dyn QrUserService>>,
|
||||||
|
Extension(auth_user): Extension<QrAuthUser>,
|
||||||
|
Path(id): Path<Uuid>,
|
||||||
|
Json(body): Json<UpdateRoleRequest>,
|
||||||
|
) -> Result<Response, AppError> {
|
||||||
|
if auth_user.role != "admin" {
|
||||||
|
return Err(AppError::ForbiddenError(
|
||||||
|
"Admin access required".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
let user = service.update_role(id, body.role).await?;
|
||||||
|
Ok(ApiSuccess(user).into_response())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
delete,
|
||||||
|
path = "/v1/qr/users/{id}",
|
||||||
|
params(("id" = Uuid, Path, description = "User ID")),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Admin: delete QR user",
|
||||||
|
example = json!({"message": "User deleted successfully", "version": "0.3.0"})),
|
||||||
|
(status = 401, description = "Unauthorized"),
|
||||||
|
(status = 403, description = "Forbidden - admin only")
|
||||||
|
),
|
||||||
|
tag = "QR - Users",
|
||||||
|
security(("Bearer" = []))
|
||||||
|
)]
|
||||||
|
pub async fn delete_user_handler(
|
||||||
|
Extension(service): Extension<Arc<dyn QrUserService>>,
|
||||||
|
Extension(auth_user): Extension<QrAuthUser>,
|
||||||
|
Path(id): Path<Uuid>,
|
||||||
|
) -> Result<Response, AppError> {
|
||||||
|
if auth_user.role != "admin" {
|
||||||
|
return Err(AppError::ForbiddenError(
|
||||||
|
"Admin access required".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
service.delete(id).await?;
|
||||||
|
Ok(
|
||||||
|
imphnen_utils::response_format::ApiMessage::ok("User deleted successfully")
|
||||||
|
.into_response(),
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
pub mod dto;
|
||||||
|
pub mod handlers;
|
||||||
|
pub mod routes;
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
use axum::{
|
||||||
|
Extension, Router,
|
||||||
|
middleware::from_fn,
|
||||||
|
routing::{delete, get, put},
|
||||||
|
};
|
||||||
|
use sqlx::PgPool;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use crate::qr::{
|
||||||
|
middleware::qr_auth::qr_auth_middleware,
|
||||||
|
users::{
|
||||||
|
application::user_service::QrUserServiceImpl,
|
||||||
|
domain::{repository::UserRepository, service::QrUserService},
|
||||||
|
infrastructure::{
|
||||||
|
http::handlers::{
|
||||||
|
delete_user_handler, get_me_handler, list_users_handler, update_me_handler,
|
||||||
|
update_role_handler,
|
||||||
|
},
|
||||||
|
persistence::postgres_user_repository::PostgresUserRepository,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn qr_users_routes(pool: Arc<PgPool>) -> Router {
|
||||||
|
let repo: Arc<dyn UserRepository> =
|
||||||
|
Arc::new(PostgresUserRepository::new(pool.clone()));
|
||||||
|
let service: Arc<dyn QrUserService> = Arc::new(QrUserServiceImpl::new(repo));
|
||||||
|
|
||||||
|
Router::new()
|
||||||
|
.route("/users/me", get(get_me_handler).put(update_me_handler))
|
||||||
|
.route("/users", get(list_users_handler))
|
||||||
|
.route("/users/{id}/role", put(update_role_handler))
|
||||||
|
.route("/users/{id}", delete(delete_user_handler))
|
||||||
|
.layer(Extension(service))
|
||||||
|
.layer(from_fn(qr_auth_middleware))
|
||||||
|
.layer(Extension(pool))
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
pub mod http;
|
||||||
|
pub mod persistence;
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
pub mod postgres_user_repository;
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
use async_trait::async_trait;
|
||||||
|
use imphnen_utils::errors::AppError;
|
||||||
|
use sqlx::FromRow;
|
||||||
|
use sqlx::PgPool;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::qr::users::domain::{
|
||||||
|
entity::{UpdateUserInput, UserEntity},
|
||||||
|
repository::UserRepository,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(FromRow)]
|
||||||
|
struct UserRow {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub email: String,
|
||||||
|
pub name: String,
|
||||||
|
pub role: String,
|
||||||
|
pub provider: String,
|
||||||
|
pub created_at: Option<chrono::DateTime<chrono::Utc>>,
|
||||||
|
pub updated_at: Option<chrono::DateTime<chrono::Utc>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<UserRow> for UserEntity {
|
||||||
|
fn from(row: UserRow) -> Self {
|
||||||
|
UserEntity {
|
||||||
|
id: row.id,
|
||||||
|
email: row.email,
|
||||||
|
name: row.name,
|
||||||
|
role: row.role,
|
||||||
|
provider: row.provider,
|
||||||
|
created_at: row.created_at,
|
||||||
|
updated_at: row.updated_at,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct PostgresUserRepository {
|
||||||
|
pool: Arc<PgPool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PostgresUserRepository {
|
||||||
|
pub fn new(pool: Arc<PgPool>) -> Self {
|
||||||
|
Self { pool }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl UserRepository for PostgresUserRepository {
|
||||||
|
async fn find_by_id(&self, id: Uuid) -> Result<Option<UserEntity>, AppError> {
|
||||||
|
sqlx::query_as::<_, UserRow>(
|
||||||
|
"SELECT id, email, name, role, provider, created_at, updated_at FROM qr_users WHERE id = $1",
|
||||||
|
)
|
||||||
|
.bind(id)
|
||||||
|
.fetch_optional(self.pool.as_ref())
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))
|
||||||
|
.map(|opt| opt.map(Into::into))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn find_all(&self) -> Result<Vec<UserEntity>, AppError> {
|
||||||
|
sqlx::query_as::<_, UserRow>(
|
||||||
|
"SELECT id, email, name, role, provider, created_at, updated_at FROM qr_users ORDER BY created_at DESC",
|
||||||
|
)
|
||||||
|
.fetch_all(self.pool.as_ref())
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))
|
||||||
|
.map(|rows| rows.into_iter().map(Into::into).collect())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update(
|
||||||
|
&self,
|
||||||
|
id: Uuid,
|
||||||
|
input: UpdateUserInput,
|
||||||
|
) -> Result<UserEntity, AppError> {
|
||||||
|
sqlx::query_as::<_, UserRow>(
|
||||||
|
"UPDATE qr_users SET name = COALESCE($1, name), email = COALESCE($2, email), updated_at = NOW() WHERE id = $3 RETURNING id, email, name, role, provider, created_at, updated_at",
|
||||||
|
)
|
||||||
|
.bind(input.name)
|
||||||
|
.bind(input.email)
|
||||||
|
.bind(id)
|
||||||
|
.fetch_one(self.pool.as_ref())
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))
|
||||||
|
.map(Into::into)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update_role(
|
||||||
|
&self,
|
||||||
|
id: Uuid,
|
||||||
|
role: String,
|
||||||
|
) -> Result<UserEntity, AppError> {
|
||||||
|
sqlx::query_as::<_, UserRow>(
|
||||||
|
"UPDATE qr_users SET role = $1, updated_at = NOW() WHERE id = $2 RETURNING id, email, name, role, provider, created_at, updated_at",
|
||||||
|
)
|
||||||
|
.bind(role)
|
||||||
|
.bind(id)
|
||||||
|
.fetch_one(self.pool.as_ref())
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))
|
||||||
|
.map(Into::into)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn delete(&self, id: Uuid) -> Result<(), AppError> {
|
||||||
|
sqlx::query("DELETE FROM qr_users WHERE id = $1")
|
||||||
|
.bind(id)
|
||||||
|
.execute(self.pool.as_ref())
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
pub mod application;
|
||||||
|
pub mod domain;
|
||||||
|
pub mod infrastructure;
|
||||||
|
pub use infrastructure::http::routes::qr_users_routes;
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
pub mod roadmap_service;
|
||||||
|
|
||||||
|
pub use roadmap_service::RoadmapServiceImpl;
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
use crate::roadmap::domain::{RoadmapEntity, RoadmapRepository, RoadmapService};
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use imphnen_utils::AppError;
|
||||||
|
use paginator_rs::PaginationParams;
|
||||||
|
use paginator_utils::PaginatorResponse;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
pub struct RoadmapServiceImpl {
|
||||||
|
repo: Arc<dyn RoadmapRepository>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RoadmapServiceImpl {
|
||||||
|
pub fn new(repo: Arc<dyn RoadmapRepository>) -> Self {
|
||||||
|
Self { repo }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl RoadmapService for RoadmapServiceImpl {
|
||||||
|
async fn list(
|
||||||
|
&self,
|
||||||
|
params: PaginationParams,
|
||||||
|
) -> Result<PaginatorResponse<RoadmapEntity>, AppError> {
|
||||||
|
self.repo.find_all(params).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get(&self, id: Uuid) -> Result<RoadmapEntity, AppError> {
|
||||||
|
self.repo.find_by_id(id).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn create(&self, entity: RoadmapEntity) -> Result<(), AppError> {
|
||||||
|
self.repo.create(entity).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update(&self, entity: RoadmapEntity) -> Result<(), AppError> {
|
||||||
|
self.repo.update(entity).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn delete(&self, id: Uuid) -> Result<(), AppError> {
|
||||||
|
self.repo.delete(id).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn vote(&self, id: Uuid) -> Result<(), AppError> {
|
||||||
|
self.repo.increment_votes(id).await
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
pub mod roadmap;
|
||||||
|
pub mod repository;
|
||||||
|
pub mod service;
|
||||||
|
|
||||||
|
pub use roadmap::RoadmapEntity;
|
||||||
|
pub use repository::RoadmapRepository;
|
||||||
|
pub use service::RoadmapService;
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
use super::roadmap::RoadmapEntity;
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use imphnen_utils::AppError;
|
||||||
|
use paginator_rs::PaginationParams;
|
||||||
|
use paginator_utils::PaginatorResponse;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait RoadmapRepository: Send + Sync {
|
||||||
|
async fn find_all(
|
||||||
|
&self,
|
||||||
|
params: PaginationParams,
|
||||||
|
) -> Result<PaginatorResponse<RoadmapEntity>, AppError>;
|
||||||
|
async fn find_by_id(&self, id: Uuid) -> Result<RoadmapEntity, AppError>;
|
||||||
|
async fn create(&self, entity: RoadmapEntity) -> Result<(), AppError>;
|
||||||
|
async fn update(&self, entity: RoadmapEntity) -> Result<(), AppError>;
|
||||||
|
async fn delete(&self, id: Uuid) -> Result<(), AppError>;
|
||||||
|
async fn increment_votes(&self, id: Uuid) -> Result<(), AppError>;
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct RoadmapEntity {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub title: String,
|
||||||
|
pub description: String,
|
||||||
|
pub status: String,
|
||||||
|
pub votes: i32,
|
||||||
|
pub is_deleted: bool,
|
||||||
|
pub created_at: DateTime<Utc>,
|
||||||
|
pub updated_at: DateTime<Utc>,
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
use super::roadmap::RoadmapEntity;
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use imphnen_utils::AppError;
|
||||||
|
use paginator_rs::PaginationParams;
|
||||||
|
use paginator_utils::PaginatorResponse;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait RoadmapService: Send + Sync {
|
||||||
|
async fn list(
|
||||||
|
&self,
|
||||||
|
params: PaginationParams,
|
||||||
|
) -> Result<PaginatorResponse<RoadmapEntity>, AppError>;
|
||||||
|
async fn get(&self, id: Uuid) -> Result<RoadmapEntity, AppError>;
|
||||||
|
async fn create(&self, entity: RoadmapEntity) -> Result<(), AppError>;
|
||||||
|
async fn update(&self, entity: RoadmapEntity) -> Result<(), AppError>;
|
||||||
|
async fn delete(&self, id: Uuid) -> Result<(), AppError>;
|
||||||
|
async fn vote(&self, id: Uuid) -> Result<(), AppError>;
|
||||||
|
}
|
||||||
@@ -0,0 +1,98 @@
|
|||||||
|
use crate::roadmap::domain::roadmap::RoadmapEntity;
|
||||||
|
use imphnen_libs::ZodValidate;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use utoipa::ToSchema;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct RoadmapCreateRequestDto {
|
||||||
|
pub title: String,
|
||||||
|
pub description: String,
|
||||||
|
#[schema(example = "upcoming")]
|
||||||
|
pub status: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ZodValidate for RoadmapCreateRequestDto {
|
||||||
|
fn zod_validate(value: &serde_json::Value) -> Result<Self, String> {
|
||||||
|
serde_json::from_value(value.clone()).map_err(|e| e.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<RoadmapCreateRequestDto> for RoadmapEntity {
|
||||||
|
fn from(dto: RoadmapCreateRequestDto) -> Self {
|
||||||
|
RoadmapEntity {
|
||||||
|
id: Uuid::new_v4(),
|
||||||
|
title: dto.title,
|
||||||
|
description: dto.description,
|
||||||
|
status: dto.status,
|
||||||
|
votes: 0,
|
||||||
|
is_deleted: false,
|
||||||
|
created_at: chrono::Utc::now(),
|
||||||
|
updated_at: chrono::Utc::now(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct RoadmapUpdateRequestDto {
|
||||||
|
pub title: String,
|
||||||
|
pub description: String,
|
||||||
|
#[schema(example = "upcoming")]
|
||||||
|
pub status: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ZodValidate for RoadmapUpdateRequestDto {
|
||||||
|
fn zod_validate(value: &serde_json::Value) -> Result<Self, String> {
|
||||||
|
serde_json::from_value(value.clone()).map_err(|e| e.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct RoadmapListItemDto {
|
||||||
|
pub id: String,
|
||||||
|
pub title: String,
|
||||||
|
pub description: String,
|
||||||
|
pub status: String,
|
||||||
|
pub votes: i32,
|
||||||
|
pub is_deleted: bool,
|
||||||
|
pub created_at: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<RoadmapEntity> for RoadmapListItemDto {
|
||||||
|
fn from(e: RoadmapEntity) -> Self {
|
||||||
|
RoadmapListItemDto {
|
||||||
|
id: e.id.to_string(),
|
||||||
|
title: e.title,
|
||||||
|
description: e.description,
|
||||||
|
status: e.status,
|
||||||
|
votes: e.votes,
|
||||||
|
is_deleted: e.is_deleted,
|
||||||
|
created_at: e.created_at.to_rfc3339(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct RoadmapDetailItemDto {
|
||||||
|
pub id: String,
|
||||||
|
pub title: String,
|
||||||
|
pub description: String,
|
||||||
|
pub status: String,
|
||||||
|
pub votes: i32,
|
||||||
|
pub created_at: String,
|
||||||
|
pub updated_at: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<RoadmapEntity> for RoadmapDetailItemDto {
|
||||||
|
fn from(e: RoadmapEntity) -> Self {
|
||||||
|
RoadmapDetailItemDto {
|
||||||
|
id: e.id.to_string(),
|
||||||
|
title: e.title,
|
||||||
|
description: e.description,
|
||||||
|
status: e.status,
|
||||||
|
votes: e.votes,
|
||||||
|
created_at: e.created_at.to_rfc3339(),
|
||||||
|
updated_at: e.updated_at.to_rfc3339(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,208 @@
|
|||||||
|
use super::dto::{
|
||||||
|
RoadmapCreateRequestDto, RoadmapDetailItemDto, RoadmapListItemDto,
|
||||||
|
RoadmapUpdateRequestDto,
|
||||||
|
};
|
||||||
|
use crate::roadmap::domain::RoadmapService;
|
||||||
|
use axum::{
|
||||||
|
Extension,
|
||||||
|
extract::Path,
|
||||||
|
http::HeaderMap,
|
||||||
|
response::{IntoResponse, Response},
|
||||||
|
};
|
||||||
|
use imphnen_entities::ResponseSuccessDto;
|
||||||
|
use imphnen_iam::{PermissionsEnum, require_permissions};
|
||||||
|
use imphnen_libs::{AppState, ValidatedJson};
|
||||||
|
use imphnen_utils::AppError;
|
||||||
|
use imphnen_utils::{ApiMessage, ApiPaginated, ApiSuccess};
|
||||||
|
use paginator_axum::PaginationQuery;
|
||||||
|
use paginator_utils::PaginatorResponse;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/v1/landing/cms/roadmap",
|
||||||
|
params(
|
||||||
|
("page" = Option<i64>, Query, description = "Page number"),
|
||||||
|
("per_page" = Option<i64>, Query, description = "Items per page"),
|
||||||
|
("search" = Option<String>, Query, description = "Search keyword"),
|
||||||
|
("sort_by" = Option<String>, Query, description = "Sort by field"),
|
||||||
|
("order" = Option<String>, Query, description = "Order ASC or DESC"),
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "[PUBLIC] Get roadmap list")
|
||||||
|
),
|
||||||
|
tag = "Roadmap"
|
||||||
|
)]
|
||||||
|
pub async fn get_roadmap_list(
|
||||||
|
Extension(service): Extension<Arc<dyn RoadmapService>>,
|
||||||
|
PaginationQuery(params): PaginationQuery,
|
||||||
|
) -> Response {
|
||||||
|
match service.list(params).await {
|
||||||
|
Ok(result) => {
|
||||||
|
let mapped = PaginatorResponse {
|
||||||
|
data: result
|
||||||
|
.data
|
||||||
|
.into_iter()
|
||||||
|
.map(RoadmapListItemDto::from)
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
|
meta: result.meta,
|
||||||
|
};
|
||||||
|
ApiPaginated(mapped).into_response()
|
||||||
|
}
|
||||||
|
Err(e) => ApiMessage::new(axum::http::StatusCode::BAD_REQUEST, e.to_string())
|
||||||
|
.into_response(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/v1/landing/cms/roadmap/detail/{id}",
|
||||||
|
params(
|
||||||
|
("id" = String, Path, description = "Roadmap item ID")
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "[PUBLIC] Get roadmap item by ID", body = ResponseSuccessDto<RoadmapDetailItemDto>)
|
||||||
|
),
|
||||||
|
tag = "Roadmap"
|
||||||
|
)]
|
||||||
|
pub async fn get_roadmap_by_id(
|
||||||
|
Extension(service): Extension<Arc<dyn RoadmapService>>,
|
||||||
|
Path(id): Path<String>,
|
||||||
|
) -> Response {
|
||||||
|
let uuid = match Uuid::parse_str(&id) {
|
||||||
|
Ok(u) => u,
|
||||||
|
Err(e) => {
|
||||||
|
return ApiMessage::new(
|
||||||
|
axum::http::StatusCode::BAD_REQUEST,
|
||||||
|
format!("Invalid UUID: {e}"),
|
||||||
|
)
|
||||||
|
.into_response();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
match service.get(uuid).await {
|
||||||
|
Ok(item) => ApiSuccess(RoadmapDetailItemDto::from(item)).into_response(),
|
||||||
|
Err(e) => ApiMessage::new(axum::http::StatusCode::NOT_FOUND, e.to_string())
|
||||||
|
.into_response(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
security(("Bearer" = [])),
|
||||||
|
path = "/v1/landing/cms/roadmap/create",
|
||||||
|
request_body = RoadmapCreateRequestDto,
|
||||||
|
responses(
|
||||||
|
(status = 201, description = "[ADMIN] Create new roadmap item")
|
||||||
|
),
|
||||||
|
tag = "Roadmap"
|
||||||
|
)]
|
||||||
|
pub async fn post_create_roadmap(
|
||||||
|
headers: HeaderMap,
|
||||||
|
Extension(state): Extension<AppState>,
|
||||||
|
Extension(service): Extension<Arc<dyn RoadmapService>>,
|
||||||
|
ValidatedJson(payload): ValidatedJson<RoadmapCreateRequestDto>,
|
||||||
|
) -> Result<impl IntoResponse, AppError> {
|
||||||
|
require_permissions!(headers, state, [PermissionsEnum::Administrator], {
|
||||||
|
let entity = payload.into();
|
||||||
|
service.create(entity).await?;
|
||||||
|
Ok(ApiMessage::created("Roadmap item created"))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
patch,
|
||||||
|
security(("Bearer" = [])),
|
||||||
|
path = "/v1/landing/cms/roadmap/update/{id}",
|
||||||
|
params(
|
||||||
|
("id" = String, Path, description = "Roadmap item ID")
|
||||||
|
),
|
||||||
|
request_body = RoadmapUpdateRequestDto,
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "[ADMIN] Update roadmap item")
|
||||||
|
),
|
||||||
|
tag = "Roadmap"
|
||||||
|
)]
|
||||||
|
pub async fn patch_update_roadmap(
|
||||||
|
headers: HeaderMap,
|
||||||
|
Extension(state): Extension<AppState>,
|
||||||
|
Extension(service): Extension<Arc<dyn RoadmapService>>,
|
||||||
|
Path(id): Path<String>,
|
||||||
|
ValidatedJson(payload): ValidatedJson<RoadmapUpdateRequestDto>,
|
||||||
|
) -> Result<impl IntoResponse, AppError> {
|
||||||
|
require_permissions!(headers, state, [PermissionsEnum::Administrator], {
|
||||||
|
let uuid = Uuid::parse_str(&id)
|
||||||
|
.map_err(|e| AppError::BadRequestError(format!("Invalid UUID: {e}")))?;
|
||||||
|
let existing = service.get(uuid).await?;
|
||||||
|
let entity = crate::roadmap::domain::RoadmapEntity {
|
||||||
|
id: existing.id,
|
||||||
|
title: payload.title,
|
||||||
|
description: payload.description,
|
||||||
|
status: payload.status,
|
||||||
|
votes: existing.votes,
|
||||||
|
is_deleted: existing.is_deleted,
|
||||||
|
created_at: existing.created_at,
|
||||||
|
updated_at: chrono::Utc::now(),
|
||||||
|
};
|
||||||
|
service.update(entity).await?;
|
||||||
|
Ok(ApiMessage::ok("Roadmap item updated"))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
delete,
|
||||||
|
security(("Bearer" = [])),
|
||||||
|
path = "/v1/landing/cms/roadmap/delete/{id}",
|
||||||
|
params(
|
||||||
|
("id" = String, Path, description = "Roadmap item ID")
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "[ADMIN] Soft delete roadmap item")
|
||||||
|
),
|
||||||
|
tag = "Roadmap"
|
||||||
|
)]
|
||||||
|
pub async fn delete_roadmap(
|
||||||
|
headers: HeaderMap,
|
||||||
|
Extension(state): Extension<AppState>,
|
||||||
|
Extension(service): Extension<Arc<dyn RoadmapService>>,
|
||||||
|
Path(id): Path<String>,
|
||||||
|
) -> Result<impl IntoResponse, AppError> {
|
||||||
|
require_permissions!(headers, state, [PermissionsEnum::Administrator], {
|
||||||
|
let uuid = Uuid::parse_str(&id)
|
||||||
|
.map_err(|e| AppError::BadRequestError(format!("Invalid UUID: {e}")))?;
|
||||||
|
service.delete(uuid).await?;
|
||||||
|
Ok(ApiMessage::ok("Roadmap item deleted"))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = "/v1/landing/cms/roadmap/vote/{id}",
|
||||||
|
params(
|
||||||
|
("id" = String, Path, description = "Roadmap item ID")
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "[PUBLIC] Vote for a roadmap item")
|
||||||
|
),
|
||||||
|
tag = "Roadmap"
|
||||||
|
)]
|
||||||
|
pub async fn post_vote_roadmap(
|
||||||
|
Extension(service): Extension<Arc<dyn RoadmapService>>,
|
||||||
|
Path(id): Path<String>,
|
||||||
|
) -> Response {
|
||||||
|
let uuid = match Uuid::parse_str(&id) {
|
||||||
|
Ok(u) => u,
|
||||||
|
Err(e) => {
|
||||||
|
return ApiMessage::new(
|
||||||
|
axum::http::StatusCode::BAD_REQUEST,
|
||||||
|
format!("Invalid UUID: {e}"),
|
||||||
|
)
|
||||||
|
.into_response();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
match service.vote(uuid).await {
|
||||||
|
Ok(()) => ApiMessage::ok("Vote recorded").into_response(),
|
||||||
|
Err(e) => ApiMessage::new(axum::http::StatusCode::BAD_REQUEST, e.to_string())
|
||||||
|
.into_response(),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
pub mod dto;
|
||||||
|
pub mod handlers;
|
||||||
|
pub mod routes;
|
||||||
|
|
||||||
|
pub use routes::{roadmap_protected_routes, roadmap_public_routes};
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
use super::handlers::{
|
||||||
|
delete_roadmap, get_roadmap_by_id, get_roadmap_list, patch_update_roadmap,
|
||||||
|
post_create_roadmap, post_vote_roadmap,
|
||||||
|
};
|
||||||
|
use crate::roadmap::application::RoadmapServiceImpl;
|
||||||
|
use crate::roadmap::domain::RoadmapService;
|
||||||
|
use crate::roadmap::infrastructure::persistence::PostgresRoadmapRepository;
|
||||||
|
use axum::{
|
||||||
|
Extension, Router,
|
||||||
|
routing::{delete, get, patch, post},
|
||||||
|
};
|
||||||
|
use sea_orm::DatabaseConnection;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
fn build_service(db: DatabaseConnection) -> Arc<dyn RoadmapService> {
|
||||||
|
let repo = Arc::new(PostgresRoadmapRepository::new(db));
|
||||||
|
Arc::new(RoadmapServiceImpl::new(repo))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn roadmap_public_routes(db: DatabaseConnection) -> Router {
|
||||||
|
let service = build_service(db);
|
||||||
|
Router::new()
|
||||||
|
.route("/roadmap", get(get_roadmap_list))
|
||||||
|
.route("/roadmap/detail/{id}", get(get_roadmap_by_id))
|
||||||
|
.route("/roadmap/vote/{id}", post(post_vote_roadmap))
|
||||||
|
.layer(Extension(service))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn roadmap_protected_routes(db: DatabaseConnection) -> Router {
|
||||||
|
let service = build_service(db);
|
||||||
|
Router::new()
|
||||||
|
.route("/roadmap/create", post(post_create_roadmap))
|
||||||
|
.route("/roadmap/update/{id}", patch(patch_update_roadmap))
|
||||||
|
.route("/roadmap/delete/{id}", delete(delete_roadmap))
|
||||||
|
.layer(Extension(service))
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
pub mod http;
|
||||||
|
pub mod persistence;
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
pub mod postgres_roadmap_repository;
|
||||||
|
|
||||||
|
pub use postgres_roadmap_repository::PostgresRoadmapRepository;
|
||||||
@@ -0,0 +1,171 @@
|
|||||||
|
use crate::roadmap::domain::{roadmap::RoadmapEntity, repository::RoadmapRepository};
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use imphnen_entities::seaorm::common::roadmap_items::{
|
||||||
|
ActiveModel as RoadmapActiveModel, Column as RoadmapColumn, Entity as RoadmapEntity_,
|
||||||
|
Model as RoadmapModel,
|
||||||
|
};
|
||||||
|
use imphnen_utils::AppError;
|
||||||
|
use paginator_rs::{PaginationParams, SortDirection};
|
||||||
|
use paginator_utils::{PaginatorResponse, PaginatorResponseMeta};
|
||||||
|
use sea_orm::prelude::*;
|
||||||
|
use sea_orm::{ActiveValue, Order, PaginatorTrait, QueryOrder};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
fn to_entity(model: RoadmapModel) -> RoadmapEntity {
|
||||||
|
RoadmapEntity {
|
||||||
|
id: model.id,
|
||||||
|
title: model.title,
|
||||||
|
description: model.description,
|
||||||
|
status: model.status,
|
||||||
|
votes: model.votes,
|
||||||
|
is_deleted: model.is_deleted,
|
||||||
|
created_at: model.created_at,
|
||||||
|
updated_at: model.updated_at,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct PostgresRoadmapRepository {
|
||||||
|
db: Arc<DatabaseConnection>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PostgresRoadmapRepository {
|
||||||
|
pub fn new(db: DatabaseConnection) -> Self {
|
||||||
|
Self { db: Arc::new(db) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl RoadmapRepository for PostgresRoadmapRepository {
|
||||||
|
async fn find_all(
|
||||||
|
&self,
|
||||||
|
params: PaginationParams,
|
||||||
|
) -> Result<PaginatorResponse<RoadmapEntity>, AppError> {
|
||||||
|
let page = params.page.max(1);
|
||||||
|
let per_page = params.per_page.clamp(1, 100);
|
||||||
|
|
||||||
|
let mut query = RoadmapEntity_::find().filter(RoadmapColumn::IsDeleted.eq(false));
|
||||||
|
|
||||||
|
if let Some(ref search) = params.search {
|
||||||
|
query = query.filter(RoadmapColumn::Title.contains(&search.query));
|
||||||
|
}
|
||||||
|
|
||||||
|
query = match params.sort_by.as_deref() {
|
||||||
|
Some("title") => match params.sort_direction {
|
||||||
|
Some(SortDirection::Desc) => query.order_by(RoadmapColumn::Title, Order::Desc),
|
||||||
|
_ => query.order_by(RoadmapColumn::Title, Order::Asc),
|
||||||
|
},
|
||||||
|
Some("votes") => match params.sort_direction {
|
||||||
|
Some(SortDirection::Asc) => query.order_by(RoadmapColumn::Votes, Order::Asc),
|
||||||
|
_ => query.order_by(RoadmapColumn::Votes, Order::Desc),
|
||||||
|
},
|
||||||
|
_ => match params.sort_direction {
|
||||||
|
Some(SortDirection::Asc) => {
|
||||||
|
query.order_by(RoadmapColumn::CreatedAt, Order::Asc)
|
||||||
|
}
|
||||||
|
_ => query.order_by(RoadmapColumn::CreatedAt, Order::Desc),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
let paginator = query.paginate(self.db.as_ref(), per_page as u64);
|
||||||
|
let total = paginator
|
||||||
|
.num_items()
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||||
|
let items = paginator
|
||||||
|
.fetch_page((page - 1) as u64)
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||||
|
|
||||||
|
let data = items.into_iter().map(to_entity).collect();
|
||||||
|
let meta = PaginatorResponseMeta::new(page, per_page, total as u32);
|
||||||
|
Ok(PaginatorResponse { data, meta })
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn find_by_id(&self, id: Uuid) -> Result<RoadmapEntity, AppError> {
|
||||||
|
let item = RoadmapEntity_::find_by_id(id)
|
||||||
|
.filter(RoadmapColumn::IsDeleted.eq(false))
|
||||||
|
.one(self.db.as_ref())
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?
|
||||||
|
.ok_or_else(|| AppError::NotFoundError("Roadmap item not found".to_string()))?;
|
||||||
|
|
||||||
|
Ok(to_entity(item))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn create(&self, entity: RoadmapEntity) -> Result<(), AppError> {
|
||||||
|
let active_model = RoadmapActiveModel {
|
||||||
|
id: ActiveValue::Set(entity.id),
|
||||||
|
title: ActiveValue::Set(entity.title),
|
||||||
|
description: ActiveValue::Set(entity.description),
|
||||||
|
status: ActiveValue::Set(entity.status),
|
||||||
|
votes: ActiveValue::Set(0),
|
||||||
|
is_deleted: ActiveValue::Set(false),
|
||||||
|
created_at: ActiveValue::Set(chrono::Utc::now()),
|
||||||
|
updated_at: ActiveValue::Set(chrono::Utc::now()),
|
||||||
|
};
|
||||||
|
|
||||||
|
RoadmapEntity_::insert(active_model)
|
||||||
|
.exec(self.db.as_ref())
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update(&self, entity: RoadmapEntity) -> Result<(), AppError> {
|
||||||
|
let mut active_model: RoadmapActiveModel = RoadmapEntity_::find_by_id(entity.id)
|
||||||
|
.one(self.db.as_ref())
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?
|
||||||
|
.ok_or_else(|| AppError::NotFoundError("Roadmap item not found".to_string()))?
|
||||||
|
.into();
|
||||||
|
|
||||||
|
active_model.title = ActiveValue::Set(entity.title);
|
||||||
|
active_model.description = ActiveValue::Set(entity.description);
|
||||||
|
active_model.status = ActiveValue::Set(entity.status);
|
||||||
|
active_model.updated_at = ActiveValue::Set(chrono::Utc::now());
|
||||||
|
|
||||||
|
active_model
|
||||||
|
.update(self.db.as_ref())
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn delete(&self, id: Uuid) -> Result<(), AppError> {
|
||||||
|
let mut active_model: RoadmapActiveModel = RoadmapEntity_::find_by_id(id)
|
||||||
|
.one(self.db.as_ref())
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?
|
||||||
|
.ok_or_else(|| AppError::NotFoundError("Roadmap item not found".to_string()))?
|
||||||
|
.into();
|
||||||
|
|
||||||
|
active_model.is_deleted = ActiveValue::Set(true);
|
||||||
|
active_model.updated_at = ActiveValue::Set(chrono::Utc::now());
|
||||||
|
active_model
|
||||||
|
.update(self.db.as_ref())
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn increment_votes(&self, id: Uuid) -> Result<(), AppError> {
|
||||||
|
let item = RoadmapEntity_::find_by_id(id)
|
||||||
|
.filter(RoadmapColumn::IsDeleted.eq(false))
|
||||||
|
.one(self.db.as_ref())
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?
|
||||||
|
.ok_or_else(|| AppError::NotFoundError("Roadmap item not found".to_string()))?;
|
||||||
|
|
||||||
|
let new_votes = item.votes + 1;
|
||||||
|
let mut active_model: RoadmapActiveModel = item.into();
|
||||||
|
active_model.votes = ActiveValue::Set(new_votes);
|
||||||
|
active_model.updated_at = ActiveValue::Set(chrono::Utc::now());
|
||||||
|
active_model
|
||||||
|
.update(self.db.as_ref())
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
pub mod application;
|
||||||
|
pub mod domain;
|
||||||
|
pub mod infrastructure;
|
||||||
|
|
||||||
|
pub use infrastructure::http::{roadmap_protected_routes, roadmap_public_routes};
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
pub mod testimonial_service;
|
||||||
|
|
||||||
|
pub use testimonial_service::TestimonialServiceImpl;
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
use crate::testimonials::domain::{
|
||||||
|
TestimonialEntity, TestimonialRepository, TestimonialService,
|
||||||
|
};
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use imphnen_utils::AppError;
|
||||||
|
use paginator_rs::PaginationParams;
|
||||||
|
use paginator_utils::PaginatorResponse;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
pub struct TestimonialServiceImpl {
|
||||||
|
repo: Arc<dyn TestimonialRepository>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TestimonialServiceImpl {
|
||||||
|
pub fn new(repo: Arc<dyn TestimonialRepository>) -> Self {
|
||||||
|
Self { repo }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl TestimonialService for TestimonialServiceImpl {
|
||||||
|
async fn list(
|
||||||
|
&self,
|
||||||
|
params: PaginationParams,
|
||||||
|
) -> Result<PaginatorResponse<TestimonialEntity>, AppError> {
|
||||||
|
self.repo.find_all(params).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get(&self, id: Uuid) -> Result<TestimonialEntity, AppError> {
|
||||||
|
self.repo.find_by_id(id).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn create(
|
||||||
|
&self,
|
||||||
|
entity: TestimonialEntity,
|
||||||
|
) -> Result<TestimonialEntity, AppError> {
|
||||||
|
self.repo.create(entity).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update(&self, entity: TestimonialEntity) -> Result<(), AppError> {
|
||||||
|
self.repo.update(entity).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn delete(&self, id: Uuid) -> Result<(), AppError> {
|
||||||
|
self.repo.delete(id).await
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
pub mod repository;
|
||||||
|
pub mod service;
|
||||||
|
pub mod testimonial;
|
||||||
|
|
||||||
|
pub use repository::TestimonialRepository;
|
||||||
|
pub use service::TestimonialService;
|
||||||
|
pub use testimonial::TestimonialEntity;
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
use super::testimonial::TestimonialEntity;
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use imphnen_utils::AppError;
|
||||||
|
use paginator_rs::PaginationParams;
|
||||||
|
use paginator_utils::PaginatorResponse;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait TestimonialRepository: Send + Sync {
|
||||||
|
async fn find_all(
|
||||||
|
&self,
|
||||||
|
params: PaginationParams,
|
||||||
|
) -> Result<PaginatorResponse<TestimonialEntity>, AppError>;
|
||||||
|
async fn find_by_id(&self, id: Uuid) -> Result<TestimonialEntity, AppError>;
|
||||||
|
async fn create(
|
||||||
|
&self,
|
||||||
|
entity: TestimonialEntity,
|
||||||
|
) -> Result<TestimonialEntity, AppError>;
|
||||||
|
async fn update(&self, entity: TestimonialEntity) -> Result<(), AppError>;
|
||||||
|
async fn delete(&self, id: Uuid) -> Result<(), AppError>;
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
use super::testimonial::TestimonialEntity;
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use imphnen_utils::AppError;
|
||||||
|
use paginator_rs::PaginationParams;
|
||||||
|
use paginator_utils::PaginatorResponse;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait TestimonialService: Send + Sync {
|
||||||
|
async fn list(
|
||||||
|
&self,
|
||||||
|
params: PaginationParams,
|
||||||
|
) -> Result<PaginatorResponse<TestimonialEntity>, AppError>;
|
||||||
|
async fn get(&self, id: Uuid) -> Result<TestimonialEntity, AppError>;
|
||||||
|
async fn create(
|
||||||
|
&self,
|
||||||
|
entity: TestimonialEntity,
|
||||||
|
) -> Result<TestimonialEntity, AppError>;
|
||||||
|
async fn update(&self, entity: TestimonialEntity) -> Result<(), AppError>;
|
||||||
|
async fn delete(&self, id: Uuid) -> Result<(), AppError>;
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct TestimonialEntity {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub user_id: Uuid,
|
||||||
|
pub user_fullname: String,
|
||||||
|
pub role: String,
|
||||||
|
pub content: String,
|
||||||
|
pub is_deleted: bool,
|
||||||
|
pub created_at: String,
|
||||||
|
pub updated_at: String,
|
||||||
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
use crate::testimonials::domain::testimonial::TestimonialEntity;
|
||||||
|
use imphnen_libs::ZodValidate;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use utoipa::ToSchema;
|
||||||
|
use zod_rs::prelude::*;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema, ZodSchema)]
|
||||||
|
pub struct TestimonialsCreateRequestDto {
|
||||||
|
#[zod(min_length(1), max_length(100))]
|
||||||
|
pub role: String,
|
||||||
|
#[zod(min_length(1), max_length(1000))]
|
||||||
|
pub content: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ZodValidate for TestimonialsCreateRequestDto {
|
||||||
|
fn zod_validate(value: &serde_json::Value) -> Result<Self, String> {
|
||||||
|
Self::validate_and_parse(value).map_err(|e| e.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema, ZodSchema)]
|
||||||
|
pub struct TestimonialsUpdateRequestDto {
|
||||||
|
#[zod(min_length(1), max_length(100))]
|
||||||
|
pub role: String,
|
||||||
|
#[zod(min_length(1), max_length(1000))]
|
||||||
|
pub content: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ZodValidate for TestimonialsUpdateRequestDto {
|
||||||
|
fn zod_validate(value: &serde_json::Value) -> Result<Self, String> {
|
||||||
|
Self::validate_and_parse(value).map_err(|e| e.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct TestimonialsListItemDto {
|
||||||
|
pub id: String,
|
||||||
|
pub user_id: String,
|
||||||
|
pub user_fullname: String,
|
||||||
|
pub role: String,
|
||||||
|
pub content: String,
|
||||||
|
pub created_at: String,
|
||||||
|
pub is_deleted: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<TestimonialEntity> for TestimonialsListItemDto {
|
||||||
|
fn from(e: TestimonialEntity) -> Self {
|
||||||
|
TestimonialsListItemDto {
|
||||||
|
id: e.id.to_string(),
|
||||||
|
user_id: e.user_id.to_string(),
|
||||||
|
user_fullname: e.user_fullname,
|
||||||
|
role: e.role,
|
||||||
|
content: e.content,
|
||||||
|
created_at: e.created_at,
|
||||||
|
is_deleted: e.is_deleted,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct TestimonialsDetailItemDto {
|
||||||
|
pub id: String,
|
||||||
|
pub user_id: String,
|
||||||
|
pub user_fullname: String,
|
||||||
|
pub role: String,
|
||||||
|
pub content: String,
|
||||||
|
pub created_at: String,
|
||||||
|
pub updated_at: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<TestimonialEntity> for TestimonialsDetailItemDto {
|
||||||
|
fn from(e: TestimonialEntity) -> Self {
|
||||||
|
TestimonialsDetailItemDto {
|
||||||
|
id: e.id.to_string(),
|
||||||
|
user_id: e.user_id.to_string(),
|
||||||
|
user_fullname: e.user_fullname,
|
||||||
|
role: e.role,
|
||||||
|
content: e.content,
|
||||||
|
created_at: e.created_at,
|
||||||
|
updated_at: e.updated_at,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,202 @@
|
|||||||
|
use super::dto::{
|
||||||
|
TestimonialsCreateRequestDto, TestimonialsDetailItemDto, TestimonialsListItemDto,
|
||||||
|
TestimonialsUpdateRequestDto,
|
||||||
|
};
|
||||||
|
use crate::testimonials::domain::{TestimonialEntity, TestimonialService};
|
||||||
|
use axum::{
|
||||||
|
Extension,
|
||||||
|
extract::Path,
|
||||||
|
http::HeaderMap,
|
||||||
|
http::StatusCode,
|
||||||
|
response::{IntoResponse, Response},
|
||||||
|
};
|
||||||
|
use imphnen_entities::ResponseSuccessDto;
|
||||||
|
use imphnen_iam::require_auth;
|
||||||
|
use imphnen_libs::{AppState, ValidatedJson};
|
||||||
|
use imphnen_utils::AppError;
|
||||||
|
use imphnen_utils::{
|
||||||
|
ApiCreated, ApiMessage, ApiPaginated, ApiSuccess, extract_email,
|
||||||
|
};
|
||||||
|
use paginator_axum::PaginationQuery;
|
||||||
|
use paginator_utils::PaginatorResponse;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/v1/landing/cms/testimonials",
|
||||||
|
params(
|
||||||
|
("page" = Option<i64>, Query, description = "Page number"),
|
||||||
|
("per_page" = Option<i64>, Query, description = "Items per page"),
|
||||||
|
("search" = Option<String>, Query, description = "Search keyword"),
|
||||||
|
("sort_by" = Option<String>, Query, description = "Sort by field"),
|
||||||
|
("order" = Option<String>, Query, description = "Order ASC or DESC"),
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "[PUBLIC] Get testimonial list")
|
||||||
|
),
|
||||||
|
tag = "Testimonials"
|
||||||
|
)]
|
||||||
|
pub async fn get_testimonial_list(
|
||||||
|
Extension(service): Extension<Arc<dyn TestimonialService>>,
|
||||||
|
PaginationQuery(params): PaginationQuery,
|
||||||
|
) -> Response {
|
||||||
|
match service.list(params).await {
|
||||||
|
Ok(result) => {
|
||||||
|
let mapped = PaginatorResponse {
|
||||||
|
data: result
|
||||||
|
.data
|
||||||
|
.into_iter()
|
||||||
|
.filter(|e| !e.is_deleted)
|
||||||
|
.map(TestimonialsListItemDto::from)
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
|
meta: result.meta,
|
||||||
|
};
|
||||||
|
ApiPaginated(mapped).into_response()
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
ApiMessage::new(StatusCode::BAD_REQUEST, e.to_string()).into_response()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/v1/landing/cms/testimonials/detail/{id}",
|
||||||
|
params(
|
||||||
|
("id" = String, Path, description = "Testimonial ID")
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "[PUBLIC] Get testimonial by ID", body = ResponseSuccessDto<TestimonialsDetailItemDto>)
|
||||||
|
),
|
||||||
|
tag = "Testimonials"
|
||||||
|
)]
|
||||||
|
pub async fn get_testimonial_by_id(
|
||||||
|
Extension(service): Extension<Arc<dyn TestimonialService>>,
|
||||||
|
Path(id): Path<String>,
|
||||||
|
) -> Response {
|
||||||
|
let uuid = match Uuid::parse_str(&id) {
|
||||||
|
Ok(u) => u,
|
||||||
|
Err(e) => {
|
||||||
|
return ApiMessage::new(StatusCode::BAD_REQUEST, format!("Invalid UUID: {e}"))
|
||||||
|
.into_response();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
match service.get(uuid).await {
|
||||||
|
Ok(t) if !t.is_deleted => {
|
||||||
|
ApiSuccess(TestimonialsDetailItemDto::from(t)).into_response()
|
||||||
|
}
|
||||||
|
Ok(_) => {
|
||||||
|
ApiMessage::new(StatusCode::NOT_FOUND, "Testimonial not found").into_response()
|
||||||
|
}
|
||||||
|
Err(e) => ApiMessage::new(StatusCode::NOT_FOUND, e.to_string()).into_response(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
security(("Bearer" = [])),
|
||||||
|
path = "/v1/landing/cms/testimonials/create",
|
||||||
|
request_body = TestimonialsCreateRequestDto,
|
||||||
|
responses(
|
||||||
|
(status = 201, description = "[USER] Create new testimonial", body = ResponseSuccessDto<TestimonialsDetailItemDto>)
|
||||||
|
),
|
||||||
|
tag = "Testimonials"
|
||||||
|
)]
|
||||||
|
pub async fn post_create_testimonial(
|
||||||
|
headers: HeaderMap,
|
||||||
|
Extension(state): Extension<AppState>,
|
||||||
|
Extension(service): Extension<Arc<dyn TestimonialService>>,
|
||||||
|
ValidatedJson(payload): ValidatedJson<TestimonialsCreateRequestDto>,
|
||||||
|
) -> Result<impl IntoResponse, AppError> {
|
||||||
|
require_auth!(headers.clone(), state, {
|
||||||
|
let email = extract_email(&headers).ok_or_else(|| {
|
||||||
|
AppError::AuthenticationError("Token tidak valid".to_string())
|
||||||
|
})?;
|
||||||
|
let user_info = state
|
||||||
|
.user_lookup_service
|
||||||
|
.get_user_by_email(&email, &state)
|
||||||
|
.await
|
||||||
|
.map_err(|_| AppError::NotFoundError("User not found".to_string()))?;
|
||||||
|
let user = user_info.basic_info;
|
||||||
|
let user_id = Uuid::parse_str(&user.id)
|
||||||
|
.map_err(|e| AppError::BadRequestError(format!("Invalid user ID: {e}")))?;
|
||||||
|
let entity = TestimonialEntity {
|
||||||
|
id: Uuid::new_v4(),
|
||||||
|
user_id,
|
||||||
|
user_fullname: user.fullname.clone(),
|
||||||
|
role: payload.role,
|
||||||
|
content: payload.content,
|
||||||
|
is_deleted: false,
|
||||||
|
created_at: chrono::Utc::now().to_rfc3339(),
|
||||||
|
updated_at: chrono::Utc::now().to_rfc3339(),
|
||||||
|
};
|
||||||
|
let created = service.create(entity).await?;
|
||||||
|
Ok(ApiCreated(TestimonialsDetailItemDto::from(created)))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
patch,
|
||||||
|
security(("Bearer" = [])),
|
||||||
|
path = "/v1/landing/cms/testimonials/update/{id}",
|
||||||
|
params(
|
||||||
|
("id" = String, Path, description = "Testimonial ID")
|
||||||
|
),
|
||||||
|
request_body = TestimonialsUpdateRequestDto,
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "[USER] Update testimonial")
|
||||||
|
),
|
||||||
|
tag = "Testimonials"
|
||||||
|
)]
|
||||||
|
pub async fn patch_update_testimonial(
|
||||||
|
headers: HeaderMap,
|
||||||
|
Extension(state): Extension<AppState>,
|
||||||
|
Extension(service): Extension<Arc<dyn TestimonialService>>,
|
||||||
|
Path(id): Path<String>,
|
||||||
|
ValidatedJson(payload): ValidatedJson<TestimonialsUpdateRequestDto>,
|
||||||
|
) -> Result<impl IntoResponse, AppError> {
|
||||||
|
require_auth!(headers, state, {
|
||||||
|
let uuid = Uuid::parse_str(&id)
|
||||||
|
.map_err(|e| AppError::BadRequestError(format!("Invalid UUID: {e}")))?;
|
||||||
|
let existing = service.get(uuid).await?;
|
||||||
|
let entity = TestimonialEntity {
|
||||||
|
id: existing.id,
|
||||||
|
user_id: existing.user_id,
|
||||||
|
user_fullname: existing.user_fullname,
|
||||||
|
role: payload.role,
|
||||||
|
content: payload.content,
|
||||||
|
is_deleted: existing.is_deleted,
|
||||||
|
created_at: existing.created_at,
|
||||||
|
updated_at: chrono::Utc::now().to_rfc3339(),
|
||||||
|
};
|
||||||
|
service.update(entity).await?;
|
||||||
|
Ok(ApiMessage::ok("Testimonial updated"))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
delete,
|
||||||
|
security(("Bearer" = [])),
|
||||||
|
path = "/v1/landing/cms/testimonials/delete/{id}",
|
||||||
|
params(
|
||||||
|
("id" = String, Path, description = "Testimonial ID")
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "[USER] Soft delete testimonial")
|
||||||
|
),
|
||||||
|
tag = "Testimonials"
|
||||||
|
)]
|
||||||
|
pub async fn delete_testimonial(
|
||||||
|
headers: HeaderMap,
|
||||||
|
Extension(state): Extension<AppState>,
|
||||||
|
Extension(service): Extension<Arc<dyn TestimonialService>>,
|
||||||
|
Path(id): Path<String>,
|
||||||
|
) -> Result<impl IntoResponse, AppError> {
|
||||||
|
require_auth!(headers, state, {
|
||||||
|
let uuid = Uuid::parse_str(&id)
|
||||||
|
.map_err(|e| AppError::BadRequestError(format!("Invalid UUID: {e}")))?;
|
||||||
|
service.delete(uuid).await?;
|
||||||
|
Ok(ApiMessage::ok("Testimonial deleted"))
|
||||||
|
})
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user