Skip to content

Commit 1221840

Browse files
committed
models/keyword: Simplify update_crate() argument
This allows us to use this function even if we only have an id and not a full `Crate` instance.
1 parent fc4a6d2 commit 1221840

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
lines changed

src/controllers/krate/publish.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
427427
}
428428

429429
// Update all keywords for this crate
430-
Keyword::update_crate(conn, &krate, &keywords)?;
430+
Keyword::update_crate(conn, krate.id, &keywords)?;
431431

432432
// Update all categories for this crate, collecting any invalid categories
433433
// in order to be able to return an error to the user.

src/models/keyword.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,26 @@ impl Keyword {
6161
&& chars.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-' || c == '+')
6262
}
6363

64-
pub fn update_crate(conn: &mut impl Conn, krate: &Crate, keywords: &[&str]) -> QueryResult<()> {
64+
pub fn update_crate(conn: &mut impl Conn, crate_id: i32, keywords: &[&str]) -> QueryResult<()> {
6565
conn.transaction(|conn| {
6666
let keywords = Keyword::find_or_create_all(conn, keywords)?;
67-
diesel::delete(CrateKeyword::belonging_to(krate)).execute(conn)?;
67+
68+
diesel::delete(crates_keywords::table)
69+
.filter(crates_keywords::crate_id.eq(crate_id))
70+
.execute(conn)?;
71+
6872
let crate_keywords = keywords
6973
.into_iter()
7074
.map(|kw| CrateKeyword {
71-
crate_id: krate.id,
75+
crate_id,
7276
keyword_id: kw.id,
7377
})
7478
.collect::<Vec<_>>();
79+
7580
diesel::insert_into(crates_keywords::table)
7681
.values(&crate_keywords)
7782
.execute(conn)?;
83+
7884
Ok(())
7985
})
8086
}

src/tests/builders/krate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl<'a> CrateBuilder<'a> {
160160
}
161161

162162
if !self.keywords.is_empty() {
163-
Keyword::update_crate(connection, &krate, &self.keywords)?;
163+
Keyword::update_crate(connection, krate.id, &self.keywords)?;
164164
}
165165

166166
if let Some(updated_at) = self.updated_at {

src/tests/routes/keywords/read.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,27 @@ async fn update_crate() {
5050
Keyword::find_or_create_all(&mut conn, &["kw1", "kw2"]).unwrap();
5151
let krate = CrateBuilder::new("fookey", user.id).expect_build(&mut conn);
5252

53-
Keyword::update_crate(&mut conn, &krate, &[]).unwrap();
53+
Keyword::update_crate(&mut conn, krate.id, &[]).unwrap();
5454
assert_eq!(cnt("kw1", &anon).await, 0);
5555
assert_eq!(cnt("kw2", &anon).await, 0);
5656

57-
Keyword::update_crate(&mut conn, &krate, &["kw1"]).unwrap();
57+
Keyword::update_crate(&mut conn, krate.id, &["kw1"]).unwrap();
5858
assert_eq!(cnt("kw1", &anon).await, 1);
5959
assert_eq!(cnt("kw2", &anon).await, 0);
6060

61-
Keyword::update_crate(&mut conn, &krate, &["kw2"]).unwrap();
61+
Keyword::update_crate(&mut conn, krate.id, &["kw2"]).unwrap();
6262
assert_eq!(cnt("kw1", &anon).await, 0);
6363
assert_eq!(cnt("kw2", &anon).await, 1);
6464

65-
Keyword::update_crate(&mut conn, &krate, &[]).unwrap();
65+
Keyword::update_crate(&mut conn, krate.id, &[]).unwrap();
6666
assert_eq!(cnt("kw1", &anon).await, 0);
6767
assert_eq!(cnt("kw2", &anon).await, 0);
6868

69-
Keyword::update_crate(&mut conn, &krate, &["kw1", "kw2"]).unwrap();
69+
Keyword::update_crate(&mut conn, krate.id, &["kw1", "kw2"]).unwrap();
7070
assert_eq!(cnt("kw1", &anon).await, 1);
7171
assert_eq!(cnt("kw2", &anon).await, 1);
7272

73-
Keyword::update_crate(&mut conn, &krate, &[]).unwrap();
73+
Keyword::update_crate(&mut conn, krate.id, &[]).unwrap();
7474
assert_eq!(cnt("kw1", &anon).await, 0);
7575
assert_eq!(cnt("kw2", &anon).await, 0);
7676
}

0 commit comments

Comments
 (0)