Skip to content

Commit 917a2bb

Browse files
committed
fix(mods): move dev assign / unassign to repository module
1 parent 546147b commit 917a2bb

7 files changed

+64
-168
lines changed

.sqlx/query-16469aa4a91fd0c6e55b56b3ae1bdbcd907942f510fe3f16683abd6f0429c86a.json

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.sqlx/query-1dab289f4e73713483b43887079957a6e93fc67e2ff5295a892f8968fba3f085.json

-15
This file was deleted.

.sqlx/query-432b742a413480d86a0150b28de79f385cabbffd90ff5458ceda3ee10416858f.json

-29
This file was deleted.

.sqlx/query-6278ed661667633ccddfd8fb6af5e48e9e4bd0e5204081abe41a1464c0c6b535.json

-15
This file was deleted.

src/database/repository/mods.rs

+27
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,33 @@ pub async fn assign_developer(
142142
Ok(())
143143
}
144144

145+
pub async fn unassign_developer(
146+
id: &str,
147+
developer_id: i32,
148+
conn: &mut PgConnection,
149+
) -> Result<(), ApiError> {
150+
sqlx::query!(
151+
"DELETE FROM mods_developers
152+
WHERE mod_id = $1
153+
AND developer_id = $2",
154+
id,
155+
developer_id
156+
)
157+
.execute(conn)
158+
.await
159+
.inspect_err(|x| {
160+
log::error!(
161+
"Couldn't unassign developer {} from mod {}: {}",
162+
developer_id,
163+
id,
164+
x
165+
)
166+
})
167+
.or(Err(ApiError::DbError))?;
168+
169+
Ok(())
170+
}
171+
145172
pub async fn is_featured(id: &str, conn: &mut PgConnection) -> Result<bool, ApiError> {
146173
Ok(sqlx::query!("SELECT featured FROM mods WHERE id = $1", id)
147174
.fetch_optional(&mut *conn)

src/endpoints/developers.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub async fn add_developer_to_mod(
116116
json.username
117117
)))?;
118118

119-
Mod::assign_dev(&path.id, target.id, &mut pool).await?;
119+
mods::assign_developer(&path.id, target.id, false, &mut pool).await?;
120120

121121
Ok(HttpResponse::NoContent())
122122
}
@@ -149,9 +149,23 @@ pub async fn remove_dev_from_mod(
149149
path.username
150150
)))?;
151151

152-
Mod::unassign_dev(&path.id, target.id, &mut pool).await?;
152+
if target.id == dev.id {
153+
return Ok(HttpResponse::Conflict().json(ApiResponse {
154+
error: "Cannot remove self from mod developer list".into(),
155+
payload: "",
156+
}));
157+
}
153158

154-
Ok(HttpResponse::NoContent())
159+
if !developers::has_access_to_mod(target.id, &path.id, &mut pool).await? {
160+
return Ok(HttpResponse::NotFound().json(ApiResponse {
161+
error: format!("{} is not a developer for this mod", target.username),
162+
payload: "",
163+
}));
164+
}
165+
166+
mods::unassign_developer(&path.id, target.id, &mut pool).await?;
167+
168+
Ok(HttpResponse::NoContent().finish())
155169
}
156170

157171
#[delete("v1/me/token")]
@@ -207,7 +221,11 @@ pub async fn update_profile(
207221
.await
208222
.or(Err(ApiError::DbAcquireError))?;
209223

210-
if !json.display_name.chars().all(|x| char::is_ascii_alphanumeric(&x)) {
224+
if !json
225+
.display_name
226+
.chars()
227+
.all(|x| char::is_ascii_alphanumeric(&x))
228+
{
211229
return Err(ApiError::BadRequest(
212230
"Display name must contain only ASCII alphanumeric characters".into(),
213231
));

src/types/models/mod_entity.rs

-105
Original file line numberDiff line numberDiff line change
@@ -649,111 +649,6 @@ impl Mod {
649649
Ok(())
650650
}
651651

652-
pub async fn assign_dev(
653-
mod_id: &str,
654-
dev_id: i32,
655-
pool: &mut PgConnection,
656-
) -> Result<(), ApiError> {
657-
struct FetchedRow {
658-
developer_id: i32,
659-
is_owner: bool,
660-
}
661-
662-
let assignment = sqlx::query_as!(
663-
FetchedRow,
664-
"SELECT md.developer_id, md.is_owner FROM mods_developers md
665-
WHERE md.mod_id = $1
666-
AND md.developer_id = $2",
667-
mod_id,
668-
dev_id
669-
)
670-
.fetch_optional(&mut *pool)
671-
.await
672-
.map_err(|e| {
673-
log::error!(
674-
"Failed to fetch existing dev for assignment on mod {}: {}",
675-
mod_id,
676-
e
677-
);
678-
ApiError::DbError
679-
})?;
680-
if assignment.is_some() {
681-
return Err(ApiError::BadRequest(format!(
682-
"This developer is already assigned on mod {}",
683-
mod_id
684-
)));
685-
}
686-
687-
sqlx::query!(
688-
"INSERT INTO mods_developers (mod_id, developer_id)
689-
VALUES ($1, $2)",
690-
mod_id,
691-
dev_id
692-
)
693-
.execute(&mut *pool)
694-
.await
695-
.map_err(|e| {
696-
log::error!("Couldn't add new developer to mod {}: {}", mod_id, e);
697-
ApiError::DbError
698-
})?;
699-
Ok(())
700-
}
701-
702-
pub async fn unassign_dev(
703-
mod_id: &str,
704-
dev_id: i32,
705-
pool: &mut PgConnection,
706-
) -> Result<(), ApiError> {
707-
struct FetchedRow {
708-
developer_id: i32,
709-
is_owner: bool,
710-
}
711-
712-
let existing = sqlx::query_as!(
713-
FetchedRow,
714-
"SELECT md.developer_id, md.is_owner FROM mods_developers md
715-
WHERE md.mod_id = $1
716-
AND md.developer_id = $2",
717-
mod_id,
718-
dev_id
719-
)
720-
.fetch_optional(&mut *pool)
721-
.await
722-
.map_err(|err| {
723-
log::error!("Failed to fetch existing developers: {}", err);
724-
ApiError::DbError
725-
})?
726-
.ok_or(ApiError::NotFound(
727-
"Developer is not assigned to mod".into(),
728-
))?;
729-
730-
if existing.is_owner {
731-
return Err(ApiError::BadRequest(
732-
"Cannot unassign the owner developer for the mod".to_string(),
733-
));
734-
}
735-
736-
sqlx::query!(
737-
"DELETE FROM mods_developers
738-
WHERE mod_id = $1 AND developer_id = $2",
739-
mod_id,
740-
dev_id
741-
)
742-
.execute(&mut *pool)
743-
.await
744-
.map_err(|e| {
745-
log::error!(
746-
"Failed to remove assigned developer {} from mod {}: {}",
747-
dev_id,
748-
mod_id,
749-
e
750-
);
751-
ApiError::DbError
752-
})?;
753-
754-
Ok(())
755-
}
756-
757652
pub async fn get_updates(
758653
ids: &[String],
759654
platforms: VerPlatform,

0 commit comments

Comments
 (0)