so://phie

verbal

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

blob: d9425ca15ce20956f814953f4cf871fcf7fba3f4 (1375 bytes)[doc][blame][raw]

1use serde::{Deserialize, Serialize};

2use sqlx::FromRow;

3

4pub trait Query {

5 fn into_keys(&self) -> String;

6 fn into_values(&self) -> String;

7 fn into_update(&self) -> String;

8}

9

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

11pub struct Model<T> {

12 pub id: i32,

13 pub inner: T,

14}

15

16impl<T> Model<T> where T: Query {

17 pub fn new(inner: T) -> Self {

18 Self { id: 0, inner }

19 }

20 pub fn from(id: i32, inner: T) -> Self {

21 Self { id, inner }

22 }

23 pub fn into_insert(&self, table: &str) -> String {

24 format!(

25 "

26 INSERT INTO {}

27 (

28 {}

29 created_at,

30 updated_at

31 )

32 VALUES

33 (

34 {}

35 CURRENT_TIMESTAMP,

36 CURRENT_TIMESTAMP

37 )

38 ",

39 &table,

40 &self.inner.into_keys(),

41 &self.inner.into_values(),

42 )

43 }

44 pub fn into_update(&self, table: &str) -> String {

45 format!(

46 "

47 UPDATE {}

48 SET

49 {}

50 updated_at = CURRENT_TIMESTAMP

51 WHERE

52 id = {}

53 ",

54 &table,

55 &self.inner.into_update(),

56 &self.id,

57 )

58 }

59}