Skip to content

Commit

Permalink
Update program creation process and CSS changes
Browse files Browse the repository at this point in the history
The program creation method in the programController is now atomic by using Mongoose transactions. This ensures that changes to both Users and Programs are either all saved or none, avoiding inconsistent states. Also, a new "gap" CSS property has been added in Login.module.scss to improve layouts.
  • Loading branch information
var-poro committed Jul 1, 2024
1 parent 85796a4 commit 890f18d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 51 deletions.
119 changes: 68 additions & 51 deletions api/src/controllers/programController.ts
Original file line number Diff line number Diff line change
@@ -1,78 +1,95 @@
import { Request, Response } from 'express';
import {Request, Response} from 'express';
import Program from '../models/Program';
import Session from '../models/Session';
import User from "../models/User";
import mongoose, {Schema} from "mongoose";

interface AuthRequest extends Request {
userId?: string;
userId?: string;
}

export const createProgram = async (req: AuthRequest, res: Response) => {
try {
const program = new Program({ ...req.body, userId: req.userId });
await program.save();
res.status(201).send(program);
} catch (error) {
res.status(400).send(error);
}
const session = await mongoose.startSession();
session.startTransaction();

try {
const program = new Program({...req.body, userId: req.userId});
const savedProgram = await program.save({session});

const user = await User.findById(req.userId).session(session);

if (user && !user.activeProgram) {
user.activeProgram = savedProgram._id as Schema.Types.ObjectId;
await user.save({session});
}

await session.commitTransaction();
session.endSession();
res.status(201).send(savedProgram);
} catch (error) {
await session.abortTransaction();
session.endSession();
res.status(400).send({error: (error as Error).message});
}
};

export const getProgram = async (req: AuthRequest, res: Response) => {
try {
const program = await Program.findOne({ _id: req.params.id, userId: req.userId });
if (!program) {
return res.status(404).send();
try {
const program = await Program.findOne({_id: req.params.id, userId: req.userId});
if (!program) {
return res.status(404).send();
}
res.send(program);
} catch (error) {
res.status(500).send(error);
}
res.send(program);
} catch (error) {
res.status(500).send(error);
}
};

export const updateProgram = async (req: AuthRequest, res: Response) => {
try {
const program = await Program.findOneAndUpdate({ _id: req.params.id, userId: req.userId }, req.body, {
new: true,
runValidators: true,
});
if (!program) {
return res.status(404).send();
try {
const program = await Program.findOneAndUpdate({_id: req.params.id, userId: req.userId}, req.body, {
new: true,
runValidators: true,
});
if (!program) {
return res.status(404).send();
}
res.send(program);
} catch (error) {
res.status(400).send(error);
}
res.send(program);
} catch (error) {
res.status(400).send(error);
}
};

export const deleteProgram = async (req: AuthRequest, res: Response) => {
try {
const program = await Program.findOneAndDelete({ _id: req.params.id, userId: req.userId });
if (!program) {
return res.status(404).send();
try {
const program = await Program.findOneAndDelete({_id: req.params.id, userId: req.userId});
if (!program) {
return res.status(404).send();
}
res.send(program);
} catch (error) {
res.status(500).send(error);
}
res.send(program);
} catch (error) {
res.status(500).send(error);
}
};

export const getAllPrograms = async (req: AuthRequest, res: Response) => {
try {
const programs = await Program.find({ userId: req.userId });
res.send(programs);
} catch (error) {
res.status(500).send(error);
}
try {
const programs = await Program.find({userId: req.userId});
res.send(programs);
} catch (error) {
res.status(500).send(error);
}
};

export const getSessionsByProgramId = async (req: AuthRequest, res: Response) => {
try {
const programId = req.params.programId;
const sessions = await Session.find({ programId }).populate('exercises');
if (!sessions) {
return res.status(404).json({ message: 'No sessions found for this program' });
try {
const programId = req.params.programId;
const sessions = await Session.find({programId}).populate('exercises');
if (!sessions) {
return res.status(404).json({message: 'No sessions found for this program'});
}
res.status(200).json(sessions);
} catch (error) {
res.status(500).json({message: 'Error fetching sessions', error});
}
res.status(200).json(sessions);
} catch (error) {
res.status(500).json({ message: 'Error fetching sessions', error });
}
};
1 change: 1 addition & 0 deletions app/src/pages/Auth/Login/Login.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
flex-direction: column;
justify-content: center;
margin: auto;
gap: var(--spacing);
height: 100svh;

.inputContainer {
Expand Down

0 comments on commit 890f18d

Please sign in to comment.