backend: working license controller

This commit is contained in:
Sphereso 2024-07-09 18:50:42 +02:00
parent e3a3c5bc78
commit cb9d3464e3
13 changed files with 252 additions and 2 deletions

View file

@ -1,5 +1,7 @@
mod auth;
mod license;
mod user;
pub use auth::AuthController;
pub use license::LicenseController;
pub use user::UserController;

View file

@ -0,0 +1,63 @@
use actix_web::{error::ErrorInternalServerError, web, Responder};
use sea_orm::{ActiveModelTrait, ActiveValue, EntityTrait, IntoActiveModel};
use serde::{Deserialize, Serialize};
use crate::{auth::AuthedUser, AppState};
use super::user::UserWithoutPassword;
#[derive(Serialize)]
struct GroupWithLicenses {
#[serde(flatten)]
group: entity::license_group::Model,
licenses: Vec<entity::license::Model>,
}
pub struct LicenseController;
impl LicenseController {
pub async fn list_groups(
state: web::Data<AppState>,
_executor: AuthedUser,
) -> actix_web::Result<impl Responder> {
let db = &state.db;
let groups = entity::prelude::LicenseGroup::find()
.find_with_related(entity::prelude::License)
.all(db)
.await
.map_err(ErrorInternalServerError)?
.into_iter()
.map(|(group, licenses)| GroupWithLicenses { group, licenses })
.collect::<Vec<_>>();
Ok(web::Json(groups))
}
pub async fn create_group(
state: web::Data<AppState>,
group: web::Json<entity::license_group::Model>,
_executor: AuthedUser,
) -> actix_web::Result<impl Responder> {
let db = &state.db;
let group = group.into_inner();
let mut group = group.into_active_model();
group.id = ActiveValue::NotSet;
let res = group.insert(db).await.map_err(ErrorInternalServerError)?;
Ok(web::Json(res))
}
pub async fn create_license(
state: web::Data<AppState>,
license: web::Json<entity::license::Model>,
_executor: AuthedUser,
) -> actix_web::Result<impl Responder> {
let db = &state.db;
let license = license.into_inner();
let mut license = license.into_active_model();
license.id = ActiveValue::NotSet;
let res = license.insert(db).await.map_err(ErrorInternalServerError)?;
Ok(web::Json(res))
}
}

View file

@ -1,4 +1,4 @@
use crate::controller::{AuthController, UserController};
use crate::controller::{AuthController, LicenseController, UserController};
use actix_web::web;
pub fn config(cfg: &mut web::ServiceConfig) {
@ -10,6 +10,12 @@ pub fn config(cfg: &mut web::ServiceConfig) {
.post(UserController::create_user),
)
.service(web::resource("/users/{user_id}"))
.service(
web::resource("/license")
.get(LicenseController::list_groups)
.post(LicenseController::create_license),
)
.route("/group", web::post().to(LicenseController::create_group))
.service(web::scope("/auth").route("/login", web::post().to(AuthController::login))),
);
}