Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions app/pages/api/getAllStats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { getAllStats } from "../../statistics/statisticsController";

export default async function handler(req: any, res: any) {
if (req.method !== 'GET') {
return res.status(405).json({ message: 'Method Not Allowed' });
}
try {
const stats = await getAllStats();
res.status(200).json(stats);
} catch (error) {
console.error("Error fetching stats: ", error);
res.status(500).json({ message: "Internal server error" });
}
}
16 changes: 16 additions & 0 deletions app/pages/api/getGroupedStats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { getGroupedStats } from "@/app/statistics/statisticsController";


export default async function handler(req: any, res: any) {
if (req.method !== 'GET') {
return res.status(405).json({ message: 'Method Not Allowed' });
}
const metric = req.headers.metric;
try {
const stats = await getGroupedStats(metric);
res.status(200).json(stats);
} catch (error) {
console.error("Error fetching stats: ", error);
res.status(500).json({ message: "Internal server error" });
}
}
15 changes: 15 additions & 0 deletions app/pages/api/getPlayerStats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { getPlayerStats } from '../../statistics/statisticsController';

export default async function handler(req: any, res: any) {
if (req.method !== 'GET') {
return res.status(405).json({ message: 'Method Not Allowed' });
}
const playerId = req.headers.playerId;
try {
const stats = await getPlayerStats(playerId);
res.status(200).json(stats);
} catch (error) {
console.error("Error fetching player stats: ", error);
res.status(500).json({ message: "Internal server error" });
}
}
16 changes: 16 additions & 0 deletions app/pages/api/getStat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { getStat } from "@/app/statistics/statisticsController";

export default async function handler(req: any, res: any) {
if (req.method !== 'GET') {
return res.status(405).json({ message: 'Method Not Allowed' });
}
const playerId = req.headers.playerId;
const metric = req.headers.metric;
try {
const stats = await getStat(playerId, metric);
res.status(200).json(stats);
} catch (error) {
console.error("Error fetching stats: ", error);
res.status(500).json({ message: "Internal server error" });
}
}
15 changes: 15 additions & 0 deletions app/pages/api/getTeamStats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { getTeamStats } from "@/app/statistics/statisticsController";

export default async function handler(req: any, res: any) {
if (req.method !== 'GET') {
return res.status(405).json({ message: 'Method Not Allowed' });
}
const teamId = req.headers.teamId;
try {
const stats = await getTeamStats(teamId);
res.status(200).json(stats);
} catch (error) {
console.error("Error fetching team stats: ", error);
res.status(500).json({ message: "Internal server error" });
}
}
16 changes: 16 additions & 0 deletions app/pages/api/updateStats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { updateStats } from "@/app/statistics/statisticsController";

export default async function handler(req: any, res: any) {
if (req.method !== 'POST') {
return res.status(405).json({ message: 'Method Not Allowed' });
}
const playerId = req.headers.playerId;
const data = req.body;
try {
const stats = await updateStats(playerId, data);
res.status(200).json(stats);
} catch (error) {
console.error("Error updating stats: ", error);
res.status(500).json({ message: "Internal server error" });
}
}
60 changes: 60 additions & 0 deletions app/statistics/statisticsController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { createClient } from "@/src/utils/supabase/server_action_client"
import { cookies } from "next/headers"

const cookieStore = cookies()
const supabase = createClient(cookieStore)

// CRUD operations for player stats

// get all stats for a given player
export async function getPlayerStats(playerId: string) {
const { data: stats, error } = await supabase.from("player_stats").select().eq("player_id", playerId)
if (error) {
console.error("Error fetching player stats: ", error)
}
return stats
}

// get all stats for a given team
export async function getTeamStats(teamId: string) {
const { data: stats, error } = await supabase.from("player_stats").select().eq("team_id", teamId)
if (error) {
console.error("Error fetching team stats: ", error)
}
return stats
}

// get all stats for all players
export async function getAllStats() {
const { data: stats, error } = await supabase.from("player_stats").select()
if (error) {
console.error("Error fetching stats: ", error)
}
return stats
}

// get stats for a particular metric, sorted in descending order
export async function getGroupedStats(metric: string) {
const { data: stats, error } = await supabase.from("player_stats").select("player_id", metric).order(metric, { ascending: false })
if (error) {
console.error("Error fetching stats: ", error)
}
return stats
}

// get any individual stat for a given player
export async function getStat(playerId: string, metric: string) {
const { data: stats, error } = await supabase.from("player_stats").select(metric).eq("player_id", playerId)
if (error) {
console.error("Error fetching stats: ", error)
}
return stats
}

// update the stats of a given player
export async function updateStats(playerId: string, data: any) {
const { error } = await supabase.from("player_stats").update(data).eq("player_id", playerId)
if (error) {
console.error("Error updating player stats: ", error)
}
}