hiteshchoudhary / chai-backend

A video series on chai aur code youtube channel
4.69k stars 697 forks source link

All TODO's complete #131

Open roushan-code opened 2 months ago

roushan-code commented 2 months ago

Hi sir, I have completed all the todos and learned many things.

I have also tested all todos with Postman.

Thanks for giving me such a great learning opportunity.

Slashz07 commented 1 month ago

can you please tell me why is userId required in getAllVideos in video controller and how can it be accessed and sent in req.query. I just cant understand, since req.query accesses key-value pairs from url ,which are set by the submission of form where user would put his search query...so from where does the userId come from

roushan-code commented 1 month ago

User-Id is required because with this route the user will get all the videos that he uploads. so the user-ID is required for authentication.

There is a condition that if the user is logged in then He will get all his uploaded videos based on query. That means the User-Id will send from frontend to backend. So that User verification should be done through backend.

frontend code will be like this ->

import axios from 'axios';

const getAllVideos = async (id) => { try { const response = await axios.get('/api/videos', { params: { page: 1, limit: 10, query: '', sortBy: 'createdAt', sortType: 1, userId: id } }); const { data } = response; console.log(data); // render video list } catch (error) { console.error(error); } };

---------OR------------

export const getAllVideos = (query= "", page= 1, sortBy= 'createdAt', sortType=1, userId= id) => async (dispatch) => { try { dispatch({ type: ALL_VIDEO_REQUEST }); let link = /api/v1/videos?keyword=${query}&page=${page}&sortBy=${sortBy}&sortType=${sortType}&userId=${userId};

    const { data } = await axios.get(link);
    dispatch({
        type: ALL_VIDEO_SUCCESS,
        payload: data,
    })
} catch (error) {
    const errorMessage = error.response ? error.response.data.message : "An error occurred";
    dispatch({
        type: ALL_VIDEO_FAIL,
        payload: errorMessage,
    });
}

};

Slashz07 commented 1 month ago

i think the feature of a user getting all of his own uploaded videos should be in another feature since there is no need of any query in that but sir has commented to use userQuery...so shouldn't a separate controller be made for that since any $match or $search would not be needed due to absence of any user query

also pls tell from if the id in const getAllVideos = async (id) => { try { .... } is being accessed from the user data that was returned when user logged in at first...

btw..thnx a lot for replying..i dont have any person to solve my doubts in person..its just self learning..i appreciate your response

roushan-code commented 1 month ago

I think the feature of a user getting all of his own uploaded videos should be in another feature since there is no need of any query in that but sir has commented to use userQuery...so shouldn't a separate controller be made for that since any $match or $search would not be needed due to absence of any user query

I have checked the code. I think you are right. User_id should be optional for getting video. Because the user who doesn't log in is also able to get videos according to query. but if the user is logged in then the user-id could pass from frontend to check what types of videos the user prefers. so that if any algorithm will be applied in the future to check what kinds of videos users prefer then they can serve videos according to their preference (like on youtube, facebook etc.).

I have applied user required at that time because of this line in video.routes.js file --> router.use(verifyJWT); // Apply verifyJWT middleware to all routes in this file The above router line is written by Sir .

also pls tell from if the id in const getAllVideos = async (id) => { try { .... } is being accessed from the user data that was returned when user logged in at first...

yes

Now I have updated the code. Thanks for your suggestion.

Slashz07 commented 1 month ago

I have applied user required at that time because of this line in video.routes.js file --> router.use(verifyJWT); // Apply verifyJWT middleware to all routes in this file The above router line is written by Sir .

but if we apply verifyjwt to alll routes, any user who isnt logged in wont be able to search any videos since verifyjwt would reject access to that user as there wont be any tokens in cookies for a not-logged-in user... is it that sir is focusing on only allowing access to logged in users??

roushan-code commented 1 month ago

but if we apply verifyjwt to alll routes, any user who isnt logged in wont be able to search any videos since verifyjwt would reject access to that user as there wont be any tokens in cookies for a not-logged-in user... is it that sir is focusing on only allowing access to logged in users??

may be sir think so.

But I have also updated the video routes in my code