user.rs 579 B

12345678910111213141516171819202122232425
  1. use tokio_postgres::{Error, GenericClient, Row};
  2. #[derive(Debug, serde::Serialize)]
  3. pub struct User {
  4. pub id: i32,
  5. pub login: String,
  6. }
  7. impl From<Row> for User {
  8. fn from(row: Row) -> Self {
  9. Self {
  10. id: row.get(0),
  11. login: row.get(1),
  12. }
  13. }
  14. }
  15. impl User {
  16. pub async fn all<C: GenericClient>(client: &C) -> Result<Vec<User>, Error> {
  17. let stmt = client.prepare("SELECT id, login FROM users").await?;
  18. let rows = client.query(&stmt, &[]).await?;
  19. Ok(rows.into_iter().map(User::from).collect())
  20. }
  21. }