Skip to content

Commit

Permalink
Added config endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
luyangliuable committed Sep 7, 2024
1 parent e8ef7bd commit 583b7ca
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 0 deletions.
43 changes: 43 additions & 0 deletions server/src/api/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use rocket::{http::Status, serde::json::Json, State};
use mongodb::{bson::{ doc, to_bson, oid::ObjectId }, results::{ InsertOneResult, UpdateResult }};

use std::str::FromStr;

use crate::models::config_model::Config;

use crate::repository::config_repo::ConfigRepo;

#[post("/config", data = "<new_config>")]
pub fn insert_config(config_repo: &State<ConfigRepo>, new_config: Json<Config>) -> Result<Json<InsertOneResult>, Status> {
let new_config = new_config.into_inner();

match config_repo.0.create(new_config) {
Ok(config) => Ok(Json(config)),
Err(_) => Err(Status::BadRequest),
}
}


#[get("/config/<id>")]
pub fn get_config(id: String, config_repo: &State<ConfigRepo>) -> Result<String, Status> {
ObjectId::from_str(&id)
.map_err(|_| Status::BadRequest)
.and_then(|object_id| config_repo.0.get(object_id).map_err(|_| Status::InternalServerError))
.and_then(|config| Ok(config.value))
}

#[patch("/config/<id>", data = "<new_config>")]
pub fn update_config(
id: &str,
config_repo: &State<ConfigRepo>,
new_config: Json<Config>,
) -> Result<Json<UpdateResult>, Status> {
let new_post_data = new_config.into_inner();
let bson_value = to_bson(&new_post_data).map_err(|_| Status::BadRequest)?;
let document = bson_value.as_document().ok_or(Status::InternalServerError)?;

match config_repo.0.update(id.to_string(), doc! { "$set": document }, None) {
Ok(update_result) => Ok(Json(update_result)),
Err(_) => Err(Status::BadRequest),
}
}
1 change: 1 addition & 0 deletions server/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pub mod local_image;
pub mod note;
pub mod message;
pub mod proxy;
pub mod config;
8 changes: 8 additions & 0 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use repository::mongo_repo::MongoRepo;
use repository::local_image_repo::LocalImageRepo;
use repository::message_repo::MessageRepo;
use repository::note_repo::NoteRepo;
use repository::config_repo::ConfigRepo;
use config::cors::CORS;

use controller::{ post_controller::PostController, local_image_controller::LocalImageController };
Expand All @@ -28,6 +29,7 @@ use models::user_model::User;
use models::local_image_model::LocalImage;
use models::message_model::Message;
use models::note_model::Note;
use models::config_model::Config;

#[get("/")]
fn index() -> &'static str {
Expand All @@ -47,6 +49,7 @@ async fn rocket() -> _ {
let local_image_repo = LocalImageRepo(MongoRepo::<LocalImage>::init("LocalImage", &*DB).await);
let message_repo = MessageRepo(MongoRepo::<Message>::init("Message", &*DB).await);
let note_repo = NoteRepo(MongoRepo::<Note>::init("Note", &*DB).await);
let config_repo = ConfigRepo(MongoRepo::<Config>::init("Config", &*DB).await);

let post_controller = PostController::new(post_repo);
let local_image_controller = LocalImageController::new(local_image_repo);
Expand All @@ -60,6 +63,7 @@ async fn rocket() -> _ {
.manage(post_controller)
.manage(local_image_controller)
.manage(note_repo)
.manage(config_repo)

.mount("/api/", routes![index])

Expand Down Expand Up @@ -93,5 +97,9 @@ async fn rocket() -> _ {
.mount("/api/", routes![api::note::get_note])
.mount("/api/", routes![api::note::get_all_note])

.mount("/api/", routes![api::config::insert_config])
.mount("/api/", routes![api::config::update_config])
.mount("/api/", routes![api::config::get_config])

.attach(CORS)
}
10 changes: 10 additions & 0 deletions server/src/models/config_model.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use mongodb::bson::oid::ObjectId;
use rocket::serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Config {
#[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
pub id: Option<ObjectId>,
pub key: String,
pub value: String,
}
1 change: 1 addition & 0 deletions server/src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pub mod local_image_model;
pub mod note_model;
pub mod utils;
pub mod message_model;
pub mod config_model;
8 changes: 8 additions & 0 deletions server/src/repository/config_repo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::models::mongo_model::MongoModel;
use crate::repository::mongo_repo::MongoRepo;

use crate::models::config_model::Config;

impl MongoModel for Config {}

pub struct ConfigRepo(pub MongoRepo<Config>);
1 change: 1 addition & 0 deletions server/src/repository/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pub mod user_repo;
pub mod note_repo;
pub mod local_image_repo;
pub mod message_repo;
pub mod config_repo;

0 comments on commit 583b7ca

Please sign in to comment.