Skip to content

Commit

Permalink
wip: field changes in challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
ElaBosak233 committed Jan 15, 2025
1 parent d74bca6 commit 789dcbe
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 30 deletions.
6 changes: 6 additions & 0 deletions crates/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ impl From<cds_db::entity::config::Model> for Config {
}
}

impl Config {
pub fn desensitize(&mut self) {
self.auth.jwt.secret_key.clear();
}
}

pub async fn init() {
let config = cds_cache::get::<Config>("config").await.unwrap();
if config.is_none() {
Expand Down
4 changes: 3 additions & 1 deletion crates/db/src/entity/challenge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct Model {
#[sea_orm(default_value = false)]
pub has_attachment: bool,
#[sea_orm(default_value = false)]
pub is_practicable: bool,
pub is_public: bool,
pub image_name: Option<String>,
#[sea_orm(default_value = 0)]
pub cpu_limit: i64,
Expand All @@ -36,6 +36,8 @@ pub struct Model {
pub envs: Vec<Env>,
#[sea_orm(column_type = "JsonBinary")]
pub flags: Vec<Flag>,
#[sea_orm(default_value = false)]
pub is_deleted: bool,
pub created_at: i64,
pub updated_at: i64,
}
Expand Down
46 changes: 38 additions & 8 deletions crates/db/src/transfer/challenge.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use sea_orm::{ColumnTrait, DbErr, EntityTrait, PaginatorTrait, QueryFilter, QuerySelect};
use std::str::FromStr;

use sea_orm::{
ColumnTrait, DbErr, EntityTrait, Order, PaginatorTrait, QueryFilter, QueryOrder, QuerySelect,
};
use serde::{Deserialize, Serialize};
use tracing::debug;

use crate::{
entity,
Expand All @@ -16,14 +21,15 @@ pub struct Challenge {
pub tags: Vec<String>,
pub is_dynamic: bool,
pub has_attachment: bool,
pub is_practicable: bool,
pub is_public: bool,
pub image_name: Option<String>,
pub cpu_limit: i64,
pub memory_limit: i64,
pub duration: i64,
pub ports: Vec<i32>,
pub envs: Vec<Env>,
pub flags: Vec<Flag>,
pub is_deleted: bool,
pub created_at: i64,
pub updated_at: i64,
}
Expand All @@ -38,14 +44,15 @@ impl From<entity::challenge::Model> for Challenge {
tags: entity.tags,
is_dynamic: entity.is_dynamic,
has_attachment: entity.has_attachment,
is_practicable: entity.is_practicable,
is_public: entity.is_public,
image_name: entity.image_name,
cpu_limit: entity.cpu_limit,
memory_limit: entity.memory_limit,
duration: entity.duration,
ports: entity.ports,
envs: entity.envs,
flags: entity.flags,
is_deleted: entity.is_deleted,
created_at: entity.created_at,
updated_at: entity.updated_at,
}
Expand All @@ -62,14 +69,15 @@ impl From<Challenge> for entity::challenge::Model {
tags: challenge.tags,
is_dynamic: challenge.is_dynamic,
has_attachment: challenge.has_attachment,
is_practicable: challenge.is_practicable,
is_public: challenge.is_public,
image_name: challenge.image_name,
cpu_limit: challenge.cpu_limit,
memory_limit: challenge.memory_limit,
duration: challenge.duration,
ports: challenge.ports,
envs: challenge.envs,
flags: challenge.flags,
is_deleted: challenge.is_deleted,
created_at: challenge.created_at,
updated_at: challenge.updated_at,
}
Expand All @@ -85,8 +93,9 @@ impl Challenge {
}

pub async fn find(
id: Option<i64>, title: Option<String>, category: Option<i32>, is_practicable: Option<bool>,
is_dynamic: Option<bool>, page: Option<u64>, size: Option<u64>,
id: Option<i64>, title: Option<String>, category: Option<i32>, is_public: Option<bool>,
is_dynamic: Option<bool>, is_deleted: Option<bool>, sorts: Option<String>, page: Option<u64>,
size: Option<u64>,
) -> Result<(Vec<Challenge>, u64), DbErr> {
let mut sql = entity::challenge::Entity::find();

Expand All @@ -102,16 +111,37 @@ pub async fn find(
sql = sql.filter(entity::challenge::Column::Category.eq(category));
}

if let Some(is_practicable) = is_practicable {
sql = sql.filter(entity::challenge::Column::IsPracticable.eq(is_practicable));
if let Some(is_public) = is_public {
sql = sql.filter(entity::challenge::Column::IsPublic.eq(is_public));
}

if let Some(is_dynamic) = is_dynamic {
sql = sql.filter(entity::challenge::Column::IsDynamic.eq(is_dynamic));
}

match is_deleted {
Some(true) => sql = sql.filter(entity::challenge::Column::IsDeleted.eq(true)),
_ => sql = sql.filter(entity::challenge::Column::IsDeleted.eq(false)),
}

let total = sql.clone().count(get_db()).await?;

if let Some(sorts) = sorts {
let sorts = sorts.split(",").collect::<Vec<&str>>();
for sort in sorts {
let col =
match crate::entity::challenge::Column::from_str(sort.replace("-", "").as_str()) {
Ok(col) => col,
Err(_) => continue,
};
if sort.starts_with("-") {
sql = sql.order_by(col, Order::Desc);
} else {
sql = sql.order_by(col, Order::Asc);
}
}
}

if let Some(page) = page {
if let Some(size) = size {
let offset = (page - 1) * size;
Expand Down
26 changes: 17 additions & 9 deletions crates/web/src/router/api/challenge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ pub struct GetRequest {
pub title: Option<String>,
pub category: Option<i32>,
pub tags: Option<Vec<String>>,
pub is_practicable: Option<bool>,
pub is_public: Option<bool>,
pub is_dynamic: Option<bool>,
pub is_detailed: Option<bool>,
pub is_deleted: Option<bool>,
pub page: Option<u64>,
pub size: Option<u64>,
pub sorts: Option<String>,
}

pub async fn get(
Expand All @@ -67,8 +69,10 @@ pub async fn get(
params.id,
params.title,
params.category,
params.is_practicable,
params.is_public,
params.is_dynamic,
params.is_deleted,
params.sorts,
params.page,
params.size,
)
Expand Down Expand Up @@ -187,7 +191,7 @@ pub struct CreateRequest {
pub description: String,
pub category: i32,
pub tags: Option<Vec<String>>,
pub is_practicable: Option<bool>,
pub is_public: Option<bool>,
pub is_dynamic: Option<bool>,
pub has_attachment: Option<bool>,
pub difficulty: Option<i64>,
Expand All @@ -213,7 +217,7 @@ pub async fn create(
description: Set(Some(body.description)),
category: Set(body.category),
tags: Set(body.tags.unwrap_or(vec![])),
is_practicable: Set(body.is_practicable.unwrap_or(false)),
is_public: Set(body.is_public.unwrap_or(false)),
is_dynamic: Set(body.is_dynamic.unwrap_or(false)),
has_attachment: Set(body.has_attachment.unwrap_or(false)),
image_name: Set(body.image_name),
Expand Down Expand Up @@ -243,7 +247,7 @@ pub struct UpdateRequest {
pub description: Option<String>,
pub category: Option<i32>,
pub tags: Option<Vec<String>>,
pub is_practicable: Option<bool>,
pub is_public: Option<bool>,
pub is_dynamic: Option<bool>,
pub has_attachment: Option<bool>,
pub difficulty: Option<i64>,
Expand Down Expand Up @@ -272,7 +276,7 @@ pub async fn update(
description: body.description.map_or(NotSet, |v| Set(Some(v))),
tags: body.tags.map_or(NotSet, Set),
category: body.category.map_or(NotSet, Set),
is_practicable: body.is_practicable.map_or(NotSet, Set),
is_public: body.is_public.map_or(NotSet, Set),
is_dynamic: body.is_dynamic.map_or(NotSet, Set),
has_attachment: body.has_attachment.map_or(NotSet, Set),
image_name: body.image_name.map_or(NotSet, |v| Set(Some(v))),
Expand Down Expand Up @@ -303,9 +307,13 @@ pub async fn delete(
return Err(WebError::Forbidden(json!("")));
}

let _ = cds_db::entity::challenge::Entity::delete_by_id(id)
.exec(get_db())
.await?;
let _ = cds_db::entity::challenge::ActiveModel {
id: Set(id),
is_deleted: Set(true),
..Default::default()
}
.update(get_db())
.await?;

Ok(WebResponse {
code: StatusCode::OK.as_u16(),
Expand Down
42 changes: 31 additions & 11 deletions crates/web/src/router/api/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ use axum::{
use cds_config::get_config;
use cds_db::{entity::user::Group, get_db};
use sea_orm::{ActiveModelTrait, ActiveValue::Set};
use serde::{Deserialize, Serialize};
use serde_json::json;

use crate::{
extract::{Extension, Json},
extract::{Extension, Json, Query},
traits::{Ext, WebError, WebResponse},
util::handle_image_multipart,
};
Expand All @@ -25,19 +26,38 @@ pub fn router() -> Router {
.route("/icon", axum::routing::delete(delete_icon))
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct GetRequest {
pub is_detailed: Option<bool>,
}

pub async fn get(
Extension(ext): Extension<Ext>,
Extension(ext): Extension<Ext>, Query(params): Query<GetRequest>,
) -> Result<WebResponse<cds_config::Config>, WebError> {
let operator = ext.operator.ok_or(WebError::Unauthorized(json!("")))?;
if operator.group != Group::Admin {
return Err(WebError::Forbidden(json!("")));
}
match params.is_detailed {
Some(true) => {
let operator = ext.operator.ok_or(WebError::Unauthorized(json!("")))?;
if operator.group != Group::Admin {
return Err(WebError::Forbidden(json!("")));
}

Ok(WebResponse {
code: StatusCode::OK.as_u16(),
data: Some(get_config().await),
..WebResponse::default()
})
Ok(WebResponse {
code: StatusCode::OK.as_u16(),
data: Some(get_config().await),
..WebResponse::default()
})
}
_ => {
let mut config = get_config().await;
config.desensitize();

Ok(WebResponse {
code: StatusCode::OK.as_u16(),
data: Some(config),
..WebResponse::default()
})
}
}
}

pub async fn update(
Expand Down
5 changes: 4 additions & 1 deletion crates/web/src/router/api/game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ pub async fn router() -> Router {
.route("/{id}/teams/{team_id}", axum::routing::delete(delete_team))
.route("/{id}/notices", axum::routing::get(get_notice))
.route("/{id}/notices", axum::routing::post(create_notice))
.route("/{id}/notices/{notice_id}", axum::routing::put(update_notice))
.route(
"/{id}/notices/{notice_id}",
axum::routing::put(update_notice),
)
.route(
"/{id}/notices/{notice_id}",
axum::routing::delete(delete_notice),
Expand Down

0 comments on commit 789dcbe

Please sign in to comment.