From 497144029ea427b8a0dedd310a4dc0b2cedc0318 Mon Sep 17 00:00:00 2001 From: Nettika Date: Sun, 25 Jan 2026 15:11:02 -0800 Subject: [PATCH] Add project creation endpoint --- src/main.rs | 14 +++++++++++++- todo.md | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index a3091ec..09f6d1a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use maud::{DOCTYPE, html}; -use rouille::router; +use rouille::{router, try_or_400}; mod db; mod project; @@ -24,6 +24,7 @@ fn main() { rouille::Response::from_data("application/javascript", js.as_ref()) }, (GET) ["/projects"] => display_projects(), + (POST) ["/projects"] => create_project(request), _ => rouille::Response::empty_404() ) }); @@ -93,3 +94,14 @@ fn display_projects() -> rouille::Response { rouille::Response::html(markup.into_string()) } + +fn create_project(request: &rouille::Request) -> rouille::Response { + let input = try_or_400!(rouille::post_input!(request, { + title: String, + })); + + match db::create_project(input.title) { + Ok(_) => rouille::Response::redirect_302("/projects"), + Err(_) => rouille::Response::text("Failed to create project").with_status_code(500), + } +} diff --git a/todo.md b/todo.md index 557fae8..01c514b 100644 --- a/todo.md +++ b/todo.md @@ -8,5 +8,5 @@ [x] Create a `project-card.js` file in src that creates a web component for displaying a project. [x] Using a Rouille router, create `GET /main.css` and `GET /project-card.js` endpoints that returns the relevant files. Use the include_bytes! macro. [x] Create a `GET /projects` endpoint. Using Maud for markup generation, have this endpoint return an HTML page that shows all projects. Each project should be a `project-card` web component. Keep the page simple: no title or any buttons currently. -[ ] Create a `POST /projects` endpoint that accepts URL encoded data and creates a new project. +[x] Create a `POST /projects` endpoint that accepts URL encoded data and creates a new project. [ ] Create a `GET /new-project` endpoint that returns a HTML page with a project creation form. Keep it simple. \ No newline at end of file