Skip to content

Commit 7b407ba

Browse files
committed
have reposting a ban update the most recent one
1 parent 66cb5a0 commit 7b407ba

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

src/database/repository/developers.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,19 @@ pub async fn check_ban(
462462
.map_err(|e| e.into())
463463
}
464464

465+
pub async fn update_ban_revoke_time(ban_id: i32, revoked_at: Option<DateTime<Utc>>, conn: &mut PgConnection) -> Result<Option<DeveloperBan>, DatabaseError> {
466+
sqlx::query_as!(DeveloperBan,
467+
"UPDATE bans
468+
SET revoked_at=$2 WHERE id=$1
469+
RETURNING
470+
id, developer_id, reason, admin_id, created_at, revoked_at",
471+
ban_id, revoked_at)
472+
.fetch_optional(&mut *conn)
473+
.await
474+
.inspect_err(|e| log::error!("Failed to update developer ban: {e}"))
475+
.map_err(|e| e.into())
476+
}
477+
465478
pub async fn delete_ban(dev_id: i32, conn: &mut PgConnection) -> Result<(), DatabaseError> {
466479
if let Some(current_ban) = check_ban(dev_id, conn).await? {
467480
sqlx::query!("UPDATE bans SET revoked_at=NOW() WHERE id=$1", current_ban.id)

src/endpoints/developers.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,15 @@ pub async fn ban_developer(
528528
.ok_or(ApiError::NotFound("Developer not found".into()))?;
529529

530530
// check ban exists
531-
if let None = developers::check_ban(path.id, &mut pool).await? {
532-
return Err(ApiError::BadRequest("This developer is already banned".into()));
531+
if let Some(ban) = developers::check_ban(path.id, &mut pool).await? {
532+
let result = developers::update_ban_revoke_time(ban.id, payload.revoked_at, &mut pool)
533+
.await?
534+
.ok_or(ApiError::InternalError("Ban was deleted between asserting its existence and updating it".into()))?;
535+
536+
return Ok(web::Json(ApiResponse {
537+
error: "".to_string(),
538+
payload: result,
539+
}))
533540
}
534541

535542
let result = developers::create_ban(

0 commit comments

Comments
 (0)