so://phie

verbal

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

blob: a4b0769a10b8d14a17acd94a954b5337b301d923 (2320 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 name: Option<String>,

21 pub account_id: i32,

22}

23

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

25pub struct Station {

26 pub id: i32,

27 pub uid: String,

28 pub name: String,

29 pub url: String,

30 pub icon: Option<String>,

31 pub homepage: Option<String>,

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

33 pub country: Option<String>,

34 pub state: Option<String>,

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

36 pub score: i32,

37 pub is_favorite: Option<bool>,

38 pub is_restricted: Option<bool>,

39 pub is_broken: Option<bool>,

40 pub is_no_track_info: Option<bool>,

41 // pub is_hidden: Option<bool>,

42}

43

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

45pub struct Favorite {

46 pub id: i32,

47 pub account_id: i32,

48 pub station_id: String,

49}

50

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

52pub struct Query {

53 pub name: String,

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

55 pub country: Option<String>,

56 pub language: Option<String>,

57 pub page: Option<i32>,

58}

59

60impl ToSql for Query {

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

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

63 writeln!(

64 sql,

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

66 self.name

67 )?;

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

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

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

71 if i > 0 {

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

73 }

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

75 }

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

77 }

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

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

80 }

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

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

83 }

84 Ok(sql)

85 }

86}

87

88#[derive(Debug, Clone, Serialize, FromRow)]

89pub struct Value(pub String);