Skip to content

Commit 60e0cfe

Browse files
committed
insert_project
1 parent ad7573c commit 60e0cfe

File tree

3 files changed

+45
-30
lines changed

3 files changed

+45
-30
lines changed

lib/api_organizations/src/projects.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use bencher_schema::{
2020
measure::{InsertMeasure, QueryMeasure},
2121
testbed::{InsertTestbed, QueryTestbed},
2222
threshold::InsertThreshold,
23-
QueryProject,
23+
InsertProject, QueryProject,
2424
},
2525
user::auth::{AuthUser, BearerToken},
2626
},
@@ -226,9 +226,11 @@ async fn post_inner(
226226
.await?;
227227
}
228228

229+
let insert_project =
230+
InsertProject::from_json(conn_lock!(context), &query_organization, json_project)?;
229231
// Create a new project
230232
let query_project =
231-
QueryProject::create(log, context, auth_user, &query_organization, json_project).await?;
233+
QueryProject::create(log, context, auth_user, &query_organization, insert_project).await?;
232234

233235
// Add a `main` branch to the project
234236
let query_branch = InsertBranch::main(log, context, query_project.id).await?;

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ use organization_role::{InsertOrganizationRole, QueryOrganizationRole};
1616
use crate::{
1717
conn_lock,
1818
context::{DbConnection, Rbac},
19-
error::{forbidden_error, resource_not_found_error, unauthorized_error, BencherResource},
19+
error::{
20+
forbidden_error, issue_error, resource_not_found_error, unauthorized_error, BencherResource,
21+
},
2022
macros::{
2123
fn_get::{fn_get, fn_get_id, fn_get_uuid},
2224
resource_id::{fn_eq_resource_id, fn_from_resource_id},
@@ -64,7 +66,15 @@ impl QueryOrganization {
6466
if let Ok(query_organization) =
6567
Self::from_resource_id(conn_lock!(context), &user_slug.clone().into())
6668
{
67-
query_organization.try_allowed(&context.rbac, auth_user, Permission::View)?;
69+
query_organization
70+
.try_allowed(&context.rbac, auth_user, Permission::View)
71+
.map_err(|err| {
72+
issue_error(
73+
"User cannot view own organization",
74+
&format!("User ({user_slug}) cannot view own organization."),
75+
err,
76+
)
77+
})?;
6878
return Ok(query_organization);
6979
}
7080

@@ -113,7 +123,7 @@ impl QueryOrganization {
113123
.is_ok()
114124
{
115125
return Err(forbidden_error(
116-
"You cannot create an organization with the same slug as your user.",
126+
"You cannot create an organization with the same slug as a different user.",
117127
));
118128
}
119129

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

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,12 @@ impl QueryProject {
156156
// The choice was either to relax the schema constraint to allow duplicate project names
157157
// or to append a number to the project name to ensure uniqueness.
158158
let name = Self::unique_name(context, &query_organization, project_name).await?;
159-
let json_project = JsonNewProject {
160-
name,
161-
slug: Some(project_slug),
162-
url: None,
163-
visibility: None,
164-
};
165-
159+
let insert_project =
160+
InsertProject::new(query_organization.id, name, project_slug, None, None);
166161
if let Some(auth_user) = auth_user {
167-
Self::create(log, context, auth_user, &query_organization, json_project).await
162+
Self::create(log, context, auth_user, &query_organization, insert_project).await
168163
} else {
169-
Self::create_inner(log, context, &query_organization, json_project).await
164+
Self::create_inner(log, context, &query_organization, insert_project).await
170165
}
171166
}
172167

@@ -252,7 +247,7 @@ impl QueryProject {
252247
context: &ApiContext,
253248
auth_user: &AuthUser,
254249
query_organization: &QueryOrganization,
255-
json_project: JsonNewProject,
250+
insert_project: InsertProject,
256251
) -> Result<Self, HttpError> {
257252
// Check to see if user has permission to create a project within the organization
258253
context
@@ -265,7 +260,7 @@ impl QueryProject {
265260
.map_err(forbidden_error)?;
266261

267262
let query_project =
268-
Self::create_inner(log, context, query_organization, json_project).await?;
263+
Self::create_inner(log, context, query_organization, insert_project).await?;
269264

270265
let timestamp = DateTime::now();
271266
// Connect the user to the project as a `Maintainer`
@@ -289,10 +284,8 @@ impl QueryProject {
289284
log: &Logger,
290285
context: &ApiContext,
291286
query_organization: &QueryOrganization,
292-
json_project: JsonNewProject,
287+
insert_project: InsertProject,
293288
) -> Result<Self, HttpError> {
294-
let insert_project =
295-
InsertProject::from_json(conn_lock!(context), query_organization, json_project)?;
296289
diesel::insert_into(project_table::table)
297290
.values(&insert_project)
298291
.execute(conn_lock!(context))
@@ -466,6 +459,26 @@ pub struct InsertProject {
466459
}
467460

468461
impl InsertProject {
462+
pub fn new(
463+
organization_id: OrganizationId,
464+
name: ResourceName,
465+
slug: Slug,
466+
url: Option<Url>,
467+
visibility: Option<Visibility>,
468+
) -> Self {
469+
let timestamp = DateTime::now();
470+
Self {
471+
uuid: ProjectUuid::new(),
472+
organization_id,
473+
name,
474+
slug,
475+
url,
476+
visibility: visibility.unwrap_or_default(),
477+
created: timestamp,
478+
modified: timestamp,
479+
}
480+
}
481+
469482
pub fn from_json(
470483
conn: &mut DbConnection,
471484
organization: &QueryOrganization,
@@ -478,17 +491,7 @@ impl InsertProject {
478491
visibility,
479492
} = project;
480493
let slug = ok_slug!(conn, &name, slug, project, QueryProject)?;
481-
let timestamp = DateTime::now();
482-
Ok(Self {
483-
uuid: ProjectUuid::new(),
484-
organization_id: organization.id,
485-
name,
486-
slug,
487-
url,
488-
visibility: visibility.unwrap_or_default(),
489-
created: timestamp,
490-
modified: timestamp,
491-
})
494+
Ok(Self::new(organization.id, name, slug, url, visibility))
492495
}
493496
}
494497

0 commit comments

Comments
 (0)