Skip to content

Commit

Permalink
Added TypeScript Dep + TSconfig.json (#11)
Browse files Browse the repository at this point in the history
Converted all models (user, dixon, workout) to have static type defs
First progress with converison on middleware and index
  • Loading branch information
Nyumat committed Jan 8, 2023
1 parent ee1b990 commit f1e3ae6
Show file tree
Hide file tree
Showing 7 changed files with 575 additions and 5 deletions.
115 changes: 115 additions & 0 deletions backend/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import express from "express";
import dotenv from "dotenv";
import os from "os";
import bodyParser from "body-parser";
import mongoose from "mongoose";
import cors from "cors";
import cloudinary from "cloudinary";

import path from "path";

// Middleware
import tracker from "./middleware/tracker.js";
import setHeaders from "./utils/setHeaders.js";

// Auth
import register from "./routes/auth/register.js";
import login from "./routes/auth/login.js";
import logout from "./routes/auth/logout.js";

// CRUD
import get_user from "./routes/crud/read/get_user.js";
import get_machines from "./routes/crud/read/get_machines.js";
import get_workouts from "./routes/crud/read/get_workouts.js";
import update_user from "./routes/crud/update/update_user.js";
import update_cardio from "./routes/crud/update/update_cardio.js";
import add_machine from "./routes/crud/update/add_machine.js";
import create_workout from "./routes/crud/create/create_workout.js";
import create_machine from "./routes/crud/create/create_machine.js";
import update_status from "./routes/crud/update/update_status.js";
import end_workout from "./routes/crud/delete/end_workout.js";
import rate_workout from "./routes/crud/update/rate_workout.js";
import add_exercise from "./routes/crud/create/create_exercise.js";
import update_set from "./routes/crud/update/update_set.js";

dotenv.config();

const PORT = process.env.PORT;



declare module 'cloudinary' {
export function config(conf: ConfigOptions): void;
}

interface ConfigOptions {
cloud_name: string;
api_key: string;
api_secret: string;
}

// Image Storage
cloudinary.config({
cloud_name: process.env.CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET
});

const app = express();

app.use(bodyParser.urlencoded({ extended: true }));
// Parse incoming requests data as JSON.
app.use(bodyParser.json());
// Allow cross-origin requests.
app.use(cors());
// Track the requests to the backend.
app.use(tracker);
// Set the headers for the requests.
app.use(setHeaders);
// Register a user endpoint.
app.use("/api/users/register", register);
// Login a user endpoint.
app.use("/api/users/login", login);
// Logout a user endpoint.
app.use("/api/users/logout", logout);
// Get user endpoint.
app.use("/api/users/get", get_user);
// Create a new workout for a user.
app.use("/api/workouts/create", create_workout);
// Add a new machine to a workout.
app.use("/api/workout/machines/add", add_machine);
// Update cardio machine 'x' with a distance and time spent.
app.use("/api/workout/machines/cardio/add", update_cardio);
// Add a machine to the dixon collectiion
app.use("/api/machine/create", create_machine);
// Get all the machines in the dixon collection
app.use("/api/machines/get", get_machines);
// Update a machine's status in a workout.
app.use("/api/machine/update_status", update_status);
// Update the user's profile
app.use("/api/users/update", update_user);
// Get the user's workout and saved workouts.
app.use("/api/workouts/get", get_workouts);
// End the user's workout.
app.use("/api/workout/end", end_workout);
// Rate the user's workout.
app.use("/api/workout/rate", rate_workout);
// Add an exercise to a workout.
app.use("/api/workout/exercises/add", add_exercise);
// Add a sets to a strength machine.
app.use("/api/workout/machines/sets/add", update_set);

app.get("/api", (req, res) => {
res.send({ username: os.userInfo().username });
});

app.listen(PORT, () => {
console.log(`⚡️ [server]: Server is running at http://localhost:${PORT}`);
});

const uri = process.env.URI as string;

mongoose
.connect(uri)
.then(() => console.log("⚡️ [server]: MongoDB connection established..."))
.catch((error) => console.error("⚡️ [server]: MongoDB connection failed:", error.message));
File renamed without changes.
44 changes: 44 additions & 0 deletions backend/models/dixon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import mongoose from "mongoose";

type DixonType = mongoose.Document & {
machine_name: string;
machine_id: string;
machine_image: string;
machine_type: string;
machine_status: boolean;
}

const dixonSchema = new mongoose.Schema<DixonType>({
machine_name: {
type: String,
required: true,
unique: true
},
machine_id: {
// uuid
type: String,
required: true,
unique: true
},
machine_image: {
// cloudinary
type: String,
required: true,
unique: true
},
machine_type: {
type: String,
enum: ["Cardio", "Strength", "Other", "None"],
default: "None",
required: true
},
machine_status: {
// 1,true = available, 0,false = in use
type: Boolean,
required: true,
default: true
}
});

const Dixon = mongoose.model<DixonType>("Dixon", dixonSchema);
export default Dixon;
129 changes: 129 additions & 0 deletions backend/models/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import mongoose from "mongoose";
import dotenv from "dotenv";
import { Workout } from "./workout.js";
dotenv.config();
import { Schema, model, connect } from 'mongoose';

enum EBloodType {
A = "A",
B = "B",
AB = "AB",
O = "O",
None = ""
}

type UserType = mongoose.Document & {
username: string;
pin: string;
initLogin?: boolean;
weight: number;
height: number;
BMI: number;
bloodType?: string;
createdAt: Date;
updatedAt: Date;
savedWorkouts: typeof Workout[];
workouts: typeof Workout[];
firstName?: string;
lastName?: string;
age?: number;
timestamps?: boolean;
}

const userSchema = new mongoose.Schema<UserType>({
username: {
type: String,
required: true,
minlength: 3,
maxlength: 200,
unique: false
},
pin: {
type: String,
required: true,
minlength: 4,
maxlength: 99999,
unique: false
},
initLogin: {
type: Boolean,
default: true
},
weight: {
type: Number,
default: 0,
validate: {
validator: Number.isInteger,
message: "{VALUE} is not an integer value"
},
required: [true, "Weight is required"],
unique: false
},
height: {
type: Number,
default: 0,
validate: {
validator: Number.isInteger,
message: "{VALUE} is not an integer value"
},
required: [true, "Height is required"],
unique: false
},
BMI: {
type: Number,
default: 0,
required: [true, "BMI is required"],
unique: false
},
bloodType: {
type: String,
enum: ["O+", "O-", "A+", "A-", "B+", "B-", "AB+", "AB-", ""],
required: false,
unique: false,
uppercase: true
},
// loginDateTime: {
// type: Date,
// required: false,
// minlength: 3,
// maxlength: 200,
// unique: false
// },
// logoutDateTime: {
// type: Date,
// required: false,
// minlength: 3,
// maxlength: 200,
// unique: false
// },
savedWorkouts: [Workout.schema],
workouts: [Workout.schema],
firstName: {
type: String,
required: false
},
lastName: {
type: String,
required: false
},
age: {
type: Number,
default: 0,
min: 0,
max: 150,
validate: {
validator: Number.isInteger,
message: "{VALUE} is not an integer value"
},
required: false,
unique: false
},
timestamps: {
type: Boolean,
default: true
}
});

const User = mongoose.model<UserType>("User", userSchema);

export default User;
Loading

0 comments on commit f1e3ae6

Please sign in to comment.