summaryrefslogtreecommitdiff
path: root/src/routes/v1/records.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/routes/v1/records.rs')
-rw-r--r--src/routes/v1/records.rs81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/routes/v1/records.rs b/src/routes/v1/records.rs
new file mode 100644
index 0000000..6c71749
--- /dev/null
+++ b/src/routes/v1/records.rs
@@ -0,0 +1,81 @@
+use crate::db;
+use crate::extractors::{Json, Jwt};
+use crate::routes::v1::{requests, zones};
+use axum::extract::Path;
+use axum::http::StatusCode;
+use axum::response::IntoResponse;
+use axum::Extension;
+use serde_json::json;
+use sqlx::{Pool, Postgres};
+use std::sync::Arc;
+
+pub async fn get_records(
+ Path(id): Path<String>,
+ Jwt(user): Jwt,
+ Extension(pool): Extension<Arc<Pool<Postgres>>>,
+) -> impl IntoResponse {
+ let domain = zones::ensure_trailing_dot(&id);
+
+ let zone = db::zones::get_zone(&pool, &domain).await;
+ if zone.is_err() {
+ return (
+ StatusCode::NOT_FOUND,
+ Json(json!({ "error": format!("Zone {domain} not found") })),
+ );
+ }
+ let zone = zone.unwrap();
+
+ if zone.owner_uuid != user.sub {
+ return (
+ StatusCode::FORBIDDEN,
+ Json(json!({ "error": "You do have permissions to access this zone" })),
+ );
+ }
+
+ let records = db::records::get_records(&pool, &zone.id).await.unwrap();
+
+ (StatusCode::OK, Json(json!(records)))
+}
+
+pub async fn create_record(
+ Path(zone_id): Path<String>,
+ Jwt(user): Jwt,
+ Json(data): Json<requests::Record>,
+ Extension(pool): Extension<Arc<Pool<Postgres>>>,
+) -> impl IntoResponse {
+ let zone = db::zones::get_zone(&pool, &zone_id).await;
+
+ if zone.is_err() {
+ return (
+ StatusCode::NOT_FOUND,
+ Json(json!({"error": "Zone not found"})),
+ );
+ }
+ let zone = zone.unwrap();
+
+ if zone.owner_uuid != user.sub {
+ return (
+ StatusCode::FORBIDDEN,
+ Json(json!({"error": "You do not have permissions to access this zone"})),
+ );
+ }
+
+ let record = db::records::create_record(
+ &pool,
+ &zone.id,
+ &data.name,
+ &data.record_type,
+ &data.content,
+ data.ttl,
+ )
+ .await;
+ if record.is_err() {
+ return (
+ StatusCode::INTERNAL_SERVER_ERROR,
+ Json(json!({"error": record.unwrap_err().to_string()})),
+ );
+ }
+ let record = record.unwrap();
+
+ (StatusCode::OK, Json(json!(record)))
+}