Skip to content

Netizen-alt/backtoschool_rust

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BackToSchool Rust (Final Project)

โปรเจกต์งานจบระบบจัดการโรงเรียนตัวอย่าง ด้วย Rust รองรับทั้ง CLI และ REST API พร้อมระบบ login + role และ export รายงานเป็น CSV/PDF

จุดเด่น

  • จัดการนักเรียน รายวิชา การลงทะเบียน และคะแนน
  • ยืนยันตัวตนด้วย Bearer Token (/login)
  • Authorization ตาม role: admin, teacher
  • REST API ด้วย axum
  • Export รายงานเป็น CSV/PDF
  • แยกโครงสร้างโฟลเดอร์แบบ layer คล้าย NestJS
  • มี unit tests สำหรับ logic สำคัญ

Architecture (Diagram)

flowchart TD
    A["Presentation Layer<br/>src/presentation/http"] --> B["Application Layer<br/>src/application"]
    B --> C["Modules (Domain)<br/>src/modules/school"]
    C --> D["Infrastructure Layer<br/>src/infrastructure/persistence"]
    E["Common Layer<br/>src/common"] --> B
    E --> A
Loading

โครงสร้างโปรเจกต์

src/
  main.rs
  lib.rs
  common/
    cli.rs
  application/
    app.rs
  modules/
    school/
      models.rs
      report.rs
  infrastructure/
    persistence/
      school_db.rs
  presentation/
    http/
      server.rs

เริ่มใช้งาน

cargo run -- --help

CLI ตัวอย่าง

cargo run -- add-student S001 "Somchai"
cargo run -- add-course CS101 "Intro to Rust"
cargo run -- enroll S001 CS101
cargo run -- grade S001 CS101 87.5
cargo run -- report-student S001
cargo run -- report-course CS101

รัน REST API

cargo run -- serve

กำหนด host/port เอง:

cargo run -- --addr 127.0.0.1:4000 serve

เมื่อเซิร์ฟเวอร์รันแล้ว เปิดหน้า UI สำหรับเทสได้ที่:

  • http://127.0.0.1:3000/ (หรือพอร์ตที่กำหนดเอง)
  • หน้า UI มีฟอร์ม login, จัดการข้อมูล, ดูรายงาน และ export CSV/PDF

บัญชีเริ่มต้น

  • admin / admin123 (admin)
  • teacher / teacher123 (teacher)

ตัวอย่างเรียก API

  1. Login เพื่อรับ token
curl -X POST http://127.0.0.1:3000/login \
  -H "Content-Type: application/json" \
  -d "{\"username\":\"teacher\",\"password\":\"teacher123\"}"
  1. เรียก endpoint ที่ต้อง auth
curl http://127.0.0.1:3000/students \
  -H "Authorization: Bearer <TOKEN>"
  1. Export รายงาน
# CSV
curl -L "http://127.0.0.1:3000/reports/student/S001/csv" \
  -H "Authorization: Bearer <TOKEN>" \
  -o student_S001.csv

# PDF
curl -L "http://127.0.0.1:3000/reports/student/S001/pdf" \
  -H "Authorization: Bearer <TOKEN>" \
  -o student_S001.pdf

Endpoints

  • GET /health
  • POST /login
  • GET /students
  • POST /students (teacher/admin)
  • GET /courses
  • POST /courses (admin only)
  • POST /enroll (teacher/admin)
  • POST /grade (teacher/admin)
  • GET /reports/student/{student_id}
  • GET /reports/course/{course_code}
  • GET /reports/student/{student_id}/csv
  • GET /reports/course/{course_code}/csv
  • GET /reports/student/{student_id}/pdf
  • GET /reports/course/{course_code}/pdf

รันทดสอบ

cargo test

หมายเหตุ

  • ค่าเริ่มต้นฐานข้อมูลคือ school_db.json
  • เปลี่ยน path ได้ด้วย --db เช่น cargo run -- --db my_school_data.json serve

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages