api/routes/modules/assignments/files/
mod.rs

1//! File Routes Module
2//!
3//! This module defines the routing for assignment file-related endpoints, including uploading, listing, downloading, and deleting files. It applies access control middleware to ensure appropriate permissions for each operation.
4
5use axum::{middleware::from_fn_with_state, Router, routing::{get, post, delete}};
6use util::state::AppState;
7use crate::auth::guards::{require_assigned_to_module, require_lecturer};
8use get::{list_files, download_file};
9use post::upload_files;
10use delete::delete_files;
11
12pub mod get;
13pub mod post;
14pub mod delete;
15
16/// Registers the routes for assignment file endpoints.
17///
18/// This function sets up the following endpoints under the current router:
19///
20/// - `POST /`: Upload files to an assignment. Access is restricted to lecturers assigned to the module.
21/// - `GET /`: List all files for an assignment. Access is restricted to users assigned to the module.
22/// - `GET /{file_id}`: Download a specific file from an assignment. Access is restricted to users assigned to the module.
23/// - `DELETE /`: Delete files from an assignment. Access is restricted to lecturers assigned to the module.
24///
25/// Routes apply appropriate middleware based on the operation:
26/// - Upload and delete operations require lecturer permissions
27/// - List and download operations require module assignment
28///
29/// # Returns
30/// An [`axum::Router`] with the file endpoints and their associated middleware.
31pub fn files_routes(app_state: AppState) -> Router<AppState> {
32    Router::new()
33        .route("/", post(upload_files).route_layer(from_fn_with_state(app_state.clone(), require_lecturer)))
34        .route("/", get(list_files).route_layer(from_fn_with_state(app_state.clone(), require_assigned_to_module)))
35        .route("/", delete(delete_files).route_layer(from_fn_with_state(app_state.clone(), require_lecturer)))
36        .route("/{file_id}", get(download_file).route_layer(from_fn_with_state(app_state.clone(), require_assigned_to_module)))
37}