Add project creation endpoint

This commit is contained in:
Nettika 2026-01-25 15:11:02 -08:00
parent 8c4691e8bf
commit 497144029e
Signed by: nettika
SSH key fingerprint: SHA256:f+PJrfIq49zrQ6dQrHj18b+PJKmAldeAMiGdj8IzXCA
2 changed files with 14 additions and 2 deletions

View file

@ -1,5 +1,5 @@
use maud::{DOCTYPE, html}; use maud::{DOCTYPE, html};
use rouille::router; use rouille::{router, try_or_400};
mod db; mod db;
mod project; mod project;
@ -24,6 +24,7 @@ fn main() {
rouille::Response::from_data("application/javascript", js.as_ref()) rouille::Response::from_data("application/javascript", js.as_ref())
}, },
(GET) ["/projects"] => display_projects(), (GET) ["/projects"] => display_projects(),
(POST) ["/projects"] => create_project(request),
_ => rouille::Response::empty_404() _ => rouille::Response::empty_404()
) )
}); });
@ -93,3 +94,14 @@ fn display_projects() -> rouille::Response {
rouille::Response::html(markup.into_string()) 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),
}
}

View file

@ -8,5 +8,5 @@
[x] Create a `project-card.js` file in src that creates a web component for displaying a project. [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] 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. [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. [ ] Create a `GET /new-project` endpoint that returns a HTML page with a project creation form. Keep it simple.