feat: ✨ crud rest for package registry
This commit is contained in:
parent
7fa6599da6
commit
1ebdf8be7d
2 changed files with 71 additions and 4 deletions
|
@ -16,6 +16,18 @@ pub struct Package {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Package {
|
impl Package {
|
||||||
|
pub fn new(name: String, publisher: Publisher, version: Version) -> Self {
|
||||||
|
Self {
|
||||||
|
name,
|
||||||
|
publisher,
|
||||||
|
version,
|
||||||
|
size: 0,
|
||||||
|
hash: String::default(),
|
||||||
|
publish_date_time: Datetime::default(),
|
||||||
|
last_update_date_time: Datetime::default(),
|
||||||
|
location: String::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
pub fn get_name(&self) -> String {
|
pub fn get_name(&self) -> String {
|
||||||
self.name.to_string()
|
self.name.to_string()
|
||||||
}
|
}
|
||||||
|
@ -50,7 +62,7 @@ impl Package {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub(crate) struct Version {
|
pub struct Version {
|
||||||
first: u8,
|
first: u8,
|
||||||
second: u8,
|
second: u8,
|
||||||
third: u8,
|
third: u8,
|
||||||
|
@ -80,7 +92,7 @@ impl Display for Version {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub(crate) struct Publisher {
|
pub struct Publisher {
|
||||||
name: String,
|
name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,33 @@
|
||||||
use axum::{extract::State, http::StatusCode, response::IntoResponse, routing::get, Json, Router};
|
use axum::{
|
||||||
|
extract::{Path, State},
|
||||||
|
http::StatusCode,
|
||||||
|
response::IntoResponse,
|
||||||
|
routing::{delete, get, patch, post},
|
||||||
|
Json, Router,
|
||||||
|
};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use tower_http::cors::CorsLayer;
|
use tower_http::cors::CorsLayer;
|
||||||
|
|
||||||
use crate::{database, AppState};
|
use crate::{
|
||||||
|
database,
|
||||||
|
package::{self, Publisher, Version},
|
||||||
|
AppState,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
struct Package {
|
||||||
|
name: String,
|
||||||
|
publisher: Publisher,
|
||||||
|
version: Version,
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn route(State(app_state): State<AppState>) -> Router {
|
pub async fn route(State(app_state): State<AppState>) -> Router {
|
||||||
Router::new()
|
Router::new()
|
||||||
.route("/", get(alive))
|
.route("/", get(alive))
|
||||||
|
.route("/package", post(create_package))
|
||||||
|
.route("/package/:package_name", get(read_package))
|
||||||
|
.route("/package/:package_name", patch(update_package))
|
||||||
|
.route("/package/:package_name", delete(delete_package))
|
||||||
.layer(CorsLayer::permissive())
|
.layer(CorsLayer::permissive())
|
||||||
.with_state(app_state)
|
.with_state(app_state)
|
||||||
}
|
}
|
||||||
|
@ -22,3 +44,36 @@ async fn alive() -> impl IntoResponse {
|
||||||
|
|
||||||
(StatusCode::OK, alive_json)
|
(StatusCode::OK, alive_json)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn create_package(Json(package): Json<Package>) -> impl IntoResponse {
|
||||||
|
let package = package::Package::new(package.name, package.publisher, package.version);
|
||||||
|
match database::create_package(package).await {
|
||||||
|
Some(package) => (StatusCode::CREATED, Json(serde_json::json!(package))),
|
||||||
|
None => (StatusCode::BAD_REQUEST, Json(serde_json::json!(""))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn read_package(Path(package_name): Path<String>) -> impl IntoResponse {
|
||||||
|
match database::read_package(package_name).await {
|
||||||
|
Some(package) => (StatusCode::OK, Json(serde_json::json!(package))),
|
||||||
|
None => (StatusCode::BAD_REQUEST, Json(serde_json::json!(""))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update_package(
|
||||||
|
Path(package_name): Path<String>,
|
||||||
|
Json(package): Json<Package>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
let package = package::Package::new(package.name, package.publisher, package.version);
|
||||||
|
match database::update_package(package_name, package).await {
|
||||||
|
Some(package) => (StatusCode::ACCEPTED, Json(serde_json::json!(package))),
|
||||||
|
None => (StatusCode::BAD_REQUEST, Json(serde_json::json!(""))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn delete_package(Path(package_name): Path<String>) -> impl IntoResponse {
|
||||||
|
match database::delete_package(package_name).await {
|
||||||
|
Some(package) => (StatusCode::NO_CONTENT, Json(serde_json::json!(package))),
|
||||||
|
None => (StatusCode::BAD_REQUEST, Json(serde_json::json!(""))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue