|
| 1 | +use actix_session::Session; |
| 2 | +use actix_web::{delete, get, post, put, web, HttpResponse, Responder}; |
| 3 | +use sqlx::PgPool; |
| 4 | +use utoipa::{OpenApi, ToSchema}; |
| 5 | + |
| 6 | +use crate::{api::ApiResult, database}; |
| 7 | + |
| 8 | +#[derive(OpenApi)] |
| 9 | +#[openapi( |
| 10 | + tags((name = "admin/gacha_banners")), |
| 11 | + paths(get_gacha_banners, create_gacha_banner, update_gacha_banner, delete_gacha_banner), |
| 12 | + components(schemas( |
| 13 | + database::gacha_banners::DbGachaBanner |
| 14 | + )) |
| 15 | +)] |
| 16 | +struct ApiDoc; |
| 17 | + |
| 18 | +pub fn openapi() -> utoipa::openapi::OpenApi { |
| 19 | + ApiDoc::openapi() |
| 20 | +} |
| 21 | + |
| 22 | +pub fn configure(cfg: &mut web::ServiceConfig) { |
| 23 | + cfg |
| 24 | + .service(get_gacha_banners) |
| 25 | + .service(create_gacha_banner) |
| 26 | + .service(update_gacha_banner) |
| 27 | + .service(delete_gacha_banner); |
| 28 | +} |
| 29 | + |
| 30 | +#[utoipa::path( |
| 31 | + tag = "admin/gacha_banners", |
| 32 | + get, |
| 33 | + path = "/api/admin/gacha_banners", |
| 34 | + responses( |
| 35 | + (status = 200, description = "List of gacha banners", body = Vec<database::gacha_banners::DbGachaBanner>), |
| 36 | + ), |
| 37 | + security(("admin" = [])) |
| 38 | +)] |
| 39 | +#[get("/api/admin/gacha_banners")] |
| 40 | +async fn get_gacha_banners(session: Session, pool: web::Data<PgPool>) -> ApiResult<impl Responder> { |
| 41 | + let Ok(Some(username)) = session.get::<String>("username") else { |
| 42 | + return Ok(HttpResponse::BadRequest().finish()); |
| 43 | + }; |
| 44 | + |
| 45 | + let admin = database::admins::exists(&username, &pool).await?; |
| 46 | + if !admin { |
| 47 | + return Ok(HttpResponse::Forbidden().finish()); |
| 48 | + } |
| 49 | + |
| 50 | + let banners = database::gacha_banners::get_all(&pool).await?; |
| 51 | + Ok(HttpResponse::Ok().json(banners)) |
| 52 | +} |
| 53 | + |
| 54 | +#[utoipa::path( |
| 55 | + tag = "admin/gacha_banners", |
| 56 | + post, |
| 57 | + path = "/api/admin/gacha_banners", |
| 58 | + request_body = database::gacha_banners::DbGachaBanner, |
| 59 | + responses( |
| 60 | + (status = 201, description = "Created gacha banner", body = database::gacha_banners::DbGachaBanner) |
| 61 | + ), |
| 62 | + security(("admin" = [])) |
| 63 | +)] |
| 64 | +#[post("/api/admin/gacha_banners")] |
| 65 | +async fn create_gacha_banner( |
| 66 | + session: Session, |
| 67 | + pool: web::Data<PgPool>, |
| 68 | + banner: web::Json<database::gacha_banners::DbGachaBanner>, |
| 69 | +) -> ApiResult<impl Responder> { |
| 70 | + let Ok(Some(username)) = session.get::<String>("username") else { |
| 71 | + return Ok(HttpResponse::BadRequest().finish()); |
| 72 | + }; |
| 73 | + |
| 74 | + let admin = database::admins::exists(&username, &pool).await?; |
| 75 | + if !admin { |
| 76 | + return Ok(HttpResponse::Forbidden().finish()); |
| 77 | + } |
| 78 | + |
| 79 | + let created_banner = database::gacha_banners::create(&banner, &pool).await?; |
| 80 | + Ok(HttpResponse::Created().json(created_banner)) |
| 81 | +} |
| 82 | + |
| 83 | +#[utoipa::path( |
| 84 | + tag = "admin/gacha_banners", |
| 85 | + put, |
| 86 | + path = "/api/admin/gacha_banners/{id}", |
| 87 | + request_body = database::gacha_banners::DbGachaBanner, |
| 88 | + responses( |
| 89 | + (status = 200, description = "Updated gacha banner", body = database::gacha_banners::DbGachaBanner), |
| 90 | + (status = 404, description = "Gacha banner not found"), |
| 91 | + ), |
| 92 | + security(("admin" = [])) |
| 93 | +)] |
| 94 | +#[put("/api/admin/gacha_banners/{id}")] |
| 95 | +async fn update_gacha_banner( |
| 96 | + session: Session, |
| 97 | + pool: web::Data<PgPool>, |
| 98 | + id: web::Path<i32>, |
| 99 | + banner: web::Json<database::gacha_banners::DbGachaBanner>, |
| 100 | +) -> ApiResult<impl Responder> { |
| 101 | + let Ok(Some(username)) = session.get::<String>("username") else { |
| 102 | + return Ok(HttpResponse::BadRequest().finish()); |
| 103 | + }; |
| 104 | + |
| 105 | + let admin = database::admins::exists(&username, &pool).await?; |
| 106 | + if !admin { |
| 107 | + return Ok(HttpResponse::Forbidden().finish()); |
| 108 | + } |
| 109 | + |
| 110 | + let mut banner = banner.into_inner(); |
| 111 | + banner.id = *id; |
| 112 | + |
| 113 | + let updated_banner = database::gacha_banners::update(&banner, &pool).await?; |
| 114 | + if updated_banner.is_none() { |
| 115 | + return Ok(HttpResponse::NotFound().finish()); |
| 116 | + } |
| 117 | + |
| 118 | + Ok(HttpResponse::Ok().json(updated_banner)) |
| 119 | +} |
| 120 | + |
| 121 | +#[utoipa::path( |
| 122 | + tag = "admin/gacha_banners", |
| 123 | + delete, |
| 124 | + path = "/api/admin/gacha_banners/{id}", |
| 125 | + responses( |
| 126 | + (status = 204, description = "Gacha banner deleted"), |
| 127 | + ), |
| 128 | + security(("admin" = [])) |
| 129 | +)] |
| 130 | +#[delete("/api/admin/gacha_banners/{id}")] |
| 131 | +async fn delete_gacha_banner( |
| 132 | + session: Session, |
| 133 | + pool: web::Data<PgPool>, |
| 134 | + id: web::Path<i32>, |
| 135 | +) -> ApiResult<impl Responder> { |
| 136 | + let Ok(Some(username)) = session.get::<String>("username") else { |
| 137 | + return Ok(HttpResponse::BadRequest().finish()); |
| 138 | + }; |
| 139 | + |
| 140 | + let admin = database::admins::exists(&username, &pool).await?; |
| 141 | + if !admin { |
| 142 | + return Ok(HttpResponse::Forbidden().finish()); |
| 143 | + } |
| 144 | + |
| 145 | + database::gacha_banners::delete_by_id(*id, &pool).await?; |
| 146 | + Ok(HttpResponse::NoContent().finish()) |
| 147 | +} |
0 commit comments