| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
use super::SeedError; |
| 10 |
use super::creators::{ProjectSpec, SeededCreator}; |
| 11 |
use crate::db::{self, DbProject, Slug, UserId}; |
| 12 |
|
| 13 |
|
| 14 |
pub struct SeededProject { |
| 15 |
|
| 16 |
pub user_id: UserId, |
| 17 |
|
| 18 |
pub project: DbProject, |
| 19 |
|
| 20 |
pub spec: &'static ProjectSpec, |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
pub async fn seed_projects( |
| 29 |
pool: &sqlx::PgPool, |
| 30 |
creators: &[SeededCreator], |
| 31 |
) -> Result<Vec<SeededProject>, SeedError> { |
| 32 |
let mut seeded = Vec::with_capacity(creators.len()); |
| 33 |
for creator in creators { |
| 34 |
let spec = creator.project; |
| 35 |
let slug = Slug::from_trusted(spec.slug.to_string()); |
| 36 |
let features: Vec<String> = spec |
| 37 |
.features |
| 38 |
.iter() |
| 39 |
.map(std::string::ToString::to_string) |
| 40 |
.collect(); |
| 41 |
|
| 42 |
let project = db::projects::create_project( |
| 43 |
pool, |
| 44 |
creator.user.id, |
| 45 |
&slug, |
| 46 |
spec.title, |
| 47 |
Some(spec.description), |
| 48 |
&features, |
| 49 |
) |
| 50 |
.await?; |
| 51 |
|
| 52 |
tracing::info!( |
| 53 |
slug = %project.slug, |
| 54 |
user_id = %creator.user.id, |
| 55 |
"example seed: created project" |
| 56 |
); |
| 57 |
seeded.push(SeededProject { |
| 58 |
user_id: creator.user.id, |
| 59 |
project, |
| 60 |
spec, |
| 61 |
}); |
| 62 |
} |
| 63 |
Ok(seeded) |
| 64 |
} |
| 65 |
|