Skip to main content

max / makenotwork

2.2 KB · 65 lines History Blame Raw
1 //! Seed one project per creator (their [`ProjectSpec`]), spread across genres.
2 //!
3 //! Each project is public the moment it is inserted: `create_project` leaves
4 //! `is_public` at its `true` default and never sets `is_sandbox`, so the pair
5 //! (durable creator + project) clears the only two public-visibility gates. The
6 //! project's pricing, items, and tiers land in [`super::items`]; this module
7 //! returns enough context ([`SeededProject`]) for that step.
8
9 use super::SeedError;
10 use super::creators::{ProjectSpec, SeededCreator};
11 use crate::db::{self, DbProject, Slug, UserId};
12
13 /// A project after insertion, with the owner and spec its items still need.
14 pub struct SeededProject {
15 /// The owning creator's id (needed for ownership-checked item/pricing writes).
16 pub user_id: UserId,
17 /// The inserted project row.
18 pub project: DbProject,
19 /// The spec that produced it, carries the item, pricing, and tier data.
20 pub spec: &'static ProjectSpec,
21 }
22
23 /// Create the one project each seeded creator owns.
24 ///
25 /// Called from [`super::run`] immediately after [`super::creators::seed_creators`],
26 /// with the creators it returned. `create_project` auto-suffixes any slug
27 /// collision and defaults the project to public, so no publish step is needed.
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