add: polls backend
TODO: polls frontend
This commit is contained in:
parent
b5e060e8ae
commit
4dfa09207e
18 changed files with 574 additions and 17 deletions
|
@ -7,7 +7,7 @@ use axum::{
|
|||
use axum_extra::extract::CookieJar;
|
||||
use tetratto_core::model::{
|
||||
addr::RemoteAddr,
|
||||
communities::Post,
|
||||
communities::{Poll, PollVote, Post},
|
||||
permissions::FinePermission,
|
||||
uploads::{MediaType, MediaUpload},
|
||||
ApiReturn, Error,
|
||||
|
@ -15,7 +15,7 @@ use tetratto_core::model::{
|
|||
use crate::{
|
||||
get_user_from_token,
|
||||
image::{save_webp_buffer, JsonMultipart},
|
||||
routes::api::v1::{CreatePost, CreateRepost, UpdatePostContent, UpdatePostContext},
|
||||
routes::api::v1::{CreatePost, CreateRepost, UpdatePostContent, UpdatePostContext, VoteInPoll},
|
||||
State,
|
||||
};
|
||||
|
||||
|
@ -66,6 +66,21 @@ pub async fn create_request(
|
|||
return Json(Error::NotAllowed.into());
|
||||
}
|
||||
|
||||
// create poll
|
||||
let poll_id = if let Some(p) = req.poll {
|
||||
match data
|
||||
.create_poll(Poll::new(
|
||||
user.id, 86400000, p.option_a, p.option_b, p.option_c, p.option_d,
|
||||
))
|
||||
.await
|
||||
{
|
||||
Ok(p) => p,
|
||||
Err(e) => return Json(e.into()),
|
||||
}
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
// ...
|
||||
let mut props = Post::new(
|
||||
req.content,
|
||||
|
@ -82,6 +97,7 @@ pub async fn create_request(
|
|||
None
|
||||
},
|
||||
user.id,
|
||||
poll_id,
|
||||
);
|
||||
|
||||
if !req.answering.is_empty() {
|
||||
|
@ -308,3 +324,39 @@ pub async fn update_context_request(
|
|||
Err(e) => Json(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn vote_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Path(id): Path<usize>,
|
||||
Json(req): Json<VoteInPoll>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let user = match get_user_from_token!(jar, data) {
|
||||
Some(ua) => ua,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
let post = match data.get_post_by_id(id).await {
|
||||
Ok(p) => p,
|
||||
Err(e) => return Json(e.into()),
|
||||
};
|
||||
|
||||
let poll = match data.get_poll_by_id(post.poll_id).await {
|
||||
Ok(p) => p,
|
||||
Err(e) => return Json(e.into()),
|
||||
};
|
||||
|
||||
// ...
|
||||
match data
|
||||
.create_pollvote(PollVote::new(user.id, poll.id, req.option))
|
||||
.await
|
||||
{
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Vote cast".to_string(),
|
||||
payload: (),
|
||||
}),
|
||||
Err(e) => Json(e.into()),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue