GIF89a php
Current File : /home/viralhoga/app_viralhoga/src/modules/youtube/youtube.controller.js
import YoutubeModel from "../../models/youtube.model.js";
import ApiError from "../../utils/ApiErrorHandler.js";
import ApiResponse from '../../utils/ApiRespinseHandler.js'
import AsyncHandler from "../../utils/AsyncHandler.js";
import YoutubeVideoModel from '../../models/youtube.url.model.js'
import path from 'path'

export const createYoutubeChannels = AsyncHandler(async (req, res) => {
    console.log('channel', req.body)
    const { channelName, channelLink } = req.body;

    if (!channelName || !channelLink) {
        throw new ApiError(400, 'All fields are required')
    }

    let channelThumbnail = ''
    if (req.file?.path) {
        channelThumbnail = `uploads/${path.basename(req.file.path)}`
    }

    const channel = await YoutubeModel.create({
        channelName,
        channelLink,
        channelThumbnail
    })

    console.log('channel', req.body)

    return res.status(201).json(
        new ApiResponse(201, 'Channel created successfully', channel)
    )

})

export const getYoutubeChannels = AsyncHandler(async (req, res) => {
    const channels = await YoutubeModel.find()
    res.status(200).json(
        new ApiResponse(200, 'Channels fetched successfully', channels)
    )
})

export const updateYoutubeChannel = AsyncHandler(async (req, res) => {
    const { id } = req.params;
    const { channelName, channelLink } = req.body;

    const channel = await YoutubeModel.findById(id);

    if (!channel) {
        throw new ApiError(404, "Channel not found");
    }

    if (channelName) {
        channel.channelName = channelName;
    }

    if (channelLink) {
        channel.channelLink = channelLink;
    }

    if (req.file?.path) {
        channel.channelThumbnail = `uploads/${path.basename(req.file.path)}`;
    }

    await channel.save();

    return res.status(200).json(
        new ApiResponse(
            200,
            "Channel updated successfully",
            channel
        )
    );
});

export const deleteYoutubeChannel = AsyncHandler(async (req, res) => {
    const { id } = req.params;

    const channel = await YoutubeModel.findById(id);

    if (!channel) {
        throw new ApiError(404, "Channel not found");
    }

    await YoutubeModel.findByIdAndDelete(id);

    return res.status(200).json(
        new ApiResponse(
            200,
            "Channel deleted successfully",
            channel
        )
    );
});

export const addYoutubeVideo = AsyncHandler(async (req, res) => {
    const { url } = req.body;

    if (!url) {
        throw new ApiError(400, "Youtube url is required");
    }

    const youtubeUrl = await YoutubeVideoModel.create({
        url
    });

    res.status(201).json({
        success: true,
        data: youtubeUrl
    });
});

export const getYoutubeVideos = AsyncHandler(async (req, res) => {
    const videos = await YoutubeVideoModel.find()
        .sort({ createdAt: -1 });

    res.status(200).json({
        success: true,
        count: videos.length,
        data: videos
    });
});

export const deleteYoutubeVideo = AsyncHandler(async (req, res) => {
    const { id } = req.params;

    const url = await YoutubeVideoModel.findByIdAndDelete(id);

    if (!url) {
        throw new ApiError(404, "Video not found");
    }

    res.status(200).json({
        success: true,
        message: "Video deleted successfully"
    });
});