Skip to content

Commit

Permalink
tournament creation fully complete
Browse files Browse the repository at this point in the history
  • Loading branch information
8bitcoffee committed Jan 17, 2024
1 parent 43046eb commit 7df2ad7
Show file tree
Hide file tree
Showing 10 changed files with 297 additions and 54 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ Used in stretch goals
|--------------|---------------------------------|---------------|--------------|---------------------|
|| Component scaffolding | 2 | 12/24 | Base |
|| Make the login page. | 4 | 12/27 | Base |
| | Router and query building | 8 | 1/3 | Base |
| | Initial league creation process | 8 | 1/3 | Base |
| | Router and query building | 8 | 1/3 | Base |
| | Initial league creation process | 8 | 1/3 | Base |
|| Stats and Team views | 8 | 1/6 | Base |
|| Scoring app - Set lineup | 4 | 1/8 | Base |
|| Scoring app - Round view | 4 | 1/8 | Base |
|| Scoring app - Match view | 4 | 1/10 | Base |
| | Conditional styling | 4 | 1/12 | Base |
| | Conditional styling | 4 | 1/12 | Base |
|| Comments and README | 4 | 1/16 | Base |
|| Calendar view | 8 | 1/18 | Stretch |
|| Google Account sign-in | 8 | 1/21 | Stretch |
Expand Down
57 changes: 34 additions & 23 deletions database_structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ CREATE TABLE "tournaments"(
"tournament_name" VARCHAR(255) NOT NULL,
"bracket" BOOLEAN NOT NULL,
"num_teams" INT,
"num_rounds" INT,
"num_matchups" INT,
"playoffs" BOOLEAN DEFAULT FALSE,
"playoff_num" INT DEFAULT 0,
"playoff_num" INT DEFAULT NULL,
"league_id" INT NOT NULL,
"complete" BOOLEAN DEFAULT FALSE
);
Expand All @@ -93,13 +93,6 @@ CREATE TABLE "teams_leagues" (
"league_id" BIGINT NOT NULL
);

-- -- Junction table for showing which league is having a tournament
-- CREATE TABLE "leagues_tournaments"(
-- "id" SERIAL PRIMARY KEY,
-- "league_id" BIGINT NOT NULL,
-- "tournament_id" BIGINT NOT NULL
-- );

-- Table for defining different scoresheet types
CREATE TABLE "scoresheets"(
"id" SERIAL PRIMARY KEY,
Expand All @@ -112,15 +105,19 @@ CREATE TABLE "scoresheets"(
-- Table for the overall matchup between two teams
CREATE TABLE "matchups"(
"id" SERIAL PRIMARY KEY,
"date" TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL,
"location_id" BIGINT NOT NULL,
"home_team_id" BIGINT NOT NULL,
"away_team_id" BIGINT NOT NULL,
"date" DATE NOT NULL,
"location_id" BIGINT DEFAULT 1,
"home_team_id" BIGINT,
"away_team_id" BIGINT,
"home_team_total" NUMERIC DEFAULT 0,
"away_team_total" NUMERIC DEFAULT 0,
"winner" BIGINT,
"home_lineup_set" BOOLEAN NOT NULL DEFAULT 'False',
"away_lineup_set" BOOLEAN NOT NULL DEFAULT 'False',
"home_lineup_set" BOOLEAN DEFAULT FALSE,
"away_lineup_set" BOOLEAN DEFAULT FALSE,
"confirmed_home" BOOLEAN DEFAULT FALSE,
"confirmed_away" BOOLEAN DEFAULT FALSE,
"confirmed" BOOLEAN DEFAULT FALSE,
"bye" BOOLEAN DEFAULT FALSE,
"tournament_id" BIGINT NOT NULL
);

Expand All @@ -130,8 +127,8 @@ CREATE TABLE "rounds"(
"matchup_id" BIGINT NOT NULL,
"home_score" NUMERIC,
"away_score" NUMERIC,
"home_handicap" NUMERIC NOT NULL,
"away_handicap" NUMERIC NOT NULL,
"home_handicap" NUMERIC,
"away_handicap" NUMERIC,
"home_total" NUMERIC,
"away_total" NUMERIC,
"round_winner" BIGINT,
Expand All @@ -141,14 +138,28 @@ CREATE TABLE "rounds"(
-- Table for each game in a round
CREATE TABLE "games"(
"id" SERIAL PRIMARY KEY,
"home_player" BIGINT NULL,
"home_player_sub" BOOLEAN NOT NULL,
"home_player_score" VARCHAR(255) NOT NULL,
"away_player" BIGINT NULL,
"away_player_sub" BOOLEAN NOT NULL,
"away_player_score" VARCHAR(255) NOT NULL,
"home_player_id" BIGINT,
"home_player_sub" BOOLEAN,
"home_player_score" VARCHAR(255),
"away_player_id" BIGINT,
"away_player_sub" BOOLEAN,
"away_player_score" VARCHAR(255),
"winner" BIGINT,
"matchup_id" BIGINT NOT NULL,
"round_id" BIGINT NOT NULL,
"game_number" INT NOT NULL
);

-- Junction Table for teams in a matchup
CREATE TABLE "teams_matchups"(
"id" SERIAL PRIMARY KEY,
"team_id" BIGINT NOT NULL,
"matchup_id" BIGINT NOT NULL
);

-- Junction Table for users in a matchup
CREATE TABLE "users_matchups"(
"id" SERIAL PRIMARY KEY,
"user_id" BIGINT NOT NULL,
"matchup_id" BIGINT NOT NULL
);
2 changes: 1 addition & 1 deletion example_data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ INSERT INTO "teams"(
"join_code"
)
VALUES
('Aint Afraid of No Cue Ball' ,2,'Gbust!'),
('Aint Afraid of No Cue Balls' ,2,'Gbust!'),
('All 4 One!',8,'A41!!!'),
('Avenge This',19,'AVNGER'),
('Just Us League',20,'Snyder'),
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"cookie-session": "^2.0.0",
"dotenv": "^16.0.3",
"express": "^4.17.1",
"luxon": "^3.4.4",
"passport": "^0.4.1",
"passport-local": "^1.0.0",
"pg": "^8.5.1",
Expand Down
124 changes: 117 additions & 7 deletions server/routes/match.router.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,128 @@ const pool = require('../modules/pool');

const router = express.Router();

/**
* GET route template
*/
// Get matchups for user
router.get('/', rejectUnauthenticated, (req, res) => {
// GET route code here
});

/**
* POST route template
*/
// Get specific matchup
router.get('/:id', rejectUnauthenticated, (req, res) => {
// GET route code here
});

// Get matchups for team
router.get('/team/:teamid', rejectUnauthenticated, (req, res) => {
// GET route code here
});

// Get matchups for league
router.get('/league/:leagueid', rejectUnauthenticated, (req, res) => {
// GET route code here
});

// POST matchup. Also populates rounds and games accordingly
router.post('/', rejectUnauthenticated, (req, res) => {
// POST route code here
let queryText = `
INSERT INTO "matchups" (
"date",
"bye",
"home_team_id",
"away_team_id",
"tournament_id"
)
VALUES ($1, $2, $3, $4, $5)
RETURNING id;
`;
pool.query(queryText,[
req.body.date,
req.body.bye,
req.body.home_team_id,
req.body.away_team_id,
req.body.tournament_id
])
.then((result) => {
const matchup_id = result.rows[0].id;
let queryText2 = `
INSERT INTO "rounds" ("matchup_id" , "round_number")
VALUES ($1, $2), ($1, $3), ($1, $4), ($1, $5), ($1, $6)
RETURNING id;
`;
pool.query(queryText2, [matchup_id, 1, 2, 3, 4, 5])
.then((result2) => {
let roundIds = [];
for(let row of result2.rows){
roundIds.push(row.id)
}
let queryText3 = `
INSERT INTO "games" ("matchup_id", "round_id", "game_number")
VALUES
($1 , $2 , $7),($1 , $2 , $8),($1 , $2 , $9),($1 , $2 , $10),($1 , $2 , $11),
($1 , $3 , $7),($1 , $3 , $8),($1 , $3 , $9),($1 , $3 , $10),($1 , $3 , $11),
($1 , $4 , $7),($1 , $4 , $8),($1 , $4 , $9),($1 , $4 , $10),($1 , $4 , $11),
($1 , $5 , $7),($1 , $5 , $8),($1 , $5 , $9),($1 , $5 , $10),($1 , $5 , $11),
($1 , $6 , $7),($1 , $6 , $8),($1 , $6 , $9),($1 , $6 , $10),($1 , $6 , $11);
`;
pool.query(queryText3,[
matchup_id,
roundIds[0],roundIds[1],roundIds[2],roundIds[3],roundIds[4],
1,2,3,4,5
])
.then(()=>{
if (req.body.home_team_id == null){
let queryText4 = `
INSERT INTO "teams_matchups" ("team_id", "matchup_id")
VALUES ($1, $2);
`;
pool.query(queryText4,[req.body.away_team_id, matchup_id])
.then(() => {res.sendStatus(201)})
.catch((error4) => {
console.error("Error in third query POST matchup", error4);
res.sendStatus(500);
})
;
}
else if (req.body.away_team_id == null){
let queryText4 = `
INSERT INTO "teams_matchups" ("team_id", "matchup_id")
VALUES ($1, $2);
`;
pool.query(queryText4,[req.body.home_team_id, matchup_id])
.then(() => {res.sendStatus(201)})
.catch((error4) => {
console.error("Error in third query POST matchup", error4);
res.sendStatus(500);
})
;
}
else {
let queryText4 = `
INSERT INTO "teams_matchups" ("team_id", "matchup_id")
VALUES ($1, $3), ($2, $3);
`;
pool.query(queryText4,[req.body.home_team_id, req.body.away_team_id, matchup_id])
.then(() => {res.sendStatus(201)})
.catch((error4) => {
console.error("Error in third query POST matchup", error4);
res.sendStatus(500);
})
;
}
})
.catch((error3) => {
console.error("Error in third query POST matchup", error3);
res.sendStatus(500);
})
})
.catch((error2) => {
console.error("Error in second query POST matchup", error2);
res.sendStatus(500);
})
})
.catch((error) => {
console.error("Error in initial query POST matchup", error);
res.sendStatus(500);
})
});

module.exports = router;
31 changes: 27 additions & 4 deletions server/routes/tournament.router.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ router.get('/league/:id', rejectUnauthenticated, (req,res) => {
router.get('/:id', rejectUnauthenticated, (req,res) => {
let queryText = `
SELECT * FROM "tournaments"
JOIN "matchups" ON "matchups"."tournament_id" = "tournaments"."id"
JOIN "rounds" ON "rounds"."matchup_id" = "matchup"."id"
JOIN "games" ON "games"."round_id" = "rounds"."id"
WHERE "tournaments"."id" = $1;
`;
pool.query(queryText,[req.params.id])
Expand All @@ -60,28 +63,48 @@ router.post('/', rejectUnauthenticated, (req, res) => {
"tournament_name",
"bracket",
"num_teams",
"num_rounds",
"num_matchups",
"playoffs",
"playoff_num",
"league_id"
)
VALUES ($1, $2, $3, $4, $5, $6, $7);
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id;
`;
pool.query(queryText,[
req.body.tournament_name,
req.body.bracket,
req.body.num_teams,
req.body.num_rounds,
req.body.num_matchups,
req.body.playoffs,
req.body.playoff_num,
req.body.league_id
])
.then(() => {res.sendStatus(201)})
.then((result) => {
console.log(result.rows[0].id);
res.send(String(result.rows[0].id)).status(201)})
.catch((error) => {
console.error("Error in tournaments POST second query", error);
res.sendStatus(500);
})
;
});

// Get all info for a specific matchup
router.get('/:id/matchup/:matchupid', rejectUnauthenticated, (req,res) => {
let queryText = `
SELECT * FROM "matchups"
JOIN "rounds" ON "rounds"."matchup_id" = "matchup"."id"
JOIN "games" ON "games"."round_id" = "rounds"."id"
WHERE "matchups"."id" = $1;
`;
pool.query(queryText,[req.params.id])
.then((result) => {res.send(result.rows)})
.catch((error) => {
console.error("Error in matchups GET", error);
res.sendStatus(500);
})
;
});

module.exports = router;
Loading

0 comments on commit 7df2ad7

Please sign in to comment.