File size: 5,593 Bytes
c01955c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { expressRepre } from "@vashuthegreat/vexpress";
import ApiError from "../utils/ApiError.js";
import asyncHandler from "../utils/asyncHandler.js";
import ApiResponse from "../utils/ApiResponse.js";
import { Query } from "mongoose";
import connectsql from "../db/connectsql.js";
import { Request,Response } from "express";
import fs from "fs"
import logger from "../logger/create.logger.js";

let db:any;
(async () => {
    db = await connectsql();
})();

async function runquery(query: string, params: any[] = []) {
    const [res]=await db.execute(query, params)
    return res
    
}

export const add_questions=expressRepre(
    {
        summary:"add questions",
        body:{
            "id": 2,
    "title": "Transpose of a Matrix",
    "difficulty": "easy",
    "category": "linear algebra",
    "problem_description": "V3JpdGUgYSBQeXRob24gZnVuY3Rpb24gdGhhdCBjb21wdXRlcyB0aGUgdHJhbnNwb3NlIG9mIGEgZ2l2ZW4gbWF0cml4Lg==",
    "starter_code": "from tinygrad.tensor import Tensor\n\ndef transpose_matrix_tg(a:Tensor) -> Tensor:\n    \"\"\"\n    Transpose a 2D matrix `a` using tinygrad.\n    Inputs are tinygrad Tensors.\n    Returns a transposed Tensor.\n    \"\"\"\n    pass",
    "example_input": "a = [[1,2,3],[4,5,6]]",
    "example_output": "[[1,4],[2,5],[3,6]]",
    "example_reasoning": "The transpose of a matrix is obtained by flipping rows and columns.",
    "learn_content": "CiMjIFRyYW5zcG9zZSBvZiBhIE1hdHJpeAoKTGV0J3MgY29uc2lkZXIgYSBtYXRyaXggJE0kIGFuZCBpdHMgdHJhbnNwb3NlICRNXlQkOgoKKipPcmlnaW5hbCBNYXRyaXggJE0kOioqCiQkCk0gPSBcYmVnaW57cG1hdHJpeH0gCmEgJiBiICYgYyBcXCAKZCAmIGUgJiBmIApcZW5ke3BtYXRyaXh9CiQkCgoqKlRyYW5zcG9zZWQgTWF0cml4ICRNXlQkOioqCiQkCk1eVCA9IFxiZWdpbntwbWF0cml4fSAKYSAmIGQgXFwgCmIgJiBlIFxcIApjICYgZiAKXGVuZHtwbWF0cml4fQokJAoKIyMjIEV4cGxhbmF0aW9uOgpUcmFuc3Bvc2luZyBhIG1hdHJpeCBpbnZvbHZlcyBjb252ZXJ0aW5nIGl0cyByb3dzIGludG8gY29sdW1ucyBhbmQgdmljZSB2ZXJzYS4gVGhpcyBvcGVyYXRpb24gaXMgZnVuZGFtZW50YWwgaW4gbGluZWFyIGFsZ2VicmEgZm9yIHZhcmlvdXMgY29tcHV0YXRpb25zIGFuZCB0cmFuc2Zvcm1hdGlvbnMu",
    "solution_code": "from tinygrad.tensor import Tensor\n\ndef transpose_matrix_tg(a) -> Tensor:\n    \"\"\"\n    Transpose a 2D matrix `a` using tinygrad.\n    Inputs are tinygrad Tensors.\n    Returns a transposed Tensor.\n    \"\"\"\n    return a.T",
    "test_cases": [
      {
        "test": "print(transpose_matrix_tg(Tensor([[1,2],[3,4],[5,6]])))",
        "expected_output": "[[1, 3, 5], [2, 4, 6]]"
      },
      {
        "test": "print(transpose_matrix_tg(Tensor([[1,2,3],[4,5,6]])))",
        "expected_output": "[[1, 4], [2, 5], [3, 6]]"
      }
    ]
        },
        response:"question added"
    },
    asyncHandler(async (req,res)=>{
        const {id,title,difficulty,category,problem_description,starter_code,example_input,example_output,example_reasoning,learn_content,solution_code,test_cases,function_name}=req.body;
        logger.info(`Adding question: ${title} (ID: ${id})`);
        const data=await runquery(`insert into questions (id,title,difficulty,category,problem_description,starter_code,example_input,example_output,example_reasoning,learn_content,solution_code,test_cases,function_name) values (?,?,?,?,?,?,?,?,?,?,?,?,?)`, [id,title,difficulty,category,problem_description,starter_code,example_input,example_output,example_reasoning,learn_content,solution_code, JSON.stringify(test_cases), function_name])
        res.status(200).json(new ApiResponse(200,data))
    })
)

export const get_available_categories=expressRepre(
    {
        summary :"fet avalable questions categories",
        response:"avalable categories"

    },
    asyncHandler(async (req:Request,res:Response)=>{
        const data=await runquery(`select distinct category from questions `);

        const categories:string[]=[]

        data.forEach((element:any) => {
            categories.push(element.category)

            
        });
        res.status(200).json(new ApiResponse(200,categories));

    })
)


export const fetch_all_questions=expressRepre(
    {

    },
    asyncHandler(async (req,res)=>{


    const data=await runquery(`select * from questions`)
    res.status(200).json(new ApiResponse(200,data))

    
    })
)
export const fetch_questionById=expressRepre({
    summary:"give id to fetch question",
    params:{
        "question_id":"1"
    },
    response:"a question"
},asyncHandler(async (req:Request,res:Response)=>{
    const { question_id } = req.params as { question_id: any };
    logger.info(`Fetching question by ID: ${question_id}`);


    const data=await runquery(`select * from questions where id=?`, [question_id])
    res.status(200).json(new ApiResponse(200,data))


}))


export const fetch_questionBycategory=expressRepre({
    summary:"give id to fetch question",
    params:{
        "category":"Linear Algebra"
    },
    response:"a question"
},asyncHandler(async (req:Request,res:Response)=>{
    let { category } = req.params as { category: string };

category=category.toLowerCase()
    const data=await runquery(`select * from questions where category=?`, [category])
    res.status(200).json(new ApiResponse(200,data))


}))



export const fetch_questionByDifficulty=expressRepre({
    summary:"give difficulty to fetch question",
    params:{
        "difficulty":"easy"
    },
    response:"a question"
},asyncHandler(async (req:Request,res:Response)=>{
    let { difficulty } = req.params as { difficulty: string };

difficulty=difficulty.toLowerCase()
    const data=await runquery(`select * from questions where difficulty=?`, [difficulty])
    res.status(200).json(new ApiResponse(200,data))


}))