-
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.
Updated server to include notes api.
- Loading branch information
1 parent
f50ba61
commit 4031e84
Showing
14 changed files
with
144 additions
and
240 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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
pub mod blogs; | ||
pub mod health; | ||
pub mod posts; | ||
pub mod user; | ||
pub mod local_image; | ||
pub mod note; | ||
pub mod message; | ||
pub mod proxy; |
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,48 @@ | ||
use rocket::{http::Status, State, serde::json::Json}; | ||
use crate::{models::note_model::Note, repository::note_repo::NoteRepo, utils::markdown_util}; | ||
use mongodb::{results::{ UpdateResult, InsertOneResult, DeleteResult }, bson::{doc, to_bson, oid::ObjectId }}; | ||
use std::str::FromStr; | ||
|
||
#[post("/note", data = "<new_note>")] | ||
pub async fn index_note(repo: &State<NoteRepo>, new_note: Json<Note>) -> Result<Json<InsertOneResult>, Status> { | ||
let mut data = new_note.into_inner(); | ||
data.date_created = Some(chrono::offset::Utc::now()); | ||
repo.0.create(data).map(Json).map_err(|_| Status::BadRequest) | ||
} | ||
|
||
|
||
#[get("/note/<id>")] | ||
pub async fn get_note(repo: &State<NoteRepo>, id: String, redis: &State<redis::Client>) -> Result<Json<Note>, Status> { | ||
ObjectId::from_str(&id) | ||
.map_err(|_| Status::BadRequest) | ||
.and_then(|object_id| repo.0.get(object_id).map_err(|_| Status::InternalServerError)) | ||
.and_then(|blog_post| match blog_post.active { | ||
Some(false) => Err(Status::NotFound), | ||
_ => Ok(blog_post), | ||
}) | ||
.and_then(|note| { | ||
markdown_util::get_content(note) | ||
.map_err(|_| Status::NotFound) | ||
.and_then(|blog_post_content| { | ||
Ok(blog_post_content) | ||
}) | ||
}) | ||
.map(Json) | ||
} | ||
|
||
#[get("/note")] | ||
pub fn get_all_note(repo: &State<NoteRepo>) -> Result<Json<Vec<Note>>, Status> { | ||
let notes = match repo.0.get_all() { | ||
Ok(posts) => posts.into_iter().filter(|post| { | ||
match post.active { | ||
Some(false) => false, | ||
_ => true | ||
} | ||
}).collect(), | ||
Err(_) => { | ||
return Err(Status::InternalServerError); | ||
}, | ||
}; | ||
|
||
Ok(Json(notes)) | ||
} |
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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
pub mod mongo_model; | ||
pub mod blog_model; | ||
pub mod post_model; | ||
pub mod user_model; | ||
pub mod user_session_token_model; | ||
pub mod local_image_model; | ||
pub mod note_model; | ||
pub mod utils; | ||
pub mod message_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,24 @@ | ||
use mongodb::bson::oid::ObjectId; | ||
use rocket::serde::{Serialize, Deserialize}; | ||
use chrono::{Utc, DateTime}; | ||
use crate::models::utils::date_format::date_format; | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone)] | ||
pub struct Note { | ||
#[serde(rename = "_id", skip_serializing_if = "Option::is_none")] | ||
pub id: Option<ObjectId>, | ||
pub heading: String, | ||
pub author: String, | ||
pub description: Option<String>, | ||
#[serde(with = "date_format", default)] | ||
pub date_created: Option<DateTime<Utc>>, | ||
#[serde(with = "date_format", default)] | ||
pub date_last_modified: Option<DateTime<Utc>>, | ||
pub file_path: String, | ||
pub tags: Option<Vec<String>>, | ||
pub reading_time_minutes: Option<i32>, | ||
pub active: Option<bool>, | ||
pub image: Option<ObjectId>, | ||
pub checksum: Option<String>, | ||
pub body: String, | ||
} |
Oops, something went wrong.