work on events endpoint

This commit is contained in:
Mitchell Marino 2023-04-10 11:18:57 -05:00
parent a93704f998
commit 4a183b6820
2 changed files with 49 additions and 2 deletions

48
src/events.rs Normal file
View File

@ -0,0 +1,48 @@
use axum::{
extract::{Query, State},
http::StatusCode,
response::IntoResponse,
Json,
};
use serde::{Deserialize, Serialize};
use serde_json::json;
use sqlx::query;
use crate::AppState;
#[derive(Copy, Clone, Hash, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct EventsPreviewQuery {
count: u32,
}
pub async fn get_events_preview(
State(app_state): State<AppState>,
Query(query): Query<EventsPreviewQuery>,
) -> impl IntoResponse {
let result = query!(
r#"
SELECT
id,
title,
description,
time_start,
time_end,
event_type,
points,
place,
attending,
price,
created_by
FROM
events
WHERE
time_start BETWEEN now() AND now() + interval '1 week'
ORDER BY
time_start
LIMIT
10;
"#
);
(StatusCode::OK, Json(json!({})))
}

View File

@ -20,7 +20,6 @@ pub struct AppState {
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
// initialize tracing
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
// Ex. // Ex.
@ -41,6 +40,7 @@ async fn main() {
.route("/", get(root)) .route("/", get(root))
.route("/user/signup", post(signup)) .route("/user/signup", post(signup))
.route("/user/signin", post(signin)) .route("/user/signin", post(signin))
.route("/event/prview", get(events::get_events_preview))
.with_state(AppState { db_pool, jwt_key }); .with_state(AppState { db_pool, jwt_key });
// run our app with hyper // run our app with hyper
@ -53,7 +53,6 @@ async fn main() {
.unwrap(); .unwrap();
} }
// basic handler that responds with a static string
async fn root() -> &'static str { async fn root() -> &'static str {
"Hello, World!" "Hello, World!"
} }