diff --git a/backend/src/secret_gift_exchange.rs b/backend/src/secret_gift_exchange.rs index fe999ee..bbc6ee0 100644 --- a/backend/src/secret_gift_exchange.rs +++ b/backend/src/secret_gift_exchange.rs @@ -1,11 +1,16 @@ +use std::collections::HashMap; + use askama::Template; use askama_axum::{IntoResponse, Response}; use axum::{ - extract::{Path, State}, response::Redirect, routing::post, Extension, Form + body::{self, Body}, + extract::{FromRequest, Path, Request, State}, + response::Redirect, + Extension, Form, Json, RequestExt, }; use axum_extra::response::Html; use chrono::Utc; -use http::StatusCode; +use http::{header::CONTENT_TYPE, StatusCode}; use serde::{Deserialize, Serialize}; use sqlx::{FromRow, SqlitePool}; @@ -213,16 +218,44 @@ pub struct ExchangeForm { } pub async fn giftexchange_save( - Path(exchange_id): Path, - Extension(user_data): Extension>, State(db_pool): State, - Form(item_form): Form, + request: Request, ) -> impl IntoResponse { - println!("Saving gift exchange: {:?}", item_form); - if is_authorized("/giftexchange", user_data.clone(), db_pool.clone()).await { - // Insert new item to database - let now = Utc::now().timestamp(); + let content_type_header = request.headers().get(CONTENT_TYPE); + let content_type = content_type_header.and_then(|value| value.to_str().ok()); - } - Redirect::to("/").into_response() + /* if let Some(content_type) = content_type { + if content_type.starts_with("application/json") { + let payload = request + .extract() + .await + .map_err(IntoResponse::into_response); + } + + if content_type.starts_with("application/x-www-form-urlencoded") { + let payload = request + .extract() + .await + .map_err(IntoResponse::into_response); + } + } */ + let (req_parts, map_request_body) = request.into_parts(); + let bytes = match body::to_bytes(map_request_body,usize::MAX).await { + Ok(bytes) => bytes, + Err(err) => { + return Err(( + StatusCode::BAD_REQUEST, + format!("failed to read request body: {}", err), + )); + } + }; + + println!("Saving gift exchange: {:?}", req_parts); + println!("Saving gift exchange: {:?} ", bytes); + Ok(Redirect::to("/").into_response()) +} + +#[derive(Debug, Serialize, Deserialize)] +struct Payload { + foo: String, } diff --git a/backend/templates/base.html b/backend/templates/base.html index e02004d..1df1c05 100644 --- a/backend/templates/base.html +++ b/backend/templates/base.html @@ -60,6 +60,7 @@ integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"> + {% block script %}{% endblock script %} diff --git a/backend/templates/giftexchange.html b/backend/templates/giftexchange.html index 1aa2161..772760d 100644 --- a/backend/templates/giftexchange.html +++ b/backend/templates/giftexchange.html @@ -5,7 +5,7 @@
@@ -20,12 +20,12 @@
- +
- + @@ -47,12 +47,11 @@
-
NameAvailable ID
+
- + @@ -109,7 +108,6 @@ }); $("#add").click(function () { - $("#output").empty(); $.each(npRows, function (index, value) { $('#non_participants').bootstrapTable('remove', { field: 'id', @@ -127,7 +125,6 @@ }); $("#remove").click(function () { - $("#p_output").empty(); $.each(pRows, function (index, value) { $('#participants').bootstrapTable('remove', { field: 'id', @@ -143,6 +140,19 @@ }); pRows = []; }); + + $("#update").click(function () { + $.each(pRows, function (index, value) { + $('#participants').bootstrapTable('insertRow', { + index: 0, + row: { + name: value.name, + id: value.id + } + }); + }); + pRows = []; + }); }) {% endblock script %} \ No newline at end of file
NameParticipating ID