137 lines
3.9 KiB
TypeScript
137 lines
3.9 KiB
TypeScript
|
|
import type { Route } from "./+types/admin.sports-seasons.$id.events.$eventId";
|
||
|
|
import { findSportsSeasonById } from "~/models/sports-season";
|
||
|
|
import { findParticipantsBySportsSeasonId } from "~/models/participant";
|
||
|
|
import {
|
||
|
|
getScoringEventById,
|
||
|
|
completeScoringEvent,
|
||
|
|
} from "~/models/scoring-event";
|
||
|
|
import {
|
||
|
|
getEventResults,
|
||
|
|
createEventResult,
|
||
|
|
updateEventResult,
|
||
|
|
deleteEventResult,
|
||
|
|
type CreateEventResultData,
|
||
|
|
type UpdateEventResultData,
|
||
|
|
} from "~/models/event-result";
|
||
|
|
|
||
|
|
export async function loader({ params }: Route.LoaderArgs) {
|
||
|
|
const sportsSeason = await findSportsSeasonById(params.id);
|
||
|
|
|
||
|
|
if (!sportsSeason) {
|
||
|
|
throw new Response("Sports season not found", { status: 404 });
|
||
|
|
}
|
||
|
|
|
||
|
|
const event = await getScoringEventById(params.eventId);
|
||
|
|
|
||
|
|
if (!event) {
|
||
|
|
throw new Response("Event not found", { status: 404 });
|
||
|
|
}
|
||
|
|
|
||
|
|
const participants = await findParticipantsBySportsSeasonId(params.id);
|
||
|
|
const results = await getEventResults(params.eventId);
|
||
|
|
|
||
|
|
return {
|
||
|
|
sportsSeason: sportsSeason as typeof sportsSeason & {
|
||
|
|
sport: { id: string; name: string; type: string; slug: string };
|
||
|
|
},
|
||
|
|
event,
|
||
|
|
participants,
|
||
|
|
results,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function action({ request, params }: Route.ActionArgs) {
|
||
|
|
const formData = await request.formData();
|
||
|
|
const intent = formData.get("intent");
|
||
|
|
|
||
|
|
if (intent === "complete") {
|
||
|
|
try {
|
||
|
|
await completeScoringEvent(params.eventId);
|
||
|
|
return { success: "Event marked as completed" };
|
||
|
|
} catch (error) {
|
||
|
|
console.error("Error completing event:", error);
|
||
|
|
return { error: "Failed to complete event" };
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (intent === "add-result") {
|
||
|
|
const participantId = formData.get("participantId");
|
||
|
|
const placement = formData.get("placement");
|
||
|
|
|
||
|
|
if (typeof participantId !== "string" || !participantId) {
|
||
|
|
return { error: "Participant is required" };
|
||
|
|
}
|
||
|
|
|
||
|
|
if (typeof placement !== "string" || !placement) {
|
||
|
|
return { error: "Placement is required" };
|
||
|
|
}
|
||
|
|
|
||
|
|
const placementNum = parseInt(placement, 10);
|
||
|
|
if (isNaN(placementNum) || placementNum < 1 || placementNum > 100) {
|
||
|
|
return { error: "Placement must be between 1 and 100" };
|
||
|
|
}
|
||
|
|
|
||
|
|
const resultData: CreateEventResultData = {
|
||
|
|
scoringEventId: params.eventId,
|
||
|
|
participantId,
|
||
|
|
placement: placementNum,
|
||
|
|
};
|
||
|
|
|
||
|
|
try {
|
||
|
|
await createEventResult(resultData);
|
||
|
|
return { success: "Result added successfully" };
|
||
|
|
} catch (error) {
|
||
|
|
console.error("Error adding result:", error);
|
||
|
|
return { error: "Failed to add result" };
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (intent === "update-result") {
|
||
|
|
const resultId = formData.get("resultId");
|
||
|
|
const placement = formData.get("placement");
|
||
|
|
|
||
|
|
if (typeof resultId !== "string" || !resultId) {
|
||
|
|
return { error: "Result ID is required" };
|
||
|
|
}
|
||
|
|
|
||
|
|
if (typeof placement !== "string" || !placement) {
|
||
|
|
return { error: "Placement is required" };
|
||
|
|
}
|
||
|
|
|
||
|
|
const placementNum = parseInt(placement, 10);
|
||
|
|
if (isNaN(placementNum) || placementNum < 1 || placementNum > 100) {
|
||
|
|
return { error: "Placement must be between 1 and 100" };
|
||
|
|
}
|
||
|
|
|
||
|
|
const updateData: UpdateEventResultData = {
|
||
|
|
placement: placementNum,
|
||
|
|
};
|
||
|
|
|
||
|
|
try {
|
||
|
|
await updateEventResult(resultId, updateData);
|
||
|
|
return { success: "Result updated successfully" };
|
||
|
|
} catch (error) {
|
||
|
|
console.error("Error updating result:", error);
|
||
|
|
return { error: "Failed to update result" };
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (intent === "delete-result") {
|
||
|
|
const resultId = formData.get("resultId");
|
||
|
|
|
||
|
|
if (typeof resultId !== "string" || !resultId) {
|
||
|
|
return { error: "Result ID is required" };
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
await deleteEventResult(resultId);
|
||
|
|
return { success: "Result deleted successfully" };
|
||
|
|
} catch (error) {
|
||
|
|
console.error("Error deleting result:", error);
|
||
|
|
return { error: "Failed to delete result" };
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return { error: "Invalid action" };
|
||
|
|
}
|