Skip to content

Commit 647bf44

Browse files
committed
create_inner
1 parent a8697bb commit 647bf44

File tree

3 files changed

+67
-30
lines changed

3 files changed

+67
-30
lines changed

lib/api_run/src/run.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ async fn post_inner(
5050
) -> Result<JsonReport, HttpError> {
5151
let query_project = match (auth_user.as_ref(), json_run.project.as_ref()) {
5252
(Some(auth_user), Some(project)) => {
53-
QueryProject::get_or_create(log, context, auth_user, project)
53+
QueryProject::get_or_create_from_project(log, context, auth_user, project)
5454
.await
5555
.map_err(|e| forbidden_error(e.to_string()))?
5656
},

lib/bencher_schema/src/model/organization/mod.rs

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl QueryOrganization {
5454
fn_get_id!(organization, OrganizationId, OrganizationUuid);
5555
fn_get_uuid!(organization, OrganizationId, OrganizationUuid);
5656

57-
pub async fn get_or_create(
57+
pub async fn get_or_create_from_user(
5858
context: &ApiContext,
5959
auth_user: &AuthUser,
6060
) -> Result<Self, HttpError> {
@@ -72,22 +72,30 @@ impl QueryOrganization {
7272
Self::create(context, auth_user, json_organization).await
7373
}
7474

75+
pub async fn get_or_create_from_context(
76+
context: &ApiContext,
77+
project_name: &ResourceName,
78+
project_slug: &Slug,
79+
) -> Result<Self, HttpError> {
80+
if let Ok(query_organization) =
81+
Self::from_resource_id(conn_lock!(context), &project_slug.clone().into())
82+
{
83+
return Ok(query_organization);
84+
}
85+
86+
let json_organization = JsonNewOrganization {
87+
name: project_name.clone(),
88+
slug: Some(project_slug.clone()),
89+
};
90+
Self::create_inner(context, json_organization).await
91+
}
92+
7593
pub async fn create(
7694
context: &ApiContext,
7795
auth_user: &AuthUser,
7896
json_organization: JsonNewOrganization,
7997
) -> Result<Self, HttpError> {
80-
// Create the organization
81-
let insert_organization =
82-
InsertOrganization::from_json(conn_lock!(context), json_organization)?;
83-
diesel::insert_into(schema::organization::table)
84-
.values(&insert_organization)
85-
.execute(conn_lock!(context))
86-
.map_err(resource_conflict_err!(Organization, insert_organization))?;
87-
let query_organization = schema::organization::table
88-
.filter(schema::organization::uuid.eq(&insert_organization.uuid))
89-
.first::<QueryOrganization>(conn_lock!(context))
90-
.map_err(resource_not_found_err!(Organization, insert_organization))?;
98+
let query_organization = Self::create_inner(context, json_organization).await?;
9199

92100
let timestamp = DateTime::now();
93101
// Connect the user to the organization as a `Leader`
@@ -106,6 +114,22 @@ impl QueryOrganization {
106114
Ok(query_organization)
107115
}
108116

117+
async fn create_inner(
118+
context: &ApiContext,
119+
json_organization: JsonNewOrganization,
120+
) -> Result<Self, HttpError> {
121+
let insert_organization =
122+
InsertOrganization::from_json(conn_lock!(context), json_organization)?;
123+
diesel::insert_into(schema::organization::table)
124+
.values(&insert_organization)
125+
.execute(conn_lock!(context))
126+
.map_err(resource_conflict_err!(Organization, insert_organization))?;
127+
schema::organization::table
128+
.filter(schema::organization::uuid.eq(&insert_organization.uuid))
129+
.first::<QueryOrganization>(conn_lock!(context))
130+
.map_err(resource_not_found_err!(Organization, insert_organization))
131+
}
132+
109133
pub fn is_allowed_resource_id(
110134
conn: &mut DbConnection,
111135
rbac: &Rbac,

lib/bencher_schema/src/model/project/mod.rs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl QueryProject {
8989
.map_err(resource_not_found_err!(Project, slug.clone()))
9090
}
9191

92-
pub async fn get_or_create(
92+
pub async fn get_or_create_from_project(
9393
log: &Logger,
9494
context: &ApiContext,
9595
auth_user: &AuthUser,
@@ -110,7 +110,8 @@ impl QueryProject {
110110
ResourceIdKind::Slug(slug) => slug,
111111
};
112112

113-
let query_organization = QueryOrganization::get_or_create(context, auth_user).await?;
113+
let query_organization =
114+
QueryOrganization::get_or_create_from_user(context, auth_user).await?;
114115
let json_project = JsonNewProject {
115116
name: slug.clone().into(),
116117
slug: Some(slug.clone()),
@@ -131,7 +132,8 @@ impl QueryProject {
131132
return Ok(query_project);
132133
}
133134

134-
let query_organization = QueryOrganization::get_or_create(context, auth_user).await?;
135+
let query_organization =
136+
QueryOrganization::get_or_create_from_user(context, auth_user).await?;
135137
let name = Self::unique_name(context, &query_organization, project_name).await?;
136138
let json_project = JsonNewProject {
137139
name,
@@ -226,29 +228,18 @@ impl QueryProject {
226228
query_organization: &QueryOrganization,
227229
json_project: JsonNewProject,
228230
) -> Result<Self, HttpError> {
229-
let insert_project =
230-
InsertProject::from_json(conn_lock!(context), query_organization, json_project)?;
231-
232231
// Check to see if user has permission to create a project within the organization
233232
context
234233
.rbac
235234
.is_allowed_organization(
236235
auth_user,
237236
bencher_rbac::organization::Permission::Create,
238-
&insert_project,
237+
query_organization,
239238
)
240239
.map_err(forbidden_error)?;
241240

242-
diesel::insert_into(project_table::table)
243-
.values(&insert_project)
244-
.execute(conn_lock!(context))
245-
.map_err(resource_conflict_err!(Project, &insert_project))?;
246-
let query_project = Self::from_uuid(
247-
conn_lock!(context),
248-
query_organization.id,
249-
insert_project.uuid,
250-
)?;
251-
slog::debug!(log, "Created project: {query_project:?}");
241+
let query_project =
242+
Self::create_inner(log, context, query_organization, json_project).await?;
252243

253244
let timestamp = DateTime::now();
254245
// Connect the user to the project as a `Maintainer`
@@ -265,6 +256,28 @@ impl QueryProject {
265256
.map_err(resource_conflict_err!(ProjectRole, insert_proj_role))?;
266257
slog::debug!(log, "Added project role: {insert_proj_role:?}");
267258

259+
Ok(query_project)
260+
}
261+
262+
async fn create_inner(
263+
log: &Logger,
264+
context: &ApiContext,
265+
query_organization: &QueryOrganization,
266+
json_project: JsonNewProject,
267+
) -> Result<Self, HttpError> {
268+
let insert_project =
269+
InsertProject::from_json(conn_lock!(context), query_organization, json_project)?;
270+
diesel::insert_into(project_table::table)
271+
.values(&insert_project)
272+
.execute(conn_lock!(context))
273+
.map_err(resource_conflict_err!(Project, &insert_project))?;
274+
let query_project = Self::from_uuid(
275+
conn_lock!(context),
276+
query_organization.id,
277+
insert_project.uuid,
278+
)?;
279+
slog::debug!(log, "Created project: {query_project:?}");
280+
268281
#[cfg(feature = "plus")]
269282
context.update_index(log, &query_project).await;
270283

0 commit comments

Comments
 (0)