so://phie

verbal

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

blob: d7207a6d4ee99f0d5bec6346804687053893fffa (2179 bytes)[doc][blame][raw]

1use serde::Deserialize;

2use serde::Serialize;

3use sqlx::FromRow;

4use std::fmt::Write;

5

6use crate::Result;

7use crate::ToSql;

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: Option<Vec<String>>,

32 pub country: Option<String>,

33 pub state: Option<String>,

34 pub languages: Option<Vec<String>>,

35 pub score: i32,

36 pub is_favorite: Option<bool>,

37 pub is_restricted: Option<bool>,

38 pub is_broken: Option<bool>,

39 pub is_no_track_info: Option<bool>,

40}

41

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

43pub struct Favorite {

44 pub id: i32,

45 pub account_id: i32,

46 pub station_id: String,

47}

48

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

50pub struct Query {

51 pub name: String,

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

53 pub country: Option<String>,

54 pub language: Option<String>,

55 pub page: Option<i32>,

56}

57

58impl ToSql for Query {

59 fn to_sql(&self) -> Result<String> {

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

61 writeln!(

62 sql,

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

64 self.name

65 )?;

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

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

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

69 if i > 0 {

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

71 }

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

73 }

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

75 }

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

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

78 }

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

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

81 }

82 Ok(sql)

83 }

84}