Skip to content

Commit 1597fb8

Browse files
committed
models/krate: Inline NewCrate::create_or_update() fn
1 parent 721ce2a commit 1597fb8

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

src/controllers/krate/publish.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,15 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
154154
return Err(cargo_err("cannot upload a crate with a reserved name"));
155155
}
156156

157-
let krate = persist.create_or_update(conn, user.id)?;
157+
let krate = conn.transaction(|conn| {
158+
// To avoid race conditions, we try to insert
159+
// first so we know whether to add an owner
160+
if let Some(krate) = persist.create(conn, user.id).optional()? {
161+
return Ok(krate);
162+
}
163+
164+
persist.update(conn)
165+
})?;
158166

159167
let owners = krate.owners(conn)?;
160168
if user.rights(&app, &owners)? < Rights::Publish {

src/models/krate.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,6 @@ pub struct NewCrate<'a> {
101101
}
102102

103103
impl<'a> NewCrate<'a> {
104-
pub fn create_or_update(self, conn: &mut PgConnection, uploader: i32) -> AppResult<Crate> {
105-
conn.transaction(|conn| {
106-
// To avoid race conditions, we try to insert
107-
// first so we know whether to add an owner
108-
if let Some(krate) = self.create(conn, uploader).optional()? {
109-
return Ok(krate);
110-
}
111-
112-
Ok(self.update(conn)?)
113-
})
114-
}
115-
116104
pub fn update(&self, conn: &mut PgConnection) -> QueryResult<Crate> {
117105
use diesel::update;
118106

0 commit comments

Comments
 (0)