gitea_pages/backend/src/api/models/repository.rs

63 lines
1.4 KiB
Rust

use juniper::{graphql_object, FieldResult, GraphQLInputObject, IntoFieldError};
use uuidv7::Uuid;
use crate::{
api::{models, scalars, Context, Error},
CONFIG,
};
#[derive(Clone, Debug)]
pub struct Repository {
pub id: Uuid,
pub user_id: Uuid,
pub name: String,
}
#[graphql_object(context = Context)]
impl Repository {
fn id(&self) -> scalars::Uuid {
scalars::Uuid(self.id)
}
async fn user(&self, context: &Context) -> FieldResult<models::user::User> {
context
.loaders
.user
.try_load(self.user_id)
.await
.map_err(|_| Error::Internal.into_field_error())
}
fn name(&self) -> &str {
&self.name
}
async fn url(&self, context: &Context, scheme: Option<bool>) -> FieldResult<String> {
let user_name = context
.loaders
.user
.try_load(self.user_id)
.await
.map_err(|_| Error::Internal.into_field_error())?
.name;
Ok(format!(
"{}{}.{}/{}",
if scheme.unwrap_or(true) {
"https://"
} else {
""
},
user_name,
CONFIG.domain,
self.name
))
}
}
#[derive(GraphQLInputObject)]
pub struct CreateRepositoryInput {
pub user: String,
pub name: String,
}