Compare commits

...

4 commits

Author SHA1 Message Date
LordMZTE 3fb76495ec small fixes and testenv
- added testenv
- added formatter config
- updated gitignore
2021-07-22 16:13:53 +02:00
Timo Ley 002faa9601 Improve random SQL query 2021-07-20 12:39:28 +02:00
Timo Ley efc419e5df Add random endpoint 2021-07-20 12:36:19 +02:00
Timo Ley c4ce0ad4f1 Add schema
added schema.sql for the MySQL database
2021-07-20 08:21:18 +00:00
6 changed files with 75 additions and 0 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
/target
.idea
*.iml
Cargo.lock

12
rustfmt.toml Normal file
View file

@ -0,0 +1,12 @@
unstable_features = true
binop_separator = "Back"
format_code_in_doc_comments = true
format_macro_matchers = true
format_strings = true
imports_layout = "HorizontalVertical"
match_block_trailing_comma = true
merge_imports = true
normalize_comments = true
use_field_init_shorthand = true
use_try_shorthand = true
wrap_comments = true

4
schema.sql Normal file
View file

@ -0,0 +1,4 @@
CREATE TABLE IF NOT EXISTS categories (num INT UNIQUE NOT NULL , id varchar(255) NOT NULL , name TEXT, PRIMARY KEY (id));
CREATE TABLE IF NOT EXISTS users (id varchar(255) NOT NULL, name TEXT, authsource JSON, PRIMARY KEY (id));
CREATE TABLE IF NOT EXISTS memes (id INT NOT NULL AUTO_INCREMENT, filename varchar(255) NOT NULL, user varchar(255) NOT NULL, category varchar(255), timestamp DATETIME, ip varchar(255), PRIMARY KEY (id), FOREIGN KEY (category) REFERENCES categories(id), FOREIGN KEY (user) REFERENCES users(id));
CREATE TABLE IF NOT EXISTS token (uid varchar(255) UNIQUE NOT NULL, token varchar(255), FOREIGN KEY (uid) REFERENCES users(id));

View file

@ -102,6 +102,30 @@ async fn users(db_pool: web::Data<MySqlPool>) -> impl Responder {
}
}
#[get("/v1/random")]
async fn random(params: web::Query<MemeFilterQuery>, db_pool: web::Data<MySqlPool>) -> impl Responder {
let q = Meme::get_random(params.0, db_pool.get_ref()).await;
match q {
Ok(random) => HttpResponse::Ok().json(MemeResponse {
status: 200,
error: None,
meme: Some(random)
}),
Err(err) => match err {
Error::RowNotFound => HttpResponse::NotFound().json(MemeResponse {
status: 404,
error: Some(String::from("Meme not found")),
meme: None
}),
_ => HttpResponse::InternalServerError().json(MemeResponse {
status: 500,
error: Some(String::from("Internal Server Error")),
meme: None
})
}
}
}
//TODO: Implement random meme endpoint
//TODO: Implement upload endpoint
@ -112,4 +136,5 @@ pub fn init(cfg: &mut web::ServiceConfig) {
cfg.service(categories);
cfg.service(user);
cfg.service(users);
cfg.service(random);
}

View file

@ -45,6 +45,23 @@ impl Meme {
Ok(q)
}
pub async fn get_random(params: MemeFilterQuery, pool: &MySqlPool) -> Result<Meme> {
let q: Meme = sqlx::query("SELECT memes.id, user, filename, category, name, UNIX_TIMESTAMP(timestamp) AS ts FROM memes, users WHERE memes.user = users.id AND (category LIKE ? AND name LIKE ? AND filename LIKE ?) ORDER BY RAND() LIMIT 1")
.bind(params.category.unwrap_or(String::from("%")))
.bind(format!("%{}%", params.user.unwrap_or(String::from(""))))
.bind(format!("%{}%", params.search.unwrap_or(String::from(""))))
.map(|row: MySqlRow| Meme::from(DBMeme {
id: row.get("id"),
filename: row.get("filename"),
user: row.get("name"),
userdir: row.get("user"),
category: row.get("category"),
timestamp: row.get("ts"),
}))
.fetch_one(pool).await?;
Ok(q)
}
}
impl From<DBMeme> for Meme {

View file

@ -0,0 +1,16 @@
version: "3.1"
services:
db:
image: mariadb:10.6.2
ports:
- 3306:3306
environment:
MARIADB_ROOT_PASSWORD: root
MARIADB_USER: jensmemes
MARIADB_PASSWORD: snens
MARIADB_DATABASE: jensmemes
adminer:
image: adminer
ports:
- 8080:8080