-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e8ef7bd
commit 583b7ca
Showing
7 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ pub mod local_image; | |
pub mod note; | ||
pub mod message; | ||
pub mod proxy; | ||
pub mod config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ pub mod local_image_model; | |
pub mod note_model; | ||
pub mod utils; | ||
pub mod message_model; | ||
pub mod config_model; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; |