Skip to content

Commit 1ef4b82

Browse files
committed
feat: created new models and workout endpoint
1 parent 944416f commit 1ef4b82

File tree

14 files changed

+382
-20
lines changed

14 files changed

+382
-20
lines changed

package-lock.json

Lines changed: 118 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,21 @@
1212
"dependencies": {
1313
"@prisma/client": "^6.13.0",
1414
"bcrypt": "^6.0.0",
15+
"cookie-parser": "^1.4.7",
1516
"cors": "^2.8.5",
1617
"express": "^5.1.0",
1718
"jsonwebtoken": "^9.0.2",
18-
"memory-cache": "^0.2.0",
19+
"redis": "^5.7.0",
1920
"zod": "^4.0.13"
2021
},
2122
"devDependencies": {
2223
"@types/bcrypt": "^6.0.0",
24+
"@types/cookie-parser": "^1.4.9",
2325
"@types/cors": "^2.8.19",
2426
"@types/express": "^5.0.3",
2527
"@types/jsonwebtoken": "^9.0.10",
26-
"@types/memory-cache": "^0.2.6",
2728
"@types/node": "^24.1.0",
29+
"@types/redis": "^4.0.11",
2830
"jest": "^30.0.5",
2931
"prisma": "^6.12.0",
3032
"ts-node": "^10.9.2",
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
-- CreateTable
2+
CREATE TABLE "exercises_enum" (
3+
"id_exercise" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
4+
"id_user" INTEGER NOT NULL,
5+
"exercise_name" TEXT NOT NULL,
6+
"muscle" TEXT NOT NULL,
7+
"description" TEXT NOT NULL,
8+
CONSTRAINT "exercises_enum_id_user_fkey" FOREIGN KEY ("id_user") REFERENCES "Users" ("id_user") ON DELETE RESTRICT ON UPDATE CASCADE
9+
);
10+
11+
-- CreateTable
12+
CREATE TABLE "workouts" (
13+
"id_workout" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
14+
"id_user" INTEGER NOT NULL,
15+
"workout_name" TEXT NOT NULL,
16+
"date" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
17+
CONSTRAINT "workouts_id_user_fkey" FOREIGN KEY ("id_user") REFERENCES "Users" ("id_user") ON DELETE RESTRICT ON UPDATE CASCADE
18+
);
19+
20+
-- CreateTable
21+
CREATE TABLE "workout_exercises" (
22+
"id_workout_exercise" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
23+
"id_workout" INTEGER NOT NULL,
24+
"id_exercise" INTEGER NOT NULL,
25+
CONSTRAINT "workout_exercises_id_exercise_fkey" FOREIGN KEY ("id_exercise") REFERENCES "exercises_enum" ("id_exercise") ON DELETE RESTRICT ON UPDATE CASCADE,
26+
CONSTRAINT "workout_exercises_id_workout_fkey" FOREIGN KEY ("id_workout") REFERENCES "workouts" ("id_workout") ON DELETE RESTRICT ON UPDATE CASCADE
27+
);
28+
29+
-- CreateTable
30+
CREATE TABLE "workout_sets" (
31+
"id_set" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
32+
"id_workout_exercise" INTEGER NOT NULL,
33+
"reps" INTEGER NOT NULL,
34+
"weight" INTEGER NOT NULL,
35+
CONSTRAINT "workout_sets_id_workout_exercise_fkey" FOREIGN KEY ("id_workout_exercise") REFERENCES "workout_exercises" ("id_workout_exercise") ON DELETE RESTRICT ON UPDATE CASCADE
36+
);

server/prisma/schema.prisma

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,58 @@ model Users {
1919
name String
2020
email String @unique
2121
password String
22+
23+
ExercisesEnum ExercisesEnum[] @relation("UserExercises")
24+
Workouts Workouts[] @relation("UserWorkouts")
25+
}
26+
27+
model ExercisesEnum {
28+
idExercise Int @id @default(autoincrement()) @map("id_exercise")
29+
idUser Int @map("id_user")
30+
exerciseName String @map("exercise_name")
31+
muscle String
32+
description String
33+
34+
user Users @relation("UserExercises", fields: [idUser], references: [idUser])
35+
36+
workoutExercise WorkoutExercises[]
37+
38+
@@map("exercises_enum")
39+
}
40+
41+
model Workouts {
42+
idWorkout Int @id @default(autoincrement()) @map("id_workout")
43+
idUser Int @map("id_user")
44+
workoutName String @map("workout_name")
45+
date DateTime @default(now())
46+
47+
user Users @relation("UserWorkouts", fields: [idUser], references: [idUser])
48+
49+
WorkoutExercises WorkoutExercises[]
50+
51+
@@map("workouts")
52+
}
53+
54+
model WorkoutExercises {
55+
idWorkoutExercise Int @id @default(autoincrement()) @map("id_workout_exercise")
56+
idWorkout Int @map("id_workout")
57+
idExercise Int @map("id_exercise")
58+
59+
workoutEnum ExercisesEnum @relation(fields: [idExercise], references: [idExercise])
60+
workout Workouts @relation(fields: [idWorkout], references: [idWorkout])
61+
62+
WorkoutSet WorkoutSets[]
63+
64+
@@map("workout_exercises")
65+
}
66+
67+
model WorkoutSets {
68+
idSet Int @id @default(autoincrement()) @map("id_set")
69+
idWorkoutExercise Int @map("id_workout_exercise")
70+
reps Int
71+
weight Int
72+
73+
workoutExercises WorkoutExercises @relation(fields: [idWorkoutExercise], references: [idWorkoutExercise])
74+
75+
@@map("workout_sets")
2276
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import z from "zod"
2+
3+
export const fullWorkoutSchema = z.object({
4+
idWorkout: z.number().int(),
5+
idUser: z.number().int("IDUSER_NULL"),
6+
workoutName: z.string("WORKOUTNAME_NULL"),
7+
date: z.date().optional()
8+
})
9+
10+
export type WorkoutDTO = z.infer<typeof fullWorkoutSchema>
11+
export type CreateWorkoutDTO = Omit<WorkoutDTO, "idWorkout" | "date">

server/src/http/server.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ import express from 'express'
22
import cors from "cors"
33
import { errorHandler } from '../middleware/errorHandler'
44
import UserRoutes from '../routes/UserRoutes'
5+
import WorkoutRoutes from "../routes/WorkoutRoutes"
6+
import cookieParser from "cookie-parser"
57

68

79
const app = express()
810

911
app.use(cors())
1012
app.use(express.json())
13+
app.use(cookieParser())
1114

1215
app.use(UserRoutes)
16+
app.use(WorkoutRoutes)
1317

1418
app.use(errorHandler)
1519

server/src/middleware/errorHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ export function errorHandler(err: unknown, req: Request, res: Response, next: Ne
1111
}
1212

1313
console.error("Unexpected error. " + err)
14-
return res.status(500).json({ error: "Server error." })
14+
return res.status(500).json({ error: "Server error.", code: (err as string).toString() })
1515
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { CreateWorkoutDTO, WorkoutDTO } from "../db/models/WorkoutModel";
2+
import prisma from "../db/connect";
3+
4+
export class WorkoutRepository {
5+
async create(workout: CreateWorkoutDTO): Promise<WorkoutDTO> {
6+
return await prisma.workouts.create({
7+
data: workout
8+
})
9+
}
10+
11+
async getWorkouts(idUser: number): Promise<WorkoutDTO[]> {
12+
return await prisma.workouts.findMany({
13+
where: {
14+
idUser: idUser
15+
}
16+
})
17+
18+
}
19+
}

0 commit comments

Comments
 (0)