2import { Hono } from "https://deno.land/x/hono/mod.ts";
3import { parseProject, readFile, serveFile } from "https://esm.town/v/std/utils@85-main/index.ts";
4import type { ApiResponse, RecommendationRequest, UserProfile } from "../shared/types";
5import { drills, initDatabase, initSampleDrills, playlists, sessions, users } from "./database";
6import { generateFootballDrills, generateMusicPlaylist, generateTrainingSession } from "./openai";
59});
60
61// API Routes
62
63// User routes
64app.post("/api/users", async c => {
65 await ensureDbInitialized();
66
69 const newUser = await users.create(body);
70
71 return c.json<ApiResponse<UserProfile>>({
72 success: true,
73 data: newUser,
74 });
75 } catch (error) {
76 return c.json<ApiResponse<null>>({
77 success: false,
78 error: error instanceof Error ? error.message : "Failed to create user",
81});
82
83app.get("/api/users/:id", async c => {
84 await ensureDbInitialized();
85
89
90 if (!user) {
91 return c.json<ApiResponse<null>>({
92 success: false,
93 error: "User not found",
95 }
96
97 return c.json<ApiResponse<UserProfile>>({
98 success: true,
99 data: user,
100 });
101 } catch (error) {
102 return c.json<ApiResponse<null>>({
103 success: false,
104 error: error instanceof Error ? error.message : "Failed to get user",
107});
108
109app.patch("/api/users/:id", async c => {
110 await ensureDbInitialized();
111
116
117 if (!updatedUser) {
118 return c.json<ApiResponse<null>>({
119 success: false,
120 error: "User not found",
122 }
123
124 return c.json<ApiResponse<UserProfile>>({
125 success: true,
126 data: updatedUser,
127 });
128 } catch (error) {
129 return c.json<ApiResponse<null>>({
130 success: false,
131 error: error instanceof Error ? error.message : "Failed to update user",
135
136// Playlist routes
137app.get("/api/playlists/user/:userId", async c => {
138 await ensureDbInitialized();
139
142 const userPlaylists = await playlists.getByUserId(userId);
143
144 return c.json<ApiResponse<typeof userPlaylists>>({
145 success: true,
146 data: userPlaylists,
147 });
148 } catch (error) {
149 return c.json<ApiResponse<null>>({
150 success: false,
151 error: error instanceof Error ? error.message : "Failed to get playlists",
155
156// Drill routes
157app.get("/api/drills", async c => {
158 await ensureDbInitialized();
159
177 }
178
179 return c.json<ApiResponse<typeof result>>({
180 success: true,
181 data: result,
182 });
183 } catch (error) {
184 return c.json<ApiResponse<null>>({
185 success: false,
186 error: error instanceof Error ? error.message : "Failed to get drills",
190
191// Session routes
192app.get("/api/sessions/user/:userId", async c => {
193 await ensureDbInitialized();
194
197 const userSessions = await sessions.getByUserId(userId);
198
199 return c.json<ApiResponse<typeof userSessions>>({
200 success: true,
201 data: userSessions,
202 });
203 } catch (error) {
204 return c.json<ApiResponse<null>>({
205 success: false,
206 error: error instanceof Error ? error.message : "Failed to get sessions",
209});
210
211app.get("/api/sessions/:id", async c => {
212 await ensureDbInitialized();
213
217
218 if (!session) {
219 return c.json<ApiResponse<null>>({
220 success: false,
221 error: "Session not found",
223 }
224
225 return c.json<ApiResponse<typeof session>>({
226 success: true,
227 data: session,
228 });
229 } catch (error) {
230 return c.json<ApiResponse<null>>({
231 success: false,
232 error: error instanceof Error ? error.message : "Failed to get session",
236
237// AI recommendation routes
238app.post("/api/recommendations/playlist", async c => {
239 await ensureDbInitialized();
240
247 const user = await users.getById(body.userId);
248 if (!user) {
249 return c.json<ApiResponse<null>>({
250 success: false,
251 error: "User not found",
256 const savedPlaylist = await playlists.create(generatedPlaylist);
257
258 return c.json<ApiResponse<typeof savedPlaylist>>({
259 success: true,
260 data: savedPlaylist,
261 });
262 } catch (error) {
263 return c.json<ApiResponse<null>>({
264 success: false,
265 error: error instanceof Error ? error.message : "Failed to generate playlist",
268});
269
270app.post("/api/recommendations/drills", async c => {
271 await ensureDbInitialized();
272
279 const user = await users.getById(body.userId);
280 if (!user) {
281 return c.json<ApiResponse<null>>({
282 success: false,
283 error: "User not found",
293 const recommendedDrills = await generateFootballDrills(user, body.request, allDrills);
294
295 return c.json<ApiResponse<typeof recommendedDrills>>({
296 success: true,
297 data: recommendedDrills,
298 });
299 } catch (error) {
300 return c.json<ApiResponse<null>>({
301 success: false,
302 error: error instanceof Error ? error.message : "Failed to generate drill recommendations",
305});
306
307app.post("/api/recommendations/session", async c => {
308 await ensureDbInitialized();
309
316 const user = await users.getById(body.userId);
317 if (!user) {
318 return c.json<ApiResponse<null>>({
319 success: false,
320 error: "User not found",
344 });
345
346 return c.json<ApiResponse<typeof newSession>>({
347 success: true,
348 data: newSession,
349 });
350 } catch (error) {
351 return c.json<ApiResponse<null>>({
352 success: false,
353 error: error instanceof Error ? error.message : "Failed to generate training session",