|
| 1 | +import { Request, Response } from 'express'; |
| 2 | +import { HistoryService } from '../services/history.service'; |
| 3 | + |
| 4 | +export class HistoryController { |
| 5 | + private historyService: HistoryService; |
| 6 | + |
| 7 | + constructor() { |
| 8 | + this.historyService = new HistoryService(); |
| 9 | + } |
| 10 | + |
| 11 | + // Endpoint for Matching Service to GET history |
| 12 | + public getUserHistory = async (req: Request, res: Response): Promise<Response> => { |
| 13 | + try { |
| 14 | + const userId = req.params['userId']; |
| 15 | + if (!userId) { |
| 16 | + return res.status(400).json({ message: 'userId is required in the URL' }); |
| 17 | + } |
| 18 | + const history = await this.historyService.getAttemptedQuestions(userId); |
| 19 | + return res.status(200).json({ attemptedQuestionIds: history }); |
| 20 | + } catch (error) { |
| 21 | + return res.status(500).json({ message: 'Server error' }); |
| 22 | + } |
| 23 | + }; |
| 24 | + |
| 25 | + // Endpoint for Matching Service to POST a new attempt |
| 26 | + public addAttempt = async (req: Request, res: Response): Promise<Response> => { |
| 27 | + try { |
| 28 | + const { userId, questionId } = req.body; |
| 29 | + if (!userId || !questionId) { |
| 30 | + return res.status(400).json({ message: 'userId and questionId are required' }); |
| 31 | + } |
| 32 | + await this.historyService.addQuestionAttempt(userId, questionId); |
| 33 | + return res.status(201).json({ message: 'History updated' }); |
| 34 | + } catch (error) { |
| 35 | + return res.status(500).json({ message: 'Server error' }); |
| 36 | + } |
| 37 | + }; |
| 38 | + |
| 39 | + // Updated endpoint to remove one or more questions from history |
| 40 | + public removeAttempts = async (req: Request, res: Response): Promise<Response> => { |
| 41 | + try { |
| 42 | + const userId = req.params['userId']; |
| 43 | + const { questionIds } = req.body; // Expect an array of question IDs in the body |
| 44 | + |
| 45 | + if (!userId) { |
| 46 | + return res.status(400).json({ message: 'userId is required in the URL' }); |
| 47 | + } |
| 48 | + if (!Array.isArray(questionIds) || questionIds.length === 0) { |
| 49 | + return res.status(400).json({ message: 'questionIds must be a non-empty array in the request body' }); |
| 50 | + } |
| 51 | + |
| 52 | + await this.historyService.removeQuestionAttempts(userId, questionIds); |
| 53 | + return res.status(200).json({ message: 'Questions removed from history' }); |
| 54 | + } catch (error) { |
| 55 | + return res.status(500).json({ message: 'Server error' }); |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + // New endpoint for users to clear their entire history |
| 60 | + public clearUserHistory = async (req: Request, res: Response): Promise<Response> => { |
| 61 | + try { |
| 62 | + const userId = req.params['userId']; |
| 63 | + if (!userId) { |
| 64 | + return res.status(400).json({ message: 'userId is required in the URL' }); |
| 65 | + } |
| 66 | + await this.historyService.clearHistory(userId); |
| 67 | + return res.status(200).json({ message: 'History cleared for user' }); |
| 68 | + } catch (error) { |
| 69 | + return res.status(500).json({ message: 'Server error' }); |
| 70 | + } |
| 71 | + }; |
| 72 | +} |
| 73 | + |
0 commit comments