moviequotebot-api/src/route.rs
Louis Vallat 5a68c80122 Added a route to list all languages
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
2021-06-09 16:24:25 +02:00

57 lines
1.5 KiB
Rust

extern crate diesel;
use actix_web::{get, web, HttpResponse, Responder};
use crate::diesel::prelude::*;
use crate::lib::establish_connection;
use crate::model::*;
#[get("/subtitles/list")]
async fn list_all_subtitles() -> impl Responder {
use crate::schema::films::dsl::*;
use crate::schema::subtitles::dsl::*;
use crate::schema::languages::dsl::*;
let connection = establish_connection().await;
let results = subtitles
.inner_join(films)
.inner_join(languages)
.load::<(Subtitle, Film, Language)>(&connection).expect("Error loading subtitles");
let mut s: Vec<SubtitleTrimmed> = vec![];
let mut f: Vec<Film> = vec![];
let mut l: Vec<Language> = vec![];
for r in results {
s.push(SubtitleTrimmed::from(r.0));
let f_id = r.1.id;
let l_id = r.2.id;
if !f.iter().any(|film| film.id == f_id) {
f.push(r.1);
}
if !l.iter().any(|language| language.id == l_id) {
l.push(r.2);
}
}
HttpResponse::Ok().json(SubtitleOutV1{
subtitles: s,
films: f,
languages: l
})
}
#[get("/languages/list")]
async fn list_all_languages() -> impl Responder {
use crate::schema::languages::dsl::*;
let connection = establish_connection().await;
let results = languages
.load::<Language>(&connection).expect("Error loading languages");
HttpResponse::Ok().json(results)
}
pub fn init_routes(config: &mut web::ServiceConfig) {
config.service(list_all_subtitles);
config.service(list_all_languages);
}