gamenight-participants #6

Merged
Roflin merged 7 commits from gamenight-participants into main 2022-05-30 21:32:48 +02:00
7 changed files with 139 additions and 27 deletions
Showing only changes of commit 2ba2026e21 - Show all commits

25
backend/Cargo.lock generated
View File

@ -518,6 +518,7 @@ checksum = "f73fe65f54d1e12b726f517d3e2135ca3125a437b6d998caf1962961f7172d9e"
dependencies = [ dependencies = [
"futures-channel", "futures-channel",
"futures-core", "futures-core",
"futures-executor",
"futures-io", "futures-io",
"futures-sink", "futures-sink",
"futures-task", "futures-task",
@ -540,12 +541,34 @@ version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c09fd04b7e4073ac7156a9539b57a484a8ea920f79c7c675d05d289ab6110d3" checksum = "0c09fd04b7e4073ac7156a9539b57a484a8ea920f79c7c675d05d289ab6110d3"
[[package]]
name = "futures-executor"
version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9420b90cfa29e327d0429f19be13e7ddb68fa1cccb09d65e5706b8c7a749b8a6"
dependencies = [
"futures-core",
"futures-task",
"futures-util",
]
[[package]] [[package]]
name = "futures-io" name = "futures-io"
version = "0.3.21" version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc4045962a5a5e935ee2fdedaa4e08284547402885ab326734432bed5d12966b" checksum = "fc4045962a5a5e935ee2fdedaa4e08284547402885ab326734432bed5d12966b"
[[package]]
name = "futures-macro"
version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33c1e13800337f4d4d7a316bf45a567dbcb6ffe087f16424852d97e97a91f512"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "futures-sink" name = "futures-sink"
version = "0.3.21" version = "0.3.21"
@ -567,6 +590,7 @@ dependencies = [
"futures-channel", "futures-channel",
"futures-core", "futures-core",
"futures-io", "futures-io",
"futures-macro",
"futures-sink", "futures-sink",
"futures-task", "futures-task",
"memchr", "memchr",
@ -584,6 +608,7 @@ dependencies = [
"diesel", "diesel",
"diesel-derive-enum", "diesel-derive-enum",
"diesel_migrations", "diesel_migrations",
"futures",
"jsonwebtoken", "jsonwebtoken",
"local-ip-address", "local-ip-address",
"password-hash", "password-hash",

View File

@ -22,4 +22,5 @@ jsonwebtoken = "8.1"
validator = { version = "0.14", features = ["derive"] } validator = { version = "0.14", features = ["derive"] }
rocket_cors = "0.6.0-alpha1" rocket_cors = "0.6.0-alpha1"
local-ip-address = "0.4" local-ip-address = "0.4"
uuid = { version = "0.8.2", features = ["v4", "serde"] } uuid = { version = "0.8.2", features = ["v4", "serde"] }
futures = "0.3.21"

View File

@ -1,3 +1,4 @@
use crate::schema::DatabaseError;
use crate::schema::gamenight::*; use crate::schema::gamenight::*;
use crate::schema::users::*; use crate::schema::users::*;
use crate::schema::DbConn; use crate::schema::DbConn;
@ -12,6 +13,7 @@ use serde::{Deserialize, Serialize};
use std::borrow::Cow; use std::borrow::Cow;
use uuid::Uuid; use uuid::Uuid;
use validator::ValidateArgs; use validator::ValidateArgs;
use futures::future::join_all;
#[derive(Debug, Responder)] #[derive(Debug, Responder)]
pub enum ApiResponseVariant { pub enum ApiResponseVariant {
@ -27,7 +29,7 @@ struct ApiResponse {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
user: Option<UserWithToken>, user: Option<UserWithToken>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
gamenights: Option<Vec<GameNight>>, gamenights: Option<Vec<GamenightOutput>>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
games: Option<Vec<Game>>, games: Option<Vec<Game>>,
} }
@ -67,7 +69,7 @@ impl ApiResponse {
} }
} }
fn gamenight_response(gamenights: Vec<GameNight>) -> Self { fn gamenight_response(gamenights: Vec<GamenightOutput>) -> Self {
Self { Self {
result: Self::SUCCES_RESULT, result: Self::SUCCES_RESULT,
message: None, message: None,
@ -130,14 +132,34 @@ impl<'r> FromRequest<'r> for User {
} }
} }
#[derive(Debug, Serialize, Deserialize)]
pub struct GamenightOutput {
#[serde(flatten)]
gamenight: Gamenight,
game_list: Vec<Game>,
}
#[get("/gamenights")] #[get("/gamenights")]
pub async fn gamenights(conn: DbConn, _user: User) -> ApiResponseVariant { pub async fn gamenights(conn: DbConn, _user: User) -> ApiResponseVariant {
match get_all_gamenights(conn).await { let gamenights = match get_all_gamenights(&conn).await {
Ok(gamenights) => { Ok(result) => result,
ApiResponseVariant::Value(json!(ApiResponse::gamenight_response(gamenights))) Err(err) => return ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string())))
} };
Err(error) => ApiResponseVariant::Value(json!(ApiResponse::error(error.to_string()))),
} let conn_ref = &conn;
let game_results : Result<Vec<GamenightOutput>, DatabaseError> = join_all(gamenights.iter().map(|gn| async move {
let games = get_games_of_gamenight(conn_ref, gn.id).await?;
Ok(GamenightOutput{
gamenight: gn.clone(),
game_list: games
})
})).await.into_iter().collect();
match game_results {
Ok(result) => ApiResponseVariant::Value(json!(ApiResponse::gamenight_response(result))),
Err(err) => ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string())))
}
} }
#[get("/gamenights", rank = 2)] #[get("/gamenights", rank = 2)]
@ -146,16 +168,16 @@ pub async fn gamenights_unauthorized() -> ApiResponseVariant {
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GameNightInput { pub struct GamenightInput {
pub name: String, pub name: String,
pub datetime: String, pub datetime: String,
pub owner_id: Option<Uuid>, pub owner_id: Option<Uuid>,
pub game_list: Vec<Game>, pub game_list: Vec<Game>,
} }
impl Into<GameNight> for GameNightInput { impl Into<Gamenight> for GamenightInput {
fn into(self) -> GameNight { fn into(self) -> Gamenight {
GameNight { Gamenight {
id: Uuid::new_v4(), id: Uuid::new_v4(),
name: self.name, name: self.name,
datetime: self.datetime, datetime: self.datetime,
@ -168,7 +190,7 @@ impl Into<GameNight> for GameNightInput {
pub async fn gamenights_post_json( pub async fn gamenights_post_json(
conn: DbConn, conn: DbConn,
user: User, user: User,
gamenight_json: Json<GameNightInput>, gamenight_json: Json<GamenightInput>,
) -> ApiResponseVariant { ) -> ApiResponseVariant {
let mut gamenight = gamenight_json.into_inner(); let mut gamenight = gamenight_json.into_inner();
gamenight.owner_id = Some(user.id); gamenight.owner_id = Some(user.id);
@ -205,7 +227,7 @@ pub async fn gamenights_post_json_unauthorized() -> ApiResponseVariant {
pub async fn gamenights_delete_json( pub async fn gamenights_delete_json(
conn: DbConn, conn: DbConn,
user: User, user: User,
delete_gamenight_json: Json<DeleteGameNight>, delete_gamenight_json: Json<DeleteGamenight>,
) -> ApiResponseVariant { ) -> ApiResponseVariant {
if user.role == Role::Admin { if user.role == Role::Admin {
if let Err(error) = delete_gamenight(&conn, delete_gamenight_json.game_id).await { if let Err(error) = delete_gamenight(&conn, delete_gamenight_json.game_id).await {

View File

@ -40,9 +40,9 @@ pub struct Game {
pub name: String, pub name: String,
} }
#[derive(Serialize, Deserialize, Debug, Queryable, Insertable)] #[derive(Serialize, Deserialize, Debug, Queryable, Insertable, Clone)]
#[table_name = "gamenight"] #[table_name = "gamenight"]
pub struct GameNight { pub struct Gamenight {
pub id: Uuid, pub id: Uuid,
pub name: String, pub name: String,
pub datetime: String, pub datetime: String,
@ -65,7 +65,7 @@ pub struct GamenightParticipantsEntry {
} }
#[derive(Serialize, Deserialize, Debug, Queryable)] #[derive(Serialize, Deserialize, Debug, Queryable)]
pub struct DeleteGameNight { pub struct DeleteGamenight {
pub game_id: Uuid, pub game_id: Uuid,
} }
@ -74,13 +74,13 @@ pub struct GamenightId {
pub gamenight_id: Uuid, pub gamenight_id: Uuid,
} }
pub async fn get_all_gamenights(conn: DbConn) -> Result<Vec<GameNight>, DatabaseError> { pub async fn get_all_gamenights(conn: &DbConn) -> Result<Vec<Gamenight>, DatabaseError> {
Ok(conn.run(|c| gamenight::table.load::<GameNight>(c)).await?) Ok(conn.run(|c| gamenight::table.load::<Gamenight>(c)).await?)
} }
pub async fn insert_gamenight( pub async fn insert_gamenight(
conn: &DbConn, conn: &DbConn,
new_gamenight: GameNight, new_gamenight: Gamenight,
game_list: Vec<Game>, game_list: Vec<Game>,
) -> Result<Uuid, DatabaseError> { ) -> Result<Uuid, DatabaseError> {
Ok(conn Ok(conn
@ -109,7 +109,7 @@ pub async fn insert_gamenight(
) )
} }
pub async fn get_gamenight(conn: &DbConn, game_id: Uuid) -> Result<GameNight, DatabaseError> { pub async fn get_gamenight(conn: &DbConn, game_id: Uuid) -> Result<Gamenight, DatabaseError> {
Ok(conn Ok(conn
.run(move |c| gamenight::table.find(game_id).first(c)) .run(move |c| gamenight::table.find(game_id).first(c))
.await?) .await?)
@ -125,6 +125,20 @@ pub async fn get_all_known_games(conn: &DbConn) -> Result<Vec<Game>, DatabaseErr
Ok(conn.run(|c| known_games::table.load::<Game>(c)).await?) Ok(conn.run(|c| known_games::table.load::<Game>(c)).await?)
} }
pub async fn get_games_of_gamenight(conn: &DbConn, gamenight_id: Uuid) -> Result<Vec<Game>, DatabaseError> {
Ok(conn.run::<_, Result<Vec<Game>, _>>(move |c| {
let linked_game_ids: Vec<GamenightGameListEntry> = gamenight_gamelist::table
.filter(gamenight_gamelist::gamenight_id.eq(gamenight_id))
.load::<GamenightGameListEntry>(c)?;
linked_game_ids.iter().map(|l| {
known_games::table
.filter(known_games::id.eq(l.game_id))
.first::<Game>(c)
}).collect()
}).await?)
}
pub async fn add_game(conn: &DbConn, game: Game) -> Result<usize, DatabaseError> { pub async fn add_game(conn: &DbConn, game: Game) -> Result<usize, DatabaseError> {
Ok(conn Ok(conn
.run(|c| { .run(|c| {

View File

@ -2,8 +2,9 @@ import './App.css';
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect } from 'react';
import MenuBar from './components/MenuBar'; import MenuBar from './components/MenuBar';
import Login from './components/Login'; import Login from './components/Login';
import Gamenights from './components/Gamenights' import Gamenights from './components/Gamenights';
import AddGameNight from './components/AddGameNight' import AddGameNight from './components/AddGameNight';
import Gamenight from './components/Gamenight';
const localStorageUserKey = 'user'; const localStorageUserKey = 'user';
@ -13,6 +14,7 @@ function App() {
const [gamenights, setGamenights] = useState([]); const [gamenights, setGamenights] = useState([]);
const [flashData, setFlashData] = useState({}); const [flashData, setFlashData] = useState({});
const [games, setGames] = useState([]); const [games, setGames] = useState([]);
const [activeGamenight, setActiveGamenight] = useState(null);
const handleLogin = (input) => { const handleLogin = (input) => {
const requestOptions = { const requestOptions = {
@ -94,6 +96,9 @@ function App() {
setUser(JSON.parse(localStorage.getItem(localStorageUserKey))); setUser(JSON.parse(localStorage.getItem(localStorageUserKey)));
}, []); }, []);
console.log(activeGamenight);
if(user === null) { if(user === null) {
return ( return (
<div className="App"> <div className="App">
@ -101,11 +106,29 @@ function App() {
</div> </div>
); );
} else { } else {
let mainview;
if(activeGamenight === null) {
mainview = <>
<AddGameNight user={user} games={games} setFlash={setFlash} refetchGamenights={refetchGamenights} />
<Gamenights
user={user}
setFlash={setFlash}
refetchGamenights={refetchGamenights}
gamenights={gamenights}
setActiveGamenight={setActiveGamenight}/>
</>
} else {
mainview = <Gamenight
gamenight={activeGamenight}
onDismis={setActiveGamenight}
/>
}
return ( return (
<> <>
<MenuBar user={user} onLogout={onLogout} /> <MenuBar user={user} onLogout={onLogout} />
<AddGameNight user={user} games={games} setFlash={setFlash} refetchGamenights={refetchGamenights} /> {mainview}
<Gamenights user={user} setFlash={setFlash} refetchGamenights={refetchGamenights} gamenights={gamenights} />
</> </>
); );
} }

View File

@ -0,0 +1,27 @@
import * as React from 'react';
function Gamenight(props) {
console.log(props.gamenight);
let games = props.gamenight.game_list.map(g =>
(
<li>
<span>{g.name}</span>
</li>
)
);
return (
<div>
<button onClick={props.onDismis(null)}>x</button>
<h3>{props.gamenight.name}</h3>
<span>{props.gamenight.datetime}</span>
<ul>
{games}
</ul>
</div>
)
}
export default Gamenight

View File

@ -33,7 +33,7 @@ function Gamenights(props) {
let gamenights = props.gamenights.map(g => let gamenights = props.gamenights.map(g =>
( (
<li> <li onClick={(e) => {console.log(g); props.setActiveGamenight(g);}}>
<span>{g.name}</span> <span>{g.name}</span>
{(props.user.id === g.owner_id || props.user.role === "Admin") && {(props.user.id === g.owner_id || props.user.role === "Admin") &&
<button onClick={() =>DeleteGameNight(g.id, g.owner)}> <button onClick={() =>DeleteGameNight(g.id, g.owner)}>