Skip to content

Commit

Permalink
Introduce avatar_url to user and teams & introduce minimal users endp…
Browse files Browse the repository at this point in the history
…oint
  • Loading branch information
lucemans committed Feb 18, 2025
1 parent d8d8ddd commit 7b40295
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 5 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions engine/migrations/0010_avatars.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Add avatar_url to users, teams, default null, optional
ALTER TABLE users ADD COLUMN avatar_url TEXT;
ALTER TABLE teams ADD COLUMN avatar_url TEXT;
1 change: 1 addition & 0 deletions engine/src/models/team/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct Team {
pub team_id: String,
pub owner_id: String,
pub name: String,
pub avatar_url: Option<String>,
pub created_at: DateTime<Utc>,
}

Expand Down
15 changes: 15 additions & 0 deletions engine/src/models/user/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,21 @@ use super::team::Team;
pub struct User {
pub user_id: String,
pub name: String,
pub avatar_url: Option<String>,
#[oai(skip)]
pub password: String,
pub created_at: DateTime<Utc>,
pub admin: Option<bool>,
}

#[derive(Debug, Serialize, Deserialize, Object, Clone)]
pub struct UserMinimal {
pub user_id: String,
pub name: String,
pub avatar_url: Option<String>,
pub admin: Option<bool>,
}

impl User {
pub async fn new(
db: &Database,
Expand Down Expand Up @@ -94,6 +103,12 @@ impl User {
.await
}

pub async fn get_all_minimal(db: &Database) -> Result<Vec<UserMinimal>, sqlx::Error> {
query_as!(UserMinimal, "SELECT user_id, name, avatar_url, admin FROM users")
.fetch_all(&db.pool)
.await
}

pub async fn can_bootstrap(db: &Database) -> Result<bool, sqlx::Error> {
Ok(query_scalar!("SELECT COUNT(*) FROM users")
.fetch_one(&db.pool)
Expand Down
17 changes: 16 additions & 1 deletion engine/src/routes/user/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use tracing::info;

use crate::{
middlewares::auth::UserAuth,
models::user::User,
models::user::{User, UserMinimal},
routes::{error::HttpError, ApiTags},
state::State,
};
Expand All @@ -26,6 +26,21 @@ impl UserApi {
Ok(Json(user))
}

#[oai(path = "/user/all", method = "get", tag = "ApiTags::User")]
pub async fn get_all_users(
&self,
state: Data<&State>,
auth: UserAuth,
) -> Result<Json<Vec<UserMinimal>>> {
auth.required()?;

let users = User::get_all_minimal(&state.database)
.await
.map_err(HttpError::from)?;

Ok(Json(users))
}

#[oai(path = "/user/:user_id", method = "get", tag = "ApiTags::User")]
pub async fn get_user_by_id(
&self,
Expand Down
43 changes: 43 additions & 0 deletions web/src/api/schema.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,40 @@ export type paths = {
patch?: never;
trace?: never;
};
"/user/all": {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
get: {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
requestBody?: never;
responses: {
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json; charset=utf-8": components["schemas"]["UserMinimal"][];
};
};
};
};
put?: never;
post?: never;
delete?: never;
options?: never;
head?: never;
patch?: never;
trace?: never;
};
"/user/{user_id}": {
parameters: {
query?: never;
Expand Down Expand Up @@ -1020,6 +1054,7 @@ export type components = {
team_id: string;
owner_id: string;
name: string;
avatar_url?: string;
/** Format: date-time */
created_at: string;
};
Expand All @@ -1041,10 +1076,18 @@ export type components = {
User: {
user_id: string;
name: string;
avatar_url?: string;
/** Format: date-time */
created_at: string;
admin?: boolean;
};
/** UserMinimal */
UserMinimal: {
user_id: string;
name: string;
avatar_url?: string;
admin?: boolean;
};
/** UserTeamInvite */
UserTeamInvite: {
invite_id: string;
Expand Down

0 comments on commit 7b40295

Please sign in to comment.