From f5611c0bfdc73f76ef5330a17a49d4b5407ae395 Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Fri, 29 Jul 2022 22:51:37 +0100 Subject: feat(GET /tag/{tags}): add ability to retrieve notes by tag(s) Placeholder `{tags}` is substituted with one or more tags separated by a plus sign `+`. --- README.md | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 0f6df09..64de347 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,8 @@ mod note; <> +<> + <> ``` @@ -135,23 +137,22 @@ use actix_web::{HttpResponse,Responder,web,get,post}; use actix_web::http::header::ContentType; ``` +Each request handlers is an *async* function that accepts zero or more parameters, extracted from a request (see [`FromRequest`](https://docs.rs/actix-web/latest/actix_web/trait.FromRequest.html) trait), and returns an [`HttpResponse`](https://docs.rs/actix-web/latest/actix_web/struct.HttpResponse.html). + ## Resources - [Tutorial](https://web.archive.org/web/20220710213947/https://hub.qovery.com/guides/tutorial/create-a-blazingly-fast-api-in-rust-part-1/) - [Introduction to `actix`](https://actix.rs/docs/getting-started/) - ## GET /notes -Each request handlers is an *async* function that accepts zero or more parameters, extracted from a request (see [`FromRequest`](https://docs.rs/actix-web/latest/actix_web/trait.FromRequest.html) trait), and returns an [`HttpResponse`](https://docs.rs/actix-web/latest/actix_web/struct.HttpResponse.html). - -Here we are requesting the list of notes currently in the system. +This handler allows to request the full list of notes currently in the system. The function takes 0 parameters and returns a JSON object. ```{#req_get_notes .rust} #[get("/notes")] pub async fn list() -> HttpResponse { - let mut notes: Notes; + let notes: Notes; <> @@ -163,6 +164,15 @@ pub async fn list() -> HttpResponse { ## POST /notes +New notes can be added by POSTing a JSON array of `NoteRequest`s of the form + +```json +{ + "body": "This is a funny note", + "tags": [ "joyce", "funny", "example" ] +} +``` + ```{#req_post_notes .rust} #[post("/notes")] pub async fn add(new_notes: web::Json) -> impl Responder { @@ -180,6 +190,26 @@ pub async fn add(new_notes: web::Json) -> impl Responder { } ``` +## GET /tag/{tags} + +This handler allows to query the set of notes for specific tags. +One or more tags separated by `+` can be passed to the request. + +```{#req_get_tags .rust} +#[get("/tag/{tags}")] +pub async fn get_tags(tags: web::Path) -> HttpResponse { + let tags = tags.split('+').map(|t| t.to_string()).collect::>(); + + let notes: Notes; + <> + let tagged = notes.into_iter().filter(|n| tags.iter().all(|t| n.tags.contains(t))).collect::>(); + + HttpResponse::Ok() + .content_type(ContentType::json()) + .json(tagged) +} +``` + # The program The main program is structured as follows @@ -209,6 +239,7 @@ async fn main() -> std::io::Result<()> { App::new() .service(note::list) .service(note::add) + .service(note::get_tags) }) .bind(("127.0.0.1", 8080))? .run() -- cgit v1.2.3