OrangeGuidanceTomestone/server/src/web/get_location.rs
2022-09-03 06:35:15 -04:00

46 lines
1.3 KiB
Rust

use std::sync::Arc;
use anyhow::Context;
use warp::{Filter, Rejection, Reply};
use warp::filters::BoxedFilter;
use crate::message::RetrievedMessage;
use crate::State;
use crate::web::AnyhowRejection;
pub fn get_location(state: Arc<State>) -> BoxedFilter<(impl Reply, )> {
warp::get()
.and(warp::path("messages"))
.and(warp::path::param())
.and(warp::path::end())
.and_then(move |location: u32| logic(Arc::clone(&state), location))
.boxed()
}
async fn logic(state: Arc<State>, location: u32) -> Result<impl Reply, Rejection> {
let id = location as i64;
let messages = sqlx::query_as!(
RetrievedMessage,
// language=sqlite
r#"
select m.id,
m.x,
m.y,
m.z,
m.message,
coalesce(sum(v.vote between 0 and 1), 0) as positive_votes,
coalesce(sum(v.vote between -1 and 0), 0) as negative_votes
from messages m
left join votes v on m.id = v.message
where m.territory = ?
group by m.id"#,
id,
)
.fetch_all(&state.db)
.await
.context("could not get messages from database")
.map_err(AnyhowRejection)
.map_err(warp::reject::custom)?;
Ok(warp::reply::json(&messages))
}