UNPKG

@defikitdotnet/education-module-ai

Version:
304 lines (303 loc) 14.6 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CategoryController = void 0; var CategoryService_1 = require("../services/CategoryService"); var controller_1 = require("../decorators/controller"); var CategoryController = /** @class */ (function () { function CategoryController(categoryService) { this.categoryService = categoryService; } /** * Create a new category */ CategoryController.prototype.createCategory = function (req, res) { return __awaiter(this, void 0, void 0, function () { var categoryData, category, error_1; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); categoryData = req.body; // Validate required fields - Only require teacherId if (!categoryData.teacherId) { res.status(400).json({ error: "Teacher ID is required" }); return [2 /*return*/]; } if (!categoryData.name) { res.status(400).json({ error: "Name is required" }); return [2 /*return*/]; } return [4 /*yield*/, this.categoryService.createCategory(categoryData)]; case 1: category = _a.sent(); res.status(201).json(category); return [3 /*break*/, 3]; case 2: error_1 = _a.sent(); res.status(400).json({ error: error_1.message }); return [3 /*break*/, 3]; case 3: return [2 /*return*/]; } }); }); }; /** * Get all categories for a teacher */ CategoryController.prototype.getCategoriesByTeacherId = function (req, res) { return __awaiter(this, void 0, void 0, function () { var teacherId, categories, error_2; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); teacherId = req.params.teacherId; if (!teacherId) { res.status(400).json({ error: "Teacher ID is required" }); return [2 /*return*/]; } return [4 /*yield*/, this.categoryService.getCategoriesByTeacherId(teacherId)]; case 1: categories = _a.sent(); res.json(categories); return [3 /*break*/, 3]; case 2: error_2 = _a.sent(); res.status(500).json({ error: error_2.message }); return [3 /*break*/, 3]; case 3: return [2 /*return*/]; } }); }); }; /** * Get a category by ID */ CategoryController.prototype.getCategoryById = function (req, res) { return __awaiter(this, void 0, void 0, function () { var categoryId, category, error_3; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); categoryId = req.params.categoryId; if (!categoryId) { res.status(400).json({ error: "Category ID is required" }); return [2 /*return*/]; } return [4 /*yield*/, this.categoryService.getCategoryById(categoryId)]; case 1: category = _a.sent(); if (!category) { res.status(404).json({ error: "Category not found" }); return [2 /*return*/]; } res.json(category); return [3 /*break*/, 3]; case 2: error_3 = _a.sent(); res.status(500).json({ error: error_3.message }); return [3 /*break*/, 3]; case 3: return [2 /*return*/]; } }); }); }; /** * Get all categories with pagination */ CategoryController.prototype.getAllCategories = function (req, res) { return __awaiter(this, void 0, void 0, function () { var page, limit, categories, totalCount, error_4; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 3, , 4]); page = parseInt(req.query.page) || 1; limit = parseInt(req.query.limit) || 20; return [4 /*yield*/, this.categoryService.getAllCategories(page, limit)]; case 1: categories = _a.sent(); return [4 /*yield*/, this.categoryService.getTotalCategoryCount()]; case 2: totalCount = _a.sent(); res.json({ data: categories, meta: { total: totalCount, page: page, limit: limit, pages: Math.ceil(totalCount / limit), }, }); return [3 /*break*/, 4]; case 3: error_4 = _a.sent(); res.status(500).json({ error: error_4.message }); return [3 /*break*/, 4]; case 4: return [2 /*return*/]; } }); }); }; /** * Update a category */ CategoryController.prototype.updateCategory = function (req, res) { return __awaiter(this, void 0, void 0, function () { var categoryId, categoryData, updatedCategory, error_5; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); categoryId = req.params.categoryId; categoryData = req.body; if (!categoryId) { res.status(400).json({ error: "Category ID is required" }); return [2 /*return*/]; } return [4 /*yield*/, this.categoryService.updateCategory(categoryId, categoryData)]; case 1: updatedCategory = _a.sent(); if (!updatedCategory) { res.status(404).json({ error: "Category not found" }); return [2 /*return*/]; } res.json(updatedCategory); return [3 /*break*/, 3]; case 2: error_5 = _a.sent(); res.status(400).json({ error: error_5.message }); return [3 /*break*/, 3]; case 3: return [2 /*return*/]; } }); }); }; /** * Delete a category */ CategoryController.prototype.deleteCategory = function (req, res) { return __awaiter(this, void 0, void 0, function () { var categoryId, deleted, error_6; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); categoryId = req.params.categoryId; if (!categoryId) { res.status(400).json({ error: "Category ID is required" }); return [2 /*return*/]; } return [4 /*yield*/, this.categoryService.deleteCategory(categoryId)]; case 1: deleted = _a.sent(); if (!deleted) { res.status(404).json({ error: "Category not found" }); return [2 /*return*/]; } res.status(204).end(); return [3 /*break*/, 3]; case 2: error_6 = _a.sent(); res.status(500).json({ error: error_6.message }); return [3 /*break*/, 3]; case 3: return [2 /*return*/]; } }); }); }; __decorate([ (0, controller_1.Post)("/") // @Use(authenticateTeacher) , __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", Promise) ], CategoryController.prototype, "createCategory", null); __decorate([ (0, controller_1.Get)("/teacher/:teacherId"), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", Promise) ], CategoryController.prototype, "getCategoriesByTeacherId", null); __decorate([ (0, controller_1.Get)("/:categoryId"), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", Promise) ], CategoryController.prototype, "getCategoryById", null); __decorate([ (0, controller_1.Get)("/"), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", Promise) ], CategoryController.prototype, "getAllCategories", null); __decorate([ (0, controller_1.Put)("/:categoryId") // @Use(authenticateTeacher) , __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", Promise) ], CategoryController.prototype, "updateCategory", null); __decorate([ (0, controller_1.Delete)("/:categoryId") // @Use(authenticateTeacher) , __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", Promise) ], CategoryController.prototype, "deleteCategory", null); CategoryController = __decorate([ (0, controller_1.Controller)("/api/categories"), __metadata("design:paramtypes", [CategoryService_1.CategoryService]) ], CategoryController); return CategoryController; }()); exports.CategoryController = CategoryController;