|
1 | 1 | use crate::types::api::ApiError;
|
| 2 | +use crate::types::mod_json::ModJson; |
2 | 3 | use crate::types::models::tag::Tag;
|
3 | 4 | use sqlx::PgConnection;
|
4 | 5 |
|
@@ -28,3 +29,120 @@ pub async fn get_all(conn: &mut PgConnection) -> Result<Vec<Tag>, ApiError> {
|
28 | 29 |
|
29 | 30 | Ok(tags)
|
30 | 31 | }
|
| 32 | + |
| 33 | +pub async fn get_for_mod(id: &str, conn: &mut PgConnection) -> Result<Vec<Tag>, ApiError> { |
| 34 | + sqlx::query!( |
| 35 | + "SELECT |
| 36 | + id, |
| 37 | + name, |
| 38 | + display_name, |
| 39 | + is_readonly |
| 40 | + FROM mod_tags mt |
| 41 | + INNER JOIN mods_mod_tags mmt ON mmt.tag_id = mt.id |
| 42 | + WHERE mmt.mod_id = $1", |
| 43 | + id |
| 44 | + ) |
| 45 | + .fetch_all(&mut *conn) |
| 46 | + .await |
| 47 | + .map_err(|e| { |
| 48 | + log::error!("mod_tags::get_tags failed: {}", e); |
| 49 | + ApiError::DbError |
| 50 | + }) |
| 51 | + .map(|vec| { |
| 52 | + vec.into_iter() |
| 53 | + .map(|i| Tag { |
| 54 | + id: i.id, |
| 55 | + display_name: i.display_name.unwrap_or(i.name.clone()), |
| 56 | + name: i.name, |
| 57 | + is_readonly: i.is_readonly, |
| 58 | + }) |
| 59 | + .collect() |
| 60 | + }) |
| 61 | +} |
| 62 | + |
| 63 | +pub async fn parse_tag_list( |
| 64 | + tags: &[String], |
| 65 | + conn: &mut PgConnection, |
| 66 | +) -> Result<Vec<Tag>, ApiError> { |
| 67 | + if tags.is_empty() { |
| 68 | + return Ok(vec![]); |
| 69 | + } |
| 70 | + |
| 71 | + let db_tags = get_all(conn).await?; |
| 72 | + |
| 73 | + let mut ret = Vec::new(); |
| 74 | + for tag in tags { |
| 75 | + if let Some(t) = db_tags.iter().find(|t| t.name == *tag) { |
| 76 | + ret.push(t.clone()); |
| 77 | + } else { |
| 78 | + return Err(ApiError::BadRequest(format!( |
| 79 | + "Tag '{}' isn't allowed. Only the following are allowed: '{}'", |
| 80 | + tag, |
| 81 | + db_tags |
| 82 | + .into_iter() |
| 83 | + .map(|t| t.name) |
| 84 | + .collect::<Vec<String>>() |
| 85 | + .join(", ") |
| 86 | + ))); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + Ok(ret) |
| 91 | +} |
| 92 | + |
| 93 | +pub async fn update_for_mod( |
| 94 | + id: &str, |
| 95 | + tags: &[Tag], |
| 96 | + conn: &mut PgConnection, |
| 97 | +) -> Result<(), ApiError> { |
| 98 | + let existing = get_for_mod(id, &mut *conn).await?; |
| 99 | + |
| 100 | + let insertable = tags |
| 101 | + .iter() |
| 102 | + .filter(|t| !existing.iter().any(|e| e.id == t.id)) |
| 103 | + .map(|x| x.id) |
| 104 | + .collect::<Vec<_>>(); |
| 105 | + |
| 106 | + let deletable = existing |
| 107 | + .iter() |
| 108 | + .filter(|e| !tags.iter().any(|t| e.id == t.id)) |
| 109 | + .map(|x| x.id) |
| 110 | + .collect::<Vec<_>>(); |
| 111 | + |
| 112 | + if !deletable.is_empty() { |
| 113 | + sqlx::query!( |
| 114 | + "DELETE FROM mods_mod_tags |
| 115 | + WHERE mod_id = $1 |
| 116 | + AND tag_id = ANY($2)", |
| 117 | + id, |
| 118 | + &deletable |
| 119 | + ) |
| 120 | + .execute(&mut *conn) |
| 121 | + .await |
| 122 | + .inspect_err(|e| log::error!("Failed to remove tags: {}", e)) |
| 123 | + .or(Err(ApiError::DbError))?; |
| 124 | + } |
| 125 | + |
| 126 | + if insertable.is_empty() { |
| 127 | + return Ok(()); |
| 128 | + } |
| 129 | + |
| 130 | + let mod_id = vec![id.into(); insertable.len()]; |
| 131 | + |
| 132 | + sqlx::query!( |
| 133 | + "INSERT INTO mods_mod_tags |
| 134 | + (mod_id, tag_id) |
| 135 | + SELECT * FROM UNNEST( |
| 136 | + $1::text[], |
| 137 | + $2::int4[] |
| 138 | + )", |
| 139 | + &mod_id, |
| 140 | + &insertable |
| 141 | + ) |
| 142 | + .execute(&mut *conn) |
| 143 | + .await |
| 144 | + .inspect_err(|e| log::error!("Failed to insert tags: {}", e)) |
| 145 | + .or(Err(ApiError::DbError))?; |
| 146 | + |
| 147 | + Ok(()) |
| 148 | +} |
0 commit comments