|
| 1 | +import { Request, Response, NextFunction } from "express"; |
| 2 | +import * as interviewService from "../services/interview.service"; |
| 3 | +import { ApiError } from "../utils/apiError"; |
| 4 | + |
| 5 | +export const getInterviews = async (req: Request, res: Response, next: NextFunction) => { |
| 6 | + const interviews = await interviewService.getInterviews(); |
| 7 | + |
| 8 | + res.status(200).json({ |
| 9 | + success: true, |
| 10 | + data: interviews, |
| 11 | + }); |
| 12 | +}; |
| 13 | + |
| 14 | +export const getInterviewById = async (req: Request, res: Response, next: NextFunction) => { |
| 15 | + const interviewId = parseInt(req.params.id); |
| 16 | + |
| 17 | + if (!interviewId) { |
| 18 | + throw new ApiError("Invalid interview ID", 400); |
| 19 | + } |
| 20 | + |
| 21 | + const interview = await interviewService.getInterviewById(interviewId); |
| 22 | + |
| 23 | + if (!interview) { |
| 24 | + throw new ApiError("Interview experience not found", 404); |
| 25 | + } |
| 26 | + |
| 27 | + res.status(200).json({ |
| 28 | + success: true, |
| 29 | + data: interview, |
| 30 | + }); |
| 31 | +}; |
| 32 | + |
| 33 | + |
| 34 | +export const createInterview = async (req: Request, res: Response, next: NextFunction) => { |
| 35 | + const memberId = req.params.memberId; |
| 36 | + |
| 37 | + if (!memberId) { |
| 38 | + throw new ApiError("Member ID is required", 400); |
| 39 | + } |
| 40 | + |
| 41 | + const { company, role, verdict, content, isAnonymous } = req.body; |
| 42 | + |
| 43 | + if (!company || !role || !verdict || !content || isAnonymous === undefined) { |
| 44 | + throw new ApiError("All fields are required", 400); |
| 45 | + } |
| 46 | + |
| 47 | + const interview = await interviewService.createInterview(memberId, { |
| 48 | + company, |
| 49 | + role, |
| 50 | + verdict, |
| 51 | + content, |
| 52 | + isAnonymous, |
| 53 | + }); |
| 54 | + |
| 55 | + res.status(201).json({ |
| 56 | + success: true, |
| 57 | + data: interview, |
| 58 | + }); |
| 59 | +}; |
| 60 | + |
| 61 | +export const updateInterviewById = async (req: Request, res: Response, next: NextFunction) => { |
| 62 | + const interviewId = parseInt(req.params.id); |
| 63 | + |
| 64 | + if (!interviewId) { |
| 65 | + throw new ApiError("Invalid interview ID", 400); |
| 66 | + } |
| 67 | + |
| 68 | + const { memberId, company, role, verdict, content, isAnonymous } = req.body; |
| 69 | + |
| 70 | + if (!memberId) { |
| 71 | + throw new ApiError("Member ID is required for verification", 400); |
| 72 | + } |
| 73 | + |
| 74 | + const existingInterview = await interviewService.getInterviewById(interviewId); |
| 75 | + |
| 76 | + if (!existingInterview) { |
| 77 | + throw new ApiError("Interview experience not found", 404); |
| 78 | + } |
| 79 | + |
| 80 | + if (existingInterview.memberId !== memberId) { |
| 81 | + throw new ApiError("You are not authorized to update this interview experience", 403); |
| 82 | + } |
| 83 | + |
| 84 | + const updatedInterview = await interviewService.updateInterviewById(interviewId, { |
| 85 | + memberId, |
| 86 | + company, |
| 87 | + role, |
| 88 | + verdict, |
| 89 | + content, |
| 90 | + isAnonymous, |
| 91 | + }); |
| 92 | + |
| 93 | + res.status(200).json({ |
| 94 | + success: true, |
| 95 | + data: updatedInterview, |
| 96 | + }); |
| 97 | +}; |
| 98 | + |
| 99 | + |
| 100 | +export const deleteInterviewById = async (req: Request, res: Response, next: NextFunction) => { |
| 101 | + const interviewId = parseInt(req.params.id); |
| 102 | + |
| 103 | + if (!interviewId) { |
| 104 | + throw new ApiError("Invalid interview ID", 400); |
| 105 | + } |
| 106 | + |
| 107 | + const existingInterview = await interviewService.getInterviewById(interviewId); |
| 108 | + |
| 109 | + if (!existingInterview) { |
| 110 | + throw new ApiError("Interview experience not found", 404); |
| 111 | + } |
| 112 | + |
| 113 | + await interviewService.deleteInterviewById(interviewId); |
| 114 | + |
| 115 | + res.status(200).json({ |
| 116 | + success: true, |
| 117 | + message: "Interview experience deleted successfully", |
| 118 | + }); |
| 119 | +}; |
| 120 | + |
| 121 | + |
0 commit comments