Skip to content

Commit

Permalink
update to use title coz qn id changes everytime but title doesnt. als…
Browse files Browse the repository at this point in the history
…o, add error handling for no testcase found
  • Loading branch information
bhnuka committed Nov 7, 2024
1 parent e775358 commit cae58f9
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 29 deletions.
2 changes: 2 additions & 0 deletions peerprep/backend/question-service/src/models/testcaseModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import mongoose, { Document, Schema } from 'mongoose';

export interface ITestcase extends Document {
questionId: number;
title: string;
input1: string;
output1: string;
input2: string;
Expand All @@ -10,6 +11,7 @@ export interface ITestcase extends Document {

const TestcaseSchema: Schema = new Schema({
questionId: { type: Number, unique: true },
title: { type: String, unique: true },
input1: { type: String, required: true },
output1: { type: String, required: true },
input2: { type: String, required: true },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import Testcase, { ITestcase } from '../models/testcaseModel';

const router = express.Router();

router.get('/testcases/:questionId', async (req: Request, res: Response) => {
const { questionId } = req.params;
router.get('/testcases/:title', async (req: Request, res: Response) => {
const { title } = req.params;
try {
const testcase: ITestcase | null = await Testcase.findOne({ questionId });
const testcase: ITestcase | null = await Testcase.findOne({ title });
if (!testcase) {
return res.status(404).json({ message: 'Testcases not found' });
}
Expand Down
14 changes: 1 addition & 13 deletions peerprep/backend/question-service/src/sampleData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,6 @@ const sampleQuestions = [
categories: 'Data Structures, Algorithms',
difficulty: 'medium',
},
{
title: 'LRU Cache Design',
description: `Design and implement an LRU (Least Recently Used) cache.`,
categories: 'Data Structures',
difficulty: 'medium',
},
{
title: 'Longest Common Subsequence',
description: `Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0.
Expand Down Expand Up @@ -141,13 +135,7 @@ const sampleQuestions = [
description: `Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection. Design an algorithm to serialize and deserialize a binary tree.`,
categories: 'Data Structures, Algorithms',
difficulty: 'hard',
},
{
title: 'Trips and Users',
description: `Given table Trips with columns id, client_id, driver_id, city_id, status, request_at, and table Users with columns users_id, banned, role, write a solution to find the cancellation rate of requests with unbanned users (both client and driver must not be banned) between specific dates.`,
categories: 'Databases',
difficulty: 'hard',
},
}
];

// Create a helper function to simulate the Express request-response cycle
Expand Down
5 changes: 3 additions & 2 deletions peerprep/frontend/src/api/testcaseApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ const API_URL = 'http://localhost:8080/api/testcases';

export interface Testcase {
questionId: number;
title: string;
input1: string;
output1: string;
input2: string;
output2: string;
}

export const getTestcasesByQuestionId = async (questionId: number): Promise<Testcase | null> => {
export const getTestcasesByTitle = async (title: string): Promise<Testcase | null> => {
try {
const response = await axios.get<Testcase>(`${API_URL}/${questionId}`);
const response = await axios.get<Testcase>(`${API_URL}/${title}`);
console.log('Fetched testcases:', response.data);
return response.data;
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { WebsocketProvider } from 'y-websocket';

import { deleteMatchedSession} from "../../api/matchingApi.ts";
import { getQuestionById } from '../../api/questionApi.ts';
import { getTestcasesByQuestionId, Testcase } from '../../api/testcaseApi.ts';
import { getTestcasesByTitle, Testcase } from '../../api/testcaseApi.ts';

const CollaborationServiceIntegratedView: React.FC = () => {
const { sessionId } = useParams<{ sessionId: string; }>();
Expand All @@ -36,7 +36,15 @@ const CollaborationServiceIntegratedView: React.FC = () => {
const navigate = useNavigate();
const [yText, setYText] = useState<Y.Text | null>(null);
const [commentoutput, setCommentOutput] = useState<string | null>(null);
const [testcases, setTestcases] = useState<Testcase | null>(null);
const [testcases, setTestcases] = useState<Testcase>({
questionId: 0,
title: "N/A",
input1: "N/A",
output1: "N/A",
input2: "N/A",
output2: "N/A"
});

console.log(commentoutput);
//let topic = 'topic';
//let difficulty = 'difficulty';
Expand All @@ -46,7 +54,6 @@ const CollaborationServiceIntegratedView: React.FC = () => {
const [difficulty, setDifficulty] = useState<string>('N/A');
const [questionTitle, setQuestionTitle] = useState<string>('N/A');
const [questionDescription, setQuestionDescription] = useState<string>('N/A');
const [questionId2, setQuestionId] = useState<number>(0);
console.log(sessionId);
const questionId = sessionId ? sessionId.split('-Q')[1] : "N/A";

Expand All @@ -64,7 +71,6 @@ const CollaborationServiceIntegratedView: React.FC = () => {
setDifficulty(response.difficulty); // Set difficulty from API response
setQuestionTitle(response.title);
setQuestionDescription(response.description);
setQuestionId(response.questionId);
}
} catch (error) {
console.error('Error fetching matched session:', error);
Expand All @@ -77,7 +83,6 @@ const CollaborationServiceIntegratedView: React.FC = () => {

useEffect(() => {
console.log(`Session ID: ${sessionId}, Topics: ${topics}, Difficulty: ${difficulty}`);
console.log(`Question: ${questionId}`);
}, [sessionId, topics, difficulty, questionId]);

useEffect(() => {
Expand Down Expand Up @@ -107,19 +112,39 @@ const CollaborationServiceIntegratedView: React.FC = () => {
useEffect(() => {
const fetchTestcases = async () => {
try {
const response2 = await getTestcasesByQuestionId(questionId2);
if (response2) {
console.log('Setting fetched testcases:', response2);
setTestcases(response2);
const response = await getTestcasesByTitle(questionTitle);
if (response) {
console.log('Setting fetched testcases:', response);
setTestcases(response);
} else {
console.log('No testcases found, setting default values');
setTestcases({
questionId: 0,
title: "N/A",
input1: "N/A",
output1: "N/A",
input2: "N/A",
output2: "N/A"
});
}
} catch (error) {
console.error('Error fetching testcases:', error);
setTestcases({
questionId: 0,
title: "N/A",
input1: "N/A",
output1: "N/A",
input2: "N/A",
output2: "N/A"
});
}
};
if (questionId2 && questionId2 !== 0) {

if (questionTitle && questionTitle !== 'N/A') {
fetchTestcases();
}
}, [questionId2]);
}, [questionTitle]);



const handleLeaveSession = () => {
Expand Down

0 comments on commit cae58f9

Please sign in to comment.