Gamenights also return their game list.

This commit is contained in:
Dennis Brentjes 2022-05-29 00:22:30 +02:00
parent 86cdbedd41
commit 2ba2026e21
7 changed files with 139 additions and 27 deletions

25
backend/Cargo.lock generated
View File

@ -518,6 +518,7 @@ checksum = "f73fe65f54d1e12b726f517d3e2135ca3125a437b6d998caf1962961f7172d9e"
dependencies = [
"futures-channel",
"futures-core",
"futures-executor",
"futures-io",
"futures-sink",
"futures-task",
@ -540,12 +541,34 @@ version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "futures-io"
version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "futures-sink"
version = "0.3.21"
@ -567,6 +590,7 @@ dependencies = [
"futures-channel",
"futures-core",
"futures-io",
"futures-macro",
"futures-sink",
"futures-task",
"memchr",
@ -584,6 +608,7 @@ dependencies = [
"diesel",
"diesel-derive-enum",
"diesel_migrations",
"futures",
"jsonwebtoken",
"local-ip-address",
"password-hash",

View File

@ -22,4 +22,5 @@ jsonwebtoken = "8.1"
validator = { version = "0.14", features = ["derive"] }
rocket_cors = "0.6.0-alpha1"
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::users::*;
use crate::schema::DbConn;
@ -12,6 +13,7 @@ use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use uuid::Uuid;
use validator::ValidateArgs;
use futures::future::join_all;
#[derive(Debug, Responder)]
pub enum ApiResponseVariant {
@ -27,7 +29,7 @@ struct ApiResponse {
#[serde(skip_serializing_if = "Option::is_none")]
user: Option<UserWithToken>,
#[serde(skip_serializing_if = "Option::is_none")]
gamenights: Option<Vec<GameNight>>,
gamenights: Option<Vec<GamenightOutput>>,
#[serde(skip_serializing_if = "Option::is_none")]
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 {
result: Self::SUCCES_RESULT,
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")]
pub async fn gamenights(conn: DbConn, _user: User) -> ApiResponseVariant {
match get_all_gamenights(conn).await {
Ok(gamenights) => {
ApiResponseVariant::Value(json!(ApiResponse::gamenight_response(gamenights)))
}
Err(error) => ApiResponseVariant::Value(json!(ApiResponse::error(error.to_string()))),
}
let gamenights = match get_all_gamenights(&conn).await {
Ok(result) => result,
Err(err) => return ApiResponseVariant::Value(json!(ApiResponse::error(err.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)]
@ -146,16 +168,16 @@ pub async fn gamenights_unauthorized() -> ApiResponseVariant {
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GameNightInput {
pub struct GamenightInput {
pub name: String,
pub datetime: String,
pub owner_id: Option<Uuid>,
pub game_list: Vec<Game>,
}
impl Into<GameNight> for GameNightInput {
fn into(self) -> GameNight {
GameNight {
impl Into<Gamenight> for GamenightInput {
fn into(self) -> Gamenight {
Gamenight {
id: Uuid::new_v4(),
name: self.name,
datetime: self.datetime,
@ -168,7 +190,7 @@ impl Into<GameNight> for GameNightInput {
pub async fn gamenights_post_json(
conn: DbConn,
user: User,
gamenight_json: Json<GameNightInput>,
gamenight_json: Json<GamenightInput>,
) -> ApiResponseVariant {
let mut gamenight = gamenight_json.into_inner();
gamenight.owner_id = Some(user.id);
@ -205,7 +227,7 @@ pub async fn gamenights_post_json_unauthorized() -> ApiResponseVariant {
pub async fn gamenights_delete_json(
conn: DbConn,
user: User,
delete_gamenight_json: Json<DeleteGameNight>,
delete_gamenight_json: Json<DeleteGamenight>,
) -> ApiResponseVariant {
if user.role == Role::Admin {
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,
}
#[derive(Serialize, Deserialize, Debug, Queryable, Insertable)]
#[derive(Serialize, Deserialize, Debug, Queryable, Insertable, Clone)]
#[table_name = "gamenight"]
pub struct GameNight {
pub struct Gamenight {
pub id: Uuid,
pub name: String,
pub datetime: String,
@ -65,7 +65,7 @@ pub struct GamenightParticipantsEntry {
}
#[derive(Serialize, Deserialize, Debug, Queryable)]
pub struct DeleteGameNight {
pub struct DeleteGamenight {
pub game_id: Uuid,
}
@ -74,13 +74,13 @@ pub struct GamenightId {
pub gamenight_id: Uuid,
}
pub async fn get_all_gamenights(conn: DbConn) -> Result<Vec<GameNight>, DatabaseError> {
Ok(conn.run(|c| gamenight::table.load::<GameNight>(c)).await?)
pub async fn get_all_gamenights(conn: &DbConn) -> Result<Vec<Gamenight>, DatabaseError> {
Ok(conn.run(|c| gamenight::table.load::<Gamenight>(c)).await?)
}
pub async fn insert_gamenight(
conn: &DbConn,
new_gamenight: GameNight,
new_gamenight: Gamenight,
game_list: Vec<Game>,
) -> Result<Uuid, DatabaseError> {
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
.run(move |c| gamenight::table.find(game_id).first(c))
.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?)
}
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> {
Ok(conn
.run(|c| {

View File

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

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 =>
(
<li>
<li onClick={(e) => {console.log(g); props.setActiveGamenight(g);}}>
<span>{g.name}</span>
{(props.user.id === g.owner_id || props.user.role === "Admin") &&
<button onClick={() =>DeleteGameNight(g.id, g.owner)}>