Compare commits
90
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1a2e0c58b6 | ||
|
|
e66f1f1634 | ||
|
|
6f45cf80c8 | ||
|
|
02be09506a | ||
|
|
b88ecd1361 | ||
|
|
fa435994f2 | ||
|
|
16c613d699 | ||
|
|
f598dedb3b | ||
|
|
cfa251337f | ||
|
|
cca9aa6318 | ||
|
|
f1e4e02697 | ||
|
|
a3c2715a85 | ||
|
|
89cc631fb0 | ||
|
|
36c12110c2 | ||
|
|
070060365a | ||
|
|
68e816f956 | ||
|
|
48db5f07ef | ||
|
|
a4b5d91906 | ||
|
|
9878279660 | ||
|
|
465cbee2c3 | ||
|
|
33a2ae86c6 | ||
|
|
d906fc4747 | ||
|
|
055ef6ac8e | ||
|
|
e63658c082 | ||
|
|
f4b0ed0a4a | ||
|
|
1e5ed26f76 | ||
|
|
d97095a38d | ||
|
|
e48507901e | ||
|
|
a43ef8ec04 | ||
|
|
874f25aa80 | ||
|
|
12a730a394 | ||
|
|
bdcd41f54d | ||
|
|
96fb8eb7d3 | ||
|
|
d2c333dbd6 | ||
|
|
054f0f63ff | ||
|
|
eb05ea5994 | ||
|
|
4f870ec4c9 | ||
|
|
31f9b6cfea | ||
|
|
07a93ff0b7 | ||
|
|
d2b098ed92 | ||
|
|
09a7f4371a | ||
|
|
b4bf78f0d6 | ||
|
|
de9c5a6dc1 | ||
|
|
8a661df059 | ||
|
|
c7c16d4197 | ||
|
|
3aec0346a9 | ||
|
|
8d2f720de1 | ||
|
|
4b8da68df4 | ||
|
|
6460979bab | ||
|
|
3ae9dbd717 | ||
|
|
0816566d03 | ||
|
|
a97044edfc | ||
|
|
9d92a9a136 | ||
|
|
8e12c79ea6 | ||
|
|
5777268441 | ||
|
|
34eaa418d5 | ||
|
|
77f12610dd | ||
|
|
5be737ad70 | ||
|
|
0ae1e26ba8 | ||
|
|
79721b7af6 | ||
|
|
093e290221 | ||
|
|
1e6957720e | ||
|
|
a0f263bb4f | ||
|
|
413a28c089 | ||
|
|
a90e8662e1 | ||
|
|
955d384f96 | ||
|
|
ea5ece9ab2 | ||
|
|
8ff532bb67 | ||
|
|
eb6cfd51ae | ||
|
|
11d6249007 | ||
|
|
497c250e6d | ||
|
|
2872c1827a | ||
|
|
f9c49d44b5 | ||
|
|
12043f027c | ||
|
|
0c28d34d57 | ||
|
|
271b00c2c4 | ||
|
|
b096f4129a | ||
|
|
7ab036f5f7 | ||
|
|
9ffe808797 | ||
|
|
acfb8259bc | ||
|
|
298144bd36 | ||
|
|
ff9d8cba69 | ||
|
|
8b2769706e | ||
|
|
b8606ef0bb | ||
|
|
44de53a49d | ||
|
|
9abab01b9c | ||
|
|
53414687ff | ||
|
|
d2ec21c7b1 | ||
|
|
4deee3a9cb | ||
|
|
b50ce2b060 |
+24
-4
@@ -1,4 +1,24 @@
|
|||||||
PORT=
|
RUST_ENV=development
|
||||||
DATABASE_URL=
|
PORT=4099
|
||||||
ACCESS_TOKEN_SECRET=
|
SURREALDB_URL=ws://localhost:8000/rpc
|
||||||
REFRESH_TOKEN_SECRET=
|
SURREALDB_USERNAME=root
|
||||||
|
SURREALDB_PASSWORD=root
|
||||||
|
SURREALDB_NAMESPACE=test
|
||||||
|
SURREALDB_DBNAME=test
|
||||||
|
ACCESS_TOKEN_SECRET=your-access-token-secret-key-here
|
||||||
|
REFRESH_TOKEN_SECRET=your-refresh-token-secret-key-here
|
||||||
|
SMTP_EMAIL=your-email@example.com
|
||||||
|
SMTP_PASSWORD=your-smtp-password
|
||||||
|
SMTP_NAME="Your App Name"
|
||||||
|
SMTP_HOST=smtp.gmail.com
|
||||||
|
REDISDB_URL=localhost
|
||||||
|
FE_URL=http://localhost
|
||||||
|
MINIO_ENDPOINT=http://localhost:9000
|
||||||
|
MINIO_BUCKET_NAME=default_bucket
|
||||||
|
MINIO_ACCESS_KEY=your-minio-access-key
|
||||||
|
MINIO_SECRET_KEY=your-minio-secret-key
|
||||||
|
MAIL_USER=your-email@example.com
|
||||||
|
MAIL_PASSWORD=your-smtp-password
|
||||||
|
MAIL_HOST=smtp.gmail.com
|
||||||
|
MAIL_PORT=465
|
||||||
|
MAIL_SECURE=true
|
||||||
|
|||||||
@@ -0,0 +1,65 @@
|
|||||||
|
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,19 @@
|
|||||||
|
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
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
# Nix
|
# Nix
|
||||||
/.direnv
|
/.direnv
|
||||||
|
/Cargo.nix
|
||||||
|
|
||||||
# Environment
|
# Environment
|
||||||
.envrc
|
.envrc
|
||||||
|
|||||||
Generated
+3513
-996
File diff suppressed because it is too large
Load Diff
+62
-10
@@ -1,21 +1,73 @@
|
|||||||
[package]
|
[workspace]
|
||||||
name = "imphnen-cms-be"
|
resolver = "2"
|
||||||
version = "0.1.0"
|
members = [
|
||||||
edition = "2021"
|
"tests",
|
||||||
|
"imphnen-iam",
|
||||||
|
"imphnen-cms",
|
||||||
|
"imphnen-libs",
|
||||||
|
"imphnen-utils",
|
||||||
|
"imphnen-gacha",
|
||||||
|
"imphnen-gateway",
|
||||||
|
"imphnen-backend",
|
||||||
|
"imphnen-entities",
|
||||||
|
"imphnen-dimentorin",
|
||||||
|
"imphnen-middleware",
|
||||||
|
]
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
axum = { version = "0.8.1", features = ["multipart"] }
|
[workspace.dependencies]
|
||||||
|
axum = { version = "0.8.4", features = ["multipart"] }
|
||||||
log = "0.4.25"
|
log = "0.4.25"
|
||||||
sea-orm = { version = "1.1.4", features = ["sqlx-postgres", "macros", "with-json", "with-uuid", "runtime-tokio", "runtime-tokio-native-tls"] }
|
|
||||||
serde = { version = "1.0.217", features = ["derive"] }
|
serde = { version = "1.0.217", features = ["derive"] }
|
||||||
serde_json = "1.0.138"
|
serde_json = "1.0.138"
|
||||||
tokio = { version = "1.43.0", features = ["full"] }
|
tokio = { version = "1.45.0", 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.39"
|
chrono = "0.4.41"
|
||||||
utoipa = { version = "5.3.1", features = ["axum_extras"] }
|
utoipa = { version = "5.3.1", features = ["axum_extras"] }
|
||||||
utoipa-swagger-ui = { version = "9.0.0", features = ["axum"] }
|
utoipa-swagger-ui = { version = "9.0.0", features = ["axum"] }
|
||||||
redis = "0.28.2"
|
lettre = { version = "0.11.16", features = ["tokio1-native-tls"] }
|
||||||
|
surrealdb = { version = "2.3.7", features = ["kv-mem", "kv-fdb"] }
|
||||||
|
thiserror = "2.0.12"
|
||||||
|
anyhow = "1.0.98"
|
||||||
|
rand = { version = "0.9.1", features = ["std", "alloc"] }
|
||||||
|
rand_distr = "0.5.1"
|
||||||
|
tower-http = { version = "0.6.4", features = ["cors", "trace"] }
|
||||||
|
http-body-util = "0.1.1"
|
||||||
|
|
||||||
|
validator = { version = "0.20.0", features = ["derive"] }
|
||||||
|
lazy_static = "1.4.0"
|
||||||
|
regex = "1.11.1"
|
||||||
|
axum-test = "17.2.0"
|
||||||
|
fancy-regex = "0.15.0"
|
||||||
|
futures = "0.3.31"
|
||||||
|
tower = "0.5.2"
|
||||||
|
env_logger = "0.11.8"
|
||||||
|
dotenvy = "0.15.7"
|
||||||
|
tracing = "0.1.40"
|
||||||
|
tracing-subscriber = { version = "0.3.18", features = ["fmt"] }
|
||||||
|
once_cell = "1.21.3"
|
||||||
|
uuid = { version = "1.8.0", features = ["v4", "fast-rng", "serde"] }
|
||||||
|
strum = { version = "0.27.1", features = ["derive"] }
|
||||||
|
strum_macros = "0.27.1"
|
||||||
|
hyper = "1.6.0"
|
||||||
|
hyper-util = "0.1.0"
|
||||||
|
async-trait = "0.1.75"
|
||||||
|
tokio-tungstenite = "0.23"
|
||||||
|
url = "2.5"
|
||||||
|
futures-util = "0.3"
|
||||||
|
|
||||||
|
tests = { path = "./tests" }
|
||||||
|
imphnen-iam = { path = "./imphnen-iam" }
|
||||||
|
imphnen-cms = { path = "./imphnen-cms" }
|
||||||
|
imphnen-libs = { path = "./imphnen-libs" }
|
||||||
|
imphnen-utils = { path = "./imphnen-utils" }
|
||||||
|
imphnen-gacha = { path = "./imphnen-gacha" }
|
||||||
|
imphnen-gateway = { path = "./imphnen-gateway" }
|
||||||
|
imphnen-backend = { path = "./imphnen-backend" }
|
||||||
|
imphnen-entities = { path = "./imphnen-entities" }
|
||||||
|
imphnen-dimentorin = { path = "./imphnen-dimentorin" }
|
||||||
|
imphnen-middleware = { path = "./imphnen-middleware" }
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
lto = "fat"
|
lto = "fat"
|
||||||
|
|||||||
+37
-12
@@ -1,22 +1,47 @@
|
|||||||
FROM rust:1.83-slim-bullseye AS builder
|
FROM rust:1.86-alpine AS builder
|
||||||
|
|
||||||
RUN apt-get update && apt-get install -y \
|
RUN apk add --no-cache \
|
||||||
build-essential \
|
curl \
|
||||||
libssl-dev \
|
musl-dev \
|
||||||
pkg-config \
|
openssl-dev \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
openssl-libs-static \
|
||||||
|
pkgconfig
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
COPY Cargo.toml Cargo.lock ./
|
COPY Cargo.toml Cargo.lock ./
|
||||||
COPY ./src ./src
|
|
||||||
|
|
||||||
RUN cargo build --release && strip /app/target/release/imphnen-cms-api
|
RUN mkdir -p imphnen-backend/src imphnen-cms/src imphnen-dimentorin/src \
|
||||||
|
imphnen-entities/src imphnen-gacha/src imphnen-gateway/src \
|
||||||
|
imphnen-iam/src imphnen-libs/src imphnen-middleware/src \
|
||||||
|
imphnen-utils/src tests/src && \
|
||||||
|
echo "fn main() {}" > imphnen-backend/src/main.rs && \
|
||||||
|
find . -name "src" -type d -exec sh -c 'echo "// dummy" > "$1/lib.rs"' _ {} \;
|
||||||
|
|
||||||
FROM gcr.io/distroless/cc AS runner
|
RUN echo '[package]\nname = "tests"\nversion = "0.1.0"\nedition = "2021"' > tests/Cargo.toml
|
||||||
|
|
||||||
WORKDIR /app
|
|
||||||
|
|
||||||
COPY --from=builder /app/target/release/imphnen-cms-api .
|
RUN echo -e '[package]\nname = "tests"\nversion = "0.1.0"\nedition = "2021"' > tests/Cargo.toml
|
||||||
|
|
||||||
CMD ["/app/imphnen-cms-api"]
|
|
||||||
|
COPY imphnen-backend ./imphnen-backend
|
||||||
|
COPY imphnen-cms ./imphnen-cms
|
||||||
|
COPY imphnen-dimentorin ./imphnen-dimentorin
|
||||||
|
COPY imphnen-entities ./imphnen-entities
|
||||||
|
COPY imphnen-gacha ./imphnen-gacha
|
||||||
|
COPY imphnen-gateway ./imphnen-gateway
|
||||||
|
COPY imphnen-iam ./imphnen-iam
|
||||||
|
COPY imphnen-libs ./imphnen-libs
|
||||||
|
COPY imphnen-middleware ./imphnen-middleware
|
||||||
|
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" \
|
||||||
|
cargo build -p imphnen-backend --release && \
|
||||||
|
strip target/release/api && \
|
||||||
|
upx --best --lzma target/release/api 2>/dev/null || true
|
||||||
|
|
||||||
|
FROM scratch AS runner
|
||||||
|
COPY --from=builder /app/target/release/api /api
|
||||||
|
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
|
||||||
|
ENTRYPOINT ["/api"]
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
# IMPHNEN Backend Service
|
||||||
|
|
||||||
|
<p align="center">
|
||||||
|
<img src="docs/logo.svg" alt="IMPHNEN">
|
||||||
|
</p>
|
||||||
|
|
||||||
|
This repository serves as the **monorepo** for all backend services of IMPHNEN. It encompasses several main services:
|
||||||
|
|
||||||
|
1. **IMPHNEN-Backend** - Provides fundamental functionalities and shared resources for other services.
|
||||||
|
2. **IMPHNEN-IAM** - Handles identity and access management across IMPHNEN applications.
|
||||||
|
3. **IMPHNEN-CMS** - Supports the cms services by IMPHNEN [Landing Page website](https://imphnen.dev/).
|
||||||
|
4. **IMPHNEN-Gacha** - Supports the gacha services by IMPHNEN [Gacha website](https://gacha.imphnen.dev/).
|
||||||
|
5. **IMPHNEN-Dimentorin** - Supports the mentoring services by IMPHNEN [Dimentorin website](https://dimentorin.imphnen.dev/).
|
||||||
|
6. **IMPHNEN-Gateway** - Acts as the API gateway, routing requests to appropriate services.
|
||||||
|
7. **IMPHNEN-Middleware** - Acts as the middleware for the API Gateway, providing authentication and authorization.
|
||||||
|
|
||||||
|
## How to Install
|
||||||
|
|
||||||
|
1. **Clone the repository**:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
git clone https://github.com/IMPHNEN/imphnen-backend-service.git
|
||||||
|
cd imphnen-backend-service
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Set up the environment**:
|
||||||
|
|
||||||
|
- Copy the example environment files:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
cp .env.example .env
|
||||||
|
```
|
||||||
|
|
||||||
|
if you use windows based system
|
||||||
|
|
||||||
|
```sh
|
||||||
|
./apply-env.ps1
|
||||||
|
```
|
||||||
|
|
||||||
|
if you use unix based system
|
||||||
|
|
||||||
|
```sh
|
||||||
|
source ./apply-env.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
- Modify the `.env` files with your specific configuration settings.
|
||||||
|
|
||||||
|
3. **Install dependencies**:
|
||||||
|
|
||||||
|
Ensure you have [Rust](https://www.rust-lang.org/) installed. Then, run:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
cargo fetch
|
||||||
|
```
|
||||||
|
|
||||||
|
4. **Run the seeders**:
|
||||||
|
|
||||||
|
to run the seeders, run:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
cargo run --bin seeder
|
||||||
|
```
|
||||||
|
|
||||||
|
## How to Run
|
||||||
|
|
||||||
|
### Development
|
||||||
|
|
||||||
|
To run the services in development mode:
|
||||||
|
|
||||||
|
1. **Start the database and other dependencies** using Docker Compose:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
docker-compose up -d
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Run using cargo run**. For example, to run the Core Service:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
cargo run --bin api
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **Run using cargo watch**. For example, to run the Core Service:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
cargo watch -x "run --bin api"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Production
|
||||||
|
|
||||||
|
For production deployment:
|
||||||
|
|
||||||
|
1. **Build the Docker image**:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
docker build -t imphnen-backend .
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Run the Docker container**:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
docker run --name imphnen-backend -d --env-file .env -p 3000:3000 imphnen-backend
|
||||||
|
```
|
||||||
|
|
||||||
|
Adjust the port and environment variables as needed.
|
||||||
|
|
||||||
|
## How to Run the Tests
|
||||||
|
|
||||||
|
1. **Run the tests**:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
cargo test -p tests
|
||||||
|
```
|
||||||
|
|
||||||
|
## How to Contribute
|
||||||
|
|
||||||
|
1. **Fork the repository** and clone it locally.
|
||||||
|
2. **Create a new branch** for your feature or fix:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
git checkout -b feat/your-feature-name
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **Make your changes**, commit them, and push to your forked repository.
|
||||||
|
4. **Create a pull request** to the `develop` branch of this repository.
|
||||||
|
|
||||||
|
If you encounter any issues or have questions, feel free to create a new issue in the repository.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
_Note: For detailed API documentation, please refer to our [API Docs](https://api.imphnen.dev/docs)._
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
@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
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
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"
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
#!/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"
|
||||||
@@ -6,3 +6,11 @@ services:
|
|||||||
ports:
|
ports:
|
||||||
- "${PORT}:${PORT}"
|
- "${PORT}:${PORT}"
|
||||||
env_file: ".env"
|
env_file: ".env"
|
||||||
|
depends_on:
|
||||||
|
- surrealdb
|
||||||
|
|
||||||
|
surrealdb:
|
||||||
|
image: surrealdb/surrealdb:latest
|
||||||
|
command: start --log trace --user root --pass root
|
||||||
|
ports:
|
||||||
|
- "8000:8000"
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 351 KiB |
Generated
+27
@@ -0,0 +1,27 @@
|
|||||||
|
{
|
||||||
|
"nodes": {
|
||||||
|
"nixpkgs": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1739020877,
|
||||||
|
"narHash": "sha256-mIvECo/NNdJJ/bXjNqIh8yeoSjVLAuDuTUzAo7dzs8Y=",
|
||||||
|
"owner": "nixos",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "a79cfe0ebd24952b580b1cf08cd906354996d547",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "nixos",
|
||||||
|
"ref": "nixos-unstable",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": {
|
||||||
|
"inputs": {
|
||||||
|
"nixpkgs": "nixpkgs"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": "root",
|
||||||
|
"version": 7
|
||||||
|
}
|
||||||
@@ -1,24 +1,32 @@
|
|||||||
{
|
{
|
||||||
description = "IMPHNEN CMS API Nix Flake";
|
description = "IMPHNEN Backend Service Nix Flake";
|
||||||
|
|
||||||
inputs = {
|
inputs = {
|
||||||
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
|
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
|
||||||
};
|
};
|
||||||
|
|
||||||
outputs = {
|
outputs = {
|
||||||
self,
|
self,
|
||||||
nixpkgs,
|
nixpkgs,
|
||||||
}: let
|
}: let
|
||||||
supportedSystems = ["x86_64-linux" "x86_64-darwin" "aarch64-darwin" "aarch64-linux"];
|
supportedSystems = ["x86_64-linux" "x86_64-darwin" "aarch64-darwin" "aarch64-linux"];
|
||||||
|
pkgsFor = system:
|
||||||
|
import nixpkgs {
|
||||||
|
inherit system;
|
||||||
|
config = {
|
||||||
|
allowUnfree = true;
|
||||||
|
};
|
||||||
|
};
|
||||||
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
|
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
|
||||||
pkgsFor = nixpkgs.legacyPackages;
|
|
||||||
in {
|
in {
|
||||||
packages = forAllSystems (system: {
|
packages = forAllSystems (system: {
|
||||||
default = pkgsFor.${system}.callPackage ./default.nix {};
|
default = (pkgsFor system).callPackage ./default.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 {};
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
[package]
|
||||||
|
name = "imphnen-backend"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
imphnen-libs.workspace = true
|
||||||
|
imphnen-utils.workspace = true
|
||||||
|
imphnen-gateway.workspace = true
|
||||||
|
imphnen-entities.workspace = true
|
||||||
|
imphnen-iam.workspace = true
|
||||||
|
imphnen-cms.workspace = true
|
||||||
|
imphnen-dimentorin.workspace = true
|
||||||
|
axum.workspace = true
|
||||||
|
serde.workspace = true
|
||||||
|
serde_json.workspace = true
|
||||||
|
utoipa.workspace = true
|
||||||
|
lazy_static.workspace = true
|
||||||
|
regex.workspace = true
|
||||||
|
validator.workspace = true
|
||||||
|
axum-test.workspace = true
|
||||||
|
surrealdb.workspace = true
|
||||||
|
rand.workspace = true
|
||||||
|
tokio.workspace = true
|
||||||
|
chrono.workspace = true
|
||||||
|
anyhow.workspace = true
|
||||||
|
tower-http.workspace = true
|
||||||
|
utoipa-swagger-ui.workspace = true
|
||||||
|
env_logger.workspace = true
|
||||||
|
tracing.workspace = true
|
||||||
|
tracing-subscriber.workspace = true
|
||||||
|
uuid.workspace=true
|
||||||
|
tokio-tungstenite.workspace = true
|
||||||
|
url.workspace = true
|
||||||
|
futures-util.workspace = true
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
use imphnen_gateway::gateway_service;
|
||||||
|
use imphnen_libs::axum_init;
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() {
|
||||||
|
env_logger::init();
|
||||||
|
axum_init(|surrealdb_ws, surrealdb_mem| async {
|
||||||
|
gateway_service(surrealdb_ws, surrealdb_mem).await
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
@@ -0,0 +1,169 @@
|
|||||||
|
use tokio_tungstenite::{connect_async, tungstenite::protocol::Message};
|
||||||
|
use url::Url;
|
||||||
|
use futures_util::{StreamExt, SinkExt};
|
||||||
|
use serde_json::json;
|
||||||
|
|
||||||
|
// Menentukan kredensial dan detail koneksi secara langsung sebagai string statis
|
||||||
|
static SURREALDB_URL_WS: &str = "ws://localhost:8000/rpc";
|
||||||
|
static SURREALDB_USERNAME: &str = "root";
|
||||||
|
static SURREALDB_PASSWORD: &str = "root";
|
||||||
|
static SURREALDB_NAMESPACE: &str = "test";
|
||||||
|
static SURREALDB_DBNAME: &str = "test";
|
||||||
|
|
||||||
|
// Daftar tabel sebagai variabel static yang tidak dapat diubah
|
||||||
|
static TABLES_TO_CLEAR: &[&str] = &[
|
||||||
|
"app_events", "users", "roles", "permissions", "gacha_rolls",
|
||||||
|
"mentor_users", "gacha_claims", "gacha_credits", "gacha_items",
|
||||||
|
"mentor_profiles", "roles_permissions", "testimonials",
|
||||||
|
];
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
// Tidak perlu memuat env lagi, karena kita menggunakan nilai hardcoded
|
||||||
|
// imphnen_libs::enviroment::load_env(); // Baris ini tidak lagi dibutuhkan
|
||||||
|
// let env = Env::new(); // Baris ini tidak lagi dibutuhkan
|
||||||
|
|
||||||
|
println!("DEBUG: URL WS: {}", SURREALDB_URL_WS);
|
||||||
|
println!("DEBUG: Username: {}", SURREALDB_USERNAME);
|
||||||
|
println!("DEBUG: Namespace: {}", SURREALDB_NAMESPACE);
|
||||||
|
println!("DEBUG: Database: {}", SURREALDB_DBNAME);
|
||||||
|
|
||||||
|
let url = Url::parse(SURREALDB_URL_WS)?; // Menggunakan SURREALDB_URL_WS statis
|
||||||
|
|
||||||
|
let (ws_stream, _) = connect_async(url).await?;
|
||||||
|
let (mut write, mut read) = ws_stream.split();
|
||||||
|
|
||||||
|
// Authenticate (signin)
|
||||||
|
let signin_query = json!({
|
||||||
|
"method": "signin",
|
||||||
|
"params": [{
|
||||||
|
"user": SURREALDB_USERNAME, // Menggunakan SURREALDB_USERNAME statis
|
||||||
|
"pass": SURREALDB_PASSWORD, // Menggunakan SURREALDB_PASSWORD statis
|
||||||
|
}],
|
||||||
|
"id": 1
|
||||||
|
}).to_string();
|
||||||
|
println!("DEBUG: Sending signin query: {}", signin_query);
|
||||||
|
write.send(Message::Text(signin_query)).await?;
|
||||||
|
|
||||||
|
let signin_response = read.next().await.ok_or("Failed to read signin response")?;
|
||||||
|
let signin_response_msg = signin_response?;
|
||||||
|
let signin_response_str = signin_response_msg.to_text()?;
|
||||||
|
println!("DEBUG: Signin response: {}", signin_response_str);
|
||||||
|
if signin_response_str.contains("\"error\":") {
|
||||||
|
return Err(format!("Signin failed: {}", signin_response_str).into());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use namespace and database
|
||||||
|
let use_query = json!({
|
||||||
|
"method": "use",
|
||||||
|
"params": [SURREALDB_NAMESPACE, SURREALDB_DBNAME], // Menggunakan NS & DB statis
|
||||||
|
"id": 2
|
||||||
|
}).to_string();
|
||||||
|
println!("DEBUG: Sending use query: {}", use_query);
|
||||||
|
write.send(Message::Text(use_query)).await?;
|
||||||
|
|
||||||
|
let use_response = read.next().await.ok_or("Failed to read use response")?;
|
||||||
|
let use_response_msg = use_response?;
|
||||||
|
let use_response_str = use_response_msg.to_text()?;
|
||||||
|
println!("DEBUG: Use response: {}", use_response_str);
|
||||||
|
if use_response_str.contains("\"error\":") {
|
||||||
|
return Err(format!("USE command failed: {}", use_response_str).into());
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("INFO: Attempting to clear database tables via WebSocket...");
|
||||||
|
|
||||||
|
let mut all_clear = true;
|
||||||
|
for (i, table) in TABLES_TO_CLEAR.iter().enumerate() {
|
||||||
|
let remove_query = format!("REMOVE TABLE {};", table);
|
||||||
|
let query_json = json!({
|
||||||
|
"method": "query",
|
||||||
|
"params": [remove_query],
|
||||||
|
"id": i + 3
|
||||||
|
}).to_string();
|
||||||
|
|
||||||
|
println!("DEBUG: Attempting REMOVE TABLE {}: {}", table, query_json);
|
||||||
|
write.send(Message::Text(query_json)).await?;
|
||||||
|
let response_result = read.next().await.ok_or("Stream ended unexpectedly")?;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
match response_result {
|
||||||
|
Ok(msg) => {
|
||||||
|
let response_str = msg.to_text()?;
|
||||||
|
if response_str.contains("\"error\":") {
|
||||||
|
println!("WARN: Failed to REMOVE TABLE {}: {}. Attempting DELETE type::{}.", table, response_str, table);
|
||||||
|
let delete_all_query = format!("DELETE FROM {};", table);
|
||||||
|
let delete_all_json = json!({
|
||||||
|
"method": "query",
|
||||||
|
"params": [delete_all_query],
|
||||||
|
"id": i + 300
|
||||||
|
}).to_string();
|
||||||
|
|
||||||
|
println!("DEBUG: Attempting DELETE {}: {}", table, delete_all_json);
|
||||||
|
write.send(Message::Text(delete_all_json)).await?;
|
||||||
|
let delete_response_result = read.next().await.ok_or("Stream ended unexpectedly during DELETE type::")?;
|
||||||
|
|
||||||
|
match delete_response_result {
|
||||||
|
Ok(delete_msg) => {
|
||||||
|
let delete_response_str = delete_msg.to_text()?;
|
||||||
|
if delete_response_str.contains("\"error\":") {
|
||||||
|
println!("ERROR: Failed to DELETE type::{} : {}", table, delete_response_str);
|
||||||
|
all_clear = false;
|
||||||
|
} else {
|
||||||
|
println!("INFO: Successfully DELETED type:: table: {}", table);
|
||||||
|
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(delete_e) => {
|
||||||
|
println!("ERROR: Error receiving response for DELETE type:: table {}: {}", table, delete_e);
|
||||||
|
all_clear = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println!("INFO: Successfully REMOVED TABLE: {}", table);
|
||||||
|
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
println!("ERROR: Error receiving response for REMOVE TABLE {}: {}", table, e);
|
||||||
|
all_clear = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if table is empty after deletion attempt
|
||||||
|
let select_query = format!("SELECT * FROM {} LIMIT 1;", table);
|
||||||
|
let select_json = json!({
|
||||||
|
"method": "query",
|
||||||
|
"params": [select_query],
|
||||||
|
"id": i + 1000
|
||||||
|
}).to_string();
|
||||||
|
write.send(Message::Text(select_json)).await?;
|
||||||
|
let select_response_result = read.next().await.ok_or("Stream ended unexpectedly during SELECT check")?;
|
||||||
|
match select_response_result {
|
||||||
|
Ok(select_msg) => {
|
||||||
|
let select_response_str = select_msg.to_text()?;
|
||||||
|
if select_response_str.contains("does not exist") {
|
||||||
|
println!("CHECK: Table '{}' does not exist after clear attempt (success).", table);
|
||||||
|
} else if select_response_str.contains("\"result\":[]") || select_response_str.contains("\"result\":[[]]") {
|
||||||
|
println!("CHECK: Table '{}' is empty after clear attempt.", table);
|
||||||
|
} else {
|
||||||
|
println!("WARNING: Table '{}' is NOT empty after clear attempt! Response: {}", table, select_response_str);
|
||||||
|
all_clear = false;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
println!("ERROR: Error receiving response for SELECT check on table {}: {}", table, e);
|
||||||
|
all_clear = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("INFO: Database clearing complete.");
|
||||||
|
|
||||||
|
if !all_clear {
|
||||||
|
eprintln!("ERROR: One or more tables could not be cleared. Check logs for details.");
|
||||||
|
return Err("Database clearing failed for one or more tables.".into());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
use imphnen_cms::v1::landing::events::events_schema::EventsSchema;
|
||||||
|
use imphnen_libs::enviroment::load_env;
|
||||||
|
use imphnen_utils::{get_iso_date, Env};
|
||||||
|
use std::error::Error;
|
||||||
|
use surrealdb::engine::any;
|
||||||
|
use surrealdb::{opt::auth::Root, sql::Thing, Uuid}; // Added Uuid
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
load_env();
|
||||||
|
let env = Env::new();
|
||||||
|
let db = any::connect(&env.surrealdb_url).await?;
|
||||||
|
db.signin(Root {
|
||||||
|
username: &env.surrealdb_username,
|
||||||
|
password: &env.surrealdb_password,
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
|
db.use_ns(env.surrealdb_namespace)
|
||||||
|
.use_db(env.surrealdb_dbname)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let events = vec![
|
||||||
|
(
|
||||||
|
"Tech Conference 2025",
|
||||||
|
"Annual technology conference featuring the latest innovations in software development, AI, and cloud computing.",
|
||||||
|
"https://techconf2025.example.com",
|
||||||
|
150.0,
|
||||||
|
Some("Jakarta Convention Center".to_string()),
|
||||||
|
false,
|
||||||
|
"2025-06-15T09:00:00Z",
|
||||||
|
"2025-06-17T18:00:00Z",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"Online Web Development Workshop",
|
||||||
|
"Comprehensive workshop covering modern web development frameworks including React, Vue, and Angular.",
|
||||||
|
"https://webdev-workshop.example.com",
|
||||||
|
75.0,
|
||||||
|
None,
|
||||||
|
true,
|
||||||
|
"2025-07-10T14:00:00Z",
|
||||||
|
"2025-07-10T17:00:00Z",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"Startup Pitch Competition",
|
||||||
|
"Exciting competition where emerging startups present their innovative ideas to a panel of expert judges and investors.",
|
||||||
|
"https://startup-pitch.example.com",
|
||||||
|
25.0,
|
||||||
|
Some("Innovation Hub Surabaya".to_string()),
|
||||||
|
false,
|
||||||
|
"2025-08-05T10:00:00Z",
|
||||||
|
"2025-08-05T16:00:00Z",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"Digital Marketing Masterclass",
|
||||||
|
"Learn advanced digital marketing strategies, social media optimization, and data-driven marketing techniques.",
|
||||||
|
"https://digital-marketing.example.com",
|
||||||
|
100.0,
|
||||||
|
None,
|
||||||
|
true,
|
||||||
|
"2025-09-20T13:00:00Z",
|
||||||
|
"2025-09-22T15:00:00Z",
|
||||||
|
),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (
|
||||||
|
name,
|
||||||
|
description,
|
||||||
|
detail_link,
|
||||||
|
price,
|
||||||
|
location,
|
||||||
|
is_online,
|
||||||
|
start_date,
|
||||||
|
end_date,
|
||||||
|
) in events
|
||||||
|
{
|
||||||
|
let uuid = Uuid::new_v4().to_string(); // Generate new UUID
|
||||||
|
let event = EventsSchema {
|
||||||
|
id: Thing::from(("app_events", uuid.as_str())), // Use generated UUID
|
||||||
|
name: name.into(),
|
||||||
|
description: description.into(),
|
||||||
|
detail_link: detail_link.into(),
|
||||||
|
price,
|
||||||
|
location,
|
||||||
|
is_online,
|
||||||
|
is_deleted: false,
|
||||||
|
start_date: start_date.into(),
|
||||||
|
end_date: end_date.into(),
|
||||||
|
created_at: get_iso_date(),
|
||||||
|
updated_at: get_iso_date(),
|
||||||
|
};
|
||||||
|
|
||||||
|
db.create::<Option<EventsSchema>>(("app_events", uuid.as_str())) // Use generated UUID
|
||||||
|
.content(event)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"✅ Inserted event: {} ({})",
|
||||||
|
name,
|
||||||
|
if is_online { "Online" } else { "In-person" }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("✅ All Events seeded");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
use imphnen_libs::enviroment::load_env;
|
||||||
|
use imphnen_utils::{get_iso_date, Env};
|
||||||
|
|
||||||
|
use std::error::Error;
|
||||||
|
use surrealdb::opt::auth::Root;
|
||||||
|
use surrealdb::sql::Thing;
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
load_env();
|
||||||
|
let env = Env::new();
|
||||||
|
use surrealdb::engine::any;
|
||||||
|
let db = any::connect(&env.surrealdb_url).await?;
|
||||||
|
db.signin(Root {
|
||||||
|
username: &env.surrealdb_username,
|
||||||
|
password: &env.surrealdb_password,
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
|
db.use_ns(env.surrealdb_namespace)
|
||||||
|
.use_db(env.surrealdb_dbname)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
db.query("DELETE type::thing('app_gacha_items', $id)")
|
||||||
|
.bind(("id", "gacha_item_test_id"))
|
||||||
|
.await?;
|
||||||
|
db.query("DELETE type::thing('app_gacha_rolls', $id)")
|
||||||
|
.bind(("id", "gacha_roll_test_id"))
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let gacha_item_id = "gacha_item_test_id";
|
||||||
|
db.query("CREATE type::thing('app_gacha_items', $id) SET name = $name, image_url = $image_url, is_deleted = $is_deleted, created_at = $created_at, updated_at = $updated_at")
|
||||||
|
.bind(("id", gacha_item_id))
|
||||||
|
.bind(("name", "Test Gacha Item"))
|
||||||
|
.bind(("image_url", "https://example.com/gacha_item.png"))
|
||||||
|
.bind(("is_deleted", false))
|
||||||
|
.bind(("created_at", get_iso_date()))
|
||||||
|
.bind(("updated_at", get_iso_date()))
|
||||||
|
.await?;
|
||||||
|
println!("Gacha Item seeded successfully!");
|
||||||
|
|
||||||
|
let gacha_roll_id = "gacha_roll_test_id";
|
||||||
|
db.query("CREATE type::thing('app_gacha_rolls', $id) SET item = $item, quantity = $quantity, weight = $weight, is_deleted = $is_deleted, created_at = $created_at, updated_at = $updated_at")
|
||||||
|
.bind(("id", gacha_roll_id))
|
||||||
|
.bind(("item", Thing::from(("app_gacha_items", gacha_item_id))))
|
||||||
|
.bind(("quantity", 10))
|
||||||
|
.bind(("weight", 1.0))
|
||||||
|
.bind(("is_deleted", false))
|
||||||
|
.bind(("created_at", get_iso_date()))
|
||||||
|
.bind(("updated_at", get_iso_date()))
|
||||||
|
.await?;
|
||||||
|
println!("Gacha Roll seeded successfully!");
|
||||||
|
|
||||||
|
println!("✅ Gacha items and rolls seeded.");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,78 @@
|
|||||||
|
use imphnen_iam::PermissionsEnum;
|
||||||
|
use imphnen_libs::enviroment::load_env;
|
||||||
|
use imphnen_utils::{get_iso_date, Env};
|
||||||
|
use serde_json::json;
|
||||||
|
use std::error::Error;
|
||||||
|
use surrealdb::engine::any;
|
||||||
|
use surrealdb::opt::auth::Root;
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
load_env();
|
||||||
|
|
||||||
|
let env = Env::new();
|
||||||
|
let db = any::connect(&env.surrealdb_url).await?;
|
||||||
|
db.signin(Root {
|
||||||
|
username: &env.surrealdb_username,
|
||||||
|
password: &env.surrealdb_password,
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
|
db.use_ns(env.surrealdb_namespace)
|
||||||
|
.use_db(env.surrealdb_dbname)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
for permission in [
|
||||||
|
PermissionsEnum::ReadListUsers,
|
||||||
|
PermissionsEnum::ReadDetailUsers,
|
||||||
|
PermissionsEnum::CreateUsers,
|
||||||
|
PermissionsEnum::DeleteUsers,
|
||||||
|
PermissionsEnum::UpdateUsers,
|
||||||
|
PermissionsEnum::ActivateUsers,
|
||||||
|
PermissionsEnum::ReadListRoles,
|
||||||
|
PermissionsEnum::ReadDetailRoles,
|
||||||
|
PermissionsEnum::CreateRoles,
|
||||||
|
PermissionsEnum::DeleteRoles,
|
||||||
|
PermissionsEnum::UpdateRoles,
|
||||||
|
PermissionsEnum::ReadListPermissions,
|
||||||
|
PermissionsEnum::ReadDetailPermissions,
|
||||||
|
PermissionsEnum::CreatePermissions,
|
||||||
|
PermissionsEnum::DeletePermissions,
|
||||||
|
PermissionsEnum::UpdatePermissions,
|
||||||
|
PermissionsEnum::CreateGachaClaims,
|
||||||
|
PermissionsEnum::ReadDetailGachaClaims,
|
||||||
|
PermissionsEnum::ReadListGachaItems,
|
||||||
|
PermissionsEnum::ReadDetailGachaItems,
|
||||||
|
PermissionsEnum::CreateGachaItems,
|
||||||
|
PermissionsEnum::DeleteGachaItems,
|
||||||
|
PermissionsEnum::UpdateGachaItems,
|
||||||
|
PermissionsEnum::ReadDetailGachaRolls,
|
||||||
|
PermissionsEnum::CreateGachaRolls,
|
||||||
|
PermissionsEnum::ExecuteGachaRolls,
|
||||||
|
PermissionsEnum::ReadListMentors,
|
||||||
|
PermissionsEnum::ReadDetailMentors,
|
||||||
|
PermissionsEnum::RegisterMentors,
|
||||||
|
PermissionsEnum::ReadOwnMentorProfile,
|
||||||
|
PermissionsEnum::UpdateOwnMentorProfile,
|
||||||
|
PermissionsEnum::ReadOwnMentorStatus,
|
||||||
|
PermissionsEnum::VerifyMentors,
|
||||||
|
PermissionsEnum::DeleteMentors,
|
||||||
|
] {
|
||||||
|
db.query("CREATE type::thing('app_permissions', $id) CONTENT $data")
|
||||||
|
.bind(("id", permission.id()))
|
||||||
|
.bind((
|
||||||
|
"data",
|
||||||
|
json!({
|
||||||
|
"name": permission.to_string(),
|
||||||
|
"is_deleted": false,
|
||||||
|
"created_at": get_iso_date(),
|
||||||
|
"updated_at": get_iso_date()
|
||||||
|
}),
|
||||||
|
))
|
||||||
|
.await?;
|
||||||
|
println!("✅ Inserted: {permission}");
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("✅ All Permissions seeded");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
use imphnen_libs::enviroment::load_env;
|
||||||
|
use imphnen_utils::{get_iso_date, Env};
|
||||||
|
use serde_json::json;
|
||||||
|
use std::error::Error;
|
||||||
|
use surrealdb::engine::any;
|
||||||
|
use surrealdb::opt::auth::Root;
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
load_env();
|
||||||
|
let env = Env::new();
|
||||||
|
let db = any::connect(&env.surrealdb_url).await?;
|
||||||
|
db.signin(Root {
|
||||||
|
username: &env.surrealdb_username,
|
||||||
|
password: &env.surrealdb_password,
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
|
db.use_ns(env.surrealdb_namespace)
|
||||||
|
.use_db(env.surrealdb_dbname)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let roles = vec![
|
||||||
|
(
|
||||||
|
"50133429-f4b1-4249-9f97-7b86e6ee9d86",
|
||||||
|
"Staf",
|
||||||
|
Some("2025-02-24T16:52:27.630453+00"),
|
||||||
|
Some("2025-02-24T16:52:27.630461+00"),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"5713cb37-dc02-4e87-8048-d7a41d352059",
|
||||||
|
"User",
|
||||||
|
None,
|
||||||
|
Some("2025-02-28T14:53:58.576688+00"),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"60f1aeb7-dad2-4e06-bcb5-be1ba510c906",
|
||||||
|
"Staff Aktivasi User",
|
||||||
|
Some("2025-02-20T02:47:09.660640+00"),
|
||||||
|
Some("2025-02-20T02:48:30.083283+00"),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"6d4fea5d-4a08-4b8a-9782-f2ab2183dcf0",
|
||||||
|
"Admin Pembayaran",
|
||||||
|
Some("2025-01-29T05:39:28.562667+00"),
|
||||||
|
Some("2025-03-12T22:56:29.597416+00"),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"f6b03f25-e416-4893-ac88-caaa690afb07",
|
||||||
|
"Admin",
|
||||||
|
None,
|
||||||
|
Some("2025-02-22T15:38:39.868306+00"),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"3b9f8c4e-6a2d-4f8a-9a12-2d6f8b3c4e5a",
|
||||||
|
"Mentor",
|
||||||
|
None,
|
||||||
|
Some("2025-07-06T10:00:00.000000+00"),
|
||||||
|
),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (id, name, _created_at, _updated_at) in roles {
|
||||||
|
db.query("DELETE type::thing('app_roles', $id)")
|
||||||
|
.bind(("id", id))
|
||||||
|
.await?;
|
||||||
|
db.query("CREATE type::thing('app_roles', $id) CONTENT $data")
|
||||||
|
.bind(("id", id))
|
||||||
|
.bind((
|
||||||
|
"data",
|
||||||
|
json!({
|
||||||
|
"name": name,
|
||||||
|
"permissions": [],
|
||||||
|
"is_deleted": false,
|
||||||
|
"created_at": get_iso_date(),
|
||||||
|
"updated_at": get_iso_date(),
|
||||||
|
}),
|
||||||
|
))
|
||||||
|
.await?;
|
||||||
|
println!("✅ Inserted role: {name}");
|
||||||
|
}
|
||||||
|
println!("✅ All Roles seeded");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -0,0 +1,134 @@
|
|||||||
|
use imphnen_iam::{get_iso_date, make_thing, Env, PermissionsEnum};
|
||||||
|
use imphnen_libs::enviroment::load_env;
|
||||||
|
use std::error::Error;
|
||||||
|
use surrealdb::engine::any;
|
||||||
|
use surrealdb::opt::auth::Root;
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
load_env();
|
||||||
|
let env = Env::new();
|
||||||
|
let db = any::connect(&env.surrealdb_url).await?;
|
||||||
|
db.signin(Root {
|
||||||
|
username: &env.surrealdb_username,
|
||||||
|
password: &env.surrealdb_password,
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
|
db.use_ns(env.surrealdb_namespace)
|
||||||
|
.use_db(env.surrealdb_dbname)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let roles_permissions = vec![
|
||||||
|
(
|
||||||
|
"f6b03f25-e416-4893-ac88-caaa690afb07",
|
||||||
|
vec![
|
||||||
|
PermissionsEnum::ReadListUsers,
|
||||||
|
PermissionsEnum::ReadDetailUsers,
|
||||||
|
PermissionsEnum::CreateUsers,
|
||||||
|
PermissionsEnum::DeleteUsers,
|
||||||
|
PermissionsEnum::UpdateUsers,
|
||||||
|
PermissionsEnum::ActivateUsers,
|
||||||
|
PermissionsEnum::ReadListRoles,
|
||||||
|
PermissionsEnum::ReadDetailRoles,
|
||||||
|
PermissionsEnum::CreateRoles,
|
||||||
|
PermissionsEnum::DeleteRoles,
|
||||||
|
PermissionsEnum::UpdateRoles,
|
||||||
|
PermissionsEnum::ReadListPermissions,
|
||||||
|
PermissionsEnum::ReadDetailPermissions,
|
||||||
|
PermissionsEnum::CreatePermissions,
|
||||||
|
PermissionsEnum::DeletePermissions,
|
||||||
|
PermissionsEnum::UpdatePermissions,
|
||||||
|
PermissionsEnum::CreateGachaClaims,
|
||||||
|
PermissionsEnum::ReadDetailGachaClaims,
|
||||||
|
PermissionsEnum::ReadListGachaItems,
|
||||||
|
PermissionsEnum::ReadDetailGachaItems,
|
||||||
|
PermissionsEnum::CreateGachaItems,
|
||||||
|
PermissionsEnum::DeleteGachaItems,
|
||||||
|
PermissionsEnum::UpdateGachaItems,
|
||||||
|
PermissionsEnum::ReadDetailGachaRolls,
|
||||||
|
PermissionsEnum::CreateGachaRolls,
|
||||||
|
PermissionsEnum::ExecuteGachaRolls,
|
||||||
|
PermissionsEnum::ReadListMentors,
|
||||||
|
PermissionsEnum::ReadDetailMentors,
|
||||||
|
PermissionsEnum::RegisterMentors,
|
||||||
|
PermissionsEnum::UpdateMentors,
|
||||||
|
PermissionsEnum::VerifyMentors,
|
||||||
|
PermissionsEnum::DeleteMentors,
|
||||||
|
],
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"3b9f8c4e-6a2d-4f8a-9a12-2d6f8b3c4e5a",
|
||||||
|
vec![
|
||||||
|
PermissionsEnum::ReadOwnMentorProfile,
|
||||||
|
PermissionsEnum::UpdateOwnMentorProfile,
|
||||||
|
PermissionsEnum::ReadOwnMentorStatus,
|
||||||
|
PermissionsEnum::ReadListMentors,
|
||||||
|
PermissionsEnum::ReadDetailMentors,
|
||||||
|
PermissionsEnum::ReadListGachaItems,
|
||||||
|
PermissionsEnum::ReadDetailGachaItems,
|
||||||
|
PermissionsEnum::ReadDetailGachaRolls,
|
||||||
|
PermissionsEnum::CreateGachaRolls,
|
||||||
|
PermissionsEnum::ExecuteGachaRolls,
|
||||||
|
],
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"5713cb37-dc02-4e87-8048-d7a41d352059",
|
||||||
|
vec![
|
||||||
|
PermissionsEnum::ReadListGachaItems,
|
||||||
|
PermissionsEnum::ReadDetailGachaItems,
|
||||||
|
PermissionsEnum::CreateGachaClaims,
|
||||||
|
PermissionsEnum::ReadDetailGachaClaims,
|
||||||
|
PermissionsEnum::ReadDetailGachaRolls,
|
||||||
|
PermissionsEnum::CreateGachaRolls,
|
||||||
|
PermissionsEnum::ExecuteGachaRolls,
|
||||||
|
PermissionsEnum::RegisterMentors,
|
||||||
|
PermissionsEnum::ReadListMentors,
|
||||||
|
PermissionsEnum::ReadDetailMentors,
|
||||||
|
PermissionsEnum::ReadOwnMentorProfile,
|
||||||
|
PermissionsEnum::ReadOwnMentorStatus,
|
||||||
|
],
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"50133429-f4b1-4249-9f97-7b86e6ee9d86",
|
||||||
|
vec![
|
||||||
|
PermissionsEnum::ReadListUsers,
|
||||||
|
PermissionsEnum::ReadListMentors,
|
||||||
|
PermissionsEnum::ReadDetailUsers,
|
||||||
|
PermissionsEnum::ActivateUsers,
|
||||||
|
PermissionsEnum::ReadListRoles,
|
||||||
|
PermissionsEnum::ReadDetailRoles,
|
||||||
|
PermissionsEnum::ReadListPermissions,
|
||||||
|
PermissionsEnum::ReadDetailPermissions,
|
||||||
|
PermissionsEnum::ReadListGachaItems,
|
||||||
|
PermissionsEnum::ReadDetailGachaItems,
|
||||||
|
PermissionsEnum::ReadListMentors,
|
||||||
|
PermissionsEnum::ReadDetailMentors,
|
||||||
|
PermissionsEnum::ReadDetailGachaRolls,
|
||||||
|
PermissionsEnum::CreateGachaRolls,
|
||||||
|
PermissionsEnum::ExecuteGachaRolls,
|
||||||
|
],
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"60f1aeb7-dad2-4e06-bcb5-be1ba510c906",
|
||||||
|
vec![PermissionsEnum::ActivateUsers],
|
||||||
|
),
|
||||||
|
("6d4fea5d-4a08-4b8a-9782-f2ab2183dcf0", vec![]),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (role_id, permissions) in roles_permissions {
|
||||||
|
let permission_refs: Vec<_> = permissions
|
||||||
|
.iter()
|
||||||
|
.map(|perm| make_thing("app_permissions", perm.id()))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
db.query("UPDATE type::thing('app_roles', $role_id) SET permissions = $permissions, updated_at = $updated_at WHERE is_deleted = false")
|
||||||
|
.bind(("role_id", role_id))
|
||||||
|
.bind(("permissions", permission_refs))
|
||||||
|
.bind(("updated_at", get_iso_date()))
|
||||||
|
.await?;
|
||||||
|
println!("✅ Permissions updated for role: {role_id}");
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("✅ All roles permissions updated!");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
use imphnen_iam::UsersSchema;
|
||||||
|
use imphnen_libs::enviroment::load_env;
|
||||||
|
use imphnen_utils::{get_iso_date, hash_password, Env};
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
|
use surrealdb::{opt::auth::Root, sql::Thing};
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
load_env();
|
||||||
|
let env = Env::new();
|
||||||
|
use surrealdb::engine::any;
|
||||||
|
let db = any::connect(&env.surrealdb_url).await?;
|
||||||
|
db.signin(Root {
|
||||||
|
username: &env.surrealdb_username,
|
||||||
|
password: &env.surrealdb_password,
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
|
db.use_ns(env.surrealdb_namespace)
|
||||||
|
.use_db(env.surrealdb_dbname)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let users = vec![
|
||||||
|
(
|
||||||
|
"c3b1d6a8-8d4f-4b36-b789-2e532ec7a7b2",
|
||||||
|
"admin@example.com",
|
||||||
|
"Admin",
|
||||||
|
"f6b03f25-e416-4893-ac88-caaa690afb07",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"a4d23fb5-9e31-423c-9842-fbd6e75a5298",
|
||||||
|
"staff@example.com",
|
||||||
|
"Staff",
|
||||||
|
"50133429-f4b1-4249-9f97-7b86e6ee9d86",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"d5e89c12-72af-4b1a-abc3-ff1234567890",
|
||||||
|
"user@example.com",
|
||||||
|
"User",
|
||||||
|
"5713cb37-dc02-4e87-8048-d7a41d352059",
|
||||||
|
),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (id, email, fullname, role_id) in users {
|
||||||
|
db.query("DELETE type::thing('app_users', $id)")
|
||||||
|
.bind(("id", id))
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let user = UsersSchema {
|
||||||
|
id: Thing::from(("app_users", id)),
|
||||||
|
fullname: fullname.into(),
|
||||||
|
email: email.into(),
|
||||||
|
password: hash_password("password").unwrap(),
|
||||||
|
avatar: None,
|
||||||
|
phone_number: "081234567890".into(),
|
||||||
|
is_active: true,
|
||||||
|
is_deleted: false,
|
||||||
|
mentor_id: None,
|
||||||
|
gender: None,
|
||||||
|
birthdate: None,
|
||||||
|
role: Thing::from(("app_roles", role_id)),
|
||||||
|
created_at: get_iso_date(),
|
||||||
|
updated_at: get_iso_date(),
|
||||||
|
};
|
||||||
|
|
||||||
|
db.create::<Option<UsersSchema>>(("app_users", id))
|
||||||
|
.content(user)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
println!("✅ Inserted user: {fullname} ({email})");
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("✅ All Users seeded");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
use std::error::Error;
|
||||||
|
use std::process::Command;
|
||||||
|
|
||||||
|
fn run_seed(bin: &str) -> Result<(), Box<dyn Error>> {
|
||||||
|
println!("🔧 Seeding: {bin}");
|
||||||
|
let status = Command::new("cargo").args(["run", "--bin", bin]).status()?;
|
||||||
|
|
||||||
|
if !status.success() {
|
||||||
|
Err(format!("❌ Failed to run seed: {bin}").into())
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
println!("🚀 Running all seeders...\n");
|
||||||
|
|
||||||
|
run_seed("seed_permissions")?;
|
||||||
|
run_seed("seed_roles")?;
|
||||||
|
run_seed("seed_roles_permissions")?;
|
||||||
|
run_seed("seed_users")?;
|
||||||
|
run_seed("seed_events")?;
|
||||||
|
run_seed("seed_gacha_rolls")?;
|
||||||
|
run_seed("seed_mentor_user")?;
|
||||||
|
println!("\n✅ All seeding completed successfully.");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
use imphnen_gateway::gateway_service;
|
||||||
|
use imphnen_libs::axum_init;
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() {
|
||||||
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
|
axum_init(|surrealdb_ws, surrealdb_mem| async {
|
||||||
|
gateway_service(surrealdb_ws, surrealdb_mem).await
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
[package]
|
||||||
|
name = "imphnen-cms"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
imphnen-iam.workspace = true
|
||||||
|
imphnen-libs.workspace = true
|
||||||
|
imphnen-utils.workspace = true
|
||||||
|
imphnen-entities.workspace = true
|
||||||
|
axum.workspace = true
|
||||||
|
serde.workspace = true
|
||||||
|
serde_json.workspace = true
|
||||||
|
utoipa.workspace = true
|
||||||
|
lazy_static.workspace = true
|
||||||
|
regex.workspace = true
|
||||||
|
validator.workspace = true
|
||||||
|
axum-test.workspace = true
|
||||||
|
surrealdb.workspace = true
|
||||||
|
rand.workspace = true
|
||||||
|
tokio.workspace = true
|
||||||
|
chrono.workspace = true
|
||||||
|
anyhow.workspace = true
|
||||||
|
tower-http.workspace = true
|
||||||
|
utoipa-swagger-ui.workspace = true
|
||||||
|
log.workspace = true
|
||||||
|
tracing.workspace = true
|
||||||
|
|
||||||
|
[package.metadata.validator.regex]
|
||||||
|
VALID_URL_REGEX = "^https?://"
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
pub mod v1;
|
||||||
|
pub use v1::*;
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
use super::{
|
||||||
|
events_dto::{
|
||||||
|
EventsCreateRequestDto, EventsDetailItemDto, EventsListItemDto,
|
||||||
|
EventsUpdateRequestDto,
|
||||||
|
},
|
||||||
|
events_service::EventsService,
|
||||||
|
};
|
||||||
|
use axum::extract::{Path, Query};
|
||||||
|
use axum::response::IntoResponse;
|
||||||
|
use axum::{Extension, Json};
|
||||||
|
use imphnen_libs::{
|
||||||
|
AppState, MessageResponseDto, MetaRequestDto, ResponseListSuccessDto,
|
||||||
|
ResponseSuccessDto,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/v1/cms/landing/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"),
|
||||||
|
("filter" = Option<String>, Query, description = "Filter value"),
|
||||||
|
("filter_by" = Option<String>, Query, description = "Field to filter by"),
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Get event list", body = ResponseListSuccessDto<Vec<EventsListItemDto>>)
|
||||||
|
),
|
||||||
|
tag = "Events"
|
||||||
|
)]
|
||||||
|
pub async fn get_event_list(
|
||||||
|
Extension(state): Extension<AppState>,
|
||||||
|
Query(meta): Query<MetaRequestDto>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
EventsService::get_event_list(&state, meta).await
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/v1/cms/landing/events/detail/{id}",
|
||||||
|
params(
|
||||||
|
("id" = String, Path, description = "Event ID")
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Get event by ID", body = ResponseSuccessDto<EventsDetailItemDto>)
|
||||||
|
),
|
||||||
|
tag = "Events"
|
||||||
|
)]
|
||||||
|
pub async fn get_event_by_id(
|
||||||
|
Extension(state): Extension<AppState>,
|
||||||
|
Path(id): Path<String>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
EventsService::get_event_by_id(&state, id).await
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
security(
|
||||||
|
("Bearer" = [])
|
||||||
|
),
|
||||||
|
path = "/v1/cms/landing/events/create",
|
||||||
|
request_body = EventsCreateRequestDto,
|
||||||
|
responses(
|
||||||
|
(status = 201, description = "Create new event", body = MessageResponseDto)
|
||||||
|
),
|
||||||
|
tag = "Events"
|
||||||
|
)]
|
||||||
|
pub async fn post_create_event(
|
||||||
|
Extension(state): Extension<AppState>,
|
||||||
|
Json(payload): Json<EventsCreateRequestDto>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
EventsService::create_event(&state, payload).await
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
patch,
|
||||||
|
security(
|
||||||
|
("Bearer" = [])
|
||||||
|
),
|
||||||
|
path = "/v1/cms/landing/events/update/{id}",
|
||||||
|
params(
|
||||||
|
("id" = String, Path, description = "Event ID")
|
||||||
|
),
|
||||||
|
request_body = EventsUpdateRequestDto,
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Update event", body = MessageResponseDto)
|
||||||
|
),
|
||||||
|
tag = "Events"
|
||||||
|
)]
|
||||||
|
pub async fn patch_update_event(
|
||||||
|
Extension(state): Extension<AppState>,
|
||||||
|
Path(id): Path<String>,
|
||||||
|
Json(payload): Json<EventsUpdateRequestDto>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
EventsService::update_event(&state, id, payload).await
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
delete,
|
||||||
|
security(
|
||||||
|
("Bearer" = [])
|
||||||
|
),
|
||||||
|
path = "/v1/cms/landing/events/delete/{id}",
|
||||||
|
params(
|
||||||
|
("id" = String, Path, description = "Event ID")
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Soft delete event", body = MessageResponseDto)
|
||||||
|
),
|
||||||
|
tag = "Events"
|
||||||
|
)]
|
||||||
|
pub async fn delete_event(
|
||||||
|
Extension(state): Extension<AppState>,
|
||||||
|
Path(id): Path<String>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
EventsService::delete_event(&state, id).await
|
||||||
|
}
|
||||||
@@ -0,0 +1,118 @@
|
|||||||
|
use lazy_static::lazy_static;
|
||||||
|
lazy_static! {
|
||||||
|
pub static ref VALID_URL_REGEX: regex::Regex =
|
||||||
|
regex::Regex::new(r"^https?://").unwrap();
|
||||||
|
}
|
||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use surrealdb::sql::Thing;
|
||||||
|
use utoipa::ToSchema;
|
||||||
|
use validator::Validate;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema, Validate)]
|
||||||
|
pub struct EventsCreateRequestDto {
|
||||||
|
#[validate(length(min = 1, message = "Name is required"))]
|
||||||
|
pub name: String,
|
||||||
|
|
||||||
|
#[validate(length(min = 1, message = "Description is required"))]
|
||||||
|
pub description: String,
|
||||||
|
|
||||||
|
#[validate(url(message = "Detail link must be a valid URL"))]
|
||||||
|
pub detail_link: String,
|
||||||
|
|
||||||
|
#[validate(range(min = 0.0, message = "Price cannot be negative"))]
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema, Validate)]
|
||||||
|
pub struct EventsUpdateRequestDto {
|
||||||
|
#[validate(length(min = 1, message = "Name is required"))]
|
||||||
|
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>,
|
||||||
|
|
||||||
|
#[validate(range(min = 0.0, message = "Price cannot be negative"))]
|
||||||
|
pub price: f64,
|
||||||
|
pub is_online: bool,
|
||||||
|
pub description: String,
|
||||||
|
#[validate(url(message = "Detail link must be a valid URL"))]
|
||||||
|
pub detail_link: String,
|
||||||
|
pub location: Option<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,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
pub struct EventsQueryDto {
|
||||||
|
pub id: Thing,
|
||||||
|
pub name: String,
|
||||||
|
pub description: String,
|
||||||
|
pub detail_link: String,
|
||||||
|
pub price: f64,
|
||||||
|
pub is_online: bool,
|
||||||
|
pub is_deleted: bool,
|
||||||
|
pub start_date: String,
|
||||||
|
pub end_date: String,
|
||||||
|
pub created_at: String,
|
||||||
|
pub updated_at: String,
|
||||||
|
pub location: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EventsQueryDto {
|
||||||
|
pub fn from(self) -> EventsListItemDto {
|
||||||
|
EventsListItemDto {
|
||||||
|
id: self.id.id.to_raw(),
|
||||||
|
name: self.name,
|
||||||
|
description: self.description,
|
||||||
|
detail_link: self.detail_link,
|
||||||
|
price: self.price,
|
||||||
|
location: self.location,
|
||||||
|
is_online: self.is_online,
|
||||||
|
start_date: self.start_date,
|
||||||
|
end_date: self.end_date,
|
||||||
|
created_at: self.created_at,
|
||||||
|
is_deleted: self.is_deleted,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,149 @@
|
|||||||
|
use super::{events_dto::EventsQueryDto, events_schema::EventsSchema};
|
||||||
|
use anyhow::{Result, bail};
|
||||||
|
use imphnen_libs::{AppState, MetaRequestDto, ResourceEnum, ResponseListSuccessDto};
|
||||||
|
use imphnen_utils::{DetailQueryBuilder, ListQueryBuilder, get_id, get_iso_date};
|
||||||
|
use std::time::Instant;
|
||||||
|
use tracing::instrument;
|
||||||
|
|
||||||
|
pub struct EventsRepository<'a> {
|
||||||
|
state: &'a AppState,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> EventsRepository<'a> {
|
||||||
|
pub fn new(state: &'a AppState) -> Self {
|
||||||
|
Self { state }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[instrument(skip(self, meta), err)]
|
||||||
|
pub async fn query_event_list(
|
||||||
|
&self,
|
||||||
|
meta: MetaRequestDto,
|
||||||
|
) -> Result<ResponseListSuccessDto<Vec<EventsQueryDto>>> {
|
||||||
|
let now = Instant::now();
|
||||||
|
let query = ListQueryBuilder::new(ResourceEnum::Events.to_string())
|
||||||
|
.with_select_fields(vec!["*"])
|
||||||
|
.with_pagination(meta.page, Some(10))
|
||||||
|
.with_sorting(meta.sort_by.as_deref(), meta.order.as_deref())
|
||||||
|
.build();
|
||||||
|
let res: Vec<EventsQueryDto> =
|
||||||
|
self.state.surrealdb_ws.query(query).await?.take(0)?;
|
||||||
|
let elapsed = now.elapsed();
|
||||||
|
if std::env::var("RUST_ENV").unwrap_or_else(|_| "development".to_string())
|
||||||
|
== "development"
|
||||||
|
{
|
||||||
|
println!("Query 'query_event_list' took: {elapsed:.2?}");
|
||||||
|
}
|
||||||
|
let data = ResponseListSuccessDto {
|
||||||
|
data: res,
|
||||||
|
meta: None,
|
||||||
|
};
|
||||||
|
Ok(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[instrument(skip(self, id), err)]
|
||||||
|
pub async fn query_event_by_id(&self, id: String) -> Result<EventsQueryDto> {
|
||||||
|
let now = Instant::now();
|
||||||
|
let db = &self.state.surrealdb_ws;
|
||||||
|
let builder = DetailQueryBuilder::new(ResourceEnum::Events.to_string())
|
||||||
|
.with_id(&id)
|
||||||
|
.with_select_fields(vec!["*"]);
|
||||||
|
let sql = builder.build();
|
||||||
|
let result: Option<EventsQueryDto> =
|
||||||
|
builder.apply_bindings(db.query(sql)).await?.take(0)?;
|
||||||
|
let elapsed = now.elapsed();
|
||||||
|
if std::env::var("RUST_ENV").unwrap_or_else(|_| "development".to_string())
|
||||||
|
== "development"
|
||||||
|
{
|
||||||
|
println!("Query 'query_event_by_id' took: {elapsed:.2?}");
|
||||||
|
}
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Some(event) => {
|
||||||
|
if event.is_deleted {
|
||||||
|
bail!("Event not found");
|
||||||
|
}
|
||||||
|
Ok(event)
|
||||||
|
}
|
||||||
|
None => bail!("Event not found"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[instrument(skip(self, data), err)]
|
||||||
|
pub async fn query_create_event(&self, data: EventsSchema) -> Result<String> {
|
||||||
|
let now = Instant::now();
|
||||||
|
let db = &self.state.surrealdb_ws;
|
||||||
|
let record: Option<EventsSchema> = db
|
||||||
|
.create(ResourceEnum::Events.to_string())
|
||||||
|
.content(data)
|
||||||
|
.await?;
|
||||||
|
let elapsed = now.elapsed();
|
||||||
|
if std::env::var("RUST_ENV").unwrap_or_else(|_| "development".to_string())
|
||||||
|
== "development"
|
||||||
|
{
|
||||||
|
println!("Query 'query_create_event' took: {elapsed:.2?}");
|
||||||
|
}
|
||||||
|
|
||||||
|
match record {
|
||||||
|
Some(_) => Ok("Success create event".into()),
|
||||||
|
None => bail!("Failed to create event"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[instrument(skip(self, data), err)]
|
||||||
|
pub async fn query_update_event(&self, data: EventsSchema) -> Result<String> {
|
||||||
|
let now = Instant::now();
|
||||||
|
let db = &self.state.surrealdb_ws;
|
||||||
|
|
||||||
|
let existing = self.query_event_by_id(data.id.id.to_raw()).await?;
|
||||||
|
if existing.is_deleted {
|
||||||
|
bail!("Event already deleted");
|
||||||
|
}
|
||||||
|
|
||||||
|
let merged = EventsSchema {
|
||||||
|
created_at: existing.created_at,
|
||||||
|
updated_at: get_iso_date(),
|
||||||
|
..data
|
||||||
|
};
|
||||||
|
|
||||||
|
let record_key = get_id(&merged.id)?;
|
||||||
|
let record: Option<EventsSchema> = db.update(record_key).merge(merged).await?;
|
||||||
|
let elapsed = now.elapsed();
|
||||||
|
if std::env::var("RUST_ENV").unwrap_or_else(|_| "development".to_string())
|
||||||
|
== "development"
|
||||||
|
{
|
||||||
|
println!("Query 'query_update_event' took: {elapsed:.2?}");
|
||||||
|
}
|
||||||
|
|
||||||
|
match record {
|
||||||
|
Some(_) => Ok("Success update event".into()),
|
||||||
|
None => bail!("Failed to update event"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[instrument(skip(self, id), err)]
|
||||||
|
pub async fn query_delete_event(&self, id: String) -> Result<String> {
|
||||||
|
let now = Instant::now();
|
||||||
|
let db = &self.state.surrealdb_ws;
|
||||||
|
let event = self.query_event_by_id(id).await?;
|
||||||
|
if event.is_deleted {
|
||||||
|
bail!("Event not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
let record_key = get_id(&event.id)?;
|
||||||
|
let record: Option<EventsSchema> = db
|
||||||
|
.update(record_key)
|
||||||
|
.merge(serde_json::json!({ "is_deleted": true }))
|
||||||
|
.await?;
|
||||||
|
let elapsed = now.elapsed();
|
||||||
|
if std::env::var("RUST_ENV").unwrap_or_else(|_| "development".to_string())
|
||||||
|
== "development"
|
||||||
|
{
|
||||||
|
println!("Query 'query_delete_event' took: {elapsed:.2?}");
|
||||||
|
}
|
||||||
|
|
||||||
|
match record {
|
||||||
|
Some(_) => Ok("Success delete event".into()),
|
||||||
|
None => bail!("Failed to delete event"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,102 @@
|
|||||||
|
use imphnen_libs::ResourceEnum;
|
||||||
|
use imphnen_utils::{get_iso_date, make_thing};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use surrealdb::Uuid;
|
||||||
|
use surrealdb::sql::Thing;
|
||||||
|
|
||||||
|
use super::events_dto::{
|
||||||
|
EventsCreateRequestDto, EventsQueryDto, EventsUpdateRequestDto,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
pub struct EventsSchema {
|
||||||
|
pub id: Thing,
|
||||||
|
pub price: f64,
|
||||||
|
pub is_online: bool,
|
||||||
|
pub is_deleted: bool,
|
||||||
|
pub name: String,
|
||||||
|
pub end_date: String,
|
||||||
|
pub start_date: String,
|
||||||
|
pub created_at: String,
|
||||||
|
pub updated_at: String,
|
||||||
|
pub description: String,
|
||||||
|
pub detail_link: String,
|
||||||
|
pub location: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for EventsSchema {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
id: make_thing(
|
||||||
|
&ResourceEnum::Events.to_string(),
|
||||||
|
&Uuid::new_v4().to_string(),
|
||||||
|
),
|
||||||
|
name: String::new(),
|
||||||
|
description: String::new(),
|
||||||
|
detail_link: String::new(),
|
||||||
|
price: 0.0,
|
||||||
|
location: None,
|
||||||
|
is_online: false,
|
||||||
|
is_deleted: false,
|
||||||
|
start_date: String::new(),
|
||||||
|
end_date: String::new(),
|
||||||
|
created_at: get_iso_date(),
|
||||||
|
updated_at: get_iso_date(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EventsSchema {
|
||||||
|
pub fn from(dto: EventsQueryDto) -> Self {
|
||||||
|
Self {
|
||||||
|
id: dto.id,
|
||||||
|
name: dto.name,
|
||||||
|
description: dto.description,
|
||||||
|
detail_link: dto.detail_link,
|
||||||
|
price: dto.price,
|
||||||
|
location: dto.location,
|
||||||
|
is_online: dto.is_online,
|
||||||
|
is_deleted: false,
|
||||||
|
start_date: dto.start_date,
|
||||||
|
end_date: dto.end_date,
|
||||||
|
created_at: dto.created_at,
|
||||||
|
updated_at: dto.updated_at,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create(payload: EventsCreateRequestDto) -> Self {
|
||||||
|
Self {
|
||||||
|
id: make_thing(
|
||||||
|
&ResourceEnum::Events.to_string(),
|
||||||
|
&Uuid::new_v4().to_string(),
|
||||||
|
),
|
||||||
|
name: payload.name,
|
||||||
|
description: payload.description,
|
||||||
|
detail_link: payload.detail_link,
|
||||||
|
price: payload.price,
|
||||||
|
location: payload.location,
|
||||||
|
is_online: payload.is_online,
|
||||||
|
is_deleted: false,
|
||||||
|
end_date: payload.end_date.to_string(),
|
||||||
|
start_date: payload.start_date.to_string(),
|
||||||
|
created_at: get_iso_date(),
|
||||||
|
updated_at: get_iso_date(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update(payload: EventsUpdateRequestDto, id: String) -> Self {
|
||||||
|
Self {
|
||||||
|
id: make_thing(&ResourceEnum::Events.to_string(), &id),
|
||||||
|
name: payload.name,
|
||||||
|
price: payload.price,
|
||||||
|
location: payload.location,
|
||||||
|
is_online: payload.is_online,
|
||||||
|
description: payload.description,
|
||||||
|
detail_link: payload.detail_link,
|
||||||
|
end_date: payload.end_date.to_string(),
|
||||||
|
start_date: payload.start_date.to_string(),
|
||||||
|
updated_at: get_iso_date(),
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
use super::{
|
||||||
|
events_dto::{
|
||||||
|
EventsCreateRequestDto, EventsDetailItemDto, EventsListItemDto, EventsQueryDto,
|
||||||
|
EventsUpdateRequestDto,
|
||||||
|
},
|
||||||
|
events_repository::EventsRepository,
|
||||||
|
events_schema::EventsSchema,
|
||||||
|
};
|
||||||
|
use axum::{http::StatusCode, response::Response};
|
||||||
|
use imphnen_libs::{
|
||||||
|
AppState, MetaRequestDto, ResponseListSuccessDto, ResponseSuccessDto,
|
||||||
|
};
|
||||||
|
use imphnen_utils::{
|
||||||
|
common_response, success_list_response, success_response, validate_request,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct EventsService;
|
||||||
|
|
||||||
|
impl EventsService {
|
||||||
|
pub async fn get_event_list(state: &AppState, meta: MetaRequestDto) -> Response {
|
||||||
|
let repo = EventsRepository::new(state);
|
||||||
|
match repo.query_event_list(meta).await {
|
||||||
|
Ok(data) => {
|
||||||
|
let items: Vec<EventsListItemDto> = data
|
||||||
|
.data
|
||||||
|
.into_iter()
|
||||||
|
.filter(|e| !e.is_deleted)
|
||||||
|
.map(EventsQueryDto::from)
|
||||||
|
.collect();
|
||||||
|
let response = ResponseListSuccessDto {
|
||||||
|
data: items,
|
||||||
|
meta: data.meta,
|
||||||
|
};
|
||||||
|
success_list_response(response)
|
||||||
|
}
|
||||||
|
Err(e) => common_response(StatusCode::BAD_REQUEST, &e.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_event_by_id(state: &AppState, id: String) -> Response {
|
||||||
|
let repo = EventsRepository::new(state);
|
||||||
|
match repo.query_event_by_id(id).await {
|
||||||
|
Ok(event) if !event.is_deleted => success_response(ResponseSuccessDto {
|
||||||
|
data: EventsDetailItemDto {
|
||||||
|
id: event.id.id.to_raw(),
|
||||||
|
name: event.name,
|
||||||
|
description: event.description,
|
||||||
|
detail_link: event.detail_link,
|
||||||
|
price: event.price,
|
||||||
|
is_online: event.is_online,
|
||||||
|
start_date: event.start_date,
|
||||||
|
end_date: event.end_date,
|
||||||
|
created_at: event.created_at,
|
||||||
|
updated_at: event.updated_at,
|
||||||
|
location: event.location,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
Ok(_) => common_response(StatusCode::NOT_FOUND, "Event not found"),
|
||||||
|
Err(e) => common_response(StatusCode::NOT_FOUND, &e.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn create_event(
|
||||||
|
state: &AppState,
|
||||||
|
payload: EventsCreateRequestDto,
|
||||||
|
) -> Response {
|
||||||
|
if let Err((status, message)) = validate_request(&payload) {
|
||||||
|
return common_response(status, &message);
|
||||||
|
}
|
||||||
|
let repo = EventsRepository::new(state);
|
||||||
|
let schema = EventsSchema::create(payload);
|
||||||
|
match repo.query_create_event(schema).await {
|
||||||
|
Ok(msg) => common_response(StatusCode::CREATED, &msg),
|
||||||
|
Err(e) => common_response(StatusCode::INTERNAL_SERVER_ERROR, &e.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update_event(
|
||||||
|
state: &AppState,
|
||||||
|
id: String,
|
||||||
|
payload: EventsUpdateRequestDto,
|
||||||
|
) -> Response {
|
||||||
|
if let Err((status, message)) = validate_request(&payload) {
|
||||||
|
return common_response(status, &message);
|
||||||
|
}
|
||||||
|
let repo = EventsRepository::new(state);
|
||||||
|
let schema = EventsSchema::update(payload, id);
|
||||||
|
match repo.query_update_event(schema).await {
|
||||||
|
Ok(msg) => common_response(StatusCode::OK, &msg),
|
||||||
|
Err(e) => common_response(StatusCode::BAD_REQUEST, &e.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn delete_event(state: &AppState, id: String) -> Response {
|
||||||
|
let repo = EventsRepository::new(state);
|
||||||
|
match repo.query_delete_event(id).await {
|
||||||
|
Ok(msg) => common_response(StatusCode::OK, &msg),
|
||||||
|
Err(e) => common_response(StatusCode::BAD_REQUEST, &e.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
use axum::{
|
||||||
|
Router,
|
||||||
|
routing::{delete, get, patch, post},
|
||||||
|
};
|
||||||
|
|
||||||
|
pub mod events_controller;
|
||||||
|
pub mod events_dto;
|
||||||
|
pub mod events_repository;
|
||||||
|
pub mod events_schema;
|
||||||
|
pub mod events_service;
|
||||||
|
|
||||||
|
pub use events_controller::*;
|
||||||
|
pub use events_dto::*;
|
||||||
|
pub use events_repository::*;
|
||||||
|
pub use events_schema::*;
|
||||||
|
pub use events_service::*;
|
||||||
|
|
||||||
|
pub fn events_public_routes() -> Router {
|
||||||
|
Router::new()
|
||||||
|
.route(
|
||||||
|
"/cms/landing/events",
|
||||||
|
get(events_controller::get_event_list),
|
||||||
|
)
|
||||||
|
.route(
|
||||||
|
"/cms/landing/events/detail/{id}",
|
||||||
|
get(events_controller::get_event_by_id),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn events_protected_routes() -> Router {
|
||||||
|
Router::new()
|
||||||
|
.route(
|
||||||
|
"/cms/landing/events/create",
|
||||||
|
post(events_controller::post_create_event),
|
||||||
|
)
|
||||||
|
.route(
|
||||||
|
"/cms/landing/events/update/{id}",
|
||||||
|
patch(events_controller::patch_update_event),
|
||||||
|
)
|
||||||
|
.route(
|
||||||
|
"/cms/landing/events/delete/{id}",
|
||||||
|
delete(events_controller::delete_event),
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
pub mod events;
|
||||||
|
pub mod testimonials;
|
||||||
|
|
||||||
|
pub use events::*;
|
||||||
|
pub use testimonials::*;
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
use axum::{
|
||||||
|
Router,
|
||||||
|
routing::{delete, get, patch, post},
|
||||||
|
};
|
||||||
|
|
||||||
|
pub mod testimonials_controller;
|
||||||
|
pub mod testimonials_dto;
|
||||||
|
pub mod testimonials_repository;
|
||||||
|
pub mod testimonials_schema;
|
||||||
|
pub mod testimonials_service;
|
||||||
|
|
||||||
|
pub use testimonials_controller::*;
|
||||||
|
pub use testimonials_dto::*;
|
||||||
|
pub use testimonials_repository::*;
|
||||||
|
pub use testimonials_schema::*;
|
||||||
|
pub use testimonials_service::*;
|
||||||
|
|
||||||
|
pub fn testimonials_public_routes() -> Router {
|
||||||
|
Router::new()
|
||||||
|
.route(
|
||||||
|
"/cms/landing/testimonials",
|
||||||
|
get(testimonials_controller::get_testimonial_list),
|
||||||
|
)
|
||||||
|
.route(
|
||||||
|
"/cms/landing/testimonials/detail/{id}",
|
||||||
|
get(testimonials_controller::get_testimonial_by_id),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn testimonials_protected_routes() -> Router {
|
||||||
|
Router::new()
|
||||||
|
.route(
|
||||||
|
"/cms/landing/testimonials/create",
|
||||||
|
post(testimonials_controller::post_create_testimonial),
|
||||||
|
)
|
||||||
|
.route(
|
||||||
|
"/cms/landing/testimonials/update/{id}",
|
||||||
|
patch(testimonials_controller::patch_update_testimonial),
|
||||||
|
)
|
||||||
|
.route(
|
||||||
|
"/cms/landing/testimonials/delete/{id}",
|
||||||
|
delete(testimonials_controller::delete_testimonial),
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -0,0 +1,124 @@
|
|||||||
|
use super::{
|
||||||
|
testimonials_dto::{
|
||||||
|
TestimonialsCreateRequestDto, TestimonialsDetailItemDto,
|
||||||
|
TestimonialsListItemDto, TestimonialsUpdateRequestDto,
|
||||||
|
},
|
||||||
|
testimonials_service::TestimonialsService,
|
||||||
|
};
|
||||||
|
use axum::extract::{Path, Query};
|
||||||
|
use axum::response::IntoResponse;
|
||||||
|
use axum::{Extension, Json};
|
||||||
|
use imphnen_iam::UsersDetailQueryDto;
|
||||||
|
use imphnen_libs::{
|
||||||
|
AppState, MessageResponseDto, MetaRequestDto, ResponseListSuccessDto,
|
||||||
|
ResponseSuccessDto,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/v1/cms/landing/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"),
|
||||||
|
("filter" = Option<String>, Query, description = "Filter value"),
|
||||||
|
("filter_by" = Option<String>, Query, description = "Field to filter by"),
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Get testimonial list", body = ResponseListSuccessDto<Vec<TestimonialsListItemDto>>)
|
||||||
|
),
|
||||||
|
tag = "Testimonials"
|
||||||
|
)]
|
||||||
|
pub async fn get_testimonial_list(
|
||||||
|
Extension(state): Extension<AppState>,
|
||||||
|
Query(meta): Query<MetaRequestDto>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
TestimonialsService::get_testimonial_list(&state, meta).await
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/v1/cms/landing/testimonials/detail/{id}",
|
||||||
|
params(
|
||||||
|
("id" = String, Path, description = "Testimonial ID")
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Get testimonial by ID", body = ResponseSuccessDto<TestimonialsDetailItemDto>)
|
||||||
|
),
|
||||||
|
tag = "Testimonials"
|
||||||
|
)]
|
||||||
|
pub async fn get_testimonial_by_id(
|
||||||
|
Extension(state): Extension<AppState>,
|
||||||
|
Path(id): Path<String>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
TestimonialsService::get_testimonial_by_id(&state, id).await
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
security(
|
||||||
|
("Bearer" = [])
|
||||||
|
),
|
||||||
|
path = "/v1/cms/landing/testimonials/create",
|
||||||
|
request_body = TestimonialsCreateRequestDto,
|
||||||
|
responses(
|
||||||
|
(status = 201, description = "Create new testimonial", body = MessageResponseDto)
|
||||||
|
),
|
||||||
|
tag = "Testimonials"
|
||||||
|
)]
|
||||||
|
pub async fn post_create_testimonial(
|
||||||
|
Extension(state): Extension<AppState>,
|
||||||
|
Extension(authenticated_user): Extension<UsersDetailQueryDto>,
|
||||||
|
Json(payload): Json<TestimonialsCreateRequestDto>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
TestimonialsService::create_testimonial(&state, payload, &authenticated_user).await
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
patch,
|
||||||
|
security(
|
||||||
|
("Bearer" = [])
|
||||||
|
),
|
||||||
|
path = "/v1/cms/landing/testimonials/update/{id}",
|
||||||
|
params(
|
||||||
|
("id" = String, Path, description = "Testimonial ID")
|
||||||
|
),
|
||||||
|
request_body = TestimonialsUpdateRequestDto,
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Update testimonial", body = MessageResponseDto)
|
||||||
|
),
|
||||||
|
tag = "Testimonials"
|
||||||
|
)]
|
||||||
|
pub async fn patch_update_testimonial(
|
||||||
|
Path(id): Path<String>,
|
||||||
|
Extension(state): Extension<AppState>,
|
||||||
|
Extension(authenticated_user): Extension<UsersDetailQueryDto>,
|
||||||
|
Json(payload): Json<TestimonialsUpdateRequestDto>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
TestimonialsService::update_testimonial(&state, id, payload, &authenticated_user)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
delete,
|
||||||
|
security(
|
||||||
|
("Bearer" = [])
|
||||||
|
),
|
||||||
|
path = "/v1/cms/landing/testimonials/delete/{id}",
|
||||||
|
params(
|
||||||
|
("id" = String, Path, description = "Testimonial ID")
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Soft delete testimonial", body = MessageResponseDto)
|
||||||
|
),
|
||||||
|
tag = "Testimonials"
|
||||||
|
)]
|
||||||
|
pub async fn delete_testimonial(
|
||||||
|
Extension(state): Extension<AppState>,
|
||||||
|
Extension(authenticated_user): Extension<UsersDetailQueryDto>,
|
||||||
|
Path(id): Path<String>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
TestimonialsService::delete_testimonial(&state, id, &authenticated_user).await
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
use imphnen_iam::users::UsersSchema;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use surrealdb::sql::Thing;
|
||||||
|
use utoipa::ToSchema;
|
||||||
|
use validator::Validate;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema, Validate)]
|
||||||
|
pub struct TestimonialsCreateRequestDto {
|
||||||
|
#[validate(length(min = 1, message = "Role is required"))]
|
||||||
|
pub role: String,
|
||||||
|
|
||||||
|
#[validate(length(
|
||||||
|
min = 1,
|
||||||
|
max = 500,
|
||||||
|
message = "Content must be between 1 and 500 characters"
|
||||||
|
))]
|
||||||
|
pub content: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema, Validate)]
|
||||||
|
pub struct TestimonialsUpdateRequestDto {
|
||||||
|
#[validate(length(min = 1, message = "Role is required"))]
|
||||||
|
pub role: String,
|
||||||
|
|
||||||
|
#[validate(length(
|
||||||
|
min = 1,
|
||||||
|
max = 500,
|
||||||
|
message = "Content must be between 1 and 500 characters"
|
||||||
|
))]
|
||||||
|
pub content: 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,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
pub struct TestimonialsQueryDto {
|
||||||
|
pub id: Thing,
|
||||||
|
pub user: UsersSchema,
|
||||||
|
pub role: String,
|
||||||
|
pub content: String,
|
||||||
|
pub is_deleted: bool,
|
||||||
|
pub created_at: String,
|
||||||
|
pub updated_at: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TestimonialsQueryDto {
|
||||||
|
pub fn from(self) -> TestimonialsListItemDto {
|
||||||
|
TestimonialsListItemDto {
|
||||||
|
id: self.id.id.to_raw(),
|
||||||
|
user_id: self.user.id.id.to_raw(),
|
||||||
|
user_fullname: self.user.fullname,
|
||||||
|
role: self.role,
|
||||||
|
content: self.content,
|
||||||
|
created_at: self.created_at,
|
||||||
|
is_deleted: self.is_deleted,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,164 @@
|
|||||||
|
use super::{
|
||||||
|
testimonials_dto::TestimonialsQueryDto, testimonials_schema::TestimonialsSchema,
|
||||||
|
};
|
||||||
|
use anyhow::{Result, bail};
|
||||||
|
use imphnen_libs::{AppState, MetaRequestDto, ResourceEnum, ResponseListSuccessDto};
|
||||||
|
use imphnen_utils::{DetailQueryBuilder, ListQueryBuilder, get_id, get_iso_date};
|
||||||
|
use serde_json;
|
||||||
|
use std::time::Instant;
|
||||||
|
use tracing::instrument;
|
||||||
|
|
||||||
|
pub struct TestimonialsRepository<'a> {
|
||||||
|
state: &'a AppState,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> TestimonialsRepository<'a> {
|
||||||
|
pub fn new(state: &'a AppState) -> Self {
|
||||||
|
Self { state }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[instrument(skip(self, meta), err)]
|
||||||
|
pub async fn query_testimonial_list(
|
||||||
|
&self,
|
||||||
|
meta: MetaRequestDto,
|
||||||
|
) -> Result<ResponseListSuccessDto<Vec<TestimonialsQueryDto>>> {
|
||||||
|
let now = Instant::now();
|
||||||
|
let query = ListQueryBuilder::new(ResourceEnum::Testimonials.to_string())
|
||||||
|
.with_select_fields(vec!["*", "user.* as user"])
|
||||||
|
.with_pagination(meta.page, Some(10))
|
||||||
|
.with_sorting(meta.sort_by.as_deref(), meta.order.as_deref())
|
||||||
|
.build();
|
||||||
|
let res: Vec<TestimonialsQueryDto> =
|
||||||
|
self.state.surrealdb_ws.query(query).await?.take(0)?;
|
||||||
|
let elapsed = now.elapsed();
|
||||||
|
if std::env::var("RUST_ENV").unwrap_or_else(|_| "development".to_string())
|
||||||
|
== "development"
|
||||||
|
{
|
||||||
|
println!("Query 'query_testimonial_list' took: {elapsed:.2?}");
|
||||||
|
}
|
||||||
|
let data = ResponseListSuccessDto {
|
||||||
|
data: res,
|
||||||
|
meta: None,
|
||||||
|
};
|
||||||
|
Ok(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[instrument(skip(self, id), err)]
|
||||||
|
pub async fn query_testimonial_by_id(
|
||||||
|
&self,
|
||||||
|
id: String,
|
||||||
|
) -> Result<TestimonialsQueryDto> {
|
||||||
|
let now = Instant::now();
|
||||||
|
let db = &self.state.surrealdb_ws;
|
||||||
|
let builder = DetailQueryBuilder::new(ResourceEnum::Testimonials.to_string())
|
||||||
|
.with_id(&id)
|
||||||
|
.with_condition("is_deleted = false")
|
||||||
|
.with_select_fields(vec!["*", "user.* as user"]);
|
||||||
|
let sql = builder.build();
|
||||||
|
let result: Option<TestimonialsQueryDto> =
|
||||||
|
builder.apply_bindings(db.query(sql)).await?.take(0)?;
|
||||||
|
let elapsed = now.elapsed();
|
||||||
|
if std::env::var("RUST_ENV").unwrap_or_else(|_| "development".to_string())
|
||||||
|
== "development"
|
||||||
|
{
|
||||||
|
println!("Query 'query_testimonial_by_id' took: {elapsed:.2?}");
|
||||||
|
}
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Some(testimonial) => {
|
||||||
|
if testimonial.is_deleted {
|
||||||
|
bail!("Testimonial not found");
|
||||||
|
}
|
||||||
|
Ok(testimonial)
|
||||||
|
}
|
||||||
|
None => bail!("Testimonial not found"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[instrument(skip(self, data), err)]
|
||||||
|
pub async fn query_create_testimonial(
|
||||||
|
&self,
|
||||||
|
data: TestimonialsSchema,
|
||||||
|
) -> Result<String> {
|
||||||
|
let now = Instant::now();
|
||||||
|
let db = &self.state.surrealdb_ws;
|
||||||
|
let record: Option<TestimonialsSchema> = db
|
||||||
|
.create(ResourceEnum::Testimonials.to_string())
|
||||||
|
.content(data)
|
||||||
|
.await?;
|
||||||
|
let elapsed = now.elapsed();
|
||||||
|
if std::env::var("RUST_ENV").unwrap_or_else(|_| "development".to_string())
|
||||||
|
== "development"
|
||||||
|
{
|
||||||
|
println!("Query 'query_create_testimonial' took: {elapsed:.2?}");
|
||||||
|
}
|
||||||
|
|
||||||
|
match record {
|
||||||
|
Some(_) => Ok("Success create testimonial".into()),
|
||||||
|
None => bail!("Failed to create testimonial"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[instrument(skip(self, data), err)]
|
||||||
|
pub async fn query_update_testimonial(
|
||||||
|
&self,
|
||||||
|
data: TestimonialsSchema,
|
||||||
|
) -> Result<String> {
|
||||||
|
let now = Instant::now();
|
||||||
|
let db = &self.state.surrealdb_ws;
|
||||||
|
|
||||||
|
let existing = self.query_testimonial_by_id(data.id.id.to_raw()).await?;
|
||||||
|
if existing.is_deleted {
|
||||||
|
bail!("Testimonial already deleted");
|
||||||
|
}
|
||||||
|
|
||||||
|
let merged = TestimonialsSchema {
|
||||||
|
created_at: existing.created_at,
|
||||||
|
updated_at: get_iso_date(),
|
||||||
|
user: existing.user.id,
|
||||||
|
..data
|
||||||
|
};
|
||||||
|
|
||||||
|
let record_key = get_id(&merged.id)?;
|
||||||
|
let record: Option<TestimonialsSchema> =
|
||||||
|
db.update(record_key).merge(merged).await?;
|
||||||
|
let elapsed = now.elapsed();
|
||||||
|
if std::env::var("RUST_ENV").unwrap_or_else(|_| "development".to_string())
|
||||||
|
== "development"
|
||||||
|
{
|
||||||
|
println!("Query 'query_update_testimonial' took: {elapsed:.2?}");
|
||||||
|
}
|
||||||
|
|
||||||
|
match record {
|
||||||
|
Some(_) => Ok("Success update testimonial".into()),
|
||||||
|
None => bail!("Failed to update testimonial"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[instrument(skip(self, id), err)]
|
||||||
|
pub async fn query_delete_testimonial(&self, id: String) -> Result<String> {
|
||||||
|
let now = Instant::now();
|
||||||
|
let db = &self.state.surrealdb_ws;
|
||||||
|
let testimonial = self.query_testimonial_by_id(id).await?;
|
||||||
|
if testimonial.is_deleted {
|
||||||
|
bail!("Testimonial not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
let record_key = get_id(&testimonial.id)?;
|
||||||
|
let record: Option<TestimonialsSchema> = db
|
||||||
|
.update(record_key)
|
||||||
|
.merge(serde_json::json!({ "is_deleted": true }))
|
||||||
|
.await?;
|
||||||
|
let elapsed = now.elapsed();
|
||||||
|
if std::env::var("RUST_ENV").unwrap_or_else(|_| "development".to_string())
|
||||||
|
== "development"
|
||||||
|
{
|
||||||
|
println!("Query 'query_delete_testimonial' took: {elapsed:.2?}");
|
||||||
|
}
|
||||||
|
|
||||||
|
match record {
|
||||||
|
Some(_) => Ok("Success delete testimonial".into()),
|
||||||
|
None => bail!("Failed to delete testimonial"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
use imphnen_libs::ResourceEnum;
|
||||||
|
use imphnen_utils::{get_iso_date, make_thing};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use surrealdb::Uuid;
|
||||||
|
use surrealdb::sql::Thing;
|
||||||
|
|
||||||
|
use super::testimonials_dto::{
|
||||||
|
TestimonialsCreateRequestDto, TestimonialsQueryDto, TestimonialsUpdateRequestDto,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
pub struct TestimonialsSchema {
|
||||||
|
pub id: Thing,
|
||||||
|
pub user: Thing,
|
||||||
|
pub role: String,
|
||||||
|
pub content: String,
|
||||||
|
pub is_deleted: bool,
|
||||||
|
pub created_at: String,
|
||||||
|
pub updated_at: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for TestimonialsSchema {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
id: make_thing(
|
||||||
|
&ResourceEnum::Testimonials.to_string(),
|
||||||
|
&Uuid::new_v4().to_string(),
|
||||||
|
),
|
||||||
|
user: make_thing(
|
||||||
|
&ResourceEnum::Users.to_string(),
|
||||||
|
&Uuid::new_v4().to_string(),
|
||||||
|
),
|
||||||
|
role: String::new(),
|
||||||
|
content: String::new(),
|
||||||
|
is_deleted: false,
|
||||||
|
created_at: get_iso_date(),
|
||||||
|
updated_at: get_iso_date(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TestimonialsSchema {
|
||||||
|
pub fn from(dto: TestimonialsQueryDto) -> Self {
|
||||||
|
Self {
|
||||||
|
id: dto.id,
|
||||||
|
user: dto.user.id,
|
||||||
|
role: dto.role,
|
||||||
|
content: dto.content,
|
||||||
|
is_deleted: dto.is_deleted,
|
||||||
|
created_at: dto.created_at,
|
||||||
|
updated_at: dto.updated_at,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create(payload: TestimonialsCreateRequestDto, user_id: &Thing) -> Self {
|
||||||
|
Self {
|
||||||
|
id: make_thing(
|
||||||
|
&ResourceEnum::Testimonials.to_string(),
|
||||||
|
&Uuid::new_v4().to_string(),
|
||||||
|
),
|
||||||
|
user: user_id.clone(),
|
||||||
|
role: payload.role,
|
||||||
|
content: payload.content,
|
||||||
|
is_deleted: false,
|
||||||
|
created_at: get_iso_date(),
|
||||||
|
updated_at: get_iso_date(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update(
|
||||||
|
payload: TestimonialsUpdateRequestDto,
|
||||||
|
id: String,
|
||||||
|
user_id: &Thing,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
id: make_thing(&ResourceEnum::Testimonials.to_string(), &id),
|
||||||
|
role: payload.role,
|
||||||
|
content: payload.content,
|
||||||
|
updated_at: get_iso_date(),
|
||||||
|
user: user_id.clone(),
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
use super::{
|
||||||
|
testimonials_dto::{
|
||||||
|
TestimonialsCreateRequestDto, TestimonialsDetailItemDto,
|
||||||
|
TestimonialsListItemDto, TestimonialsUpdateRequestDto,
|
||||||
|
},
|
||||||
|
testimonials_repository::TestimonialsRepository,
|
||||||
|
testimonials_schema::TestimonialsSchema,
|
||||||
|
};
|
||||||
|
use axum::{http::StatusCode, response::Response};
|
||||||
|
use imphnen_libs::{
|
||||||
|
AppState, MetaRequestDto, ResponseListSuccessDto, ResponseSuccessDto,
|
||||||
|
};
|
||||||
|
use imphnen_utils::{
|
||||||
|
common_response, success_list_response, success_response, validate_request,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct TestimonialsService;
|
||||||
|
|
||||||
|
impl TestimonialsService {
|
||||||
|
pub async fn get_testimonial_list(
|
||||||
|
state: &AppState,
|
||||||
|
meta: MetaRequestDto,
|
||||||
|
) -> Response {
|
||||||
|
let repo = TestimonialsRepository::new(state);
|
||||||
|
match repo.query_testimonial_list(meta).await {
|
||||||
|
Ok(data) => {
|
||||||
|
let items: Vec<TestimonialsListItemDto> = data
|
||||||
|
.data
|
||||||
|
.into_iter()
|
||||||
|
.filter(|e| !e.is_deleted)
|
||||||
|
.map(|e| e.from())
|
||||||
|
.collect();
|
||||||
|
let response = ResponseListSuccessDto {
|
||||||
|
data: items,
|
||||||
|
meta: data.meta,
|
||||||
|
};
|
||||||
|
success_list_response(response)
|
||||||
|
}
|
||||||
|
Err(e) => common_response(StatusCode::BAD_REQUEST, &e.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_testimonial_by_id(state: &AppState, id: String) -> Response {
|
||||||
|
let repo = TestimonialsRepository::new(state);
|
||||||
|
match repo.query_testimonial_by_id(id).await {
|
||||||
|
Ok(testimonial) if !testimonial.is_deleted => {
|
||||||
|
success_response(ResponseSuccessDto {
|
||||||
|
data: TestimonialsDetailItemDto {
|
||||||
|
id: testimonial.id.id.to_raw(),
|
||||||
|
user_id: testimonial.user.id.id.to_raw(),
|
||||||
|
user_fullname: testimonial.user.fullname,
|
||||||
|
role: testimonial.role,
|
||||||
|
content: testimonial.content,
|
||||||
|
created_at: testimonial.created_at,
|
||||||
|
updated_at: testimonial.updated_at,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Ok(_) => common_response(StatusCode::NOT_FOUND, "Testimonial not found"),
|
||||||
|
Err(e) => common_response(StatusCode::NOT_FOUND, &e.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn create_testimonial(
|
||||||
|
state: &AppState,
|
||||||
|
payload: TestimonialsCreateRequestDto,
|
||||||
|
authenticated_user: &imphnen_iam::UsersDetailQueryDto,
|
||||||
|
) -> Response {
|
||||||
|
if let Err((status, message)) = validate_request(&payload) {
|
||||||
|
return common_response(status, &message);
|
||||||
|
}
|
||||||
|
let repo = TestimonialsRepository::new(state);
|
||||||
|
let schema = TestimonialsSchema::create(payload, &authenticated_user.id);
|
||||||
|
match repo.query_create_testimonial(schema).await {
|
||||||
|
Ok(msg) => common_response(StatusCode::CREATED, &msg),
|
||||||
|
Err(e) => common_response(StatusCode::INTERNAL_SERVER_ERROR, &e.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update_testimonial(
|
||||||
|
state: &AppState,
|
||||||
|
id: String,
|
||||||
|
payload: TestimonialsUpdateRequestDto,
|
||||||
|
authenticated_user: &imphnen_iam::UsersDetailQueryDto,
|
||||||
|
) -> Response {
|
||||||
|
if let Err((status, message)) = validate_request(&payload) {
|
||||||
|
return common_response(status, &message);
|
||||||
|
}
|
||||||
|
let repo = TestimonialsRepository::new(state);
|
||||||
|
let schema = TestimonialsSchema::update(payload, id, &authenticated_user.id);
|
||||||
|
match repo.query_update_testimonial(schema).await {
|
||||||
|
Ok(msg) => common_response(StatusCode::OK, &msg),
|
||||||
|
Err(e) => common_response(StatusCode::BAD_REQUEST, &e.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn delete_testimonial(
|
||||||
|
state: &AppState,
|
||||||
|
id: String,
|
||||||
|
_authenticated_user: &imphnen_iam::UsersDetailQueryDto,
|
||||||
|
) -> Response {
|
||||||
|
let repo = TestimonialsRepository::new(state);
|
||||||
|
match repo.query_delete_testimonial(id).await {
|
||||||
|
Ok(msg) => common_response(StatusCode::OK, &msg),
|
||||||
|
Err(e) => common_response(StatusCode::BAD_REQUEST, &e.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
pub mod landing;
|
||||||
|
|
||||||
|
pub use landing::*;
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
[package]
|
||||||
|
name = "imphnen-dimentorin"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
imphnen-libs.workspace = true
|
||||||
|
imphnen-utils.workspace = true
|
||||||
|
imphnen-entities.workspace = true
|
||||||
|
imphnen-iam.workspace = true
|
||||||
|
axum.workspace = true
|
||||||
|
serde.workspace = true
|
||||||
|
serde_json.workspace = true
|
||||||
|
utoipa.workspace = true
|
||||||
|
lazy_static.workspace = true
|
||||||
|
regex.workspace = true
|
||||||
|
validator.workspace = true
|
||||||
|
axum-test.workspace = true
|
||||||
|
surrealdb.workspace = true
|
||||||
|
rand.workspace = true
|
||||||
|
tokio.workspace = true
|
||||||
|
chrono.workspace = true
|
||||||
|
anyhow.workspace = true
|
||||||
|
tower-http.workspace = true
|
||||||
|
utoipa-swagger-ui.workspace = true
|
||||||
|
tracing.workspace = true
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
dotenvy.workspace = true
|
||||||
|
http-body-util.workspace = true
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
pub mod v1;
|
||||||
|
pub use v1::*;
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user