Snai3i-MarketPlace / backend / src / services / course / video.service.ts
video.service.ts
Raw
import { HttpCodes } from '../../config/Errors';
import { db } from '../../settings';
import { ResultSetHeader } from 'mysql2';
import { ErrorResponseC, SuccessResponseC } from '../services.response';
import courseLogs, { ICourseLogs, courseLogger } from './course.logs';
import { formatString } from '../../utils/Strings';

export class VideoServices {
  static insertVideos = async (videos: VideoI[]): Promise<ResponseT> => {
    try {
      const sqlInsertQuery =
        'INSERT INTO videos (chapter_id, title, url, videoLength, position) VALUES ?';
      const values = videos.map((video) => [
        video.chapter_id,
        video.title,
        video.url,
        video.videoLength,
        video.position,
      ]);
      const [result]: any = await db.query<ResultSetHeader[]>(sqlInsertQuery, [
        values,
      ]);
      const videoIds = videos.map((_, position) => result.insertId + position);
      const resp: ICode<ICourseLogs> = courseLogs.INSERT_VIDEOS_SUCCESS;
      const msg = formatString(resp.message, {
        videoIds: videoIds.join(', '),
      });
      courseLogger.info(msg, { type: resp.type });
      return new SuccessResponseC(resp.type, {}, msg, HttpCodes.Created.code);
    } catch (err) {
      const msg = formatString(courseLogs.VIDEO_ERROR_GENERIC.message, {
        error: (err as Error)?.message || '',
      });
      courseLogger.error(msg, err as Error);
      return new ErrorResponseC(
        courseLogs.VIDEO_ERROR_GENERIC.type,
        HttpCodes.InternalServerError.code,
        msg
      );
    }
  };

  static updateVideos = async (videos: VideoI[]): Promise<ResponseT> => {
    try {
      const sqlUpdateQuery =
        'UPDATE videos SET title = ?, url = ?, videoLength = ? WHERE video_id = ?';
      const values: any = videos.map((video) => [
        video.title,
        video.url,
        video.videoLength,
        video.video_id,
      ]);
      for (const value of values) {
        // const [result]: any =
        await db.query<ResultSetHeader>(sqlUpdateQuery, [
          value.title,
          value.url,
          value.videoLength,
          value.video_id,
        ]);
        // if (result.affectedRows === 0) {
        //   const msg = formatString(courseLogs.VIDEO_ERROR_NOT_FOUND.message, {
        //     videoId: videos.map((video) => video.video_id).join(', '),
        //   });
        //   courseLogger.error(msg);
        //   return new ErrorResponseC(
        //     courseLogs.VIDEO_ERROR_NOT_FOUND.type,
        //     HttpCodes.NotFound.code,
        //     msg
        //   );
        // }
      }

      const resp: ICode<ICourseLogs> = courseLogs.UPDATE_VIDEOS_SUCCESS;
      const msg = formatString(resp.message, {
        videoIds: videos.map((video) => video.video_id).join(', '),
      });
      courseLogger.info(msg, { type: resp.type });
      return new SuccessResponseC(resp.type, {}, msg, HttpCodes.Accepted.code);
    } catch (err) {
      const msg = formatString(courseLogs.VIDEO_ERROR_GENERIC.message, {
        error: (err as Error)?.message || '',
      });
      courseLogger.error(msg, err as Error);
      return new ErrorResponseC(
        courseLogs.VIDEO_ERROR_GENERIC.type,
        HttpCodes.InternalServerError.code,
        msg
      );
    }
  };

  static deleteVideos = async (video_ids: number[]): Promise<ResponseT> => {
    try {
      const sqlDeleteQuery = 'DELETE FROM videos WHERE video_id IN (?)';

      const [result]: any = await db.query<ResultSetHeader[]>(sqlDeleteQuery, [
        video_ids,
      ]);
      if (result.affectedRows === 0) {
        const msg = formatString(courseLogs.VIDEO_ERROR_NOT_FOUND.message, {
          videoId: video_ids.join(', '),
        });
        courseLogger.error(msg);
        return new ErrorResponseC(
          courseLogs.VIDEO_ERROR_NOT_FOUND.type,
          HttpCodes.NotFound.code,
          msg
        );
      }
      const resp: ICode<ICourseLogs> = courseLogs.DELETE_VIDEOS_SUCCESS;
      const msg = formatString(resp.message, {
        videoIds: video_ids.join(', '),
      });
      courseLogger.info(msg, { type: resp.type });
      return new SuccessResponseC(resp.type, {}, msg, HttpCodes.Accepted.code);
    } catch (err) {
      const msg = formatString(courseLogs.VIDEO_ERROR_GENERIC.message, {
        error: (err as Error)?.message || '',
      });
      courseLogger.error(msg, err as Error);
      return new ErrorResponseC(
        courseLogs.VIDEO_ERROR_GENERIC.type,
        HttpCodes.InternalServerError.code,
        msg
      );
    }
  };

  static identifyAddedVideos = (oldVideos: VideoI[], newVideos: VideoI[]) => {
    const addedVideos = newVideos.filter(
      (newVideo) =>
        !oldVideos.some((oldVideo) => oldVideo.video_id === newVideo.video_id)
    );
    return addedVideos;
  };
  static identifyDeletedVideos = (oldVideos: VideoI[], newVideos: VideoI[]) => {
    const deletedVideos = oldVideos.filter(
      (oldVideo) =>
        !newVideos.some((newVideo) => newVideo.video_id === oldVideo.video_id)
    );
    return deletedVideos;
  };

  static identifyUpdatedVideos = (oldVideos: VideoI[], newVideos: VideoI[]) => {
    const updatedVideos = newVideos.filter((newVideo) =>
      oldVideos.some(
        (oldVideo) =>
          oldVideo.video_id === newVideo.video_id &&
          (oldVideo.title !== newVideo.title ||
            oldVideo.url !== newVideo.url ||
            oldVideo.position !== newVideo.position ||
            oldVideo.videoLength !== newVideo.videoLength)
      )
    );
    return updatedVideos;
  };
}