so://phie

verbal

A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal

blob: 519aed20b43c9cd36e2e333e4edb8cc205d12a3c (2058 bytes)[doc][blame][raw]

1use serde::Deserialize;

2use serde::Serialize;

3use sqlx::FromRow;

4use std::fmt::Write;

5

6use crate::IntoSql;

7use crate::Result;

8

9#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]

10pub struct Account {

11 pub id: i32,

12 pub name: Option<String>,

13 pub language: String,

14}

15

16#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]

17pub struct Device {

18 pub id: i32,

19 pub uid: String,

20 pub account_id: i32,

21}

22

23#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]

24pub struct Station {

25 pub id: i32,

26 pub uid: String,

27 pub name: String,

28 pub url: String,

29 pub icon: Option<String>,

30 pub homepage: Option<String>,

31 pub tags: Vec<String>,

32 pub country: Option<String>,

33 pub state: Option<String>,

34 pub languages: Vec<String>,

35 pub score: i32,

36 pub is_favorite: Option<bool>,

37}

38

39#[derive(Debug, Serialize, Deserialize, FromRow)]

40pub struct Favorite {

41 pub id: i32,

42 pub account_id: i32,

43 pub station_id: String,

44}

45

46#[derive(Debug, Default, Deserialize)]

47pub struct Query {

48 pub name: String,

49 pub tags: Option<Vec<String>>,

50 pub country: Option<String>,

51 pub language: Option<String>,

52 pub page: Option<i32>,

53}

54

55impl IntoSql for Query {

56 fn into_sql(self) -> Result<String> {

57 let mut sql = String::new();

58 writeln!(

59 sql,

60 "WHERE LOWER(station.name) LIKE LOWER('%{}%')",

61 self.name

62 )?;

63 if let Some(tags) = &self.tags {

64 write!(sql, "AND station.tags = array[")?;

65 for (i, tag) in tags.iter().enumerate() {

66 if i > 0 {

67 write!(sql, ", ")?;

68 }

69 write!(sql, "LOWER('{}')", tag)?;

70 }

71 writeln!(sql, "]")?;

72 }

73 if let Some(country) = &self.country {

74 writeln!(sql, "AND station.country = UPPER('{}')", country)?;

75 }

76 if let Some(language) = &self.language {

77 writeln!(sql, "AND station.languages = array[LOWER('{}')]", language)?;

78 }

79 Ok(sql)

80 }

81}