-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update program creation process and CSS changes
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
Showing
2 changed files
with
69 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters