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

// Packs Table:
// pack_id (Primary Key)
// name,
// description,
// nb_teachers_accounts,
// nb_students_accounts,
// lms_access,
// additional_price, // price/hours/student exemple 1000/16/1 if no additional price put 0
// status,
// most_popular

export const getPacks = async (req: MyRequest<UserI>, res: Response) => {
  const result = await PackServices.getPacks();
  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 getPackById = async (req: MyRequest<UserI>, res: Response) => {
  const { pack_id } = req.params;
  const result = await PackServices.getPackById(parseInt(pack_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 getActivePacks = async (req: MyRequest<UserI>, res: Response) => {
  const result = await PackServices.getActivePacks();
  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 createPack = async (req: MyRequest<UserI>, res: Response) => {
  const {
    name,
    description,
    nb_teachers_accounts,
    nb_students_accounts,
    lms_access,
    additional_price,
    status,
    most_popular,
  } = req.body;
  const result = await PackServices.createPack(
    name,
    description,
    nb_teachers_accounts,
    nb_students_accounts,
    lms_access,
    additional_price,
    status,
    most_popular
  );
  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 deletePack = async (req: MyRequest<UserI>, res: Response) => {
  const { pack_id } = req.params;
  const result = await PackServices.deletePack(parseInt(pack_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 updatePack = async (req: MyRequest<UserI>, res: Response) => {
  const { pack_id } = req.params;
  const {
    name,
    description,
    nb_teachers_accounts,
    nb_students_accounts,
    lms_access,
    additional_price,
    status,
    most_popular,
  } = req.body;
  const result = await PackServices.updatePack(
    parseInt(pack_id),
    name,
    description,
    nb_teachers_accounts,
    nb_students_accounts,
    lms_access,
    additional_price,
    status,
    most_popular
  );

  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 updatePackStatus = async (
  req: MyRequest<UserI>,
  res: Response
) => {
  const { pack_id } = req.params;
  const { status } = req.body;
  const result = await PackServices.updatePackStatus(parseInt(pack_id), 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);
};