Snai3i-MarketPlace / backend / src / controllers / course.controller.ts
course.controller.ts
Raw
import { Response } from 'express';
import { MyRequest } from '../types/Express';
import { ErrorResponse, SuccessResponse } from '../utils/Response';
import { CourseService } from '../services/course/course.service';
import {
  ErrorResponseC,
  SuccessResponseC,
} from '../services/services.response';
import { TagServices } from '../services/course/tag.service';

export const GetSnai3iCourseById = async (
  req: MyRequest<UserI>,
  res: Response
) => {
  const { course_id } = req.params;
  const result = await CourseService.getSnai3iCourseById(parseInt(course_id));
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const GetMarketCourseById = async (
  req: MyRequest<UserI>,
  res: Response
) => {
  const { course_id } = req.params;
  const result = await CourseService.getMarketCourseById(parseInt(course_id));
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const GetAllSnai3iCourses = async (
  req: MyRequest<UserI>,
  res: Response
) => {
  const result = await CourseService.getAllSnai3iCourses();
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};
export const GetActiveSnai3iCourses = async (
  req: MyRequest<UserI>,
  res: Response
) => {
  const result = await CourseService.getActiveSnai3iCourses();
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const GetCoursesByInstructor = async (
  req: MyRequest<UserI>,
  res: Response
) => {
  const { inst_designer_id } = req.params;
  const result = await CourseService.getCoursesByInstructor(
    parseInt(inst_designer_id)
  );
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const GetCoursesBySchool = async (
  req: MyRequest<UserI>,
  res: Response
) => {
  const { school_id } = req.params;
  const result = await CourseService.getCoursesBySchool(parseInt(school_id));
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const GetCoursesByTeacher = async (
  req: MyRequest<UserI>,
  res: Response
) => {
  const { teacher_id } = req.params;
  const result = await CourseService.getCoursesByTeacher(parseInt(teacher_id));
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const GetMarketCourses = async (
  req: MyRequest<UserI>,
  res: Response
) => {
  const result = await CourseService.getMarketCourses();
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const GetActiveMarketCourses = async (
  req: MyRequest<UserI>,
  res: Response
) => {
  const result = await CourseService.getActiveMarketCourses();
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const CreateSnai3iCourse = async (
  req: MyRequest<UserI>,
  res: Response
) => {
  const { title, description, chapters, thumbnail } = req.body;
  const result = await CourseService.createSnai3iCourse(
    title,
    description,
    chapters,
    thumbnail
  );
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const CreateMarketCourse = async (
  req: MyRequest<UserI>,
  res: Response
) => {
  const {
    inst_designer_id,
    inst_designer_firstName,
    inst_designer_lastName,
    title,
    description,
    totalHours,
    price,
    tags,
    chaptersCount,
    thumbnail,
    videoThumbnail,
    status = 'pendingCreation',
  } = req.body;
  const result = await CourseService.createMarketCourse(
    title,
    description,
    totalHours,
    price,
    tags,
    chaptersCount,
    inst_designer_id ?? null,
    inst_designer_firstName ?? null,
    inst_designer_lastName ?? null,
    thumbnail,
    videoThumbnail,
    status
  );
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const DeleteCourse = async (req: MyRequest<UserI>, res: Response) => {
  const { course_id } = req.params;
  const result = await CourseService.deleteCourse(parseInt(course_id));
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const UpdateSnai3iCourse = async (
  req: MyRequest<UserI>,
  res: Response
) => {
  const { course_id } = req.params;
  const result = await CourseService.updateCourse(
    parseInt(course_id),
    req.body
  );
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const UpdateMarketCourse = async (
  req: MyRequest<UserI>,
  res: Response
) => {
  const { course_id } = req.params;
  const {
    title,
    description,
    totalHours,
    price,
    tags,
    chaptersCount,
    status = 'pendingUpdating',
    thumbnail,
    videoThumbnail,
  } = req.body;
  const result = await CourseService.updateMarketCourse(
    parseInt(course_id),
    title,
    description,
    totalHours,
    price,
    tags,
    chaptersCount,
    status,
    thumbnail,
    videoThumbnail
  );
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const UpdateCourseStatus = async (
  req: MyRequest<UserI>,
  res: Response
) => {
  const { course_id } = req.params;
  const result = await CourseService.updateCourseStatus(
    parseInt(course_id),
    req.body.status
  );
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const GetTags = async (req: MyRequest<UserI>, res: Response) => {
  const result = await TagServices.getTags();
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};