Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
prplnorangesoda committed Sep 12, 2024
2 parents e14bc02 + add7ff1 commit d0147d4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 22 deletions.
2 changes: 1 addition & 1 deletion backend-src/apiv1/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@ pub async fn post_users_team(
let user_team = user_team.into_inner();
// fetch the team to get its id
let team = db::get_team_from_id(&client, user_team.team_id).await?;
let users = db::get_team_players(&client, team).await?;
let users = db::get_team_players(&client, &team).await?;
Ok(HttpResponse::Ok().json(users))
}
45 changes: 30 additions & 15 deletions backend-src/apiv1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use crate::models::MiniUser;
use crate::models::User;
use crate::steamapi;
use crate::PlayerSummaryAccess;
use actix_web::{get, post, web, Error, HttpResponse, Responder};
use actix_web::{get, web, Error, HttpResponse, Responder};
use deadpool_postgres::{Client, Pool};
use serde::Deserialize;
use serde::Serialize;
use std::collections::HashMap;

pub mod admin;
Expand Down Expand Up @@ -57,7 +59,7 @@ pub async fn get_league(
}

/// All parameters that a valid openid request should have.
static OPENID_NECESSARY_PARAMETERS: &'static [&'static str] = &[
static OPENID_NECESSARY_PARAMETERS: &[&str] = &[
"openid.ns",
"openid.claimed_id",
"openid.signed",
Expand All @@ -77,7 +79,7 @@ pub async fn openid_landing(
for key in OPENID_NECESSARY_PARAMETERS {
// because key is &&str, we have to dereference it to a pure &str
// in order for it to not yell at us in compilation
if let None = inner.get(*key) {
if inner.contains_key(*key) {
log::warn!("A malformed OpenId landing was received: {inner:?}");
return Ok(HttpResponse::BadRequest()
.body("Your openid landing was malformed in some way. Report this!"));
Expand Down Expand Up @@ -228,11 +230,8 @@ pub async fn add_user_with_steamid(

if let Some(rootid) = &state.root_user_steamid {
if rootid == steamid {
match db::set_super_user(db_client, &add_user_resp).await {
Ok(user) => {
log::info!("Set super user {user:?}")
}
Err(_) => {}
if let Ok(user) = db::set_super_user(db_client, &add_user_resp).await {
log::info!("Set super user {user:?}")
};
}
}
Expand All @@ -249,19 +248,35 @@ async fn get_all_leagues(state: web::Data<AppState>) -> Result<HttpResponse, Err
Ok(HttpResponse::Ok().json(leagues))
}

#[derive(Serialize, Deserialize)]
struct TeamResponse {
pub id: i64,
pub leagueid: i64,
pub team_name: String,
pub players: Vec<User>,
}

#[get("/api/v1/teams/{team_id}")]
async fn get_team(path: web::Path<u32>) -> impl Responder {
log::debug!("GET request at /teams/id");
async fn get_team(state: web::Data<AppState>, path: web::Path<i64>) -> Result<HttpResponse, Error> {
log::debug!("GET request at /api/v1/teams/{path}");
let team_id = path.into_inner();
log::trace!("Getting info for team id {team_id}");
if team_id != 3 {
return HttpResponse::NotFound().body("Team id not found");
if team_id < 0 {
return Err(MyError::NotFound.into());
}
HttpResponse::Ok().body(format!("Team {team_id}"))
let client = state.pool.get().await.map_err(MyError::PoolError)?;
let team = db::get_team_from_id(&client, team_id).await?;
let players = db::get_team_players(&client, &team).await?;
let resp = TeamResponse {
id: team.id,
leagueid: team.leagueid,
team_name: team.team_name,
players,
};
Ok(HttpResponse::Ok().json(resp))
}

#[get("/login")]
async fn get_openid(data: web::Data<AppState>) -> impl Responder {
async fn get_openid(data: web::Data<AppState>) -> HttpResponse {
log::debug!("GET request at /login");
HttpResponse::Found()
.insert_header(("Location", data.into_inner().steam_auth_url.clone()))
Expand Down
8 changes: 6 additions & 2 deletions backend-src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tokio_pg_mapper::FromTokioPostgresRow;
use crate::{
authorization::create_authorization_for_user,
errors::MyError,
models::{Authorization, League, MiniLeague, MiniUser, Team, User, UserTeam},
models::{Authorization, League, MiniLeague, MiniTeam, MiniUser, Team, User, UserTeam},
permission::UserPermission,
};

Expand Down Expand Up @@ -34,7 +34,7 @@ pub async fn get_team_from_id(client: &Client, team_id: i64) -> Result<Team, MyE
results
}

pub async fn get_team_players(client: &Client, team: Team) -> Result<Vec<User>, MyError> {
pub async fn get_team_players(client: &Client, team: &Team) -> Result<Vec<User>, MyError> {
let _stmt = "SELECT $table_fields FROM userTeam WHERE teamid=$1 AND leagueid=$2";
let _stmt = _stmt.replace("$table_fields", &UserTeam::sql_table_fields());
let stmt = client.prepare(&_stmt).await.unwrap();
Expand Down Expand Up @@ -126,6 +126,10 @@ pub async fn get_leagues(client: &Client) -> Result<Vec<League>, MyError> {
Ok(results)
}

// pub async fn add_team(client: &Client, league: League, team: &MiniTeam) -> Result<Team, MyError> {
// let _stmt = "INSERT INTO teams(leagueid, teamname)"
// }

pub async fn add_league(client: &Client, league: MiniLeague) -> Result<League, MyError> {
let _stmt = "INSERT INTO leagues(name) VALUES ($1) RETURNING $table_fields";
let _stmt = _stmt.replace("$table_fields", &League::sql_table_fields());
Expand Down
7 changes: 7 additions & 0 deletions backend-src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ pub struct League {
pub name: String,
}

#[derive(Debug, Deserialize, PostgresMapper, Serialize)]
#[pg_mapper(table = "teams")]
pub struct MiniTeam {
pub leagueid: i64,
pub team_name: String,
}

#[derive(Debug, Deserialize, PostgresMapper, Serialize)]
#[pg_mapper(table = "teams")]
pub struct Team {
Expand Down
2 changes: 1 addition & 1 deletion backend-src/permission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl User {
if self.check_has_permission(permission) {
return;
}
self.permissions = self.permissions + permission as i64;
self.permissions += permission as i64;
}
}

Expand Down
3 changes: 0 additions & 3 deletions sql/test_users.sql

This file was deleted.

0 comments on commit d0147d4

Please sign in to comment.