Skip to content

Commit

Permalink
Add getWatchHistory endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
hiteshchoudhary committed Dec 20, 2023
1 parent 94a01db commit a573049
Showing 1 changed file with 57 additions and 2 deletions.
59 changes: 57 additions & 2 deletions src/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { User} from "../models/user.model.js"
import {uploadOnCloudinary} from "../utils/cloudinary.js"
import { ApiResponse } from "../utils/ApiResponse.js";
import jwt from "jsonwebtoken"
import mongoose from "mongoose";


const generateAccessAndRefereshTokens = async(userId) =>{
Expand Down Expand Up @@ -425,6 +426,60 @@ const getUserChannelProfile = asyncHandler(async(req, res) => {
)
})

const getWatchHistory = asyncHandler(async(req, res) => {
const user = await User.aggregate([
{
$match: {
_id: new mongoose.Types.ObjectId(req.user._id)
}
},
{
$lookup: {
from: "videos",
localField: "watchHistory",
foreignField: "_id",
as: "watchHistory",
pipeline: [
{
$lookup: {
from: "users",
localField: "owner",
foreignField: "_id",
as: "owner",
pipeline: [
{
$project: {
fullName: 1,
username: 1,
avatar: 1
}
}
]
}
},
{
$addFields:{
owner:{
$first: "$owner"
}
}
}
]
}
}
])

return res
.status(200)
.json(
new ApiResponse(
200,
user[0].watchHistory,
"Watch history fetched successfully"
)
)
})


export {
registerUser,
Expand All @@ -436,6 +491,6 @@ export {
updateAccountDetails,
updateUserAvatar,
updateUserCoverImage,
getUserChannelProfile

getUserChannelProfile,
getWatchHistory
}

0 comments on commit a573049

Please sign in to comment.