Skip to content

Commit

Permalink
Good ui progress
Browse files Browse the repository at this point in the history
  • Loading branch information
fatfingers23 committed Nov 12, 2023
1 parent ae874cc commit 643e816
Show file tree
Hide file tree
Showing 17 changed files with 526 additions and 165 deletions.
74 changes: 60 additions & 14 deletions trackscape-discord-api/src/controllers/clan_controller.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use actix_web::{get, web, Error, HttpResponse, Scope};
use bson::Bson::ObjectId;

Check warning on line 2 in trackscape-discord-api/src/controllers/clan_controller.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `bson::Bson::ObjectId`

Check warning on line 2 in trackscape-discord-api/src/controllers/clan_controller.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `bson::Bson::ObjectId`
use log::{error, info};
use serde::{Deserialize, Serialize};
use trackscape_discord_shared::database::clan_mates::ClanMates;
use std::str::FromStr;
use trackscape_discord_shared::database::clan_mates::{ClanMateModel, ClanMates};
use trackscape_discord_shared::database::guilds_db::RegisteredGuildModel;

Check warning on line 7 in trackscape-discord-api/src/controllers/clan_controller.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `trackscape_discord_shared::database::guilds_db::RegisteredGuildModel`

Check warning on line 7 in trackscape-discord-api/src/controllers/clan_controller.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `trackscape_discord_shared::database::guilds_db::RegisteredGuildModel`
use trackscape_discord_shared::database::BotMongoDb;
use web::Data;

Expand All @@ -11,6 +15,15 @@ struct ClanViewModel {
registered_members: u64,
}

#[derive(Deserialize, Serialize)]
struct ClanDetail {
id: String,
name: String,
discord_guild_id: String,
registered_members: u64,
members: Vec<ClanMateModel>,
}

#[get("/list")]
async fn list_clans(mongodb: Data<BotMongoDb>) -> Result<HttpResponse, Error> {
let result = mongodb.guilds.list_clans().await;
Expand All @@ -19,6 +32,9 @@ async fn list_clans(mongodb: Data<BotMongoDb>) -> Result<HttpResponse, Error> {
Ok(clans) => {
let mut view_models: Vec<ClanViewModel> = Vec::new();
for clan in clans {
if clan.clan_name.is_none() {
continue;
}
view_models.push(ClanViewModel {
id: clan.id.to_string(),
name: clan.clan_name.unwrap(),
Expand All @@ -29,25 +45,55 @@ async fn list_clans(mongodb: Data<BotMongoDb>) -> Result<HttpResponse, Error> {
.unwrap(),
});
}
// .map(async |clan| {
// return ClanViewModel {
// id: clan.id.to_string(),
// name: clan.clan_name.unwrap(),
// registered_members: mongodb
// .clan_mates
// .get_clan_member_count(clan.guild_id)
// .await
// .unwrap(),
// };
// })
// .collect();

Ok(HttpResponse::Ok().json(view_models))
}
Err(_) => Ok(HttpResponse::InternalServerError().body("Failed to list clans.")),
}
}

#[get("/{id}/detail")]
async fn detail(
mongodb: Data<BotMongoDb>,
path: web::Path<(String,)>,
) -> Result<HttpResponse, Error> {
info!("{:?}", path);
let id = path.into_inner().0;
let id = bson::oid::ObjectId::from_str(id.as_str()).unwrap();
let registered_guild_query = mongodb.guilds.get_by_id(id).await;
match registered_guild_query {
Ok(possible_registered_guild) => match possible_registered_guild {
None => {
return Ok(HttpResponse::NotFound().body("Clan not found."));
}
Some(registered_guild) => {
//return clan details

let detail = ClanDetail {
id: registered_guild.id.to_string(),
name: registered_guild.clan_name.unwrap(),
discord_guild_id: registered_guild.guild_id.to_string(),
registered_members: mongodb
.clan_mates
.get_clan_member_count(registered_guild.guild_id)
.await
.unwrap(),
members: mongodb
.clan_mates
.get_clan_mates_by_guild_id(registered_guild.guild_id)
.await
.unwrap(),
};
return Ok(HttpResponse::Ok().json(detail));
}
},
Err(err) => {
error!("Failed to get clan by id: {}", err);
return Ok(HttpResponse::BadRequest().body("There was an issue with the request"));
}
}
}

pub fn clan_controller() -> Scope {
web::scope("/clans").service(list_clans)
web::scope("/clans").service(list_clans).service(detail)
}
2 changes: 1 addition & 1 deletion trackscape-discord-bot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl EventHandler for Bot {
None => {}
Some(guild_id) => {
let guild_id = guild_id.0;
let guild = self.mongo_db.guilds.get_guild_by_discord_id(guild_id).await;
let guild = self.mongo_db.guilds.get_by_guild_id(guild_id).await;
match guild {
Ok(guild) => {
if guild.is_none() {
Expand Down
17 changes: 17 additions & 0 deletions trackscape-discord-shared/src/database/clan_mates.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::database::ClanMatesDb;
use anyhow::Error;
use async_trait::async_trait;
use futures::TryStreamExt;
use mockall::predicate::*;
use mockall::*;
use mongodb::bson::{doc, DateTime};
Expand All @@ -15,6 +16,7 @@ pub struct ClanMateModel {
pub player_name: String,
pub wom_player_id: Option<u64>,
pub previous_names: Vec<String>,
pub rank: Option<String>,
pub created_at: DateTime,
}

Expand All @@ -28,6 +30,7 @@ impl ClanMateModel {
wom_player_id,
previous_names: Vec::new(),
player_name,
rank: None,
created_at: DateTime::now(),
}
}
Expand Down Expand Up @@ -62,6 +65,8 @@ pub trait ClanMates {
) -> Result<Option<ClanMateModel>, anyhow::Error>;

async fn get_clan_member_count(&self, guild_id: u64) -> Result<u64, Error>;

async fn get_clan_mates_by_guild_id(&self, guild_id: u64) -> Result<Vec<ClanMateModel>, Error>;
}

#[async_trait]
Expand Down Expand Up @@ -137,4 +142,16 @@ impl ClanMates for ClanMatesDb {
let result = collection.count_documents(filter, None).await?;
Ok(result)
}

async fn get_clan_mates_by_guild_id(&self, guild_id: u64) -> Result<Vec<ClanMateModel>, Error> {
let collection = self
.db
.collection::<ClanMateModel>(ClanMateModel::COLLECTION_NAME);
let filter = doc! {
"guild_id": bson::to_bson(&guild_id).unwrap(),
};
let result = collection.find(filter, None).await?;
let clan_mates = result.try_collect().await?;
Ok(clan_mates)
}
}
28 changes: 15 additions & 13 deletions trackscape-discord-shared/src/database/guilds_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,6 @@ impl GuildsDb {
}
}

pub async fn get_guild_by_discord_id(
&self,
discord_id: u64,
) -> Result<Option<RegisteredGuildModel>, anyhow::Error> {
let collection = self
.db
.collection::<RegisteredGuildModel>(RegisteredGuildModel::COLLECTION_NAME);
let filter = doc! {"guild_id": bson::to_bson(&discord_id).unwrap()};
Ok(collection
.find_one(filter, None)
.await
.expect("Failed to find document for the Discord guild."))
}
pub async fn get_guild_by_code_and_clan_name(
&self,
code: String,
Expand Down Expand Up @@ -226,4 +213,19 @@ impl GuildsDb {
Err(e) => Err(anyhow::Error::new(e)),
};
}

pub async fn get_by_id(
&self,
id: bson::oid::ObjectId,
) -> Result<Option<RegisteredGuildModel>, mongodb::error::Error> {
let collection = self
.db
.collection::<RegisteredGuildModel>(RegisteredGuildModel::COLLECTION_NAME);
let filter = doc! { "_id": bson::to_bson(&id).unwrap()};
let result = collection.find_one(filter.clone(), None).await;
return match result {
Ok(possible_guild) => Ok(possible_guild),
Err(e) => Err(e),
};
}
}
30 changes: 16 additions & 14 deletions trackscape-discord-ui/package-lock.json

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

2 changes: 1 addition & 1 deletion trackscape-discord-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@vue/test-utils": "^2.4.1",
"@vue/tsconfig": "^0.4.0",
"autoprefixer": "^10.4.15",
"daisyui": "^3.7.4",
"daisyui": "^4.0.1",
"eslint": "^8.49.0",
"eslint-plugin-vue": "^9.17.0",
"jsdom": "^22.1.0",
Expand Down
7 changes: 5 additions & 2 deletions trackscape-discord-ui/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import NavMenu from "@/components/nav-menu.vue";
<template>
<nav-menu/>
<main class="bg-base-200">
<RouterView />
<div class="container mx-auto md:p-8 p-3 min-h-screen">
<RouterView />
</div>
<footer class="
footer
p-10
Expand All @@ -16,7 +18,8 @@ import NavMenu from "@/components/nav-menu.vue";
<div>
<div class="w-10 rounded-full">
<img class="rounded-full"
src="@/assets/img/Trackscape_Logo_icon.png"/>
src="@/assets/img/Trackscape_Logo_icon.png"
alt="Trackscape logo"/>
</div>
<p>Trackscape.<br>OSRS tooling for clans!</p>
</div>
Expand Down
78 changes: 42 additions & 36 deletions trackscape-discord-ui/src/components/DataTable.vue
Original file line number Diff line number Diff line change
@@ -1,42 +1,48 @@
<!--<script setup lang="ts">-->
<!-- import type {PropType} from "vue";-->
<script setup lang="ts">
import type {PropType} from "vue";
<!-- const props = defineProps({-->
<!-- data: {-->
<!-- type: Array as PropType<any[]>,-->
<!-- required: true-->
<!-- },-->
<!-- columns: {-->
<!-- type: Array as PropType<any[]>,-->
<!-- required: true-->
<!-- },-->
<!-- options: {-->
<!-- type: Object as PropType<any>,-->
<!-- default: () => ({})-->
<!-- }-->
<!-- })-->
<!--</script>-->
type column = {
name: string,
key: string
}
<!--<template>-->
<!-- <table class="table">-->
<!-- &lt;!&ndash; head &ndash;&gt;-->
<!-- <thead>-->
<!-- <tr>-->
<!-- <th>Name</th>-->
<!-- </tr>-->
<!-- </thead>-->
<!-- <tbody>-->
const props = defineProps({
data: {
type: Array as PropType<any[]>,
required: true
},
columns: {
type: Array as PropType<column[]>,
required: true
},
<!-- <tr v-for="(clan, index) in data"-->
<!-- :key="index">-->
<!-- <th>{{clan.name}}</th>-->
})
</script>

<!-- </tr>-->
<template>
<table class="table">
<thead>
<tr>
<th v-for="(column, index) in props.columns"
:key="index">{{ column.name }}</th>
</tr>
</thead>
<tbody>
<tr v-for="(item, index) in data"
:key="index">
<th v-for="(column, index) in props.columns"
:key="index">
<slot name="row-item"
:column="column"
:item="item">
{{ item[column.key] }}
</slot>
</th>
</tr>
</tbody>
</table>
</template>

<!-- </tbody>-->
<!-- </table>-->
<!--</template>-->
<style scoped>
<!--<style scoped>-->

<!--</style>-->
</style>
Loading

0 comments on commit 643e816

Please sign in to comment.