Community::groups reuses the raw row mapper and its Raw → Community conversion
Browse files at this commita44630773c1f45e69e1bba0ebf0b1d79174a1ae4Parent: 28b5420- refactor
- community::groups reuses the raw row mapper and its Raw → Community conversion
1 changed file +15 -13
crates/server/src/db/community.rsmodified+15 -13
| @@ -39,7 +39,10 @@ fn raw(r: &mut Reader) -> Result<Raw, ServerError> { | |||
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | pub async fn read(id: Id, viewer: Id, db: &Connection) -> Result<Community, ServerError> { | 41 | pub async fn read(id: Id, viewer: Id, db: &Connection) -> Result<Community, ServerError> { |
| 42 | - let raw = one(db, READ, (id, viewer), raw).await?; | 42 | + community(one(db, READ, (id, viewer), raw).await?, db).await |
| 43 | +} | ||
| 44 | + | ||
| 45 | +async fn community(raw: Raw, db: &Connection) -> Result<Community, ServerError> { | ||
| 43 | let kind = match raw.kind { | 46 | let kind = match raw.kind { |
| 44 | Kind::Group => CommunityKind::Group, | 47 | Kind::Group => CommunityKind::Group, |
| 45 | Kind::Direct => CommunityKind::Direct { | 48 | Kind::Direct => CommunityKind::Direct { |
| @@ -85,21 +88,20 @@ pub async fn communities_of(user_id: Id, db: &Connection) -> Result<Vec<Communit | |||
| 85 | } | 88 | } |
| 86 | 89 | ||
| 87 | pub async fn groups(db: &Connection) -> Result<Vec<Community>, ServerError> { | 90 | pub async fn groups(db: &Connection) -> Result<Vec<Community>, ServerError> { |
| 88 | - all( | 91 | + let raws = all( |
| 89 | db, | 92 | db, |
| 90 | - "SELECT id, name, (SELECT avatar.id FROM avatar WHERE avatar.community_id = community.id), voice_enabled FROM community WHERE kind = ?1 ORDER BY name", | 93 | + "SELECT id, name, kind, (SELECT avatar.id FROM avatar WHERE avatar.community_id = community.id), voice_enabled, NULL FROM community WHERE kind = ?1 ORDER BY name", |
| 91 | (Kind::Group.as_str(),), | 94 | (Kind::Group.as_str(),), |
| 92 | - |r| { | 95 | + raw, |
| 93 | - Ok(Community { | ||
| 94 | - id: r.get()?, | ||
| 95 | - name: r.get()?, | ||
| 96 | - kind: CommunityKind::Group, | ||
| 97 | - avatar_id: r.get()?, | ||
| 98 | - voice_enabled: r.get()?, | ||
| 99 | - }) | ||
| 100 | - }, | ||
| 101 | ) | 96 | ) |
| 102 | - .await | 97 | + .await?; |
| 98 | + | ||
| 99 | + let mut groups = Vec::new(); | ||
| 100 | + for raw in raws { | ||
| 101 | + groups.push(community(raw, db).await?); | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + Ok(groups) | ||
| 103 | } | 105 | } |
| 104 | 106 | ||
| 105 | pub async fn update( | 107 | pub async fn update( |