jmserver/src/v1/models.rs

147 lines
3 KiB
Rust
Raw Normal View History

2021-12-29 18:33:31 +01:00
use reqwest::StatusCode;
use serde::{Deserialize, Serialize, Serializer};
2022-07-19 21:11:13 +02:00
use crate::models::{Category, Meme, MemeOptions, User, UserIdentifier};
2022-01-16 17:46:09 +01:00
2021-12-29 18:33:31 +01:00
fn serialize_status<S>(x: &StatusCode, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
s.serialize_u16(x.as_u16())
}
2021-07-19 22:29:03 +02:00
#[derive(Serialize)]
2022-01-16 17:46:09 +01:00
pub struct V1Meme {
2021-07-19 22:29:03 +02:00
pub id: String,
pub link: String,
pub category: String,
pub user: String,
pub timestamp: String,
2021-12-29 18:33:31 +01:00
pub ipfs: String,
2021-07-19 22:29:03 +02:00
}
//Responses
#[derive(Serialize)]
pub struct MemesResponse {
pub status: i32,
pub error: Option<String>,
2022-01-16 17:46:09 +01:00
pub memes: Option<Vec<V1Meme>>,
2021-07-19 22:29:03 +02:00
}
#[derive(Serialize)]
pub struct MemeResponse {
pub status: i32,
pub error: Option<String>,
2022-01-16 17:46:09 +01:00
pub meme: Option<V1Meme>,
2021-07-19 22:29:03 +02:00
}
#[derive(Serialize)]
pub struct CategoriesResponse {
pub status: i32,
pub error: Option<String>,
pub categories: Option<Vec<Category>>,
}
#[derive(Serialize)]
pub struct CategoryResponse {
pub status: i32,
pub error: Option<String>,
pub category: Option<Category>,
}
#[derive(Serialize)]
pub struct UsersResponse {
pub status: i32,
pub error: Option<String>,
pub users: Option<Vec<User>>,
}
#[derive(Serialize)]
pub struct UserResponse {
pub status: i32,
pub error: Option<String>,
pub user: Option<User>,
}
#[derive(Serialize)]
pub struct UploadResponse {
pub status: i32,
pub error: Option<String>,
2021-12-29 18:33:31 +01:00
pub files: Option<Vec<String>>,
pub token: String,
2021-12-29 18:33:31 +01:00
}
#[derive(Serialize)]
pub struct ErrorResponse {
#[serde(serialize_with = "serialize_status")]
pub status: StatusCode,
2022-01-02 17:25:23 +01:00
pub error: String,
2021-07-19 22:29:03 +02:00
}
//Query
#[derive(Deserialize)]
pub struct IDQuery {
pub id: String,
}
#[derive(Deserialize)]
pub struct MemeIDQuery {
pub id: i32,
}
#[derive(Deserialize)]
pub struct UserIDQuery {
pub id: Option<String>,
pub token: Option<String>,
pub name: Option<String>,
}
2022-01-16 17:46:09 +01:00
impl V1Meme {
pub fn new(meme: Meme, cdn: String) -> Self {
Self {
id: meme.id.to_string(),
link: format!("{}/{}/{}", cdn, meme.userid, meme.filename),
category: meme.category,
user: meme.username,
timestamp: meme.timestamp.to_string(),
ipfs: meme.ipfs,
}
}
}
impl From<UserIDQuery> for UserIdentifier {
fn from(query: UserIDQuery) -> Self {
if let Some(id) = query.id {
Self::Id(id)
} else if let Some(token) = query.token {
Self::Token(token)
} else if let Some(name) = query.name {
Self::Username(name)
} else {
Self::Null
}
}
2021-07-19 22:29:03 +02:00
}
2022-07-19 21:11:13 +02:00
#[derive(Deserialize)]
pub struct MemeFilter {
pub category: Option<String>,
pub user: Option<String>,
pub search: Option<String>,
}
impl From<MemeFilter> for MemeOptions {
fn from(filter: MemeFilter) -> Self {
Self {
category: filter.category,
user_id: None,
username: filter.user,
search: filter.search,
2023-07-07 16:03:59 +02:00
limit: None,
2022-07-19 21:11:13 +02:00
after: None,
}
}
}